mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Add sync.Benaphore
This commit is contained in:
+33
-1
@@ -2,7 +2,7 @@ package sync
|
|||||||
|
|
||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
|
|
||||||
cpu_relax :: inline proc() {
|
cpu_relax :: inline proc "contextless" () {
|
||||||
intrinsics.cpu_relax();
|
intrinsics.cpu_relax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,3 +29,35 @@ ticket_mutex_lock :: inline proc(m: ^Ticket_Mutex) {
|
|||||||
ticket_mutex_unlock :: inline proc(m: ^Ticket_Mutex) {
|
ticket_mutex_unlock :: inline proc(m: ^Ticket_Mutex) {
|
||||||
atomic_add(&m.serving, 1, .Relaxed);
|
atomic_add(&m.serving, 1, .Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Benaphore :: struct {
|
||||||
|
counter: int,
|
||||||
|
sema: Semaphore,
|
||||||
|
}
|
||||||
|
|
||||||
|
benaphore_init :: proc(b: ^Benaphore) {
|
||||||
|
intrinsics.atomic_store(&b.counter, 0);
|
||||||
|
semaphore_init(&b.sema);
|
||||||
|
}
|
||||||
|
|
||||||
|
benaphore_destroy :: proc(b: ^Benaphore) {
|
||||||
|
semaphore_destroy(&b.sema);
|
||||||
|
}
|
||||||
|
|
||||||
|
benaphore_lock :: proc(b: ^Benaphore) {
|
||||||
|
if intrinsics.atomic_add_acq(&b.counter, 1) > 1 {
|
||||||
|
semaphore_wait_for(&b.sema);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
benaphore_try_lock :: proc(b: ^Benaphore) -> bool {
|
||||||
|
v, _ := intrinsics.atomic_cxchg_acq(&b.counter, 1, 0);
|
||||||
|
return v == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
benaphore_unlock :: proc(b: ^Benaphore) {
|
||||||
|
if intrinsics.atomic_sub_rel(&b.counter, 1) > 0 {
|
||||||
|
semaphore_post(&b.sema);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ Blocking_Mutex :: struct {
|
|||||||
|
|
||||||
|
|
||||||
blocking_mutex_init :: proc(m: ^Blocking_Mutex) {
|
blocking_mutex_init :: proc(m: ^Blocking_Mutex) {
|
||||||
//
|
m^ = Blocking_Mutex{};
|
||||||
}
|
}
|
||||||
|
|
||||||
blocking_mutex_destroy :: proc(m: ^Blocking_Mutex) {
|
blocking_mutex_destroy :: proc(m: ^Blocking_Mutex) {
|
||||||
|
|||||||
Reference in New Issue
Block a user