Skip to content

Commit 93c9f74

Browse files
committed
add futex semaphore support derived from hutchinson port
1 parent 8e78560 commit 93c9f74

File tree

5 files changed

+264
-5
lines changed

5 files changed

+264
-5
lines changed

configure.ac

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ AC_CHECK_FUNC([sem_init],
276276
[have_sem_init=true], [have_sem_init=false]
277277
)
278278

279+
AC_CHECK_HEADER(linux/futex.h, [have_futex=true], [have_futex=false])
280+
279281
#
280282
# We support both Mach semaphores and POSIX semaphores; if the former are
281283
# available, prefer them.
@@ -284,6 +286,9 @@ AC_MSG_CHECKING([what semaphore type to use]);
284286
AS_IF([test "x$have_mach" = "xtrue"],
285287
[AC_DEFINE(USE_MACH_SEM, 1, [Define to use Mach semaphores])
286288
AC_MSG_RESULT([Mach semaphores])],
289+
[test "x$have_futex" = "xtrue"],
290+
[AC_DEFINE(USE_FUTEX_SEM, 1, [Define to use Futex semaphores])
291+
AC_MSG_RESULT([Futex semaphores])],
287292
[test "x$have_sem_init" = "xtrue"],
288293
[AC_DEFINE(USE_POSIX_SEM, 1, [Define to use POSIX semaphores])
289294
AC_MSG_RESULT([POSIX semaphores])],

src/internal.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
230230
#include <search.h>
231231
#if USE_POSIX_SEM
232232
#include <semaphore.h>
233+
#elif USE_FUTEX_SEM
234+
#include <sys/syscall.h>
235+
#include <linux/futex.h>
233236
#endif
237+
234238
#include <signal.h>
235239
#include <stdarg.h>
236240
#include <stdbool.h>
@@ -242,6 +246,34 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
242246
#include <unistd.h>
243247
#endif
244248

249+
#if USE_POSIX_SEM
250+
#include <semaphore.h>
251+
#elif USE_FUTEX_SEM
252+
#include <sys/syscall.h>
253+
#include <linux/futex.h>
254+
#endif
255+
256+
#if defined(__APPLE__)
257+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
258+
#define DISPATCH_LITTLE_ENDIAN
259+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
260+
#define DISPATCH_BIG_ENDIAN
261+
#endif
262+
263+
#elif defined(__linux__)
264+
#include <endian.h>
265+
#if __BYTE_ORDER == __LITTLE_ENDIAN
266+
#define DISPATCH_LITTLE_ENDIAN
267+
#elif __BYTE_ORDER == __BIG_ENDIAN
268+
#define DISPATCH_BIG_ENDIAN
269+
#endif
270+
271+
#endif /* defined(__APPLE__) */
272+
273+
#if !(defined(DISPATCH_BIG_ENDIAN) || defined(DISPATCH_LITTLE_ENDIAN))
274+
#error "Unable to determine platform endianness."
275+
#endif
276+
245277
#ifndef __has_builtin
246278
#define __has_builtin(x) 0
247279
#endif

src/queue.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ _dispatch_root_queue_init_pthread_pool(dispatch_root_queue_context_t qc,
879879
/* XXXRW: POSIX semaphores don't support LIFO? */
880880
int ret = sem_init(&(pqc->dpq_thread_mediator.dsema_sem), 0, 0);
881881
(void)dispatch_assume_zero(ret);
882+
#elif USE_FUTEX_SEM
883+
int ret = _dispatch_futex_init(&(pqc->dpq_thread_mediator.dsema_futex));
884+
(void)dispatch_assume_zero(ret);
882885
#endif
883886
}
884887
#endif // DISPATCH_USE_PTHREAD_POOL

0 commit comments

Comments
 (0)