2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 22:53:22 +03:00
|
|
|
module sync
|
2019-07-24 18:36:20 +03:00
|
|
|
|
2020-07-15 11:22:33 +03:00
|
|
|
import time
|
|
|
|
|
2020-06-22 20:23:42 +03:00
|
|
|
// TODO: The suggestion of using CriticalSection instead of mutex
|
|
|
|
// was discussed. Needs consideration.
|
|
|
|
|
2019-07-24 18:36:20 +03:00
|
|
|
// Mutex HANDLE
|
2020-09-25 13:02:32 +03:00
|
|
|
type MHANDLE = voidptr
|
2020-07-15 11:22:33 +03:00
|
|
|
// Semaphore HANDLE
|
2020-09-25 13:02:32 +03:00
|
|
|
type SHANDLE = voidptr
|
2019-06-22 22:53:22 +03:00
|
|
|
|
2019-10-25 17:24:40 +03:00
|
|
|
//[init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function.
|
2020-05-10 08:58:54 +03:00
|
|
|
|
|
|
|
[ref_only]
|
2019-10-24 13:19:27 +03:00
|
|
|
pub struct Mutex {
|
2019-07-24 18:36:20 +03:00
|
|
|
mut:
|
2019-08-29 11:48:03 +03:00
|
|
|
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
|
2019-06-22 22:53:22 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:53:28 +03:00
|
|
|
[ref_only]
|
|
|
|
pub struct RwMutex {
|
|
|
|
mut:
|
|
|
|
mx C.SRWLOCK // mutex handle
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:22:33 +03:00
|
|
|
pub struct Semaphore {
|
|
|
|
mut:
|
|
|
|
sem SHANDLE
|
|
|
|
}
|
|
|
|
|
2019-08-29 11:48:03 +03:00
|
|
|
enum MutexState {
|
|
|
|
broken
|
|
|
|
waiting
|
|
|
|
released
|
|
|
|
abandoned
|
|
|
|
destroyed
|
|
|
|
}
|
2019-06-22 22:53:22 +03:00
|
|
|
|
2020-01-19 22:32:22 +03:00
|
|
|
pub fn new_mutex() &Mutex {
|
|
|
|
sm := &Mutex{}
|
2019-10-25 17:24:40 +03:00
|
|
|
unsafe {
|
2020-04-02 21:58:07 +03:00
|
|
|
mut m := sm
|
2020-05-27 06:42:48 +03:00
|
|
|
m.mx = MHANDLE(C.CreateMutex(0, false, 0))
|
2019-10-25 17:24:40 +03:00
|
|
|
if isnil(m.mx) {
|
|
|
|
m.state = .broken // handle broken and mutex state are broken
|
|
|
|
return sm
|
2020-04-02 21:58:07 +03:00
|
|
|
}
|
2019-10-25 17:24:40 +03:00
|
|
|
}
|
2020-01-19 22:32:22 +03:00
|
|
|
return sm
|
2019-10-25 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:53:28 +03:00
|
|
|
pub fn new_rwmutex() &RwMutex {
|
|
|
|
m := &RwMutex{}
|
|
|
|
C.InitializeSRWLock(&m.mx)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-07-04 13:44:25 +03:00
|
|
|
pub fn (mut m Mutex) m_lock() {
|
2019-08-29 11:48:03 +03:00
|
|
|
// if mutex handle not initalized
|
|
|
|
if isnil(m.mx) {
|
2020-05-27 06:42:48 +03:00
|
|
|
m.mx = MHANDLE(C.CreateMutex(0, false, 0))
|
2019-08-29 11:48:03 +03:00
|
|
|
if isnil(m.mx) {
|
|
|
|
m.state = .broken // handle broken and mutex state are broken
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 21:58:07 +03:00
|
|
|
state := C.WaitForSingleObject(m.mx, C.INFINITE) // infinite wait
|
2019-10-30 17:29:05 +03:00
|
|
|
/* TODO fix match/enum combo
|
2019-08-29 11:48:03 +03:00
|
|
|
m.state = match state {
|
2020-04-02 21:58:07 +03:00
|
|
|
C.WAIT_ABANDONED { .abandoned }
|
|
|
|
C.WAIT_OBJECT_0 { .waiting }
|
2019-10-30 16:43:40 +03:00
|
|
|
else { .broken }
|
2019-08-29 11:48:03 +03:00
|
|
|
}
|
2019-10-30 17:29:05 +03:00
|
|
|
*/
|
2020-04-02 21:58:07 +03:00
|
|
|
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
|
|
|
|
}
|
2019-06-22 22:53:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut m Mutex) unlock() {
|
2019-08-29 11:48:03 +03:00
|
|
|
if m.state == .waiting {
|
2020-04-02 21:58:07 +03:00
|
|
|
if C.ReleaseMutex(m.mx) {
|
2019-08-29 11:48:03 +03:00
|
|
|
m.state = .broken
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.state = .released
|
2019-07-25 07:25:25 +03:00
|
|
|
}
|
2019-07-24 18:36:20 +03:00
|
|
|
|
2020-07-05 23:53:28 +03:00
|
|
|
// RwMutex has separate read- and write locks
|
|
|
|
pub fn (mut m RwMutex) r_lock() {
|
|
|
|
C.AcquireSRWLockShared(&m.mx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut m RwMutex) w_lock() {
|
|
|
|
C.AcquireSRWLockExclusive(&m.mx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Windows SRWLocks have different function to unlock
|
|
|
|
// So provide two functions here, too, to have a common interface
|
|
|
|
pub fn (mut m RwMutex) r_unlock() {
|
|
|
|
C.ReleaseSRWLockShared(&m.mx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut m RwMutex) w_unlock() {
|
|
|
|
C.ReleaseSRWLockExclusive(&m.mx)
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut m Mutex) destroy() {
|
2019-08-29 11:48:03 +03:00
|
|
|
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
|
2019-07-25 07:25:25 +03:00
|
|
|
}
|
2020-07-15 11:22:33 +03:00
|
|
|
|
2020-08-06 16:28:19 +03:00
|
|
|
[inline]
|
2020-07-15 11:22:33 +03:00
|
|
|
pub fn new_semaphore() Semaphore {
|
2020-08-06 16:28:19 +03:00
|
|
|
return new_semaphore_init(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_semaphore_init(n u32) Semaphore {
|
2020-07-15 11:22:33 +03:00
|
|
|
return Semaphore{
|
2020-08-06 16:28:19 +03:00
|
|
|
sem: SHANDLE(C.CreateSemaphore(0, n, C.INT32_MAX, 0))
|
2020-07-15 11:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s Semaphore) post() {
|
|
|
|
C.ReleaseSemaphore(s.sem, 1, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (s Semaphore) wait() {
|
|
|
|
C.WaitForSingleObject(s.sem, C.INFINITE)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-08-06 16:28:19 +03:00
|
|
|
|
|
|
|
pub fn (s Semaphore) destroy() bool {
|
|
|
|
return C.CloseHandle(s.sem) != 0
|
|
|
|
}
|