2009-03-04 04:28:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/atomics.h>
|
|
|
|
#include <time.h>
|
2010-06-28 23:10:14 +02:00
|
|
|
#include <limits.h>
|
2010-06-03 03:12:12 +02:00
|
|
|
|
2013-10-10 00:50:50 +02:00
|
|
|
#include "private/bionic_atomic_inline.h"
|
|
|
|
#include "private/bionic_futex.h"
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
/* In this implementation, a semaphore contains a
|
|
|
|
* 31-bit signed value and a 1-bit 'shared' flag
|
|
|
|
* (for process-sharing purpose).
|
|
|
|
*
|
|
|
|
* We use the value -1 to indicate contention on the
|
|
|
|
* semaphore, 0 or more to indicate uncontended state,
|
|
|
|
* any value lower than -2 is invalid at runtime.
|
|
|
|
*
|
|
|
|
* State diagram:
|
|
|
|
*
|
|
|
|
* post(1) ==> 2
|
|
|
|
* post(0) ==> 1
|
|
|
|
* post(-1) ==> 1, then wake all waiters
|
|
|
|
*
|
|
|
|
* wait(2) ==> 1
|
|
|
|
* wait(1) ==> 0
|
|
|
|
* wait(0) ==> -1 then wait for a wake up + loop
|
|
|
|
* wait(-1) ==> -1 then wait for a wake up + loop
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Use the upper 31-bits for the counter, and the lower one
|
|
|
|
* for the shared flag.
|
2010-06-03 03:12:12 +02:00
|
|
|
*/
|
2010-06-28 23:10:14 +02:00
|
|
|
#define SEMCOUNT_SHARED_MASK 0x00000001
|
|
|
|
#define SEMCOUNT_VALUE_MASK 0xfffffffe
|
|
|
|
#define SEMCOUNT_VALUE_SHIFT 1
|
|
|
|
|
|
|
|
/* Maximum unsigned value that can be stored in the semaphore.
|
|
|
|
* One bit is used for the shared flag, another one for the
|
|
|
|
* sign bit, leaving us with only 30 bits.
|
|
|
|
*/
|
|
|
|
#define SEM_MAX_VALUE 0x3fffffff
|
|
|
|
|
|
|
|
/* convert a value into the corresponding sem->count bit pattern */
|
|
|
|
#define SEMCOUNT_FROM_VALUE(val) (((val) << SEMCOUNT_VALUE_SHIFT) & SEMCOUNT_VALUE_MASK)
|
|
|
|
|
|
|
|
/* convert a sem->count bit pattern into the corresponding signed value */
|
|
|
|
#define SEMCOUNT_TO_VALUE(sval) ((int)(sval) >> SEMCOUNT_VALUE_SHIFT)
|
|
|
|
|
|
|
|
/* the value +1 as a sem->count bit-pattern. */
|
|
|
|
#define SEMCOUNT_ONE SEMCOUNT_FROM_VALUE(1)
|
|
|
|
|
|
|
|
/* the value -1 as a sem->count bit-pattern. */
|
|
|
|
#define SEMCOUNT_MINUS_ONE SEMCOUNT_FROM_VALUE(-1)
|
|
|
|
|
|
|
|
#define SEMCOUNT_DECREMENT(sval) (((sval) - (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK)
|
|
|
|
#define SEMCOUNT_INCREMENT(sval) (((sval) + (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK)
|
|
|
|
|
|
|
|
/* return the shared bitflag from a semaphore */
|
|
|
|
#define SEM_GET_SHARED(sem) ((sem)->count & SEMCOUNT_SHARED_MASK)
|
2010-06-03 03:12:12 +02:00
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
int sem_init(sem_t *sem, int pshared, unsigned int value)
|
|
|
|
{
|
|
|
|
if (sem == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-03 03:12:12 +02:00
|
|
|
/* ensure that 'value' can be stored in the semaphore */
|
2010-06-28 23:10:14 +02:00
|
|
|
if (value > SEM_MAX_VALUE) {
|
2010-06-03 03:12:12 +02:00
|
|
|
errno = EINVAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
sem->count = SEMCOUNT_FROM_VALUE(value);
|
2010-06-03 03:12:12 +02:00
|
|
|
if (pshared != 0)
|
2010-06-28 23:10:14 +02:00
|
|
|
sem->count |= SEMCOUNT_SHARED_MASK;
|
2010-06-03 03:12:12 +02:00
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_destroy(sem_t *sem)
|
|
|
|
{
|
2010-06-28 23:10:14 +02:00
|
|
|
int count;
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
if (sem == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-28 23:10:14 +02:00
|
|
|
count = SEMCOUNT_TO_VALUE(sem->count);
|
|
|
|
if (count < 0) {
|
2009-03-04 04:28:35 +01:00
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-03 03:12:12 +02:00
|
|
|
sem->count = 0;
|
2009-03-04 04:28:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sem_t *sem_open(const char *name, int oflag, ...)
|
|
|
|
{
|
|
|
|
name=name;
|
|
|
|
oflag=oflag;
|
|
|
|
|
|
|
|
errno = ENOSYS;
|
|
|
|
return SEM_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_close(sem_t *sem)
|
|
|
|
{
|
|
|
|
if (sem == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int sem_unlink(const char * name)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
/* Decrement a semaphore's value atomically,
|
|
|
|
* and return the old one. As a special case,
|
|
|
|
* this returns immediately if the value is
|
|
|
|
* negative (i.e. -1)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
__sem_dec(volatile unsigned int *pvalue)
|
|
|
|
{
|
|
|
|
unsigned int shared = (*pvalue & SEMCOUNT_SHARED_MASK);
|
|
|
|
unsigned int old, new;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do {
|
|
|
|
old = (*pvalue & SEMCOUNT_VALUE_MASK);
|
|
|
|
ret = SEMCOUNT_TO_VALUE(old);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
new = SEMCOUNT_DECREMENT(old);
|
|
|
|
}
|
2011-11-15 15:47:02 +01:00
|
|
|
while (__bionic_cmpxchg((int)(old|shared),
|
2010-06-28 23:10:14 +02:00
|
|
|
(int)(new|shared),
|
|
|
|
(volatile int *)pvalue) != 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as __sem_dec, but will not touch anything if the
|
|
|
|
* value is already negative *or* 0. Returns the old value.
|
2010-06-03 03:12:12 +02:00
|
|
|
*/
|
2009-03-04 04:28:35 +01:00
|
|
|
static int
|
2010-06-28 23:10:14 +02:00
|
|
|
__sem_trydec(volatile unsigned int *pvalue)
|
2009-03-04 04:28:35 +01:00
|
|
|
{
|
2010-06-28 23:10:14 +02:00
|
|
|
unsigned int shared = (*pvalue & SEMCOUNT_SHARED_MASK);
|
|
|
|
unsigned int old, new;
|
|
|
|
int ret;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
do {
|
2010-06-28 23:10:14 +02:00
|
|
|
old = (*pvalue & SEMCOUNT_VALUE_MASK);
|
|
|
|
ret = SEMCOUNT_TO_VALUE(old);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
new = SEMCOUNT_DECREMENT(old);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2011-11-15 15:47:02 +01:00
|
|
|
while (__bionic_cmpxchg((int)(old|shared),
|
2010-06-28 23:10:14 +02:00
|
|
|
(int)(new|shared),
|
|
|
|
(volatile int *)pvalue) != 0);
|
2010-06-03 03:12:12 +02:00
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
return ret;
|
2010-06-03 03:12:12 +02:00
|
|
|
}
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
|
|
|
|
/* "Increment" the value of a semaphore atomically and
|
|
|
|
* return its old value. Note that this implements
|
|
|
|
* the special case of "incrementing" any negative
|
|
|
|
* value to +1 directly.
|
|
|
|
*
|
|
|
|
* NOTE: The value will _not_ wrap above SEM_VALUE_MAX
|
2010-06-03 03:12:12 +02:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
__sem_inc(volatile unsigned int *pvalue)
|
|
|
|
{
|
2010-06-28 23:10:14 +02:00
|
|
|
unsigned int shared = (*pvalue & SEMCOUNT_SHARED_MASK);
|
|
|
|
unsigned int old, new;
|
|
|
|
int ret;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2010-06-03 03:12:12 +02:00
|
|
|
do {
|
2010-06-28 23:10:14 +02:00
|
|
|
old = (*pvalue & SEMCOUNT_VALUE_MASK);
|
|
|
|
ret = SEMCOUNT_TO_VALUE(old);
|
|
|
|
|
|
|
|
/* Can't go higher than SEM_MAX_VALUE */
|
|
|
|
if (ret == SEM_MAX_VALUE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* If the counter is negative, go directly to +1,
|
|
|
|
* otherwise just increment */
|
|
|
|
if (ret < 0)
|
|
|
|
new = SEMCOUNT_ONE;
|
|
|
|
else
|
|
|
|
new = SEMCOUNT_INCREMENT(old);
|
|
|
|
}
|
2011-11-15 15:47:02 +01:00
|
|
|
while ( __bionic_cmpxchg((int)(old|shared),
|
2010-06-28 23:10:14 +02:00
|
|
|
(int)(new|shared),
|
|
|
|
(volatile int*)pvalue) != 0);
|
|
|
|
|
|
|
|
return ret;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2010-05-28 22:31:45 +02:00
|
|
|
/* lock a semaphore */
|
2009-03-04 04:28:35 +01:00
|
|
|
int sem_wait(sem_t *sem)
|
|
|
|
{
|
2010-06-03 03:12:12 +02:00
|
|
|
unsigned shared;
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
if (sem == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-03 03:12:12 +02:00
|
|
|
shared = SEM_GET_SHARED(sem);
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
for (;;) {
|
2010-06-28 23:10:14 +02:00
|
|
|
if (__sem_dec(&sem->count) > 0)
|
2009-03-04 04:28:35 +01:00
|
|
|
break;
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
__futex_wait_ex(&sem->count, shared, shared|SEMCOUNT_MINUS_ONE, NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2010-05-28 22:31:45 +02:00
|
|
|
ANDROID_MEMBAR_FULL();
|
2009-03-04 04:28:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
|
|
|
|
{
|
|
|
|
int ret;
|
2010-06-03 03:12:12 +02:00
|
|
|
unsigned int shared;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
if (sem == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* POSIX says we need to try to decrement the semaphore
|
2010-06-28 23:10:14 +02:00
|
|
|
* before checking the timeout value. Note that if the
|
|
|
|
* value is currently 0, __sem_trydec() does nothing.
|
|
|
|
*/
|
|
|
|
if (__sem_trydec(&sem->count) > 0) {
|
2010-05-28 22:31:45 +02:00
|
|
|
ANDROID_MEMBAR_FULL();
|
2009-03-04 04:28:35 +01:00
|
|
|
return 0;
|
2010-05-28 22:31:45 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
/* Check it as per Posix */
|
2009-03-04 04:28:35 +01:00
|
|
|
if (abs_timeout == NULL ||
|
|
|
|
abs_timeout->tv_sec < 0 ||
|
|
|
|
abs_timeout->tv_nsec < 0 ||
|
|
|
|
abs_timeout->tv_nsec >= 1000000000)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-03 03:12:12 +02:00
|
|
|
shared = SEM_GET_SHARED(sem);
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
for (;;) {
|
|
|
|
struct timespec ts;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Posix mandates CLOCK_REALTIME here */
|
|
|
|
clock_gettime( CLOCK_REALTIME, &ts );
|
|
|
|
ts.tv_sec = abs_timeout->tv_sec - ts.tv_sec;
|
|
|
|
ts.tv_nsec = abs_timeout->tv_nsec - ts.tv_nsec;
|
|
|
|
if (ts.tv_nsec < 0) {
|
|
|
|
ts.tv_nsec += 1000000000;
|
|
|
|
ts.tv_sec -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ts.tv_sec < 0 || ts.tv_nsec < 0) {
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
/* Try to grab the semaphore. If the value was 0, this
|
|
|
|
* will also change it to -1 */
|
|
|
|
if (__sem_dec(&sem->count) > 0) {
|
|
|
|
ANDROID_MEMBAR_FULL();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Contention detected. wait for a wakeup event */
|
|
|
|
ret = __futex_wait_ex(&sem->count, shared, shared|SEMCOUNT_MINUS_ONE, &ts);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
/* return in case of timeout or interrupt */
|
|
|
|
if (ret == -ETIMEDOUT || ret == -EINTR) {
|
|
|
|
errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
/* Unlock a semaphore */
|
2009-03-04 04:28:35 +01:00
|
|
|
int sem_post(sem_t *sem)
|
|
|
|
{
|
2010-06-03 03:12:12 +02:00
|
|
|
unsigned int shared;
|
2010-06-28 23:10:14 +02:00
|
|
|
int old;
|
2010-06-03 03:12:12 +02:00
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
if (sem == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
2010-06-03 03:12:12 +02:00
|
|
|
shared = SEM_GET_SHARED(sem);
|
|
|
|
|
2010-05-28 22:31:45 +02:00
|
|
|
ANDROID_MEMBAR_FULL();
|
2010-06-28 23:10:14 +02:00
|
|
|
old = __sem_inc(&sem->count);
|
|
|
|
if (old < 0) {
|
|
|
|
/* contention on the semaphore, wake up all waiters */
|
|
|
|
__futex_wake_ex(&sem->count, shared, INT_MAX);
|
|
|
|
}
|
|
|
|
else if (old == SEM_MAX_VALUE) {
|
|
|
|
/* overflow detected */
|
|
|
|
errno = EOVERFLOW;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sem_trywait(sem_t *sem)
|
|
|
|
{
|
|
|
|
if (sem == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
if (__sem_trydec(&sem->count) > 0) {
|
2010-05-28 22:31:45 +02:00
|
|
|
ANDROID_MEMBAR_FULL();
|
2009-03-04 04:28:35 +01:00
|
|
|
return 0;
|
|
|
|
} else {
|
2010-02-12 21:18:37 +01:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
/* Note that Posix requires that sem_getvalue() returns, in
|
|
|
|
* case of contention, the negative of the number of waiting
|
|
|
|
* threads.
|
|
|
|
*
|
|
|
|
* However, code that depends on this negative value to be
|
|
|
|
* meaningful is most probably racy. The GLibc sem_getvalue()
|
|
|
|
* only returns the semaphore value, which is 0, in case of
|
|
|
|
* contention, so we will mimick this behaviour here instead
|
|
|
|
* for better compatibility.
|
|
|
|
*/
|
2009-03-04 04:28:35 +01:00
|
|
|
int sem_getvalue(sem_t *sem, int *sval)
|
|
|
|
{
|
2010-06-28 23:10:14 +02:00
|
|
|
int val;
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
if (sem == NULL || sval == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-28 23:10:14 +02:00
|
|
|
val = SEMCOUNT_TO_VALUE(sem->count);
|
|
|
|
if (val < 0)
|
|
|
|
val = 0;
|
|
|
|
|
|
|
|
*sval = val;
|
2009-03-04 04:28:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|