-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use WinAPI concurrency primitives on Windows ports (remove winpthreads
)
#58
Conversation
04f42af
to
c2319cc
Compare
winpthreads
)
8176695
to
59187e1
Compare
deedfb5
to
eb4a3c1
Compare
1f0e3e5
to
1a40d58
Compare
It checks itself the value of err.
Its definition is already guarded by the DEBUG macro.
bb6aec9
to
792265e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, even if this scraps out much of my code :-)
I just noted a couple of small suggestions:
-
04b344f Implement concurrency primitives using WinAPI
Maybe add in the commit message explanations about:- how code is reshuffled (what moves into
unix.c
, etc.) - detaching threads on Windows; also add a small comment in the
_detach
function about that (the handle cannot be duplicated, can it?)
Is there a reason not to
typedef HANDLE caml_plat_thread
instead of casting it on occurrences?Could
caml_plat_thread_create
have the same signature on Windows (maybe asserting that only the supported attributes are used (onlyNULL
, maybe? or also detached?)) - how code is reshuffled (what moves into
396428c
to
324a583
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve reviewed the range-diff with the previous version I had reviewed. This looks good!
Remove the dependency of st_bt_lock_acquire() and st_bt_lock_release() on st_masterlock.
324a583
to
07b017f
Compare
Concurrency refactors and cleanups
Co-authored-by: David Allsopp <david.allsopp@metastack.com>
Substitute and remove the copyright header. sed -i \ -e '/#include "st_pthreads.h"/{r otherlibs/systhreads/st_pthreads.h' \ -e 'd}' \ otherlibs/systhreads/st_win32.h Done to ease the diff when switching to WinAPI primitives.
169dbf5
to
2fb4afe
Compare
Don't include pthread.h on Windows to avoid using pthreads emulation libraries (such as winpthreads) by mistake. Functions from platform.c that used pthreads are moved to unix.c, their Windows counterparts are implemented in win32.c. Introduce a new sync_win32.h header, counterpart of sync_unix.h. Introduce new caml_plat_thread and caml_plat_thread_attr to abstract over the system's thread id/handle type and thread attributes. Use pairs of SRWLOCK and CONDITION_VARIABLES to replace pthreads mutexes and condition variables. POSIX states: > The pthread_mutex_lock() function shall fail if: > [EDEADLK] > The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread > already owns the mutex. The Windows documentation states: > Exclusive mode SRW locks cannot be acquired recursively. If a thread > tries to acquire a lock that it already holds, that attempt will > [...] deadlock (for AcquireSRWLockExclusive). Deadlocks caused by recursive attemps to lock cannot be detected on Windows.
See also PR 10220 and issue 8857. SetLastError is called when accessing the TLS value This_thread. (cherry picked from commit 9384357)
2fb4afe
to
42d0a83
Compare
The goal is to remove winpthreads (a library emulating pthreads on top of old Windows APIs), and use modern Windows APIs in both the MSVC and MinGW-w64 ports.
It would be great if this PR could be considered for inclusion in 5.3: although we were already using the system winpthreads for the MinGW-w64 port, we introduced it as a submodule during the 5.3 development cycle to support the MSVC port. If we could remove it now, end users won't ever (unknowingly) depend on it.
One commit removes winpthreads (breaks OCaml build on Windows) and another implements the
caml_plat_*
andst_*
functions required to abstract over pthreads and Windows concurrency APIs.Note that Windows Mutex Objects cannot be used with condition variables. The right pairing consists of Slim Reader/Writer Locks (SRW locks) and condition variables. SRW locks support shared locking (only readers) and exclusive locking (one writer). I've chosen to use exclusive locking in all cases. Windows condition variables have the problem that they cannot be statically initialized.
Both pthreads and WinAPI are quite similar, except for a few points that I've noted:
The type of a function that starts a thread. They return a
void *
pointer on pthreads, and anunsigned int
on Windows, but it seems not to be a concern for us.We use pthread
ERRORCHECK
mutexes. POSIX states:The Windows documentation states:
Deadlocks caused by recursive attempts to lock cannot be detected on Windows. We cannot either detect double unlocks, or attempts to unlock a lock owned by another thread. This would revert the
Stdlib.Mutex
module guarantees to those of before OCaml 4.12 on Windows only.I've thus chosen to implement so-called
ERRORCHECK
mutexes with a pair of a SRWLock, and the thread id of the owner, accessed atomically. The value 0 is used when the thread is not owned, and is a valid sentinel value.A Windows thread is "detached" if all handles referring to the thread have been closed: it cannot be joined (waited on in Windows terminology) anymore but is not ended. pthread has a thread attribute to control the detached state.