2003-04-01 03:46:29 +00:00
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
|
2003-04-01 03:46:29 +00:00
|
|
|
* Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice(s), this list of conditions and the following disclaimer as
|
|
|
|
* the first lines of this file unmodified other than the possible
|
|
|
|
* addition of one or more copyright notices.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice(s), 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 HOLDER(S) ``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 HOLDER(S) 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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
#include "namespace.h"
|
2005-09-01 15:21:23 +00:00
|
|
|
#include <sys/types.h>
|
2005-04-02 01:20:00 +00:00
|
|
|
#include <sys/queue.h>
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <errno.h>
|
2005-04-02 01:20:00 +00:00
|
|
|
#include <fcntl.h>
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <pthread.h>
|
2005-04-02 01:20:00 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <_semaphore.h>
|
|
|
|
#include "un-namespace.h"
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
#include "thr_private.h"
|
|
|
|
|
|
|
|
|
|
|
|
__weak_reference(_sem_init, sem_init);
|
|
|
|
__weak_reference(_sem_destroy, sem_destroy);
|
2005-04-02 01:20:00 +00:00
|
|
|
__weak_reference(_sem_getvalue, sem_getvalue);
|
2003-04-01 03:46:29 +00:00
|
|
|
__weak_reference(_sem_trywait, sem_trywait);
|
2005-04-02 01:20:00 +00:00
|
|
|
__weak_reference(_sem_wait, sem_wait);
|
|
|
|
__weak_reference(_sem_timedwait, sem_timedwait);
|
2003-04-01 03:46:29 +00:00
|
|
|
__weak_reference(_sem_post, sem_post);
|
|
|
|
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static inline int
|
|
|
|
sem_check_validity(sem_t *sem)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((sem != NULL) && ((*sem)->magic == SEM_MAGIC))
|
|
|
|
return (0);
|
|
|
|
else {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static sem_t
|
|
|
|
sem_alloc(unsigned int value, semid_t semid, int system_sem)
|
|
|
|
{
|
|
|
|
sem_t sem;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
if (value > SEM_VALUE_MAX) {
|
|
|
|
errno = EINVAL;
|
2005-04-02 01:20:00 +00:00
|
|
|
return (NULL);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
sem = (sem_t)malloc(sizeof(struct sem));
|
|
|
|
if (sem == NULL) {
|
2003-04-01 03:46:29 +00:00
|
|
|
errno = ENOSPC;
|
2005-04-02 01:20:00 +00:00
|
|
|
return (NULL);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2006-09-06 04:04:10 +00:00
|
|
|
bzero(sem, sizeof(*sem));
|
2003-04-01 03:46:29 +00:00
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* Fortunatly count and nwaiters are adjacency, so we can
|
|
|
|
* use umtx_wait to wait on it, umtx_wait needs an address
|
|
|
|
* can be accessed as a long interger.
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
sem->count = (u_int32_t)value;
|
|
|
|
sem->nwaiters = 0;
|
|
|
|
sem->magic = SEM_MAGIC;
|
|
|
|
sem->semid = semid;
|
|
|
|
sem->syssem = system_sem;
|
|
|
|
return (sem);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_sem_init(sem_t *sem, int pshared, unsigned int value)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
semid_t semid;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
semid = (semid_t)SEM_USER;
|
|
|
|
if ((pshared != 0) && (ksem_init(&semid, value) != 0))
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
(*sem) = sem_alloc(value, semid, pshared);
|
|
|
|
if ((*sem) == NULL) {
|
|
|
|
if (pshared != 0)
|
|
|
|
ksem_destroy(semid);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_sem_destroy(sem_t *sem)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
int retval;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a system semaphore let the kernel track it otherwise
|
|
|
|
* make sure there are no waiters.
|
|
|
|
*/
|
|
|
|
if ((*sem)->syssem != 0)
|
|
|
|
retval = ksem_destroy((*sem)->semid);
|
|
|
|
else {
|
|
|
|
retval = 0;
|
|
|
|
(*sem)->magic = 0;
|
|
|
|
}
|
|
|
|
if (retval == 0)
|
|
|
|
free(*sem);
|
|
|
|
return (retval);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
int retval;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*sem)->syssem != 0)
|
|
|
|
retval = ksem_getvalue((*sem)->semid, sval);
|
|
|
|
else {
|
|
|
|
*sval = (int)(*sem)->count;
|
|
|
|
retval = 0;
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
return (retval);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_sem_trywait(sem_t *sem)
|
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
int val;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*sem)->syssem != 0)
|
|
|
|
return (ksem_trywait((*sem)->semid));
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
while ((val = (*sem)->count) > 0) {
|
|
|
|
if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
|
|
|
|
return (0);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
errno = EAGAIN;
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 06:01:02 +00:00
|
|
|
static void
|
|
|
|
sem_cancel_handler(void *arg)
|
|
|
|
{
|
|
|
|
sem_t *sem = arg;
|
|
|
|
|
|
|
|
atomic_add_int(&(*sem)->nwaiters, -1);
|
2008-03-11 03:26:47 +00:00
|
|
|
if ((*sem)->nwaiters && (*sem)->count)
|
2008-04-29 03:58:18 +00:00
|
|
|
_thr_umtx_wake(&(*sem)->count, 1, 0);
|
2007-11-21 06:01:02 +00:00
|
|
|
}
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_sem_wait(sem_t *sem)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread;
|
2006-11-24 09:57:38 +00:00
|
|
|
int val, retval;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
curthread = _get_curthread();
|
|
|
|
if ((*sem)->syssem != 0) {
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_enter(curthread);
|
2005-04-02 01:20:00 +00:00
|
|
|
retval = ksem_wait((*sem)->semid);
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_leave(curthread);
|
2005-04-02 01:20:00 +00:00
|
|
|
return (retval);
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
_pthread_testcancel();
|
|
|
|
do {
|
|
|
|
while ((val = (*sem)->count) > 0) {
|
2007-11-23 05:42:52 +00:00
|
|
|
if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
|
2005-04-02 01:20:00 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2007-11-23 05:42:52 +00:00
|
|
|
atomic_add_int(&(*sem)->nwaiters, 1);
|
2007-11-21 06:01:02 +00:00
|
|
|
THR_CLEANUP_PUSH(curthread, sem_cancel_handler, sem);
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_enter(curthread);
|
2008-04-29 03:58:18 +00:00
|
|
|
retval = _thr_umtx_wait_uint(&(*sem)->count, 0, NULL, 0);
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_leave(curthread);
|
2007-11-21 06:01:02 +00:00
|
|
|
THR_CLEANUP_POP(curthread, 0);
|
2007-11-23 05:42:52 +00:00
|
|
|
atomic_add_int(&(*sem)->nwaiters, -1);
|
2005-04-02 01:20:00 +00:00
|
|
|
} while (retval == 0);
|
|
|
|
errno = retval;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-03-28 21:46:55 +00:00
|
|
|
_sem_timedwait(sem_t * __restrict sem,
|
|
|
|
const struct timespec * __restrict abstime)
|
2005-04-02 01:20:00 +00:00
|
|
|
{
|
|
|
|
struct timespec ts, ts2;
|
|
|
|
struct pthread *curthread;
|
2006-11-24 09:57:38 +00:00
|
|
|
int val, retval;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
curthread = _get_curthread();
|
|
|
|
if ((*sem)->syssem != 0) {
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_enter(curthread);
|
2005-04-02 01:20:00 +00:00
|
|
|
retval = ksem_timedwait((*sem)->semid, abstime);
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_leave(curthread);
|
2005-04-02 01:20:00 +00:00
|
|
|
return (retval);
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* The timeout argument is only supposed to
|
|
|
|
* be checked if the thread would have blocked.
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
_pthread_testcancel();
|
|
|
|
do {
|
|
|
|
while ((val = (*sem)->count) > 0) {
|
2007-11-23 05:42:52 +00:00
|
|
|
if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
|
2005-04-02 01:20:00 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (abstime == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
TIMESPEC_SUB(&ts2, abstime, &ts);
|
2007-11-23 05:42:52 +00:00
|
|
|
atomic_add_int(&(*sem)->nwaiters, 1);
|
|
|
|
THR_CLEANUP_PUSH(curthread, sem_cancel_handler, sem);
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_enter(curthread);
|
2008-04-29 03:58:18 +00:00
|
|
|
retval = _thr_umtx_wait_uint((uint32_t*)&(*sem)->count, 0, &ts2, 0);
|
2006-11-24 09:57:38 +00:00
|
|
|
_thr_cancel_leave(curthread);
|
2007-11-21 06:01:02 +00:00
|
|
|
THR_CLEANUP_POP(curthread, 0);
|
2007-11-23 05:42:52 +00:00
|
|
|
atomic_add_int(&(*sem)->nwaiters, -1);
|
2005-04-02 01:20:00 +00:00
|
|
|
} while (retval == 0);
|
|
|
|
errno = retval;
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2007-11-23 05:42:52 +00:00
|
|
|
/*
|
|
|
|
* sem_post() is required to be safe to call from within
|
|
|
|
* signal handlers, these code should work as that.
|
|
|
|
*/
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_sem_post(sem_t *sem)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2007-11-23 05:42:52 +00:00
|
|
|
int retval = 0;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*sem)->syssem != 0)
|
|
|
|
return (ksem_post((*sem)->semid));
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2007-11-23 05:42:52 +00:00
|
|
|
atomic_add_rel_int(&(*sem)->count, 1);
|
2007-11-21 06:01:02 +00:00
|
|
|
|
|
|
|
if ((*sem)->nwaiters) {
|
2008-04-29 03:58:18 +00:00
|
|
|
retval = _thr_umtx_wake(&(*sem)->count, 1, 0);
|
2008-01-07 02:26:29 +00:00
|
|
|
if (retval != 0)
|
|
|
|
retval = -1;
|
2007-11-21 06:01:02 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
return (retval);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|