I have my own ideas about how a pointer-sized mutex could be made: The integer is used as a set of flags, counter, index, and a spinlock that guards an entry in a table of pre-allocated kernel objects given to mutexes only when threads are contending, and released afterward (only need at most N table entries for N threads).
Watching MS’ SRWLOCK structure change leads me to believe they conditionally store the tail of an xor list (or a table index?) in the pointer when contested. The first 4 bits are flags of some kind.
To clarify
I was wondering if there were any other ideas about making a user mode mutex that is the size of a pointer and only requires the allocation of a kernel object if it is contested. My idea involves spinlocking, so I am hoping there exists something that does not.
7
Is the mutex supposed to be reentrant? If so, you’ll need a few bits to hold a recursion count. Also a thread/process ID. You might also want to keep a pointer or some kind of reference to a queue to hold IDs of threads waiting on the mutex, depends on whether you want threads to queue up or simply let them acquire the mutex based on their scheduling order. Your pointers are 64 bits, right? 😉
1