-
Notifications
You must be signed in to change notification settings - Fork 17
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
Make futex and condition variable waiting interfaces more similar. #46
Open
VoxSciurorum
wants to merge
1
commit into
dev
Choose a base branch
from
jfc/futex
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−150
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ set(CHEETAH_SOURCES | |
personality.c | ||
sched_stats.c | ||
scheduler.c | ||
condvar.c | ||
) | ||
|
||
set(CHEETAH_ABI_SOURCE | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
#include <err.h> | ||
#include <stdatomic.h> | ||
|
||
#include "mutex.h" | ||
|
||
#if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ | ||
#define HAVE_ERRC 1 | ||
#endif | ||
|
||
#if USE_FUTEX | ||
|
||
#include <stdbool.h> | ||
#include <limits.h> // INT_MAX | ||
|
||
#ifdef __linux__ | ||
#include <errno.h> | ||
#include <linux/futex.h> | ||
#include <sys/syscall.h> | ||
#include <unistd.h> | ||
#define WAIT FUTEX_WAIT_PRIVATE | ||
#define WAKE FUTEX_WAKE_PRIVATE | ||
#endif | ||
|
||
#ifdef __FreeBSD__ | ||
#include <sys/types.h> | ||
#include <sys/umtx.h> | ||
#define WAIT UMTX_OP_WAIT | ||
#define WAKE UMTX_OP_WAKE_PRIVATE | ||
#endif | ||
|
||
#ifdef __OpenBSD__ | ||
// This is not tested. | ||
#include <sys/time.h> | ||
#include <sys/futex.h> | ||
#define WAIT FUTEX_WAIT | ||
#define WAKE FUTEX_WAKE | ||
#endif | ||
|
||
// Convenience wrapper for futex syscall. | ||
// In this context only success (true) or failure needs to be returned. | ||
// Linux syscall() returns long. Here the values fit in an int. | ||
// OpenBSD syscall() returns int. | ||
// BSD _umtx_op returns int. | ||
static inline bool futex(futex_t *obj, int futex_op, futex_t val) { | ||
#if defined __linux__ | ||
return syscall(SYS_futex, obj, futex_op, val, NULL, NULL, 0) >= 0; | ||
#elif defined __FreeBSD__ | ||
return _umtx_op(obj, futex_op, 0, NULL, NULL) >= 0; | ||
#elif defined __OpenBSD__ | ||
// This is not tested. | ||
return futex(obj, futex_op, 0, NULL, NULL) >= 0; | ||
#else | ||
// TODO: Private interface __ulock_wait on Mac OS? | ||
// TODO: C++20 std::atomic<>::wait, notify_one, notify_all | ||
#error "no futex implementation" | ||
return false; | ||
#endif | ||
} | ||
|
||
// Wait for the object to be unequal to the value. | ||
// Acquire here pairs with release in cond_post. | ||
void cond_wait(futex_t *obj, futex_val_t val) { | ||
while (atomic_load_explicit(obj, memory_order_acquire) == val) { | ||
if (futex(obj, WAIT, val)) { | ||
// Formally the futex operation does not include a fence. | ||
atomic_thread_fence(memory_order_acquire); | ||
break; | ||
} | ||
if (errno != EAGAIN) | ||
err(EXIT_FAILURE, "futex(FUTEX_WAIT)"); | ||
} | ||
} | ||
|
||
// Set the futex pointed to by `obj` to `val`, and wake up one | ||
// thread waiting on that futex. | ||
void cond_post(futex_t *obj, futex_val_t val) { | ||
atomic_store_explicit(obj, val, memory_order_release); | ||
if (!futex(obj, WAKE, 1)) | ||
err(EXIT_FAILURE, "futex(FUTEX_WAKE)"); | ||
} | ||
|
||
// Set the futex pointed to by `obj` to `val`, and wake up all | ||
// threads waiting on that futex. | ||
void cond_broadcast(futex_t *obj, futex_val_t val) { | ||
atomic_store_explicit(obj, val, memory_order_release); | ||
if (!futex(obj, WAKE, INT_MAX)) | ||
err(EXIT_FAILURE, "futex(FUTEX_WAKE)"); | ||
} | ||
|
||
void cond_wake_some(futex_t *obj, int count) { | ||
if (!futex(obj, WAKE, count)) | ||
err(EXIT_FAILURE, "futex(FUTEX_WAKE)"); | ||
} | ||
|
||
#else // begin pthread implementation | ||
|
||
#include <pthread.h> | ||
|
||
#if HAVE_ERRC | ||
#define ERRCHK(MSG) \ | ||
if (__builtin_expect(error, 0)) errc(EXIT_FAILURE, error, MSG) | ||
#else | ||
#define ERRCHK(MSG) \ | ||
if (__builtin_expect(error, 0)) errx(EXIT_FAILURE, MSG " returned %d", error) | ||
#endif | ||
|
||
void cond_wait(futex_t *obj, futex_val_t val, | ||
pthread_cond_t *cond, pthread_mutex_t *mutex) { | ||
int error; | ||
// No fast path check. This is only called if *obj == val. | ||
error = pthread_mutex_lock(mutex); | ||
ERRCHK("pthread_mutex_lock"); | ||
while (atomic_load_explicit(obj, memory_order_acquire) == val) { | ||
error = pthread_cond_wait(cond, mutex); | ||
ERRCHK("pthread_cond_wait"); | ||
} | ||
error = pthread_mutex_unlock(mutex); | ||
ERRCHK("pthread_mutex_unlock"); | ||
} | ||
|
||
void cond_post(futex_t *obj, futex_val_t val, | ||
pthread_cond_t *cond, pthread_mutex_t *mutex) { | ||
int error; | ||
|
||
error = pthread_mutex_lock(mutex); | ||
ERRCHK("pthread_mutex_lock"); | ||
|
||
atomic_store_explicit(obj, val, memory_order_release); | ||
error = pthread_cond_signal(cond); | ||
ERRCHK("pthread_cond_signal"); | ||
error = pthread_mutex_unlock(mutex); | ||
ERRCHK("pthread_mutex_unlock"); | ||
} | ||
|
||
void cond_broadcast(futex_t *obj, futex_val_t val, | ||
pthread_cond_t *cond, pthread_mutex_t *mutex) { | ||
int error; | ||
|
||
error = pthread_mutex_lock(mutex); | ||
ERRCHK("pthread_mutex_lock"); | ||
atomic_store_explicit(obj, val, memory_order_release); | ||
error = pthread_cond_broadcast(cond); | ||
ERRCHK("pthread_mutex_broadcast"); | ||
error = pthread_mutex_unlock(mutex); | ||
ERRCHK("pthread_mutex_unlock"); | ||
} | ||
|
||
void cond_wake_some_locked(futex_t *obj, futex_val_t val, | ||
pthread_cond_t *cond, int count) { | ||
int error; | ||
|
||
atomic_store_explicit(obj, val, memory_order_release); | ||
while (count-- > 0) { | ||
error = pthread_cond_signal(cond); | ||
ERRCHK("pthread_cond_signal"); | ||
} | ||
} | ||
|
||
#endif // USE_FUTEX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add a condvar.h?