Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / audio / arts / patches / patch-ah
blobdfe2e382654ce73128508e96d1ec3b35f1ec1d8b
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
5 @@ -27,13 +27,158 @@
6  /* only compile this if we have libpthread available */
7  #ifdef HAVE_LIBPTHREAD
8  
9 +#include <signal.h>
10  #include <gsl/gslconfig.h>
12  #include <sys/types.h>
13  #include <stddef.h>
14  #include <stdarg.h>
15  #include <pthread.h>
16 +#include <unistd.h>
17 +#ifdef _POSIX_SEMAPHORES
18  #include <semaphore.h>
19 +#else
21 +#include <errno.h>
22 +#include <limits.h>
24 +struct sem_t {
25 +        pthread_mutex_t lock;
26 +        pthread_cond_t gtzero;
27 +        unsigned int count;
28 +        unsigned int nwaiters;
29 +};
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 *);
38 +static int
39 +sem_init(sem_t *sem, int pshared, unsigned int value)
42 +       /*
43 +        * Range check the arguments.
44 +        */
45 +       if (pshared != 0) {
46 +               errno = EPERM;
47 +               return -1;
48 +       }
50 +       if (value > INT_MAX) {
51 +               errno = EINVAL;
52 +               return -1;
53 +       }
55 +       /*
56 +        * Initialize the semaphore.
57 +        */
58 +       if (pthread_mutex_init(&sem->lock, NULL) != 0) {
59 +               errno = ENOMEM;
60 +               return -1;
61 +       }
63 +       if (pthread_cond_init(&sem->gtzero, NULL) != 0) {
64 +               pthread_mutex_destroy(&sem->lock);
65 +               errno = ENOMEM;
66 +               return -1;
67 +       }
68 +       
69 +       sem->count = value;
70 +       sem->nwaiters = 0;
72 +       return 0;
75 +static int
76 +sem_destroy(sem_t *sem)
78 +       
79 +       /* Make sure there are no waiters. */
80 +       pthread_mutex_lock(&sem->lock);
81 +       if (sem->nwaiters > 0) {
82 +               pthread_mutex_unlock(&sem->lock);
83 +               errno = EBUSY;
84 +               return -1;
85 +       }
86 +       pthread_mutex_unlock(&sem->lock);
87 +       
88 +       pthread_mutex_destroy(&sem->lock);
89 +       pthread_cond_destroy(&sem->gtzero);
91 +       return 0;
94 +static int
95 +sem_wait(sem_t *sem)
97 +       pthread_testcancel();
98 +       
99 +       pthread_mutex_lock(&sem->lock);
101 +       while (sem->count == 0) {
102 +               sem->nwaiters++;
103 +               pthread_cond_wait(&sem->gtzero, &sem->lock);
104 +       }
105 +       sem->count--;
107 +       pthread_mutex_unlock(&sem->lock);
109 +       pthread_testcancel();
110 +       return 0;
113 +static int
114 +sem_trywait(sem_t *sem)
116 +       int     retval;
118 +       pthread_mutex_lock(&sem->lock);
120 +       if (sem->count > 0) {
121 +               sem->count--;
122 +               retval = 0;
123 +       } else {
124 +               errno = EAGAIN;
125 +               retval = -1;
126 +       }
127 +       
128 +       pthread_mutex_unlock(&sem->lock);
130 +       return retval;
133 +static int
134 +sem_post(sem_t *sem)
136 +       pthread_mutex_lock(&sem->lock);
138 +       sem->count++;
139 +       if (sem->nwaiters > 0) {
140 +               pthread_cond_broadcast(&sem->gtzero);
141 +       }
143 +       pthread_mutex_unlock(&sem->lock);
145 +       return 0;
148 +static int
149 +sem_getvalue(sem_t *sem, int *sval)
152 +       pthread_mutex_lock(&sem->lock);
153 +       *sval = sem->count;
154 +       pthread_mutex_unlock(&sem->lock);
156 +       return 0;
158 +#endif
161  #include <debug.h>
162  #include <string.h>
164 @@ -186,10 +331,12 @@ public:
165         Thread_impl(Thread *thread) : thread(thread) {
166         }
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");
173 +#endif
174         }
175         static pthread_key_t privateDataKey;
176         static void *threadStartInternal(void *impl)