
Hi, Wilbur Lang wrote:
Hi,
As win32 version of shared mutexes are based "spin and wait", which leads to poor performance and priority inversions*. I have an idea about simulate PTHREAD_PROCESS_SHARED with DuplicateHandle() under win32.
1. anonymous mutex keeps the process id (e.g. hprocess) where CreateMutex called and HANDLE of mutex (e.g. hmutex). 2. if another process want to shared this mutex, it call DuplicateHandle() with hSourceProcessHandle=hprocess and hSourceHandle=hmutex, and return hmutex_dup. 3. the process use hmutex_dup to lock/unlock/try_lock/timed_lock 4. we can optimize performance with cache hmutex_dup.
and a specialized scoped_lock required for anonymous mutex under windows which didn't use handle directly, but DuplicateHandle.
The main problem with storing the process id is that I want anonymous mutexes to work with memory mapped files. If a process creates a managed mapped file(which includes a mutex) fills it with data and then unmaps it, it should be able to map it again and continue working. If several processes map it and then unmap it, all references to that mutex are destroyed and Windows would destroy the mutex. We'll need to re-create the windows mutex although the object is still there in shared memory. This behavior is possible with POSIX mutexes with PTHREAD_PROCESS_SHARED attribute and it's the most difficult one to emulate in Windows. Regards, Ion