fixed some threading primitives
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 20 Oct 2021 04:09:41 +0000 (23:09 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 20 Oct 2021 04:09:41 +0000 (23:09 -0500)
core/runtime/common.onyx
core/sync/barrier.onyx
core/sync/condition_variable.onyx
core/sync/semaphore.onyx

index 030d4d9416ba2e5157b889aa119113d3cddda4a3..8273ddc38521a275b0b202eb676b60b03c55cdf9 100644 (file)
@@ -32,6 +32,7 @@ __runtime_initialize :: () {
 // standard library when you are dropped directly into a function.
 __thread_initialize :: () {
     __tls_base = raw_alloc(alloc.heap_allocator, __tls_size);
+    memory.set(__tls_base, 0, __tls_size);
 
     __initialize(^context);
     context.allocator = alloc.heap_allocator;
index fe954288ee4ea81ceaf8f1dff807851adf6a3ddc..8e7e84252ece5d00caddab166b97cdee0b1a5cee 100644 (file)
@@ -25,15 +25,14 @@ barrier_destroy :: (b: ^Barrier) {
 
 barrier_wait :: (b: ^Barrier) {
     mutex_lock(^b.mutex);
+    defer mutex_unlock(^b.mutex);
 
     local_gen := b.generation;
     b.index += 1;
 
     if b.index < b.thread_count {
-        mutex_unlock(^b.mutex);
-
         while local_gen == b.generation && b.index < b.thread_count {
-            condition_wait(^b.cond);
+            condition_wait(^b.cond, ^b.mutex);
         }
         return;
     }
@@ -41,7 +40,5 @@ barrier_wait :: (b: ^Barrier) {
     b.index = 0;
     b.generation += 1;
     condition_broadcast(^b.cond);
-    
-    mutex_unlock(^b.mutex);
     return;
 }
\ No newline at end of file
index 4ad53dcaba72fb5c6e857f65c77dec381776fa68..b670c085bb6b4b1f5c079d97a5b8646ff92e8c01 100644 (file)
@@ -23,16 +23,18 @@ condition_destroy :: (c: ^Condition_Variable) {
     mutex_destroy(^c.mutex);
 }
 
-condition_wait :: (c: ^Condition_Variable) {
-    self: Condition_Variable.Node;
-
-    critical_section(^c.mutex, #code {
-        self.next = c.queue;
-        c.queue   = ^self;
-        semaphore_init(^self.semaphore, 0);
-    });
-
-    semaphore_wait(^self.semaphore);
+condition_wait :: (c: ^Condition_Variable, m: ^Mutex) {
+    node: Condition_Variable.Node;
+
+    mutex_lock(^c.mutex);
+    node.next = c.queue;
+    c.queue   = ^node;
+    semaphore_init(^node.semaphore, 0);
+    mutex_unlock(^c.mutex);
+
+    mutex_unlock(m);
+    semaphore_wait(^node.semaphore);
+    mutex_lock(m);
 }
 
 condition_signal :: (c: ^Condition_Variable) {
@@ -40,7 +42,6 @@ condition_signal :: (c: ^Condition_Variable) {
 
     if c.queue != null {
         semaphore_post(^c.queue.semaphore);
-        semaphore_destroy(^c.queue.semaphore);
         c.queue = c.queue.next;
     }
 }
@@ -50,7 +51,6 @@ condition_broadcast :: (c: ^Condition_Variable) {
 
     while c.queue != null {
         semaphore_post(^c.queue.semaphore);
-        semaphore_destroy(^c.queue.semaphore);
         c.queue = c.queue.next;
     }
 }
\ No newline at end of file
index 9ee288fa57dae7bbbaa84d6041b389c307058873..2a31af6d9e5a09baf52833d184ef1ed9dbe609f6 100644 (file)
@@ -17,9 +17,9 @@ semaphore_destroy :: (s: ^Semaphore) {
     mutex_destroy(^s.mutex);
 }
 
-semaphore_post :: (s: ^Semaphore) {
+semaphore_post :: (s: ^Semaphore, count := 1) {
     scoped_mutex(^s.mutex);
-    s.counter += 1;
+    s.counter += count;
     __atomic_notify(^s.counter, maximum = 1);
 }