2
0
Эх сурвалжийг харах

Fix semaphores on MacOS. (#500)

Zeta 3 жил өмнө
parent
commit
6405fe5055
3 өөрчлөгдсөн 25 нэмэгдсэн , 26 устгасан
  1. 1 1
      src/debugger.c
  2. 1 5
      src/hl.h
  3. 23 20
      src/std/thread.c

+ 1 - 1
src/debugger.c

@@ -116,7 +116,7 @@ static void hl_debug_loop( hl_module *m ) {
 	debugger_stopped = true;
 }
 
-bool hl_module_debug( hl_module *m, int port, bool wait ) {
+h_bool hl_module_debug( hl_module *m, int port, h_bool wait ) {
 	hl_socket *s;
 	hl_socket_init();
 	s = hl_socket_new(false);

+ 1 - 5
src/hl.h

@@ -182,17 +182,13 @@
 #else
 #	define C_FUNCTION_BEGIN
 #	define C_FUNCTION_END
-#	ifndef true
-#		define true 1
-#		define false 0
-		typedef unsigned char bool;
-#	endif
 #endif
 
 typedef intptr_t int_val;
 typedef long long int64;
 typedef unsigned long long uint64;
 
+#include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <memory.h>

+ 23 - 20
src/std/thread.c

@@ -19,6 +19,7 @@
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  */
+
 #include <hl.h>
 
 #if !defined(HL_THREADS)
@@ -187,7 +188,7 @@ DEFINE_PRIM(_VOID, mutex_free, _MUTEX);
 
 // ------------------ SEMAPHORE
 
-HL_API hl_semaphore *hl_semaphore_alloc(int value) {
+HL_PRIM hl_semaphore *hl_semaphore_alloc(int value) {
 #	if !defined(HL_THREADS)
 	static struct _hl_semaphore null_semaphore = {0};
 	return (hl_condition *)&null_semaphore;
@@ -210,7 +211,7 @@ HL_API hl_semaphore *hl_semaphore_alloc(int value) {
 #	endif
 }
 
-HL_API void hl_semaphore_acquire(hl_semaphore *sem) {
+HL_PRIM void hl_semaphore_acquire(hl_semaphore *sem) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	WaitForSingleObject(sem->sem, INFINITE);
@@ -223,7 +224,7 @@ HL_API void hl_semaphore_acquire(hl_semaphore *sem) {
 #	endif
 }
 
-HL_API bool hl_semaphore_try_acquire(hl_semaphore *sem, vdynamic *timeout) {
+HL_PRIM bool hl_semaphore_try_acquire(hl_semaphore *sem, vdynamic *timeout) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	return WaitForSingleObject(sem->sem,
@@ -232,7 +233,7 @@ HL_API bool hl_semaphore_try_acquire(hl_semaphore *sem, vdynamic *timeout) {
 #	else
 #	ifdef __APPLE__
 	return dispatch_semaphore_wait(
-	           sem, dispatch_time(DISPATCH_TIME_NOW,
+	           sem->sem, dispatch_time(DISPATCH_TIME_NOW,
 	                              (int64_t)((timeout ? timeout->v.d : 0) *
 	                                        1000 * 1000 * 1000))) == 0;
 #	else
@@ -258,20 +259,20 @@ HL_API bool hl_semaphore_try_acquire(hl_semaphore *sem, vdynamic *timeout) {
 #	endif
 }
 
-HL_API void hl_semaphore_release(hl_semaphore *sem) {
+HL_PRIM void hl_semaphore_release(hl_semaphore *sem) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	ReleaseSemaphore(sem->sem, 1, NULL);
 #	else
 #	ifdef __APPLE__
-	dispatch_semaphore_signal(&sem->sem);
+	dispatch_semaphore_signal(sem->sem);
 #	else
 	sem_post(&sem->sem);
 #	endif
 #	endif
 }
 
-HL_API void hl_semaphore_free(hl_semaphore *sem) {
+HL_PRIM void hl_semaphore_free(hl_semaphore *sem) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	if (sem->free) {
@@ -280,7 +281,9 @@ HL_API void hl_semaphore_free(hl_semaphore *sem) {
 	}
 #	else
 	if (sem->free) {
+#ifndef __APPLE__
 		sem_destroy(&sem->sem);
+#endif
 		sem->free = NULL;
 	}
 #	endif
@@ -294,7 +297,7 @@ DEFINE_PRIM(_VOID, semaphore_release, _SEMAPHORE);
 DEFINE_PRIM(_VOID, semaphore_free, _SEMAPHORE);
 // ------------------ CONDITION
 
-HL_API hl_condition *hl_condition_alloc() {
+HL_PRIM hl_condition *hl_condition_alloc() {
 #	if !defined(HL_THREADS)
 	static struct _hl_condition null_condition = {0};
 	return (hl_condition *)&null_condition;
@@ -321,7 +324,7 @@ HL_API hl_condition *hl_condition_alloc() {
 	return cond;
 #	endif
 }
-HL_API void hl_condition_acquire(hl_condition *cond) {
+HL_PRIM void hl_condition_acquire(hl_condition *cond) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	EnterCriticalSection(&cond->cs);
@@ -330,7 +333,7 @@ HL_API void hl_condition_acquire(hl_condition *cond) {
 #	endif
 }
 
-HL_API bool hl_condition_try_acquire(hl_condition *cond) {
+HL_PRIM bool hl_condition_try_acquire(hl_condition *cond) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	return (bool)TryEnterCriticalSection(&cond->cs);
@@ -339,7 +342,7 @@ HL_API bool hl_condition_try_acquire(hl_condition *cond) {
 #	endif
 }
 
-HL_API void hl_condition_release(hl_condition *cond) {
+HL_PRIM void hl_condition_release(hl_condition *cond) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	LeaveCriticalSection(&cond->cs);
@@ -347,7 +350,7 @@ HL_API void hl_condition_release(hl_condition *cond) {
 	pthread_mutex_unlock(&cond->mutex);
 #	endif
 }
-HL_API void hl_condition_wait(hl_condition *cond) {
+HL_PRIM void hl_condition_wait(hl_condition *cond) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	SleepConditionVariableCS(&cond->cond, &cond->cs, INFINITE);
@@ -356,7 +359,7 @@ HL_API void hl_condition_wait(hl_condition *cond) {
 #	endif
 }
 
-HL_API bool hl_condition_timed_wait(hl_condition *cond, double timeout) {
+HL_PRIM bool hl_condition_timed_wait(hl_condition *cond, double timeout) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	SleepConditionVariableCS(&cond->cond, &cond->cs,
@@ -378,7 +381,7 @@ HL_API bool hl_condition_timed_wait(hl_condition *cond, double timeout) {
 #	endif
 }
 
-HL_API void hl_condition_signal(hl_condition *cond) {
+HL_PRIM void hl_condition_signal(hl_condition *cond) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	WakeConditionVariable(&cond->cond);
@@ -386,7 +389,7 @@ HL_API void hl_condition_signal(hl_condition *cond) {
 	pthread_cond_signal(&cond->cond);
 #	endif
 }
-HL_API void hl_condition_broadcast(hl_condition *cond) {
+HL_PRIM void hl_condition_broadcast(hl_condition *cond) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	WakeAllConditionVariable(&cond->cond);
@@ -394,7 +397,7 @@ HL_API void hl_condition_broadcast(hl_condition *cond) {
 	pthread_cond_broadcast(&cond->cond);
 #	endif
 }
-HL_API void hl_condition_free(hl_condition *cond) {
+HL_PRIM void hl_condition_free(hl_condition *cond) {
 #	if !defined(HL_THREADS)
 #	elif defined(HL_WIN)
 	if (cond->free) {
@@ -534,7 +537,7 @@ static void hl_deque_free( hl_deque *q ) {
 #	endif
 }
 
-HL_API hl_deque *hl_deque_alloc() {
+HL_PRIM hl_deque *hl_deque_alloc() {
 	hl_deque *q = (hl_deque*)hl_gc_alloc_finalizer(sizeof(hl_deque));
 	q->free = hl_deque_free;
 	q->first = NULL;
@@ -551,7 +554,7 @@ HL_API hl_deque *hl_deque_alloc() {
 	return q;
 }
 
-HL_API void hl_deque_add( hl_deque *q, vdynamic *msg ) {
+HL_PRIM void hl_deque_add( hl_deque *q, vdynamic *msg ) {
 	tqueue *t = (tqueue*)hl_gc_alloc_raw(sizeof(tqueue));
 	t->msg = msg;
 	t->next = NULL;
@@ -565,7 +568,7 @@ HL_API void hl_deque_add( hl_deque *q, vdynamic *msg ) {
 	UNLOCK(q->lock);
 }
 
-HL_API void hl_deque_push( hl_deque *q, vdynamic *msg ) {
+HL_PRIM void hl_deque_push( hl_deque *q, vdynamic *msg ) {
 	tqueue *t = (tqueue*)hl_gc_alloc_raw(sizeof(tqueue));
 	t->msg = msg;
 	LOCK(q->lock);
@@ -577,7 +580,7 @@ HL_API void hl_deque_push( hl_deque *q, vdynamic *msg ) {
 	UNLOCK(q->lock);
 }
 
-HL_API vdynamic *hl_deque_pop( hl_deque *q, bool block ) {
+HL_PRIM vdynamic *hl_deque_pop( hl_deque *q, bool block ) {
 	vdynamic *msg;
 	hl_blocking(true);
 	LOCK(q->lock);