1 /* $OpenBSD: synch.h,v 1.6 2019/10/24 12:32:18 sthen Exp $ */ 2 /* 3 * Copyright (c) 2017 Martin Pieuchot 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/atomic.h> 19 #include <sys/time.h> 20 #include <sys/futex.h> 21 22 static inline int 23 _wake(volatile uint32_t *p, int n) 24 { 25 return futex(p, FUTEX_WAKE, n, NULL, NULL); 26 } 27 28 static inline void 29 _wait(volatile uint32_t *p, int val) 30 { 31 while (*p != (uint32_t)val) 32 futex(p, FUTEX_WAIT, val, NULL, NULL); 33 } 34 35 static inline int 36 _twait(volatile uint32_t *p, int val, clockid_t clockid, const struct timespec *abs) 37 { 38 struct timespec rel; 39 40 if (abs == NULL) 41 return futex(p, FUTEX_WAIT, val, NULL, NULL); 42 43 if (abs->tv_nsec >= 1000000000 || clock_gettime(clockid, &rel)) 44 return (EINVAL); 45 46 rel.tv_sec = abs->tv_sec - rel.tv_sec; 47 if ((rel.tv_nsec = abs->tv_nsec - rel.tv_nsec) < 0) { 48 rel.tv_sec--; 49 rel.tv_nsec += 1000000000; 50 } 51 if (rel.tv_sec < 0) 52 return (ETIMEDOUT); 53 54 return futex(p, FUTEX_WAIT, val, &rel, NULL); 55 } 56 57 static inline int 58 _requeue(volatile uint32_t *p, int n, int m, volatile uint32_t *q) 59 { 60 return futex(p, FUTEX_REQUEUE, n, (void *)(long)m, q); 61 } 62