[std/locks]close #7998(complete condition variables) (#17711)

* close #7998
* workaround genode

* Update lib/system/syslocks.nim
This commit is contained in:
flywind 2021-04-19 16:51:13 +08:00 • committed by GitHub
commit dc89b21257
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 45 deletions

View file

@ -66,13 +66,18 @@ proc deinitCond*(cond: var Cond) {.inline.} =
deinitSysCond(cond) deinitSysCond(cond)
proc wait*(cond: var Cond, lock: var Lock) {.inline.} = proc wait*(cond: var Cond, lock: var Lock) {.inline.} =
## waits on the condition variable `cond`. ## Waits on the condition variable `cond`.
waitSysCond(cond, lock) waitSysCond(cond, lock)
proc signal*(cond: var Cond) {.inline.} = proc signal*(cond: var Cond) {.inline.} =
## sends a signal to the condition variable `cond`. ## Sends a signal to the condition variable `cond`.
signalSysCond(cond) signalSysCond(cond)
proc broadcast*(cond: var Cond) {.inline.} =
## Unblocks all threads currently blocked on the
## specified condition variable `cond`.
broadcastSysCond(cond)
template withLock*(a: Lock, body: untyped) = template withLock*(a: Lock, body: untyped) =
## Acquires the given lock, executes the statements in body and ## Acquires the given lock, executes the statements in body and
## releases the lock after the statements finish executing. ## releases the lock after the statements finish executing.

View file

@ -71,6 +71,11 @@ struct Nim::SysCond
{ {
_semaphore.up(); _semaphore.up();
} }
void broadcastSysCond()
{
_semaphore.up();
}
}; };
#endif #endif

View file

@ -24,7 +24,9 @@ when defined(windows):
LockSemaphore: int LockSemaphore: int
SpinCount: int SpinCount: int
SysCond = Handle RTL_CONDITION_VARIABLE {.importc: "RTL_CONDITION_VARIABLE", header: "synchapi.h".} = object
thePtr {.importc: "ptr".} : Handle
SysCond = RTL_CONDITION_VARIABLE
proc initSysLock(L: var SysLock) {.importc: "InitializeCriticalSection", proc initSysLock(L: var SysLock) {.importc: "InitializeCriticalSection",
header: "<windows.h>".} header: "<windows.h>".}
@ -48,30 +50,29 @@ when defined(windows):
proc deinitSys(L: var SysLock) {.importc: "DeleteCriticalSection", proc deinitSys(L: var SysLock) {.importc: "DeleteCriticalSection",
header: "<windows.h>".} header: "<windows.h>".}
proc createEvent(lpEventAttributes: pointer, proc initializeConditionVariable(
bManualReset, bInitialState: int32, conditionVariable: var SysCond
lpName: cstring): SysCond {.stdcall, noSideEffect, ) {.stdcall, noSideEffect, dynlib: "kernel32", importc: "InitializeConditionVariable".}
dynlib: "kernel32", importc: "CreateEventA".}
proc closeHandle(hObject: Handle) {.stdcall, noSideEffect, proc sleepConditionVariableCS(
dynlib: "kernel32", importc: "CloseHandle".} conditionVariable: var SysCond,
proc waitForSingleObject(hHandle: Handle, dwMilliseconds: int32): int32 {. PCRITICAL_SECTION: var SysLock,
stdcall, dynlib: "kernel32", importc: "WaitForSingleObject", noSideEffect.} dwMilliseconds: int
): int32 {.stdcall, noSideEffect, dynlib: "kernel32", importc: "SleepConditionVariableCS".}
proc signalSysCond(hEvent: SysCond) {.stdcall, noSideEffect,
dynlib: "kernel32", importc: "SetEvent".} proc signalSysCond(hEvent: var SysCond) {.stdcall, noSideEffect,
dynlib: "kernel32", importc: "WakeConditionVariable".}
proc broadcastSysCond(hEvent: var SysCond) {.stdcall, noSideEffect,
dynlib: "kernel32", importc: "WakeAllConditionVariable".}
proc initSysCond(cond: var SysCond) {.inline.} = proc initSysCond(cond: var SysCond) {.inline.} =
cond = createEvent(nil, 0'i32, 0'i32, nil) initializeConditionVariable(cond)
proc deinitSysCond(cond: var SysCond) {.inline.} = proc deinitSysCond(cond: var SysCond) {.inline.} =
closeHandle(cond) discard
proc waitSysCond(cond: var SysCond, lock: var SysLock) = proc waitSysCond(cond: var SysCond, lock: var SysLock) =
releaseSys(lock) discard sleepConditionVariableCS(cond, lock, -1'i32)
discard waitForSingleObject(cond, -1'i32)
acquireSys(lock)
proc waitSysCondWindows(cond: var SysCond) =
discard waitForSingleObject(cond, -1'i32)
elif defined(genode): elif defined(genode):
const const
@ -94,6 +95,8 @@ elif defined(genode):
noSideEffect, importcpp.} noSideEffect, importcpp.}
proc signalSysCond(cond: var SysCond) {. proc signalSysCond(cond: var SysCond) {.
noSideEffect, importcpp.} noSideEffect, importcpp.}
proc broadcastSysCond(cond: var SysCond) {.
noSideEffect, importcpp.}
else: else:
type type
@ -181,7 +184,7 @@ else:
releaseSysAux(L) releaseSysAux(L)
when insideRLocksModule: when insideRLocksModule:
let SysLockType_Reentrant{.importc: "PTHREAD_MUTEX_RECURSIVE", let SysLockType_Reentrant {.importc: "PTHREAD_MUTEX_RECURSIVE",
header: "<pthread.h>".}: SysLockType header: "<pthread.h>".}: SysLockType
proc initSysLockAttr(a: var SysLockAttr) {. proc initSysLockAttr(a: var SysLockAttr) {.
importc: "pthread_mutexattr_init", header: "<pthread.h>", noSideEffect.} importc: "pthread_mutexattr_init", header: "<pthread.h>", noSideEffect.}
@ -194,10 +197,12 @@ else:
proc deinitSysCondAux(cond: var SysCondObj) {.noSideEffect, proc deinitSysCondAux(cond: var SysCondObj) {.noSideEffect,
importc: "pthread_cond_destroy", header: "<pthread.h>".} importc: "pthread_cond_destroy", header: "<pthread.h>".}
proc waitSysCondAux(cond: var SysCondObj, lock: var SysLockObj) {. proc waitSysCondAux(cond: var SysCondObj, lock: var SysLockObj): cint {.
importc: "pthread_cond_wait", header: "<pthread.h>", noSideEffect.} importc: "pthread_cond_wait", header: "<pthread.h>", noSideEffect.}
proc signalSysCondAux(cond: var SysCondObj) {. proc signalSysCondAux(cond: var SysCondObj) {.
importc: "pthread_cond_signal", header: "<pthread.h>", noSideEffect.} importc: "pthread_cond_signal", header: "<pthread.h>", noSideEffect.}
proc broadcastSysCondAux(cond: var SysCondObj) {.
importc: "pthread_cond_broadcast", header: "<pthread.h>", noSideEffect.}
when defined(ios): when defined(ios):
proc initSysCond(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) = proc initSysCond(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) =
@ -209,9 +214,11 @@ else:
c_free(cond) c_free(cond)
template waitSysCond(cond: var SysCond, lock: var SysLock) = template waitSysCond(cond: var SysCond, lock: var SysLock) =
waitSysCondAux(cond[], lock[]) discard waitSysCondAux(cond[], lock[])
template signalSysCond(cond: var SysCond) = template signalSysCond(cond: var SysCond) =
signalSysCondAux(cond[]) signalSysCondAux(cond[])
template broadcastSysCond(cond: var SysCond) =
broadcastSysCondAux(cond[])
else: else:
template initSysCond(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) = template initSysCond(cond: var SysCond, cond_attr: ptr SysCondAttr = nil) =
initSysCondAux(cond, cond_attr) initSysCondAux(cond, cond_attr)
@ -219,8 +226,10 @@ else:
deinitSysCondAux(cond) deinitSysCondAux(cond)
template waitSysCond(cond: var SysCond, lock: var SysLock) = template waitSysCond(cond: var SysCond, lock: var SysLock) =
waitSysCondAux(cond, lock) discard waitSysCondAux(cond, lock)
template signalSysCond(cond: var SysCond) = template signalSysCond(cond: var SysCond) =
signalSysCondAux(cond) signalSysCondAux(cond)
template broadcastSysCond(cond: var SysCond) =
broadcastSysCondAux(cond)
{.pop.} {.pop.}

View file

@ -20,13 +20,11 @@ when not declared(NimString):
type type
CondVar = object CondVar = object
c: SysCond c: SysCond
when defined(posix):
stupidLock: SysLock stupidLock: SysLock
counter: int counter: int
proc createCondVar(): CondVar = proc createCondVar(): CondVar =
initSysCond(result.c) initSysCond(result.c)
when defined(posix):
initSysLock(result.stupidLock) initSysLock(result.stupidLock)
#acquireSys(result.stupidLock) #acquireSys(result.stupidLock)
@ -34,17 +32,13 @@ proc destroyCondVar(c: var CondVar) {.inline.} =
deinitSysCond(c.c) deinitSysCond(c.c)
proc await(cv: var CondVar) = proc await(cv: var CondVar) =
when defined(posix):
acquireSys(cv.stupidLock) acquireSys(cv.stupidLock)
while cv.counter <= 0: while cv.counter <= 0:
waitSysCond(cv.c, cv.stupidLock) waitSysCond(cv.c, cv.stupidLock)
dec cv.counter dec cv.counter
releaseSys(cv.stupidLock) releaseSys(cv.stupidLock)
else:
waitSysCondWindows(cv.c)
proc signal(cv: var CondVar) = proc signal(cv: var CondVar) =
when defined(posix):
acquireSys(cv.stupidLock) acquireSys(cv.stupidLock)
inc cv.counter inc cv.counter
releaseSys(cv.stupidLock) releaseSys(cv.stupidLock)
@ -57,7 +51,6 @@ type
proc createFastCondVar(): FastCondVar = proc createFastCondVar(): FastCondVar =
initSysCond(result.slow.c) initSysCond(result.slow.c)
when defined(posix):
initSysLock(result.slow.stupidLock) initSysLock(result.slow.stupidLock)
#acquireSys(result.slow.stupidLock) #acquireSys(result.slow.stupidLock)
result.event = false result.event = false

View file

@ -17,6 +17,9 @@ when defined(windows):
proc winResumeThread(hThread: SysThread): int32 {. proc winResumeThread(hThread: SysThread): int32 {.
stdcall, dynlib: "kernel32", importc: "ResumeThread".} stdcall, dynlib: "kernel32", importc: "ResumeThread".}
proc waitForSingleObject(hHandle: SysThread, dwMilliseconds: int32): int32 {.
stdcall, dynlib: "kernel32", importc: "WaitForSingleObject".}
proc waitForMultipleObjects(nCount: int32, proc waitForMultipleObjects(nCount: int32,
lpHandles: ptr SysThread, lpHandles: ptr SysThread,
bWaitAll: int32, bWaitAll: int32,