From: Brendan Hansen Date: Wed, 20 Oct 2021 04:09:41 +0000 (-0500) Subject: fixed some threading primitives X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a293f9e6d08fad259f5ae98efc4f1317bdfeb2fd;p=onyx.git fixed some threading primitives --- diff --git a/core/runtime/common.onyx b/core/runtime/common.onyx index 030d4d94..8273ddc3 100644 --- a/core/runtime/common.onyx +++ b/core/runtime/common.onyx @@ -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; diff --git a/core/sync/barrier.onyx b/core/sync/barrier.onyx index fe954288..8e7e8425 100644 --- a/core/sync/barrier.onyx +++ b/core/sync/barrier.onyx @@ -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 diff --git a/core/sync/condition_variable.onyx b/core/sync/condition_variable.onyx index 4ad53dca..b670c085 100644 --- a/core/sync/condition_variable.onyx +++ b/core/sync/condition_variable.onyx @@ -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 diff --git a/core/sync/semaphore.onyx b/core/sync/semaphore.onyx index 9ee288fa..2a31af6d 100644 --- a/core/sync/semaphore.onyx +++ b/core/sync/semaphore.onyx @@ -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); }