2000-01-20 07:54:49 +00:00
|
|
|
/*
|
2000-07-18 01:38:19 +00:00
|
|
|
* Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
|
2000-01-20 07:54:49 +00:00
|
|
|
* 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$
|
|
|
|
*/
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
#include "namespace.h"
|
2005-09-01 15:21:23 +00:00
|
|
|
#include <sys/types.h>
|
2004-02-03 05:50:07 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2000-01-20 07:54:49 +00:00
|
|
|
#include <pthread.h>
|
2004-02-03 05:50:07 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <_semaphore.h>
|
2003-04-18 05:04:16 +00:00
|
|
|
#include "un-namespace.h"
|
2004-02-03 05:50:07 +00:00
|
|
|
#include "libc_private.h"
|
2002-09-16 08:45:36 +00:00
|
|
|
#include "thr_private.h"
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
|
|
|
|
extern int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
|
|
|
|
extern int pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *,
|
|
|
|
struct timespec *);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2001-04-10 04:19:21 +00:00
|
|
|
__weak_reference(_sem_init, sem_init);
|
|
|
|
__weak_reference(_sem_wait, sem_wait);
|
2004-02-03 05:50:07 +00:00
|
|
|
__weak_reference(_sem_timedwait, sem_timedwait);
|
2001-04-10 04:19:21 +00:00
|
|
|
__weak_reference(_sem_post, sem_post);
|
2001-01-24 13:03:38 +00:00
|
|
|
|
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
static inline int
|
|
|
|
sem_check_validity(sem_t *sem)
|
2000-01-20 07:54:49 +00:00
|
|
|
{
|
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
if ((sem != NULL) && ((*sem)->magic == SEM_MAGIC))
|
|
|
|
return (0);
|
|
|
|
else {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
decrease_nwaiters(void *arg)
|
|
|
|
{
|
|
|
|
sem_t *sem = (sem_t *)arg;
|
|
|
|
|
|
|
|
(*sem)->nwaiters--;
|
2000-01-20 07:54:49 +00:00
|
|
|
/*
|
2004-02-03 05:50:07 +00:00
|
|
|
* this function is called from cancellation point,
|
|
|
|
* the mutex should already be hold.
|
2000-01-20 07:54:49 +00:00
|
|
|
*/
|
2004-02-03 05:50:07 +00:00
|
|
|
_pthread_mutex_unlock(&(*sem)->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sem_t
|
|
|
|
sem_alloc(unsigned int value, semid_t semid, int system_sem)
|
|
|
|
{
|
|
|
|
sem_t sem;
|
2000-01-20 07:54:49 +00:00
|
|
|
|
|
|
|
if (value > SEM_VALUE_MAX) {
|
2000-02-16 19:34:53 +00:00
|
|
|
errno = EINVAL;
|
2004-02-03 05:50:07 +00:00
|
|
|
return (NULL);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
sem = (sem_t)malloc(sizeof(struct sem));
|
|
|
|
if (sem == NULL) {
|
2000-02-16 19:34:53 +00:00
|
|
|
errno = ENOSPC;
|
2004-02-03 05:50:07 +00:00
|
|
|
return (NULL);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the semaphore.
|
|
|
|
*/
|
2004-02-03 05:50:07 +00:00
|
|
|
if (_pthread_mutex_init(&sem->lock, NULL) != 0) {
|
|
|
|
free(sem);
|
2000-02-16 19:34:53 +00:00
|
|
|
errno = ENOSPC;
|
2004-02-03 05:50:07 +00:00
|
|
|
return (NULL);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
if (_pthread_cond_init(&sem->gtzero, NULL) != 0) {
|
|
|
|
_pthread_mutex_destroy(&sem->lock);
|
|
|
|
free(sem);
|
2000-02-16 19:34:53 +00:00
|
|
|
errno = ENOSPC;
|
2004-02-03 05:50:07 +00:00
|
|
|
return (NULL);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
sem->count = (u_int32_t)value;
|
|
|
|
sem->nwaiters = 0;
|
|
|
|
sem->magic = SEM_MAGIC;
|
|
|
|
sem->semid = semid;
|
|
|
|
sem->syssem = system_sem;
|
|
|
|
return (sem);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-02-03 05:50:07 +00:00
|
|
|
_sem_init(sem_t *sem, int pshared, unsigned int value)
|
2000-01-20 07:54:49 +00:00
|
|
|
{
|
2004-02-03 05:50:07 +00:00
|
|
|
semid_t semid;
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
semid = (semid_t)SEM_USER;
|
2004-02-03 05:50:07 +00:00
|
|
|
if ((pshared != 0) && (ksem_init(&semid, value) != 0))
|
|
|
|
return (-1);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
(*sem) = sem_alloc(value, semid, pshared);
|
|
|
|
if ((*sem) == NULL) {
|
|
|
|
if (pshared != 0)
|
|
|
|
ksem_destroy(semid);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-01-24 13:03:38 +00:00
|
|
|
_sem_wait(sem_t *sem)
|
2000-01-20 07:54:49 +00:00
|
|
|
{
|
2004-02-06 15:20:56 +00:00
|
|
|
struct pthread *curthread;
|
2004-02-03 05:50:07 +00:00
|
|
|
int retval;
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
curthread = _get_curthread();
|
2004-02-06 15:20:56 +00:00
|
|
|
if ((*sem)->syssem != 0) {
|
|
|
|
_thr_cancel_enter(curthread);
|
2004-02-03 05:50:07 +00:00
|
|
|
retval = ksem_wait((*sem)->semid);
|
2004-02-06 15:20:56 +00:00
|
|
|
_thr_cancel_leave(curthread, retval != 0);
|
|
|
|
}
|
2004-02-03 05:50:07 +00:00
|
|
|
else {
|
2004-02-06 15:20:56 +00:00
|
|
|
pthread_testcancel();
|
2004-02-03 05:50:07 +00:00
|
|
|
_pthread_mutex_lock(&(*sem)->lock);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-06 15:20:56 +00:00
|
|
|
while ((*sem)->count <= 0) {
|
2004-02-03 05:50:07 +00:00
|
|
|
(*sem)->nwaiters++;
|
2004-12-18 18:07:37 +00:00
|
|
|
THR_CLEANUP_PUSH(curthread, decrease_nwaiters, sem);
|
2004-02-03 05:50:07 +00:00
|
|
|
pthread_cond_wait(&(*sem)->gtzero, &(*sem)->lock);
|
2004-12-18 18:07:37 +00:00
|
|
|
THR_CLEANUP_POP(curthread, 0);
|
2004-02-03 05:50:07 +00:00
|
|
|
(*sem)->nwaiters--;
|
|
|
|
}
|
|
|
|
(*sem)->count--;
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
_pthread_mutex_unlock(&(*sem)->lock);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
|
|
|
retval = 0;
|
2000-02-16 19:34:53 +00:00
|
|
|
}
|
2004-02-06 15:20:56 +00:00
|
|
|
return (retval);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-02-03 05:50:07 +00:00
|
|
|
_sem_timedwait(sem_t * __restrict sem,
|
|
|
|
struct timespec * __restrict abs_timeout)
|
2000-01-20 07:54:49 +00:00
|
|
|
{
|
2004-02-06 15:20:56 +00:00
|
|
|
struct pthread *curthread;
|
2003-04-18 05:04:16 +00:00
|
|
|
int retval;
|
2004-02-03 05:50:07 +00:00
|
|
|
int timeout_invalid;
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-06 15:20:56 +00:00
|
|
|
if ((*sem)->syssem != 0) {
|
|
|
|
curthread = _get_curthread();
|
|
|
|
_thr_cancel_enter(curthread);
|
2004-02-03 05:50:07 +00:00
|
|
|
retval = ksem_timedwait((*sem)->semid, abs_timeout);
|
2004-02-06 15:20:56 +00:00
|
|
|
_thr_cancel_leave(curthread, retval != 0);
|
|
|
|
}
|
2004-02-03 05:50:07 +00:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* The timeout argument is only supposed to
|
|
|
|
* be checked if the thread would have blocked.
|
|
|
|
* This is checked outside of the lock so a
|
|
|
|
* segfault on an invalid address doesn't end
|
|
|
|
* up leaving the mutex locked.
|
|
|
|
*/
|
2004-02-06 15:20:56 +00:00
|
|
|
pthread_testcancel();
|
2004-02-03 05:50:07 +00:00
|
|
|
timeout_invalid = (abs_timeout->tv_nsec >= 1000000000) ||
|
|
|
|
(abs_timeout->tv_nsec < 0);
|
|
|
|
_pthread_mutex_lock(&(*sem)->lock);
|
|
|
|
|
2004-02-06 15:20:56 +00:00
|
|
|
if ((*sem)->count <= 0) {
|
2004-02-03 05:50:07 +00:00
|
|
|
if (timeout_invalid) {
|
|
|
|
_pthread_mutex_unlock(&(*sem)->lock);
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
(*sem)->nwaiters++;
|
|
|
|
pthread_cleanup_push(decrease_nwaiters, sem);
|
|
|
|
pthread_cond_timedwait(&(*sem)->gtzero,
|
|
|
|
&(*sem)->lock, abs_timeout);
|
|
|
|
pthread_cleanup_pop(0);
|
|
|
|
(*sem)->nwaiters--;
|
|
|
|
}
|
|
|
|
if ((*sem)->count == 0) {
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
(*sem)->count--;
|
|
|
|
retval = 0;
|
|
|
|
}
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
_pthread_mutex_unlock(&(*sem)->lock);
|
|
|
|
}
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-06 15:20:56 +00:00
|
|
|
return (retval);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-02-03 05:50:07 +00:00
|
|
|
_sem_post(sem_t *sem)
|
2000-01-20 07:54:49 +00:00
|
|
|
{
|
2004-02-03 05:50:07 +00:00
|
|
|
struct pthread *curthread;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (sem_check_validity(sem) != 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if ((*sem)->syssem != 0)
|
|
|
|
retval = ksem_post((*sem)->semid);
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* sem_post() is required to be safe to call from within
|
|
|
|
* signal handlers. Thus, we must enter a critical region.
|
|
|
|
*/
|
|
|
|
curthread = _get_curthread();
|
|
|
|
_thr_critical_enter(curthread);
|
|
|
|
_pthread_mutex_lock(&(*sem)->lock);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
(*sem)->count++;
|
|
|
|
if ((*sem)->nwaiters > 0)
|
|
|
|
_pthread_cond_signal(&(*sem)->gtzero);
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2004-02-03 05:50:07 +00:00
|
|
|
_pthread_mutex_unlock(&(*sem)->lock);
|
|
|
|
_thr_critical_leave(curthread);
|
|
|
|
retval = 0;
|
|
|
|
}
|
2000-01-20 07:54:49 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
return (retval);
|
2000-01-20 07:54:49 +00:00
|
|
|
}
|