1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sync: don't force Mutex and Semaphore to be reference (#8331)

This commit is contained in:
Uwe Krüger
2021-01-29 19:52:14 +01:00
committed by GitHub
parent d370e4de9f
commit 4a955d9c54
18 changed files with 541 additions and 296 deletions

View File

@ -5,6 +5,10 @@ module sync
import time
fn C.InitializeConditionVariable(voidptr)
fn C.WakeConditionVariable(voidptr)
fn C.SleepConditionVariableSRW(voidptr, voidptr, u32, u32) int
// TODO: The suggestion of using CriticalSection instead of mutex
// was discussed. Needs consideration.
@ -15,90 +19,50 @@ type SHANDLE = voidptr
//[init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function.
[ref_only]
// `SRWLOCK` is much more performant that `Mutex` on Windows, so use that in both cases since we don't want to share with other processes
pub struct Mutex {
mut:
mx MHANDLE // mutex handle
state MutexState // mutex state
cycle_wait i64 // waiting cycles (implemented only with atomic)
cycle_woken i64 // woken cycles ^
reader_sem u32 // reader semarphone
writer_sem u32 // writer semaphones
mx C.SRWLOCK // mutex handle
}
[ref_only]
pub struct RwMutex {
mut:
mx C.SRWLOCK // mutex handle
}
pub struct Semaphore {
struct Semaphore {
mtx C.SRWLOCK
cond C.CONDITION_VARIABLE
mut:
sem SHANDLE
}
enum MutexState {
broken
waiting
released
abandoned
destroyed
count u32
}
pub fn new_mutex() &Mutex {
sm := &Mutex{}
unsafe {
mut m := sm
m.mx = MHANDLE(C.CreateMutex(0, false, 0))
if isnil(m.mx) {
m.state = .broken // handle broken and mutex state are broken
return sm
}
}
return sm
}
pub fn new_rwmutex() &RwMutex {
m := &RwMutex{}
C.InitializeSRWLock(&m.mx)
mut m := &Mutex{}
m.init()
return m
}
pub fn new_rwmutex() &RwMutex {
mut m := &RwMutex{}
m.init()
return m
}
pub fn (mut m Mutex) init() {
C.InitializeSRWLock(&m.mx)
}
pub fn (mut m RwMutex) init() {
C.InitializeSRWLock(&m.mx)
}
pub fn (mut m Mutex) m_lock() {
// if mutex handle not initalized
if isnil(m.mx) {
m.mx = MHANDLE(C.CreateMutex(0, false, 0))
if isnil(m.mx) {
m.state = .broken // handle broken and mutex state are broken
return
}
}
state := C.WaitForSingleObject(m.mx, C.INFINITE) // infinite wait
/* TODO fix match/enum combo
m.state = match state {
C.WAIT_ABANDONED { .abandoned }
C.WAIT_OBJECT_0 { .waiting }
else { .broken }
}
*/
if state == C.WAIT_ABANDONED {
m.state = .abandoned
// FIXME Use C constant instead
} else if state == 0 /* C.WAIT_OBJECT_0 */ {
m.state = .waiting
} else {
m.state = .broken
}
C.AcquireSRWLockExclusive(&m.mx)
}
pub fn (mut m Mutex) unlock() {
if m.state == .waiting {
if C.ReleaseMutex(m.mx) {
m.state = .broken
return
}
}
m.state = .released
C.ReleaseSRWLockExclusive(&m.mx)
}
// RwMutex has separate read- and write locks
@ -121,40 +85,103 @@ pub fn (mut m RwMutex) w_unlock() {
}
pub fn (mut m Mutex) destroy() {
if m.state == .waiting {
m.unlock() // unlock mutex before destroying
}
C.CloseHandle(m.mx) // destroy mutex
m.state = .destroyed // setting up reference to invalid state
// nothing to do
}
[inline]
pub fn new_semaphore() Semaphore {
pub fn new_semaphore() &Semaphore {
return new_semaphore_init(0)
}
pub fn new_semaphore_init(n u32) Semaphore {
return Semaphore{
sem: SHANDLE(C.CreateSemaphore(0, n, C.INT32_MAX, 0))
pub fn new_semaphore_init(n u32) &Semaphore {
mut sem := &Semaphore{}
sem.init(n)
return sem
}
pub fn (mut sem Semaphore) init(n u32) {
C.atomic_store_u32(&sem.count, n)
C.InitializeSRWLock(&sem.mtx)
C.InitializeConditionVariable(&sem.cond)
}
pub fn (mut sem Semaphore) post() {
mut c := C.atomic_load_u32(&sem.count)
for c > 1 {
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c+1) { return }
}
C.AcquireSRWLockExclusive(&sem.mtx)
c = C.atomic_fetch_add_u32(&sem.count, 1)
if c == 0 {
C.WakeConditionVariable(&sem.cond)
}
C.ReleaseSRWLockExclusive(&sem.mtx)
}
pub fn (s Semaphore) post() {
C.ReleaseSemaphore(s.sem, 1, 0)
pub fn (mut sem Semaphore) wait() {
mut c := C.atomic_load_u32(&sem.count)
for c > 0 {
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return }
}
C.AcquireSRWLockExclusive(&sem.mtx)
c = C.atomic_load_u32(&sem.count)
for {
if c == 0 {
C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, C.INFINITE, 0)
c = C.atomic_load_u32(&sem.count)
}
for c > 0 {
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) {
if c > 1 {
C.WakeConditionVariable(&sem.cond)
}
goto unlock
}
}
}
unlock:
C.ReleaseSRWLockExclusive(&sem.mtx)
}
pub fn (s Semaphore) wait() {
C.WaitForSingleObject(s.sem, C.INFINITE)
pub fn (mut sem Semaphore) try_wait() bool {
mut c := C.atomic_load_u32(&sem.count)
for c > 0 {
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return true }
}
return false
}
pub fn (s Semaphore) try_wait() bool {
return C.WaitForSingleObject(s.sem, 0) == 0
}
pub fn (s Semaphore) timed_wait(timeout time.Duration) bool {
return C.WaitForSingleObject(s.sem, timeout / time.millisecond) == 0
pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool {
mut c := C.atomic_load_u32(&sem.count)
for c > 0 {
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) { return true }
}
C.AcquireSRWLockExclusive(&sem.mtx)
t_ms := u32(timeout / time.millisecond)
mut res := 0
c = C.atomic_load_u32(&sem.count)
for {
if c == 0 {
res = C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, t_ms, 0)
if res == 0 {
goto unlock
}
c = C.atomic_load_u32(&sem.count)
}
for c > 0 {
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c-1) {
if c > 1 {
C.WakeConditionVariable(&sem.cond)
}
goto unlock
}
}
}
unlock:
C.ReleaseSRWLockExclusive(&sem.mtx)
return res != 0
}
pub fn (s Semaphore) destroy() bool {
return C.CloseHandle(s.sem) != 0
return true
}