tsort: replace with openbsd version
[unleashed.git] / contrib / libjeffpc / synch.c
blob12b4db4724a50513da4216fe04097e50114bc34d
1 /*
2 * Copyright (c) 2015-2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
23 #include <stdio.h>
24 #include <stdlib.h>
26 #include <jeffpc/error.h>
27 #include <jeffpc/synch.h>
28 #include <jeffpc/config.h>
30 void mxinit(struct lock *l)
32 VERIFY0(pthread_mutex_init(&l->lock, NULL));
35 void mxdestroy(struct lock *l)
37 VERIFY0(pthread_mutex_destroy(&l->lock));
40 void mxlock(struct lock *l)
42 VERIFY0(pthread_mutex_lock(&l->lock));
45 void mxunlock(struct lock *l)
47 VERIFY0(pthread_mutex_unlock(&l->lock));
50 void rwinit(struct rwlock *l)
52 VERIFY0(pthread_rwlock_init(&l->lock, NULL));
55 void rwdestroy(struct rwlock *l)
57 VERIFY0(pthread_rwlock_destroy(&l->lock));
60 void rwlock(struct rwlock *l, bool wr)
62 if (wr)
63 VERIFY0(pthread_rwlock_wrlock(&l->lock));
64 else
65 VERIFY0(pthread_rwlock_rdlock(&l->lock));
68 void rwunlock(struct rwlock *l)
70 VERIFY0(pthread_rwlock_unlock(&l->lock));
73 void condinit(struct cond *c)
75 VERIFY0(pthread_cond_init(&c->cond, NULL));
78 void conddestroy(struct cond *c)
80 VERIFY0(pthread_cond_destroy(&c->cond));
83 void condwait(struct cond *c, struct lock *l)
85 VERIFY0(pthread_cond_wait(&c->cond, &l->lock));
88 int condreltimedwait(struct cond *c, struct lock *l,
89 const struct timespec *reltime)
91 int ret;
93 #ifdef HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
94 ret = -pthread_cond_reltimedwait_np(&c->cond, &l->lock, reltime);
95 #else
96 struct timespec abstime;
97 struct timespec now;
99 VERIFY0(clock_gettime(CLOCK_REALTIME, &now));
101 while ((now.tv_nsec + reltime->tv_nsec) >= 1000000000) {
102 now.tv_sec++;
103 now.tv_nsec -= 1000000000;
106 abstime.tv_sec = now.tv_sec + reltime->tv_sec;
107 abstime.tv_nsec = now.tv_nsec + reltime->tv_nsec;
109 ret = -pthread_cond_timedwait(&c->cond, &l->lock, &abstime);
110 #endif
112 if ((ret != 0) && (ret != -ETIMEDOUT))
113 panic("%s failed: %s", __func__, xstrerror(ret));
115 return ret;
118 void condsig(struct cond *c)
120 VERIFY0(pthread_cond_signal(&c->cond));
123 void condbcast(struct cond *c)
125 VERIFY0(pthread_cond_broadcast(&c->cond));