This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[RFC 2/2][BZ #12674] Make semaphores race-free.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Fri, 6 Dec 2013 13:24:02 +0100
- Subject: [RFC 2/2][BZ #12674] Make semaphores race-free.
- Authentication-results: sourceware.org; auth=none
- References: <20131206103626 dot GA20423 at domone dot podge>
Now we look how fix race condition in semaphores. Earlier thread allows
us focus on idea without needlessly going into assembly.
If we look to semaphore layout it is
/* Semaphore variable structure. */
struct new_sem
{
unsigned int value;
int private;
unsigned long int nwaiters;
};
struct old_sem
{
unsigned int value;
};
A new_sem is 12 or 16 bytes whether you are on 64-bit system. On x64
that is enough as you can update structure atomically by cmpxchg16b.
However our data structure is bit wasteful. Do we really need 64bit
nwaiters?
Field private consists of single bit. We could squash that to nwaiters
which should be ok until somebody can make machine that could handle
1000000000 threads.
With this change we fit into 8 bytes which is enough for hardware
compare-and-swap on most architectures that matter.
Then we need separate implementation for these that have only 4byte CAS.
Among these are
arm, pa, sh, sparc32, i486 (but not pentium)
For these there are several ways how proceed, one is decrease value limit
to 1<<24 and then we have 1 bit for PRIVATE and 7 bits for nwaiters.
When there are 127 waiters then we must disable this optimization and
keep nwaiters on 127 which is equivalent to old semaphore behaviour.
Comments?