make 'echo' threadsafe on Windows; fixes #8511
This commit is contained in:
parent
e403ef25ac
commit
e6738ba9d9
1 changed files with 8 additions and 0 deletions
|
|
@ -417,12 +417,18 @@ proc setStdIoUnbuffered() =
|
||||||
discard c_setvbuf(stdin, nil, IONBF, 0)
|
discard c_setvbuf(stdin, nil, IONBF, 0)
|
||||||
|
|
||||||
when declared(stdout):
|
when declared(stdout):
|
||||||
|
when defined(windows) and compileOption("threads"):
|
||||||
|
var echoLock: SysLock
|
||||||
|
initSysLock echoLock
|
||||||
|
|
||||||
proc echoBinSafe(args: openArray[string]) {.compilerProc.} =
|
proc echoBinSafe(args: openArray[string]) {.compilerProc.} =
|
||||||
# flockfile deadlocks some versions of Android 5.x.x
|
# flockfile deadlocks some versions of Android 5.x.x
|
||||||
when not defined(windows) and not defined(android) and not defined(nintendoswitch):
|
when not defined(windows) and not defined(android) and not defined(nintendoswitch):
|
||||||
proc flockfile(f: File) {.importc, noDecl.}
|
proc flockfile(f: File) {.importc, noDecl.}
|
||||||
proc funlockfile(f: File) {.importc, noDecl.}
|
proc funlockfile(f: File) {.importc, noDecl.}
|
||||||
flockfile(stdout)
|
flockfile(stdout)
|
||||||
|
when defined(windows) and compileOption("threads"):
|
||||||
|
acquireSys echoLock
|
||||||
for s in args:
|
for s in args:
|
||||||
discard c_fwrite(s.cstring, s.len, 1, stdout)
|
discard c_fwrite(s.cstring, s.len, 1, stdout)
|
||||||
const linefeed = "\n" # can be 1 or more chars
|
const linefeed = "\n" # can be 1 or more chars
|
||||||
|
|
@ -430,5 +436,7 @@ when declared(stdout):
|
||||||
discard c_fflush(stdout)
|
discard c_fflush(stdout)
|
||||||
when not defined(windows) and not defined(android) and not defined(nintendoswitch):
|
when not defined(windows) and not defined(android) and not defined(nintendoswitch):
|
||||||
funlockfile(stdout)
|
funlockfile(stdout)
|
||||||
|
when defined(windows) and compileOption("threads"):
|
||||||
|
releaseSys echoLock
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue