2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:53:22 +02:00
|
|
|
module sync
|
2019-07-24 17:36:20 +02:00
|
|
|
|
2020-07-15 10:22:33 +02:00
|
|
|
import time
|
|
|
|
|
2021-02-09 11:44:18 +01:00
|
|
|
#include <synchapi.h>
|
2021-06-18 11:44:18 +02:00
|
|
|
#include <time.h>
|
2021-02-09 11:44:18 +01:00
|
|
|
|
2021-06-18 11:44:18 +02:00
|
|
|
fn C.GetSystemTimeAsFileTime(lpSystemTimeAsFileTime &C._FILETIME)
|
2021-01-29 19:52:14 +01:00
|
|
|
fn C.InitializeConditionVariable(voidptr)
|
|
|
|
fn C.WakeConditionVariable(voidptr)
|
|
|
|
fn C.SleepConditionVariableSRW(voidptr, voidptr, u32, u32) int
|
|
|
|
|
2020-06-22 19:23:42 +02:00
|
|
|
// TODO: The suggestion of using CriticalSection instead of mutex
|
|
|
|
// was discussed. Needs consideration.
|
|
|
|
|
2019-07-24 17:36:20 +02:00
|
|
|
// Mutex HANDLE
|
2020-09-25 12:02:32 +02:00
|
|
|
type MHANDLE = voidptr
|
2021-05-08 12:32:29 +02:00
|
|
|
|
2020-07-15 10:22:33 +02:00
|
|
|
// Semaphore HANDLE
|
2020-09-25 12:02:32 +02:00
|
|
|
type SHANDLE = voidptr
|
2019-06-22 21:53:22 +02:00
|
|
|
|
2019-10-25 16:24:40 +02: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 07:58:54 +02:00
|
|
|
|
2021-01-29 19:52:14 +01: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 15:52:01 +01:00
|
|
|
[heap]
|
2019-10-24 12:19:27 +02:00
|
|
|
pub struct Mutex {
|
2019-07-24 17:36:20 +02:00
|
|
|
mut:
|
2021-05-08 12:32:29 +02:00
|
|
|
mx C.SRWLOCK // mutex handle
|
2019-06-22 21:53:22 +02:00
|
|
|
}
|
|
|
|
|
2021-02-13 15:52:01 +01:00
|
|
|
[heap]
|
2020-07-05 22:53:28 +02:00
|
|
|
pub struct RwMutex {
|
|
|
|
mut:
|
2021-05-08 12:32:29 +02:00
|
|
|
mx C.SRWLOCK // mutex handle
|
2020-07-05 22:53:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-13 15:52:01 +01:00
|
|
|
[heap]
|
2021-01-29 19:52:14 +01:00
|
|
|
struct Semaphore {
|
2021-05-08 12:32:29 +02:00
|
|
|
mtx C.SRWLOCK
|
2021-01-29 19:52:14 +01:00
|
|
|
cond C.CONDITION_VARIABLE
|
2020-07-15 10:22:33 +02:00
|
|
|
mut:
|
2021-01-29 19:52:14 +01:00
|
|
|
count u32
|
2019-08-29 10:48:03 +02:00
|
|
|
}
|
2019-06-22 21:53:22 +02:00
|
|
|
|
2020-01-19 20:32:22 +01:00
|
|
|
pub fn new_mutex() &Mutex {
|
2021-01-29 19:52:14 +01:00
|
|
|
mut m := &Mutex{}
|
|
|
|
m.init()
|
|
|
|
return m
|
2019-10-25 16:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 22:53:28 +02:00
|
|
|
pub fn new_rwmutex() &RwMutex {
|
2021-01-29 19:52:14 +01:00
|
|
|
mut m := &RwMutex{}
|
|
|
|
m.init()
|
2020-07-05 22:53:28 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:52:14 +01:00
|
|
|
pub fn (mut m Mutex) init() {
|
|
|
|
C.InitializeSRWLock(&m.mx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut m RwMutex) init() {
|
|
|
|
C.InitializeSRWLock(&m.mx)
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:23:55 +01:00
|
|
|
pub fn (mut m Mutex) @lock() {
|
2021-01-29 19:52:14 +01:00
|
|
|
C.AcquireSRWLockExclusive(&m.mx)
|
2019-06-22 21:53:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut m Mutex) unlock() {
|
2021-01-29 19:52:14 +01:00
|
|
|
C.ReleaseSRWLockExclusive(&m.mx)
|
2019-07-25 06:25:25 +02:00
|
|
|
}
|
2019-07-24 17:36:20 +02:00
|
|
|
|
2020-07-05 22:53:28 +02:00
|
|
|
// RwMutex has separate read- and write locks
|
2021-01-30 15:23:55 +01:00
|
|
|
pub fn (mut m RwMutex) @rlock() {
|
2020-07-05 22:53:28 +02:00
|
|
|
C.AcquireSRWLockShared(&m.mx)
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:23:55 +01:00
|
|
|
pub fn (mut m RwMutex) @lock() {
|
2020-07-05 22:53:28 +02: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 15:23:55 +01:00
|
|
|
pub fn (mut m RwMutex) runlock() {
|
2020-07-05 22:53:28 +02:00
|
|
|
C.ReleaseSRWLockShared(&m.mx)
|
|
|
|
}
|
|
|
|
|
2021-01-30 15:23:55 +01:00
|
|
|
pub fn (mut m RwMutex) unlock() {
|
2020-07-05 22:53:28 +02:00
|
|
|
C.ReleaseSRWLockExclusive(&m.mx)
|
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut m Mutex) destroy() {
|
2021-01-29 19:52:14 +01:00
|
|
|
// nothing to do
|
2019-07-25 06:25:25 +02:00
|
|
|
}
|
2020-07-15 10:22:33 +02:00
|
|
|
|
2020-08-06 15:28:19 +02:00
|
|
|
[inline]
|
2021-01-29 19:52:14 +01:00
|
|
|
pub fn new_semaphore() &Semaphore {
|
2020-08-06 15:28:19 +02:00
|
|
|
return new_semaphore_init(0)
|
|
|
|
}
|
|
|
|
|
2021-01-29 19:52:14 +01:00
|
|
|
pub fn new_semaphore_init(n u32) &Semaphore {
|
|
|
|
mut sem := &Semaphore{}
|
|
|
|
sem.init(n)
|
|
|
|
return sem
|
2020-07-15 10:22:33 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:52:14 +01: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 12:32:29 +02:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c + 1) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-29 19:52:14 +01: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 10:22:33 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:52:14 +01:00
|
|
|
pub fn (mut sem Semaphore) wait() {
|
|
|
|
mut c := C.atomic_load_u32(&sem.count)
|
|
|
|
for c > 0 {
|
2021-05-08 12:32:29 +02:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-29 19:52:14 +01:00
|
|
|
}
|
|
|
|
C.AcquireSRWLockExclusive(&sem.mtx)
|
|
|
|
c = C.atomic_load_u32(&sem.count)
|
2021-05-08 12:32:29 +02:00
|
|
|
|
|
|
|
outer: for {
|
2021-01-29 19:52:14 +01: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 12:32:29 +02:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
2021-01-29 19:52:14 +01:00
|
|
|
if c > 1 {
|
|
|
|
C.WakeConditionVariable(&sem.cond)
|
|
|
|
}
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2021-01-29 19:52:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.ReleaseSRWLockExclusive(&sem.mtx)
|
2020-07-15 10:22:33 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:52:14 +01:00
|
|
|
pub fn (mut sem Semaphore) try_wait() bool {
|
|
|
|
mut c := C.atomic_load_u32(&sem.count)
|
|
|
|
for c > 0 {
|
2021-05-08 12:32:29 +02:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-29 19:52:14 +01:00
|
|
|
}
|
|
|
|
return false
|
2020-07-15 10:22:33 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:52:14 +01: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 12:32:29 +02:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-29 19:52:14 +01:00
|
|
|
}
|
2021-06-18 11:44:18 +02: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))
|
|
|
|
mut t_ms := u32(timeout / time.millisecond)
|
2021-01-29 19:52:14 +01:00
|
|
|
C.AcquireSRWLockExclusive(&sem.mtx)
|
|
|
|
mut res := 0
|
|
|
|
c = C.atomic_load_u32(&sem.count)
|
2021-05-08 12:32:29 +02:00
|
|
|
|
|
|
|
outer: for {
|
2021-01-29 19:52:14 +01:00
|
|
|
if c == 0 {
|
|
|
|
res = C.SleepConditionVariableSRW(&sem.cond, &sem.mtx, t_ms, 0)
|
|
|
|
if res == 0 {
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2021-01-29 19:52:14 +01:00
|
|
|
}
|
|
|
|
c = C.atomic_load_u32(&sem.count)
|
|
|
|
}
|
|
|
|
for c > 0 {
|
2021-05-08 12:32:29 +02:00
|
|
|
if C.atomic_compare_exchange_weak_u32(&sem.count, &c, c - 1) {
|
2021-01-29 19:52:14 +01:00
|
|
|
if c > 1 {
|
|
|
|
C.WakeConditionVariable(&sem.cond)
|
|
|
|
}
|
2021-02-03 15:19:42 +01:00
|
|
|
break outer
|
2021-01-29 19:52:14 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-18 11:44:18 +02: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 19:52:14 +01:00
|
|
|
}
|
|
|
|
C.ReleaseSRWLockExclusive(&sem.mtx)
|
|
|
|
return res != 0
|
2020-07-15 10:22:33 +02:00
|
|
|
}
|
2020-08-06 15:28:19 +02:00
|
|
|
|
2021-06-18 11:44:18 +02:00
|
|
|
pub fn (s Semaphore) destroy() {
|
2020-08-06 15:28:19 +02:00
|
|
|
}
|