Skip to content

Commit a50c179

Browse files
committed
combined futex based semaphore support derived from hutchinson port
1 parent 8e78560 commit a50c179

File tree

5 files changed

+254
-7
lines changed

5 files changed

+254
-7
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,14 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
228228
#endif
229229
#include <limits.h>
230230
#include <search.h>
231+
#if USE_FUTEX_SEM
232+
#include <sys/syscall.h>
233+
#include <linux/futex.h>
234+
#endif
231235
#if USE_POSIX_SEM
232236
#include <semaphore.h>
233237
#endif
238+
234239
#include <signal.h>
235240
#include <stdarg.h>
236241
#include <stdbool.h>
@@ -242,6 +247,24 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
242247
#include <unistd.h>
243248
#endif
244249

250+
#if defined(__APPLE__)
251+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
252+
#define DISPATCH_LITTLE_ENDIAN
253+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
254+
#define DISPATCH_BIG_ENDIAN
255+
#endif
256+
#else
257+
#include <endian.h>
258+
#if __BYTE_ORDER == __LITTLE_ENDIAN
259+
#define DISPATCH_LITTLE_ENDIAN
260+
#elif __BYTE_ORDER == __BIG_ENDIAN
261+
#define DISPATCH_BIG_ENDIAN
262+
#else
263+
#error "please define DISPATCH_LITTLE_ENDIAN or DISPATCH_BIG_ENDIAN for your architecture / platform"
264+
#endif
265+
#endif /* defined(__APPLE__) */
266+
267+
245268
#ifndef __has_builtin
246269
#define __has_builtin(x) 0
247270
#endif

src/queue.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ _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+
pqc->dpq_thread_mediator.dsema_futex = DISPATCH_FUTEX_INIT;
882884
#endif
883885
}
884886
#endif // DISPATCH_USE_PTHREAD_POOL

0 commit comments

Comments
 (0)