Replace non-reentrant C stdlib calls with thread-safe alternatives#25594
Replace non-reentrant C stdlib calls with thread-safe alternatives#25594capocasa wants to merge 2 commits intonim-lang:develfrom
Conversation
|
The only thing that matters if the glibc / the OS's libc isn't threadsafe, in reality, in 2026. "Known not to be threadsafe on SunOS in 1998" -- I couldn't care less. |
4443b12 to
5888efa
Compare
|
Okay so first off that patch was way too invasive, it's clean now. Here is a test program and is results: |
|
TLDR; the test program proves that 2026 glibc and musl are not, in fact, thread safe in the way the Nim stdlib is currently calling them. |
|
Hint: used config file 'D:\a\Nim\Nim\compiler\nim.cfg' [Conf] |
|
Testing platforms |
a52d050 to
abd8ace
Compare
|
Done- replaces non-thread-safe calls that glibc and windows keep around for their extreme backwards compatibility with respective thread-safe extensions, does not touch anything else. There's one hitch, I resorted to an ugly hack to prevent a musl regression. Clean way forward has been previously discussed. |
| let protoent = winlean.getprotobyname(name.cstring) | ||
| elif defined(linux) and not defined(android): | ||
| var protoent: ptr posix.Protoent | ||
| {.emit: ";\n#ifdef __GLIBC__".} |
There was a problem hiding this comment.
That's too ugly, we need to find a better solution for it. Or we simply add our own locks around the existing getprotobyname() calls. The new Muslim libC is the future; if it lacks the new awesome getprotobyname_r with its 5 parameter ptr ptr shit we can't use it.
Summary
During pharao development I noticed some theoretical and reproducible non-thread-safe C calls in the Nim stdlib. Here is a list of C functions known not to be thread safe. I could reproduce data corruption on some of them for threaded access, while others seem to be thread safe in glibc but possibly not in other C libs. Performance should be equivalent.
timeslocaltimelocaltime_s(MSVC) /localtime_r(MinGW, POSIX)oserrors,syncio,osprocstrerrorstrerror_r(via shared helper)nativesocketsgetprotobynamegetprotobyname_rnativesocketsgetservbynamegetservbyname_rnativesocketsgetservbyportgetservbyport_rnativesocketsgethostbynamegetaddrinfonativesocketsgethostbyaddrgetnameinfostrerror_ruses a bit of C code to detect a glibc function signature special case. An explicittzsetis added tolocaltime_rinstead oflocaltime's implicit one.This is a bit more change than I'd hope for but I'm not sure how to be more concise about it. Questions welcome.