Skip to content

add futex semaphore support derived from hutchinson port #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ AC_CHECK_FUNC([sem_init],
[have_sem_init=true], [have_sem_init=false]
)

AC_CHECK_HEADER(linux/futex.h, [have_futex=true], [have_futex=false])

#
# We support both Mach semaphores and POSIX semaphores; if the former are
# available, prefer them.
Expand All @@ -284,6 +286,9 @@ AC_MSG_CHECKING([what semaphore type to use]);
AS_IF([test "x$have_mach" = "xtrue"],
[AC_DEFINE(USE_MACH_SEM, 1, [Define to use Mach semaphores])
AC_MSG_RESULT([Mach semaphores])],
[test "x$have_futex" = "xtrue"],
[AC_DEFINE(USE_FUTEX_SEM, 1, [Define to use Futex semaphores])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USE_FUTEX sounds better, Futexes are not semaphores, it's more like "[Define to use futex(7) when appropriate]"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that I can tell you'll want to use FUTEX for other things, so I wouldn't make this scoped to "semaphores" only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this particular case we want to be symmetric on the USE_xxxxx_SEM, so that should stay.
Will see whether we can be more generic here from which we derive USE_FUTEX_SEM.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough

AC_MSG_RESULT([Futex semaphores])],
[test "x$have_sem_init" = "xtrue"],
[AC_DEFINE(USE_POSIX_SEM, 1, [Define to use POSIX semaphores])
AC_MSG_RESULT([POSIX semaphores])],
Expand Down
23 changes: 23 additions & 0 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,14 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
#endif
#include <limits.h>
#include <search.h>
#if USE_FUTEX_SEM
#include <sys/syscall.h>
#include <linux/futex.h>
#endif
#if USE_POSIX_SEM
#include <semaphore.h>
#endif

#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
Expand All @@ -242,6 +247,24 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
#include <unistd.h>
#endif

#if defined(__APPLE__)
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define DISPATCH_LITTLE_ENDIAN
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define DISPATCH_BIG_ENDIAN
#endif
#else
#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define DISPATCH_LITTLE_ENDIAN
#elif __BYTE_ORDER == __BIG_ENDIAN
#define DISPATCH_BIG_ENDIAN
#else
#error "please define DISPATCH_LITTLE_ENDIAN or DISPATCH_BIG_ENDIAN for your architecture / platform"
#endif
#endif /* defined(__APPLE__) */


#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ _dispatch_root_queue_init_pthread_pool(dispatch_root_queue_context_t qc,
/* XXXRW: POSIX semaphores don't support LIFO? */
int ret = sem_init(&(pqc->dpq_thread_mediator.dsema_sem), 0, 0);
(void)dispatch_assume_zero(ret);
#elif USE_FUTEX_SEM
pqc->dpq_thread_mediator.dsema_futex = DISPATCH_FUTEX_INIT;
#endif
}
#endif // DISPATCH_USE_PTHREAD_POOL
Expand Down
Loading