2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 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
|
|
|
|
|
2021-02-09 13:44:18 +03:00
|
|
|
#include <synchapi.h>
|
2021-06-18 12:44:18 +03:00
|
|
|
#include <time.h>
|
2021-02-09 13:44:18 +03:00
|
|
|
|
2021-06-18 12:44:18 +03:00
|
|
|
fn C.GetSystemTimeAsFileTime(lpSystemTimeAsFileTime &C._FILETIME)
|
2021-01-29 21:52:14 +03:00
|
|
|
fn C.InitializeConditionVariable(voidptr)
|
|
|
|
fn C.WakeConditionVariable(voidptr)
|
|
|
|
fn C.SleepConditionVariableSRW(voidptr, voidptr, u32, u32) int
|
|
|
|
|
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
|
2021-05-08 13:32:29 +03:00
|
|
|
|
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
|
|
|
|
2021-01-29 21:52:14 +03:00
|
|
|
// `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
|
2021-02-13 17:52:01 +03:00
|
|
|
[heap]
|
2019-10-24 13:19:27 +03:00
|
|
|
pub struct Mutex {
|
2019-07-24 18:36:20 +03:00
|
|
|
mut:
|
2021-05-08 13:32:29 +03:00
|
|
|
mx C.SRWLOCK // mutex handle
|
2019-06-22 22:53:22 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 17:52:01 +03:00
|
|
|
[heap]
|
2020-07-05 23:53:28 +03:00
|
|
|
pub struct RwMutex {
|
|
|
|
mut:
|
2021-05-08 13:32:29 +03:00
|
|
|
mx C.SRWLOCK // mutex handle
|
2020-07-05 23:53:28 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 17:52:01 +03:00
|
|
|
[heap]
|
2022-06-19 17:42:22 +03:00
|
|
|
pub struct Semaphore {
|
2021-05-08 13:32:29 +03:00
|
|
|
mtx C.SRWLOCK
|
2021-01-29 21:52:14 +03:00
|
|
|
cond C.CONDITION_VARIABLE
|
2020-07-15 11:22:33 +03:00
|
|
|
mut:
|
2021-01-29 21:52:14 +03:00
|
|
|
count u32
|
2019-08-29 11:48:03 +03:00
|
|
|
}
|
2019-06-22 22:53:22 +03:00
|
|
|
|
2020-01-19 22:32:22 +03:00
|
|
|
pub fn new_mutex() &Mutex {
|
2021-01-29 21:52:14 +03:00
|
|
|
mut m := &Mutex{}
|
|
|
|
m.init()
|
|
|
|
return m
|
2019-10-25 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:53:28 +03:00
|
|
|
pub fn new_rwmutex() &RwMutex {
|
2021-01-29 21:52:14 +03:00
|
|
|
mut m := &RwMutex{}
|
|
|
|
m.init()
|
2020-07-05 23:53:28 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-01-29 21:52:14 +03:00
|
|
|
pub fn (mut m Mutex) init() {
|
|
|
|
C.InitializeSRWLock(&m.mx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut m RwMutex) init() {
|
|
|
|
C.InitializeSRWLock(&m.mx)
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:23:55 +03:00
|
|
|
pub fn (mut m Mutex) @lock() {
|
2021-01-29 21:52:14 +03:00
|
|
|
C.AcquireSRWLockExclusive(&m.mx)
|
2019-06-22 22:53:22 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut m Mutex) unlock() {
|
2021-01-29 21:52:14 +03:00
|
|
|
C.ReleaseSRWLockExclusive(&m.mx)
|
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
|
2021-01-30 17:23:55 +03:00
|
|
|
pub fn (mut m RwMutex) @rlock() {
|
2020-07-05 23:53:28 +03:00
|
|
|
C.AcquireSRWLockShared(&m.mx)
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:23:55 +03:00
|
|
|
pub fn (mut m RwMutex) @lock() {
|
2020-07-05 23:53:28 +03:00
|
|
|
C.AcquireSRWLockExclusive(&m.mx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Windows SRWLocks have different function to unlock
|
|
|
|
// So provide two functions here, too, to have a common interface
|
2021-01-30 17:23:55 +03:00
|
|
|
pub fn (mut m RwMutex) runlock() {
|
2020-07-05 23:53:28 +03:00
|
|
|
C.ReleaseSRWLockShared(&m.mx)
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:23:55 +03:00
|
|
|
pub fn (mut m RwMutex) unlock() {
|
2020-07-05 23:53:28 +03:00
|
|
|
C.ReleaseSRWLockExclusive(&m.mx)
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut m Mutex) destroy() {
|
2021-01-29 21:52:14 +03:00
|
|
|
// nothing to do
|
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]
|
2021-01-29 21:52:14 +03:00
|
|
|
pub fn new_semaphore() &Semaphore {
|
2020-08-06 16:28:19 +03:00
|
|
|
return new_semaphore_init(0)
|
|
|
|
}
|
|
|
|
|
2021-01-29 21:52:14 +03:00
|
|
|
pub fn new_semaphore_init(n u32) &Semaphore {
|
|
|
|
mut sem := &Semaphore{}
|
|
|
|
sem.init(n)
|
|
|
|
return sem
|
2020-07-15 11:22:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:52:14 +03:00
|
|
|
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 {
|
2021-05-08 13:32:29 +03:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c + 1) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
|
|
|
C.AcquireSRWLockExclusive(&sem.mtx)
|
|
|
|
c = C.atomic_fetch_add_u32(&sem.count, 1)
|
|
|
|
if c == 0 {
|
|
|
|
C.WakeConditionVariable(&sem.cond)
|
|
|
|
}
|
|
|
|
C.ReleaseSRWLockExclusive(&sem.mtx)
|
2020-07-15 11:22:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:52:14 +03:00
|
|
|
pub fn (mut sem Semaphore) wait() {
|
|
|
|
mut c := C.atomic_load_u32(&sem.count)
|
|
|
|
for c > 0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
|
|
|
C.AcquireSRWLockExclusive(&sem.mtx)
|
|
|
|
c = C.atomic_load_u32(&sem.count)
|
2021-05-08 13:32:29 +03:00
|
|
|
|
|
|
|
outer: for {
|
2021-01-29 21:52:14 +03:00
|
|
|
if c == 0 {
|
|
|
|
C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, C.INFINITE, 0)
|
|
|
|
c = C.atomic_load_u32(&sem.count)
|
|
|
|
}
|
|
|
|
for c > 0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
2021-01-29 21:52:14 +03:00
|
|
|
if c > 1 {
|
|
|
|
C.WakeConditionVariable(&sem.cond)
|
|
|
|
}
|
2021-02-03 17:19:42 +03:00
|
|
|
break outer
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.ReleaseSRWLockExclusive(&sem.mtx)
|
2020-07-15 11:22:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:52:14 +03:00
|
|
|
pub fn (mut sem Semaphore) try_wait() bool {
|
|
|
|
mut c := C.atomic_load_u32(&sem.count)
|
|
|
|
for c > 0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
|
|
|
return false
|
2020-07-15 11:22:33 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:52:14 +03:00
|
|
|
pub fn (mut sem Semaphore) timed_wait(timeout time.Duration) bool {
|
|
|
|
mut c := C.atomic_load_u32(&sem.count)
|
|
|
|
for c > 0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
2021-06-18 12:44:18 +03:00
|
|
|
mut ft_start := C._FILETIME{}
|
|
|
|
C.GetSystemTimeAsFileTime(&ft_start)
|
|
|
|
time_end := ((u64(ft_start.dwHighDateTime) << 32) | ft_start.dwLowDateTime) +
|
|
|
|
u64(timeout / (100 * time.nanosecond))
|
2023-07-31 11:18:12 +03:00
|
|
|
mut t_ms := u32(timeout.sys_milliseconds())
|
2021-01-29 21:52:14 +03:00
|
|
|
C.AcquireSRWLockExclusive(&sem.mtx)
|
|
|
|
mut res := 0
|
|
|
|
c = C.atomic_load_u32(&sem.count)
|
2021-05-08 13:32:29 +03:00
|
|
|
|
|
|
|
outer: for {
|
2021-01-29 21:52:14 +03:00
|
|
|
if c == 0 {
|
|
|
|
res = C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, t_ms, 0)
|
|
|
|
if res == 0 {
|
2021-02-03 17:19:42 +03:00
|
|
|
break outer
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
|
|
|
c = C.atomic_load_u32(&sem.count)
|
|
|
|
}
|
|
|
|
for c > 0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
2021-01-29 21:52:14 +03:00
|
|
|
if c > 1 {
|
|
|
|
C.WakeConditionVariable(&sem.cond)
|
|
|
|
}
|
2021-02-03 17:19:42 +03:00
|
|
|
break outer
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-18 12:44:18 +03:00
|
|
|
C.GetSystemTimeAsFileTime(&ft_start)
|
|
|
|
time_now := ((u64(ft_start.dwHighDateTime) << 32) | ft_start.dwLowDateTime) // in 100ns
|
|
|
|
if time_now > time_end {
|
|
|
|
break outer // timeout exceeded
|
|
|
|
}
|
|
|
|
t_ms = u32((time_end - time_now) / 10000)
|
2021-01-29 21:52:14 +03:00
|
|
|
}
|
|
|
|
C.ReleaseSRWLockExclusive(&sem.mtx)
|
|
|
|
return res != 0
|
2020-07-15 11:22:33 +03:00
|
|
|
}
|
2020-08-06 16:28:19 +03:00
|
|
|
|
2021-06-18 12:44:18 +03:00
|
|
|
pub fn (s Semaphore) destroy() {
|
2020-08-06 16:28:19 +03:00
|
|
|
}
|