2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 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
|
|
|
|
|
|
|
// Mutex HANDLE
|
|
|
|
type MHANDLE 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.
|
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
|
|
|
}
|
|
|
|
|
2019-08-29 11:48:03 +03:00
|
|
|
enum MutexState {
|
|
|
|
broken
|
|
|
|
waiting
|
|
|
|
released
|
|
|
|
abandoned
|
|
|
|
destroyed
|
|
|
|
}
|
2019-06-22 22:53:22 +03:00
|
|
|
|
2019-07-24 18:36:20 +03:00
|
|
|
const (
|
2019-08-29 11:48:03 +03:00
|
|
|
INFINITE = 0xffffffff
|
2019-07-24 18:36:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject#return-value
|
|
|
|
const (
|
2019-08-29 11:48:03 +03:00
|
|
|
WAIT_ABANDONED = 0x00000080
|
|
|
|
WAIT_IO_COMPLETION = 0x000000C0
|
|
|
|
WAIT_OBJECT_0 = 0x00000000
|
|
|
|
WAIT_TIMEOUT = 0x00000102
|
|
|
|
WAIT_FAILED = 0xFFFFFFFF
|
2019-07-24 18:36:20 +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 {
|
|
|
|
mut m := sm
|
|
|
|
m.mx = C.CreateMutex(0, false, 0)
|
|
|
|
if isnil(m.mx) {
|
|
|
|
m.state = .broken // handle broken and mutex state are broken
|
|
|
|
return sm
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 22:32:22 +03:00
|
|
|
return sm
|
2019-10-25 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:36:20 +03:00
|
|
|
pub fn (m mut Mutex) lock() {
|
2019-08-29 11:48:03 +03:00
|
|
|
// if mutex handle not initalized
|
|
|
|
if isnil(m.mx) {
|
|
|
|
m.mx = 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, 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 {
|
2019-10-30 16:43:40 +03:00
|
|
|
WAIT_ABANDONED { .abandoned }
|
|
|
|
WAIT_OBJECT_0 { .waiting }
|
|
|
|
else { .broken }
|
2019-08-29 11:48:03 +03:00
|
|
|
}
|
2019-10-30 17:29:05 +03:00
|
|
|
*/
|
|
|
|
mut s := MutexState.broken
|
|
|
|
if state == WAIT_ABANDONED { s = .abandoned }
|
|
|
|
else if state == WAIT_OBJECT_0 { s = .waiting }
|
|
|
|
m.state = s
|
2019-06-22 22:53:22 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:36:20 +03:00
|
|
|
pub fn (m mut Mutex) unlock() {
|
2019-08-29 11:48:03 +03:00
|
|
|
if m.state == .waiting {
|
2019-11-25 04:35:41 +03:00
|
|
|
if 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
|
|
|
|
|
|
|
pub fn (m mut 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
|
|
|
}
|