1 $NetBSD: patch-ah,v 1.3 2004/04/20 12:15:51 markd Exp $
3 --- mcop_mt/threads_posix.cc.orig 2005-09-10 20:13:32.000000000 +1200
4 +++ mcop_mt/threads_posix.cc
6 /* only compile this if we have libpthread available */
10 #include <gsl/gslconfig.h>
12 #include <sys/types.h>
17 +#ifdef _POSIX_SEMAPHORES
18 #include <semaphore.h>
25 + pthread_mutex_t lock;
26 + pthread_cond_t gtzero;
28 + unsigned int nwaiters;
31 +static int sem_init(sem_t *, int, unsigned int);
32 +static int sem_destroy(sem_t *);
33 +static int sem_wait(sem_t *);
34 +static int sem_trywait(sem_t *);
35 +static int sem_post(sem_t *);
36 +static int sem_getvalue(sem_t *, int *);
39 +sem_init(sem_t *sem, int pshared, unsigned int value)
43 + * Range check the arguments.
50 + if (value > INT_MAX) {
56 + * Initialize the semaphore.
58 + if (pthread_mutex_init(&sem->lock, NULL) != 0) {
63 + if (pthread_cond_init(&sem->gtzero, NULL) != 0) {
64 + pthread_mutex_destroy(&sem->lock);
76 +sem_destroy(sem_t *sem)
79 + /* Make sure there are no waiters. */
80 + pthread_mutex_lock(&sem->lock);
81 + if (sem->nwaiters > 0) {
82 + pthread_mutex_unlock(&sem->lock);
86 + pthread_mutex_unlock(&sem->lock);
88 + pthread_mutex_destroy(&sem->lock);
89 + pthread_cond_destroy(&sem->gtzero);
97 + pthread_testcancel();
99 + pthread_mutex_lock(&sem->lock);
101 + while (sem->count == 0) {
103 + pthread_cond_wait(&sem->gtzero, &sem->lock);
107 + pthread_mutex_unlock(&sem->lock);
109 + pthread_testcancel();
114 +sem_trywait(sem_t *sem)
118 + pthread_mutex_lock(&sem->lock);
120 + if (sem->count > 0) {
128 + pthread_mutex_unlock(&sem->lock);
134 +sem_post(sem_t *sem)
136 + pthread_mutex_lock(&sem->lock);
139 + if (sem->nwaiters > 0) {
140 + pthread_cond_broadcast(&sem->gtzero);
143 + pthread_mutex_unlock(&sem->lock);
149 +sem_getvalue(sem_t *sem, int *sval)
152 + pthread_mutex_lock(&sem->lock);
153 + *sval = sem->count;
154 + pthread_mutex_unlock(&sem->lock);
164 @@ -186,10 +331,12 @@ public:
165 Thread_impl(Thread *thread) : thread(thread) {
167 void setPriority(int priority) {
168 +#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
169 struct sched_param sp;
170 sp.sched_priority = priority;
171 if (pthread_setschedparam(pthread, SCHED_FIFO, &sp))
172 arts_debug("Thread::setPriority: sched_setscheduler failed");
175 static pthread_key_t privateDataKey;
176 static void *threadStartInternal(void *impl)