Skip to content

Commit 3d7728e

Browse files
committed
Lock implementation changes for OpenBSD.
OpenBSD has futex, but not futex_pi. Segregate futex functionality from pi-futex functionality with a new preprocessor symbol.
1 parent 413ed24 commit 3d7728e

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

Diff for: src/shims/lock.c

+26-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ _dispatch_thread_switch(dispatch_lock value, dispatch_lock_options_t flags,
5656
#endif
5757
#endif
5858

59+
#if defined(__unix__)
60+
#if !HAVE_UL_UNFAIR_LOCK && !HAVE_FUTEX_PI
61+
DISPATCH_ALWAYS_INLINE
62+
static inline void
63+
_dispatch_thread_switch(dispatch_lock value, dispatch_lock_options_t flags,
64+
uint32_t timeout)
65+
{
66+
(void)value;
67+
(void)flags;
68+
(void)timeout;
69+
}
70+
#endif
71+
#endif
72+
5973
#pragma mark - semaphores
6074

6175
#if USE_MACH_SEM
@@ -395,8 +409,10 @@ _dispatch_unfair_lock_wake(uint32_t *uaddr, uint32_t flags)
395409
#include <sys/time.h>
396410
#ifdef __ANDROID__
397411
#include <sys/syscall.h>
398-
#else
412+
#elif __linux__
399413
#include <syscall.h>
414+
#else
415+
#include <sys/futex.h>
400416
#endif /* __ANDROID__ */
401417

402418
DISPATCH_ALWAYS_INLINE
@@ -405,7 +421,12 @@ _dispatch_futex(uint32_t *uaddr, int op, uint32_t val,
405421
const struct timespec *timeout, uint32_t *uaddr2, uint32_t val3,
406422
int opflags)
407423
{
424+
#if __linux__
408425
return (int)syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3);
426+
#else
427+
(void)val3;
428+
return futex(uaddr, op | opflags, (int)val, timeout, uaddr2);
429+
#endif
409430
}
410431

411432
// returns 0, ETIMEDOUT, EFAULT, EINTR, EWOULDBLOCK
@@ -455,6 +476,7 @@ _dispatch_futex_wake(uint32_t *uaddr, int wake, int opflags)
455476
DISPATCH_INTERNAL_CRASH(errno, "_dlock_wake() failed");
456477
}
457478

479+
#if HAVE_FUTEX_PI
458480
static void
459481
_dispatch_futex_lock_pi(uint32_t *uaddr, struct timespec *timeout, int detect,
460482
int opflags)
@@ -472,6 +494,7 @@ _dispatch_futex_unlock_pi(uint32_t *uaddr, int opflags)
472494
if (rc == 0) return;
473495
DISPATCH_CLIENT_CRASH(errno, "futex_unlock_pi() failed");
474496
}
497+
#endif
475498

476499
#endif
477500
#pragma mark - wait for address
@@ -606,7 +629,7 @@ _dispatch_unfair_lock_lock_slow(dispatch_unfair_lock_t dul,
606629
}
607630
}
608631
}
609-
#elif HAVE_FUTEX
632+
#elif HAVE_FUTEX_PI
610633
void
611634
_dispatch_unfair_lock_lock_slow(dispatch_unfair_lock_t dul,
612635
dispatch_lock_options_t flags)
@@ -643,7 +666,7 @@ _dispatch_unfair_lock_unlock_slow(dispatch_unfair_lock_t dul, dispatch_lock cur)
643666
if (_dispatch_lock_has_waiters(cur)) {
644667
_dispatch_unfair_lock_wake(&dul->dul_lock, 0);
645668
}
646-
#elif HAVE_FUTEX
669+
#elif HAVE_FUTEX_PI
647670
// futex_unlock_pi() handles both OWNER_DIED which we abuse & WAITERS
648671
_dispatch_futex_unlock_pi(&dul->dul_lock, FUTEX_PRIVATE_FLAG);
649672
#else

Diff for: src/shims/lock.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ _dispatch_lock_owner(dispatch_lock lock_value)
100100
return lock_value & DLOCK_OWNER_MASK;
101101
}
102102

103+
#elif defined(__OpenBSD__)
104+
105+
typedef uint32_t dispatch_tid;
106+
typedef uint32_t dispatch_lock;
107+
108+
#define DLOCK_OWNER_NULL ((dispatch_tid)0)
109+
#define DLOCK_OWNER_MASK ((dispatch_lock)0xfffffffc)
110+
#define DLOCK_WAITERS_BIT ((dispatch_lock)0x00000001)
111+
#define DLOCK_FAILED_TRYLOCK_BIT ((dispatch_lock)0x00000002)
112+
113+
#define _dispatch_tid_self() ((dispatch_tid)(_dispatch_get_tsd_base()->tid))
114+
115+
DISPATCH_ALWAYS_INLINE
116+
static inline dispatch_tid
117+
_dispatch_lock_owner(dispatch_lock lock_value)
118+
{
119+
return lock_value & DLOCK_OWNER_MASK;
120+
}
121+
103122
#else
104123
# error define _dispatch_lock encoding scheme for your platform here
105124
#endif
@@ -167,10 +186,15 @@ _dispatch_lock_has_failed_trylock(dispatch_lock lock_value)
167186
#endif
168187

169188
#ifndef HAVE_FUTEX
170-
#ifdef __linux__
189+
#if defined(__linux__)
190+
#define HAVE_FUTEX 1
191+
#define HAVE_FUTEX_PI 1
192+
#elif defined(__OpenBSD__)
171193
#define HAVE_FUTEX 1
194+
#define HAVE_FUTEX_PI 0
172195
#else
173196
#define HAVE_FUTEX 0
197+
#define HAVE_FUTEX_PI 0
174198
#endif
175199
#endif // HAVE_FUTEX
176200

0 commit comments

Comments
 (0)