2005-10-19 06:15:03 +04:00
|
|
|
/* $NetBSD: sem.c,v 1.9 2005/10/19 02:15:03 chs Exp $ */
|
2003-01-23 01:51:42 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2003-01-24 04:54:02 +03:00
|
|
|
* by Jason R. Thorpe of Wasabi Systems, Inc.
|
2003-01-23 01:51:42 +03:00
|
|
|
*
|
|
|
|
* 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, this list of conditions and the following disclaimer.
|
|
|
|
* 2. 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
2003-01-20 23:10:19 +03: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.
|
|
|
|
*/
|
|
|
|
|
2003-03-08 11:03:34 +03:00
|
|
|
#include <sys/cdefs.h>
|
2005-10-19 06:15:03 +04:00
|
|
|
__RCSID("$NetBSD: sem.c,v 1.9 2005/10/19 02:15:03 chs Exp $");
|
2003-03-08 11:03:34 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ksem.h>
|
|
|
|
#include <sys/queue.h>
|
2003-01-20 23:10:19 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
#include "pthread.h"
|
|
|
|
#include "pthread_int.h"
|
2003-01-20 23:10:19 +03:00
|
|
|
|
|
|
|
struct _sem_st {
|
2003-01-23 01:51:42 +03:00
|
|
|
unsigned int usem_magic;
|
|
|
|
#define USEM_MAGIC 0x09fa4012
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
LIST_ENTRY(_sem_st) usem_list;
|
|
|
|
semid_t usem_semid; /* 0 -> user (non-shared) */
|
|
|
|
#define USEM_USER 0 /* assumes kernel does not use NULL */
|
|
|
|
sem_t *usem_identity;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
/* Protects data below. */
|
|
|
|
pthread_spin_t usem_interlock;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
struct pthread_queue_t usem_waiters;
|
|
|
|
unsigned int usem_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sem_alloc(unsigned int value, semid_t semid, sem_t *semp);
|
2003-01-20 23:10:19 +03:00
|
|
|
static void sem_free(sem_t sem);
|
|
|
|
|
2003-01-20 23:52:24 +03:00
|
|
|
static LIST_HEAD(, _sem_st) named_sems = LIST_HEAD_INITIALIZER(&named_sems);
|
2003-01-20 23:10:19 +03:00
|
|
|
static pthread_mutex_t named_sems_mtx = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sem_free(sem_t sem)
|
|
|
|
{
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
sem->usem_magic = 0;
|
2003-01-20 23:10:19 +03:00
|
|
|
free(sem);
|
|
|
|
}
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
static int
|
|
|
|
sem_alloc(unsigned int value, semid_t semid, sem_t *semp)
|
2003-01-20 23:10:19 +03:00
|
|
|
{
|
|
|
|
sem_t sem;
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
if (value > SEM_VALUE_MAX)
|
|
|
|
return (EINVAL);
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
if ((sem = malloc(sizeof(struct _sem_st))) == NULL)
|
|
|
|
return (ENOSPC);
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
sem->usem_magic = USEM_MAGIC;
|
|
|
|
pthread_lockinit(&sem->usem_interlock);
|
|
|
|
PTQ_INIT(&sem->usem_waiters);
|
|
|
|
sem->usem_count = value;
|
|
|
|
sem->usem_semid = semid;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
*semp = sem;
|
|
|
|
return (0);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_init(sem_t *sem, int pshared, unsigned int value)
|
|
|
|
{
|
|
|
|
semid_t semid;
|
2003-01-23 01:51:42 +03:00
|
|
|
int error;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
semid = USEM_USER;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
if (pshared && _ksem_init(value, &semid) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if ((error = sem_alloc(value, semid, sem)) != 0) {
|
2003-01-23 02:18:06 +03:00
|
|
|
if (semid != USEM_USER)
|
|
|
|
_ksem_destroy(semid);
|
2003-01-23 01:51:42 +03:00
|
|
|
errno = error;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_destroy(sem_t *sem)
|
|
|
|
{
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_t self;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((*sem)->usem_semid != USEM_USER) {
|
|
|
|
if (_ksem_destroy((*sem)->usem_semid))
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
self = pthread__self();
|
|
|
|
pthread_spinlock(self, &(*sem)->usem_interlock);
|
|
|
|
if (!PTQ_EMPTY(&(*sem)->usem_waiters)) {
|
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
|
|
|
errno = EBUSY;
|
|
|
|
return (-1);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
sem_free(*sem);
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
return (0);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sem_t *
|
|
|
|
sem_open(const char *name, int oflag, ...)
|
|
|
|
{
|
2003-01-23 01:51:42 +03:00
|
|
|
sem_t *sem, s;
|
2003-01-20 23:10:19 +03:00
|
|
|
semid_t semid;
|
|
|
|
mode_t mode;
|
|
|
|
unsigned int value;
|
2003-01-23 01:51:42 +03:00
|
|
|
int error;
|
|
|
|
va_list ap;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
|
|
|
mode = 0;
|
|
|
|
value = 0;
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
if (oflag & O_CREAT) {
|
2003-01-20 23:10:19 +03:00
|
|
|
va_start(ap, oflag);
|
|
|
|
mode = va_arg(ap, int);
|
|
|
|
value = va_arg(ap, unsigned int);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
|
2003-01-20 23:10:19 +03:00
|
|
|
/*
|
2003-01-23 01:51:42 +03:00
|
|
|
* We can be lazy and let the kernel handle the oflag,
|
2003-01-20 23:10:19 +03:00
|
|
|
* we'll just merge duplicate IDs into our list.
|
|
|
|
*/
|
|
|
|
if (_ksem_open(name, oflag, mode, value, &semid) == -1)
|
|
|
|
return (SEM_FAILED);
|
2003-01-23 01:51:42 +03:00
|
|
|
|
2003-01-20 23:10:19 +03:00
|
|
|
/*
|
2003-01-23 01:51:42 +03:00
|
|
|
* Search for a duplicate ID, we must return the same sem_t *
|
2003-01-20 23:10:19 +03:00
|
|
|
* if we locate one.
|
|
|
|
*/
|
|
|
|
pthread_mutex_lock(&named_sems_mtx);
|
2003-01-23 01:51:42 +03:00
|
|
|
LIST_FOREACH(s, &named_sems, usem_list) {
|
|
|
|
if (s->usem_semid == semid) {
|
|
|
|
pthread_mutex_unlock(&named_sems_mtx);
|
|
|
|
return (s->usem_identity);
|
|
|
|
}
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
|
|
|
|
if ((sem = malloc(sizeof(*sem))) == NULL) {
|
|
|
|
error = ENOSPC;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if ((error = sem_alloc(value, semid, sem)) != 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&named_sems, *sem, usem_list);
|
|
|
|
(*sem)->usem_identity = sem;
|
2003-01-20 23:10:19 +03:00
|
|
|
pthread_mutex_unlock(&named_sems_mtx);
|
2003-01-23 01:51:42 +03:00
|
|
|
|
2003-01-20 23:10:19 +03:00
|
|
|
return (sem);
|
2003-01-23 01:51:42 +03:00
|
|
|
|
|
|
|
bad:
|
2003-01-20 23:10:19 +03:00
|
|
|
pthread_mutex_unlock(&named_sems_mtx);
|
|
|
|
_ksem_close(semid);
|
|
|
|
if (sem != NULL) {
|
|
|
|
if (*sem != NULL)
|
|
|
|
sem_free(*sem);
|
|
|
|
free(sem);
|
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
errno = error;
|
2003-01-20 23:10:19 +03:00
|
|
|
return (SEM_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_close(sem_t *sem)
|
|
|
|
{
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
|
2003-01-20 23:10:19 +03:00
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((*sem)->usem_semid == USEM_USER) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2003-01-20 23:10:19 +03:00
|
|
|
pthread_mutex_lock(&named_sems_mtx);
|
2003-01-23 01:51:42 +03:00
|
|
|
if (_ksem_close((*sem)->usem_semid) == -1) {
|
2003-01-20 23:10:19 +03:00
|
|
|
pthread_mutex_unlock(&named_sems_mtx);
|
|
|
|
return (-1);
|
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
LIST_REMOVE((*sem), usem_list);
|
2003-01-20 23:10:19 +03:00
|
|
|
pthread_mutex_unlock(&named_sems_mtx);
|
|
|
|
sem_free(*sem);
|
|
|
|
free(sem);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_unlink(const char *name)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (_ksem_unlink(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_wait(sem_t *sem)
|
|
|
|
{
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_t self;
|
2005-10-16 04:07:24 +04:00
|
|
|
extern int pthread__started;
|
2003-01-23 01:51:42 +03:00
|
|
|
|
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
#endif
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
self = pthread__self();
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
if ((*sem)->usem_semid != USEM_USER) {
|
|
|
|
pthread__testcancel(self);
|
|
|
|
return (_ksem_wait((*sem)->usem_semid));
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
2005-10-16 04:07:24 +04:00
|
|
|
if (pthread__started == 0) {
|
2005-10-19 06:15:03 +04:00
|
|
|
sigset_t set, oset;
|
|
|
|
|
|
|
|
sigfillset(&set);
|
|
|
|
(void) sigprocmask(SIG_SETMASK, &set, &oset);
|
|
|
|
for (;;) {
|
|
|
|
if ((*sem)->usem_count > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void) sigsuspend(&oset);
|
|
|
|
}
|
|
|
|
(*sem)->usem_count--;
|
|
|
|
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
|
|
|
|
return 0;
|
2005-10-16 04:07:24 +04:00
|
|
|
}
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
for (;;) {
|
|
|
|
pthread_spinlock(self, &(*sem)->usem_interlock);
|
|
|
|
pthread_spinlock(self, &self->pt_statelock);
|
|
|
|
if (self->pt_cancel) {
|
|
|
|
pthread_spinunlock(self, &self->pt_statelock);
|
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*sem)->usem_count > 0) {
|
|
|
|
pthread_spinunlock(self, &self->pt_statelock);
|
|
|
|
break;
|
|
|
|
}
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
PTQ_INSERT_TAIL(&(*sem)->usem_waiters, self, pt_sleep);
|
|
|
|
self->pt_state = PT_STATE_BLOCKED_QUEUE;
|
|
|
|
self->pt_sleepobj = *sem;
|
|
|
|
self->pt_sleepq = &(*sem)->usem_waiters;
|
|
|
|
self->pt_sleeplock = &(*sem)->usem_interlock;
|
|
|
|
pthread_spinunlock(self, &self->pt_statelock);
|
|
|
|
|
|
|
|
/* XXX What about signals? */
|
|
|
|
|
|
|
|
pthread__block(self, &(*sem)->usem_interlock);
|
|
|
|
/* interlock is not held when we return */
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
(*sem)->usem_count--;
|
|
|
|
|
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
return (0);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_trywait(sem_t *sem)
|
|
|
|
{
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_t self;
|
2005-10-19 06:15:03 +04:00
|
|
|
extern int pthread__started;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
#endif
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
if ((*sem)->usem_semid != USEM_USER)
|
|
|
|
return (_ksem_trywait((*sem)->usem_semid));
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2005-10-19 06:15:03 +04:00
|
|
|
if (pthread__started == 0) {
|
|
|
|
sigset_t set, oset;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
sigfillset(&set);
|
|
|
|
(void) sigprocmask(SIG_SETMASK, &set, &oset);
|
|
|
|
if ((*sem)->usem_count > 0) {
|
|
|
|
(*sem)->usem_count--;
|
|
|
|
} else {
|
|
|
|
errno = EAGAIN;
|
|
|
|
rv = -1;
|
|
|
|
}
|
|
|
|
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
self = pthread__self();
|
|
|
|
|
|
|
|
pthread_spinlock(self, &(*sem)->usem_interlock);
|
|
|
|
|
|
|
|
if ((*sem)->usem_count == 0) {
|
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
2003-01-20 23:10:19 +03:00
|
|
|
errno = EAGAIN;
|
2003-01-23 01:51:42 +03:00
|
|
|
return (-1);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
(*sem)->usem_count--;
|
|
|
|
|
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
|
|
|
|
|
|
|
return (0);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_post(sem_t *sem)
|
|
|
|
{
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_t self, blocked;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((*sem)->usem_semid != USEM_USER)
|
|
|
|
return (_ksem_post((*sem)->usem_semid));
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
self = pthread__self();
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_spinlock(self, &(*sem)->usem_interlock);
|
|
|
|
(*sem)->usem_count++;
|
|
|
|
blocked = PTQ_FIRST(&(*sem)->usem_waiters);
|
2003-11-25 02:54:13 +03:00
|
|
|
if (blocked) {
|
2003-01-23 01:51:42 +03:00
|
|
|
PTQ_REMOVE(&(*sem)->usem_waiters, blocked, pt_sleep);
|
2003-11-25 02:54:13 +03:00
|
|
|
/* Give the head of the blocked queue another try. */
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread__sched(self, blocked);
|
2003-11-25 02:54:13 +03:00
|
|
|
}
|
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
return (0);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
|
|
|
|
{
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_t self;
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|
2003-01-23 01:51:42 +03:00
|
|
|
#endif
|
|
|
|
if ((*sem)->usem_semid != USEM_USER)
|
|
|
|
return (_ksem_getvalue((*sem)->usem_semid, sval));
|
|
|
|
|
|
|
|
self = pthread__self();
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
pthread_spinlock(self, &(*sem)->usem_interlock);
|
|
|
|
*sval = (int) (*sem)->usem_count;
|
|
|
|
pthread_spinunlock(self, &(*sem)->usem_interlock);
|
2003-01-20 23:10:19 +03:00
|
|
|
|
2003-01-23 01:51:42 +03:00
|
|
|
return (0);
|
2003-01-20 23:10:19 +03:00
|
|
|
}
|