소스 검색

don't use spinlock (too high threads contention)

Nicolas Cannasse 7 년 전
부모
커밋
fe5062ab0b
3개의 변경된 파일5개의 추가작업 그리고 47개의 파일을 삭제
  1. 5 6
      src/alloc.c
  2. 0 6
      src/hl.h
  3. 0 35
      src/std/thread.c

+ 5 - 6
src/alloc.c

@@ -151,7 +151,7 @@ static struct {
 	int count;
 	bool stopping_world;
 	hl_thread_info **threads;
-	hl_spinlock *global_lock;
+	hl_mutex *global_lock;
 } gc_threads;
 
 HL_THREAD_STATIC_VAR hl_thread_info *current_thread;
@@ -210,11 +210,10 @@ static void gc_global_lock( bool lock ) {
 			hl_fatal("Can't lock GC in hl_blocking section");
 		if( mt ) gc_save_context(t);
 		t->gc_blocking++;
-		while ( mt && !hl_spinlock_acquire(gc_threads.global_lock, t, 100000))
-			hl_thread_yield();
+		if( mt ) hl_mutex_acquire(gc_threads.global_lock);
 	} else {
 		t->gc_blocking--;
-		if( mt ) hl_spinlock_release(gc_threads.global_lock);
+		if( mt ) hl_mutex_release(gc_threads.global_lock);
 	}
 }
 #endif
@@ -299,7 +298,7 @@ HL_API void hl_unregister_thread() {
 	free(t);
 	current_thread = NULL;
 	// don't use gc_global_lock(false)
-	hl_spinlock_release(gc_threads.global_lock);
+	hl_mutex_release(gc_threads.global_lock);
 }
 
 HL_API void *hl_gc_threads_info() {
@@ -1031,7 +1030,7 @@ static void hl_gc_init() {
 		gc_flags |= GC_DUMP_MEM;
 #	endif
 	memset(&gc_threads,0,sizeof(gc_threads));
-	gc_threads.global_lock = hl_spinlock_alloc();
+	gc_threads.global_lock = hl_mutex_alloc();
 }
 
 // ---- UTILITIES ----------------------

+ 0 - 6
src/hl.h

@@ -631,11 +631,9 @@ HL_API vdynamic *hl_dyn_call_safe( vclosure *c, vdynamic **args, int nargs, bool
 struct _hl_thread;
 struct _hl_mutex;
 struct _hl_tls;
-struct _hl_spinlock;
 typedef struct _hl_thread hl_thread;
 typedef struct _hl_mutex hl_mutex;
 typedef struct _hl_tls hl_tls;
-typedef struct _hl_spinlock hl_spinlock;
 
 HL_API hl_thread *hl_thread_start( void *callback, void *param, bool withGC );
 HL_API hl_thread *hl_thread_current( void );
@@ -654,10 +652,6 @@ HL_API void hl_tls_set( hl_tls *l, void *value );
 HL_API void *hl_tls_get( hl_tls *l );
 HL_API void hl_tls_free( hl_tls *l );
 
-HL_API hl_spinlock *hl_spinlock_alloc(void);
-HL_API bool hl_spinlock_acquire( hl_spinlock *s, void *value, int count );
-HL_API void hl_spinlock_release( hl_spinlock *s );
-
 // ----------------------- ALLOC --------------------------------------------------
 
 #define MEM_HAS_PTR(kind)	(!((kind)&2))

+ 0 - 35
src/std/thread.c

@@ -174,41 +174,6 @@ HL_PRIM void *hl_tls_get( hl_tls *l ) {
 #	endif
 }
 
-// ----------------- SPINLOCK
-
-HL_PRIM hl_spinlock *hl_spinlock_alloc() {
-#	if !defined(HL_THREADS)
-	return NULL;
-#	elif defined(HL_WIN)
-	hl_spinlock *s = _aligned_malloc(sizeof(hl_spinlock),32);
-	s->value = NULL;
-	return s;
-#	else
-#	endif
-}
-
-HL_PRIM bool hl_spinlock_acquire( hl_spinlock *s, void *value, int count ) {
-#	if !defined(HL_THREADS)
-	return true;
-#	elif defined(HL_WIN)
-	while (s->value != value) {
-		if (count-- == 0) return false;
-		InterlockedCompareExchangePointer(&s->value, value, NULL);
-	}
-	return true;
-#	else
-#	endif
-}
-
-HL_PRIM void hl_spinlock_release( hl_spinlock *s ) {
-#	if !defined(HL_THREADS)
-	return true;
-#	elif defined(HL_WIN)
-	s->value = NULL;
-#	else
-#	endif
-}
-
 // ----------------- THREAD
 
 HL_PRIM hl_thread *hl_thread_current() {