Skip to content
Snippets Groups Projects
Commit 7c250a0b authored by deuce's avatar deuce
Browse files

Back out xpbsem stuff... doing it differently.

parent fcb94b13
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,6 @@ include $(SRC_ROOT)/build/Common.gmake
ifdef XP_SEM
MTOBJS += $(MTOBJODIR)$(DIRSEP)xpsem$(OFILE)
endif
MTOBJS += $(MTOBJODIR)$(DIRSEP)xpbsem$(OFILE)
MT_CFLAGS += -DLINK_LIST_THREADSAFE
......
/*
* $Id$
*
* 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: src/lib/libc_r/uthread/uthread_sem.c,v 1.3.2.1 2000/07/18 02:05:57 jasone Exp $
*/
#include <errno.h>
#include "xpbsem.h"
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>
#include "gen_defs.h"
int
bsem_init(bsem_t *bsem, unsigned int value)
{
int retval;
/*
* Range check the arguments.
*/
if (value)
value=TRUE;
*bsem = (bsem_t)malloc(sizeof(struct bsem));
if (*bsem == NULL) {
errno = ENOSPC;
retval = -1;
goto RETURN;
}
/*
* Initialize the semaphore.
*/
if (pthread_mutex_init(&(*bsem)->lock, NULL) != 0) {
free(*bsem);
errno = ENOSPC;
retval = -1;
goto RETURN;
}
if (pthread_cond_init(&(*bsem)->gtzero, NULL) != 0) {
pthread_mutex_destroy(&(*bsem)->lock);
free(*bsem);
errno = ENOSPC;
retval = -1;
goto RETURN;
}
(*bsem)->count = (u_int32_t)value;
(*bsem)->nwaiters = 0;
(*bsem)->magic = BSEM_MAGIC;
retval = 0;
RETURN:
return retval;
}
int
bsem_destroy(bsem_t *bsem)
{
int retval;
_BSEM_CHECK_VALIDITY(bsem);
/* Make sure there are no waiters. */
pthread_mutex_lock(&(*bsem)->lock);
if ((*bsem)->nwaiters > 0) {
pthread_mutex_unlock(&(*bsem)->lock);
errno = EBUSY;
retval = -1;
goto RETURN;
}
pthread_mutex_unlock(&(*bsem)->lock);
pthread_mutex_destroy(&(*bsem)->lock);
pthread_cond_destroy(&(*bsem)->gtzero);
(*bsem)->magic = 0;
free(*bsem);
retval = 0;
RETURN:
return retval;
}
int
bsem_wait(bsem_t *bsem)
{
int retval;
pthread_testcancel();
_BSEM_CHECK_VALIDITY(bsem);
pthread_mutex_lock(&(*bsem)->lock);
while (!((*bsem)->count)) {
(*bsem)->nwaiters++;
pthread_cond_wait(&(*bsem)->gtzero, &(*bsem)->lock);
(*bsem)->nwaiters--;
}
pthread_mutex_unlock(&(*bsem)->lock);
retval = 0;
RETURN:
pthread_testcancel();
return retval;
}
int
bsem_trywait(bsem_t *bsem)
{
int retval;
_BSEM_CHECK_VALIDITY(bsem);
pthread_mutex_lock(&(*bsem)->lock);
if ((*bsem)->count)
retval = 0;
else {
errno = EAGAIN;
retval = -1;
}
pthread_mutex_unlock(&(*bsem)->lock);
RETURN:
return retval;
}
int
bsem_post(bsem_t *bsem)
{
int retval;
_BSEM_CHECK_VALIDITY(bsem);
pthread_mutex_lock(&(*bsem)->lock);
(*bsem)->count=TRUE;
if ((*bsem)->nwaiters > 0) {
/*
* We must use pthread_cond_broadcast() rather than
* to wake up ALL waiting threads
*/
pthread_cond_broadcast(&(*bsem)->gtzero);
}
pthread_mutex_unlock(&(*bsem)->lock);
retval = 0;
RETURN:
return retval;
}
int
bsem_getvalue(bsem_t *bsem, int *sval)
{
int retval;
_BSEM_CHECK_VALIDITY(bsem);
pthread_mutex_lock(&(*bsem)->lock);
*sval = (int)(*bsem)->count;
pthread_mutex_unlock(&(*bsem)->lock);
retval = 0;
RETURN:
return retval;
}
int
bsem_timedwait(bsem_t *bsem, const struct timespec *abs_timeout)
{
int retval=0;
pthread_testcancel();
_BSEM_CHECK_VALIDITY(bsem);
pthread_mutex_lock(&(*bsem)->lock);
while (!((*bsem)->count)) {
(*bsem)->nwaiters++;
retval=pthread_cond_timedwait(&(*bsem)->gtzero, &(*bsem)->lock, abs_timeout);
(*bsem)->nwaiters--;
if(retval) {
errno=retval;
retval=-1;
break;
}
}
pthread_mutex_unlock(&(*bsem)->lock);
RETURN:
pthread_testcancel();
return retval;
}
int
bsem_reset(bsem_t *bsem)
{
int retval;
_BSEM_CHECK_VALIDITY(bsem);
pthread_mutex_lock(&(*bsem)->lock);
(*bsem)->count=FALSE;
pthread_mutex_unlock(&(*bsem)->lock);
retval = 0;
RETURN:
return retval;
}
#ifndef _XPBSEM_H_
#define _XPBSEM_H_
/*
* $Id$
*
* semaphore.h: POSIX 1003.1b semaphores
*/
/*-
* Copyright (c) 1996, 1997
* HD Associates, Inc. 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, 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 HD Associates, Inc
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES 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 HD ASSOCIATES 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.
*
* $FreeBSD: src/sys/posix4/semaphore.h,v 1.6 2000/01/20 07:55:42 jasone Exp $
*/
#include <limits.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
/* Opaque type definition. */
struct bsem;
typedef struct bsem *bsem_t;
#ifdef __solaris__
typedef unsigned int u_int32_t;
#endif
#if defined(__cplusplus)
extern "C" {
#endif
int bsem_init (bsem_t *, unsigned int);
int bsem_destroy (bsem_t *);
int bsem_wait (bsem_t *);
int bsem_trywait (bsem_t *);
int bsem_post (bsem_t *);
int bsem_getvalue (bsem_t *, int *);
int bsem_reset (bsem_t *);
int bsem_timedwait (bsem_t *bsem, const struct timespec *abs_timeout);
#if defined(__cplusplus)
}
#endif
/*
* $Id$
*/
struct bsem {
#define BSEM_MAGIC ((u_int32_t) 0x09fa4013)
u_int32_t magic;
pthread_mutex_t lock;
pthread_cond_t gtzero;
u_int32_t count;
u_int32_t nwaiters;
};
#define _BSEM_CHECK_VALIDITY(bsem) \
if (bsem==NULL || (*(bsem))->magic != BSEM_MAGIC) { \
errno = EINVAL; \
retval = -1; \
goto RETURN; \
}
#endif /* _XPBSEM_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment