Add sync.Barrier; Add sync.Blocking_Mutex for unix

This commit is contained in:
gingerBill
2020-06-27 11:23:37 +01:00
parent 2b18f43b65
commit 9fdebebd28
4 changed files with 212 additions and 63 deletions
+24 -26
View File
@@ -4,24 +4,6 @@ package sync
import win32 "core:sys/windows"
import "core:time"
Mutex :: struct {
_critical_section: win32.CRITICAL_SECTION,
}
Blocking_Mutex :: struct {
_handle: win32.SRWLOCK,
}
Condition_Mutex_Ptr :: union{^Mutex, ^Blocking_Mutex};
// Blocks until signalled.
// When signalled, awakens exactly one waiting thread.
Condition :: struct {
_handle: win32.CONDITION_VARIABLE,
mutex: Condition_Mutex_Ptr,
}
// When waited upon, blocks until the internal count is greater than zero, then subtracts one.
// Posting to the semaphore increases the count by one, or the provided amount.
@@ -29,11 +11,6 @@ Semaphore :: struct {
_handle: win32.HANDLE,
}
RW_Lock :: struct {
_handle: win32.SRWLOCK,
}
semaphore_init :: proc(s: ^Semaphore, initial_count := 0) {
s._handle = win32.CreateSemaphoreW(nil, i32(initial_count), 1<<31-1, nil);
}
@@ -53,6 +30,11 @@ semaphore_wait_for :: proc(s: ^Semaphore) {
}
Mutex :: struct {
_critical_section: win32.CRITICAL_SECTION,
}
mutex_init :: proc(m: ^Mutex, spin_count := 0) {
win32.InitializeCriticalSectionAndSpinCount(&m._critical_section, u32(spin_count));
}
@@ -73,6 +55,11 @@ mutex_unlock :: proc(m: ^Mutex) {
win32.LeaveCriticalSection(&m._critical_section);
}
Blocking_Mutex :: struct {
_handle: win32.SRWLOCK,
}
blocking_mutex_init :: proc(m: ^Blocking_Mutex) {
//
}
@@ -94,6 +81,15 @@ blocking_mutex_unlock :: proc(m: ^Blocking_Mutex) {
}
// Blocks until signalled.
// When signalled, awakens exactly one waiting thread.
Condition :: struct {
_handle: win32.CONDITION_VARIABLE,
mutex: Condition_Mutex_Ptr,
}
condition_init :: proc(c: ^Condition, mutex: Condition_Mutex_Ptr) -> bool {
assert(mutex != nil);
win32.InitializeConditionVariable(&c._handle);
@@ -143,6 +139,11 @@ condition_wait_for_timeout :: proc(c: ^Condition, duration: time.Duration) -> bo
RW_Lock :: struct {
_handle: win32.SRWLOCK,
}
rw_lock_init :: proc(l: ^RW_Lock) {
l._handle = win32.SRWLOCK_INIT;
}
@@ -167,6 +168,3 @@ rw_lock_read_unlock :: proc(l: ^RW_Lock) {
rw_lock_write_unlock :: proc(l: ^RW_Lock) {
win32.ReleaseSRWLockExclusive(&l._handle);
}