sync2.Auto_Reset_Event; Make atomic operations names clearer

This commit is contained in:
gingerBill
2021-05-06 14:00:01 +01:00
parent 27f5aadd5a
commit 502ad0c10b
5 changed files with 138 additions and 61 deletions
+42 -12
View File
@@ -122,6 +122,36 @@ barrier_wait :: proc(b: ^Barrier) -> (is_leader: bool) {
}
Auto_Reset_Event :: struct {
// status == 0: Event is reset and no threads are waiting
// status == 1: Event is signaled
// status == -N: Event is reset and N threads are waiting
status: i32,
sema: Sema,
}
auto_reset_event_signal :: proc(e: ^Auto_Reset_Event) {
old_status := atomic_load_relaxed(&e.status);
for {
new_status := old_status + 1 if old_status < 1 else 1;
if _, ok := atomic_compare_exchange_weak_release(&e.status, old_status, new_status); ok {
break;
}
if old_status < 0 {
sema_post(&e.sema);
}
}
}
auto_reset_event_wait :: proc(e: ^Auto_Reset_Event) {
old_status := atomic_sub_acquire(&e.status, 1);
if old_status < 1 {
sema_wait(&e.sema);
}
}
Ticket_Mutex :: struct {
ticket: uint,
@@ -130,7 +160,7 @@ Ticket_Mutex :: struct {
ticket_mutex_lock :: #force_inline proc(m: ^Ticket_Mutex) {
ticket := atomic_add_relaxed(&m.ticket, 1);
for ticket != atomic_load_acq(&m.serving) {
for ticket != atomic_load_acquire(&m.serving) {
cpu_relax();
}
}
@@ -142,23 +172,23 @@ ticket_mutex_unlock :: #force_inline proc(m: ^Ticket_Mutex) {
Benaphore :: struct {
counter: int,
counter: i32,
sema: Sema,
}
benaphore_lock :: proc(b: ^Benaphore) {
if atomic_add_acq(&b.counter, 1) > 1 {
if atomic_add_acquire(&b.counter, 1) > 1 {
sema_wait(&b.sema);
}
}
benaphore_try_lock :: proc(b: ^Benaphore) -> bool {
v, _ := atomic_cxchg_acq(&b.counter, 1, 0);
v, _ := atomic_compare_exchange_strong_acquire(&b.counter, 1, 0);
return v == 0;
}
benaphore_unlock :: proc(b: ^Benaphore) {
if atomic_sub_rel(&b.counter, 1) > 0 {
if atomic_sub_release(&b.counter, 1) > 0 {
sema_post(&b.sema);
}
}
@@ -166,13 +196,13 @@ benaphore_unlock :: proc(b: ^Benaphore) {
Recursive_Benaphore :: struct {
counter: int,
owner: int,
recursion: int,
recursion: i32,
sema: Sema,
}
recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) {
tid := runtime.current_thread_id();
if atomic_add_acq(&b.counter, 1) > 1 {
if atomic_add_acquire(&b.counter, 1) > 1 {
if tid != b.owner {
sema_wait(&b.sema);
}
@@ -185,10 +215,10 @@ recursive_benaphore_lock :: proc(b: ^Recursive_Benaphore) {
recursive_benaphore_try_lock :: proc(b: ^Recursive_Benaphore) -> bool {
tid := runtime.current_thread_id();
if b.owner == tid {
atomic_add_acq(&b.counter, 1);
atomic_add_acquire(&b.counter, 1);
}
if v, _ := atomic_cxchg_acq(&b.counter, 1, 0); v != 0 {
if v, _ := atomic_compare_exchange_strong_acquire(&b.counter, 1, 0); v != 0 {
return false;
}
// inside the lock
@@ -205,7 +235,7 @@ recursive_benaphore_unlock :: proc(b: ^Recursive_Benaphore) {
if recursion == 0 {
b.owner = 0;
}
if atomic_sub_rel(&b.counter, 1) > 0 {
if atomic_sub_release(&b.counter, 1) > 0 {
if recursion == 0 {
sema_post(&b.sema);
}
@@ -223,7 +253,7 @@ Once :: struct {
}
once_do :: proc(o: ^Once, fn: proc()) {
if atomic_load_acq(&o.done) == false {
if atomic_load_acquire(&o.done) == false {
_once_do_slow(o, fn);
}
}
@@ -234,6 +264,6 @@ _once_do_slow :: proc(o: ^Once, fn: proc()) {
defer mutex_unlock(&o.m);
if !o.done {
fn();
atomic_store_rel(&o.done, true);
atomic_store_release(&o.done, true);
}
}