14 Commits

Author SHA1 Message Date
Jens Gustedt
32482f61da revise the definition of multiple basic locks in the code
In all cases this is just a change from two volatile int to one.
2018-01-09 13:15:27 -05:00
Rich Felker
086793ad99 fix mmap leak in sem_open failure path for link call
the leak was found by static analysis (reported by Alexander Monakov),
not tested/observed, but seems to have occured both when failing due
to O_EXCL, and in a race condition with O_CREAT but not O_EXCL where a
semaphore by the same name was created concurrently.
2015-04-21 13:24:46 -04:00
Rich Felker
56fbaa3bbe make all objects used with atomic operations volatile
the memory model we use internally for atomics permits plain loads of
values which may be subject to concurrent modification without
requiring that a special load function be used. since a compiler is
free to make transformations that alter the number of loads or the way
in which loads are performed, the compiler is theoretically free to
break this usage. the most obvious concern is with atomic cas
constructs: something of the form tmp=*p;a_cas(p,tmp,f(tmp)); could be
transformed to a_cas(p,*p,f(*p)); where the latter is intended to show
multiple loads of *p whose resulting values might fail to be equal;
this would break the atomicity of the whole operation. but even more
fundamental breakage is possible.

with the changes being made now, objects that may be modified by
atomics are modeled as volatile, and the atomic operations performed
on them by other threads are modeled as asynchronous stores by
hardware which happens to be acting on the request of another thread.
such modeling of course does not itself address memory synchronization
between cores/cpus, but that aspect was already handled. this all
seems less than ideal, but it's the best we can do without mandating a
C11 compiler and using the C11 model for atomics.

in the case of pthread_once_t, the ABI type of the underlying object
is not volatile-qualified. so we are assuming that accessing the
object through a volatile-qualified lvalue via casts yields volatile
access semantics. the language of the C standard is somewhat unclear
on this matter, but this is an assumption the linux kernel also makes,
and seems to be the correct interpretation of the standard.
2015-03-03 22:50:02 -05:00
Rich Felker
7c20a11801 fix temp file leak in sem_open on successful creation of new semaphore 2013-06-26 21:41:51 -04:00
Rich Felker
a033cd22aa fix bug whereby sem_open leaked its own internal slots on failure 2013-06-26 21:39:15 -04:00
Rich Felker
52d4444f8e in sem_open, don't leak vm mapping if fstat fails
fstat should not fail under normal circumstances, so this fix is
mostly theoretical.
2013-06-26 21:35:56 -04:00
Rich Felker
e44849f5cf protect sem_open against cancellation
also fix one minor bug: failure to free the early-reserved slot when
the semaphore later found to already be mapped.
2012-09-30 19:44:45 -04:00
Rich Felker
bf258341b7 overhaul sem_open
this function was overly complicated and not even obviously correct.
avoid using openat/linkat just like in shm_open, and instead expand
pathname using code shared with shm_open. remove bogus (and dangerous,
with priorities) use of spinlocks.

this commit also heavily streamlines the code and ensures there are no
failure cases that can happen after a new semaphore has been created
in the filesystem, since that case is unreportable.
2012-09-30 19:35:40 -04:00
Rich Felker
3d8d90c5cc sem_open should make process-shared semaphores
this did not matter because we don't yet treat process-shared special.
when private futex support is added, however, it will matter.
2012-09-29 16:49:32 -04:00
Rich Felker
39f296a95b use O_CLOEXEC to open semaphore files in sem_open 2012-09-29 16:48:52 -04:00
Rich Felker
6e53a6eca4 fix useless use of potentially-uninitialized mode variable in sem_open 2011-06-26 16:34:05 -04:00
Rich Felker
682a0f271f fix failure behavior of sem_open when sem does not exist 2011-03-10 22:05:16 -05:00
Rich Felker
81af503610 fix sem_open and sem_close to obey posix semantics
multiple opens of the same named semaphore must return the same
pointer, and only the last close can unmap it. thus the ugly global
state keeping track of mappings. the maximum number of distinct named
semaphores that can be opened is limited sufficiently small that the
linear searches take trivial time, especially compared to the syscall
overhead of these functions.
2011-03-10 21:34:19 -05:00
Rich Felker
6fc5fdbdc7 implement POSIX semaphores 2011-03-04 00:45:59 -05:00