Skip to content
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

Implement waitable_atomic for FreeBSD #1607

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions src/mongo/platform/waitable_atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#ifdef __linux__
#include <linux/futex.h>
#include <sys/syscall.h>
#elif defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/umtx.h>
#elif defined(_WIN32)
#include <synchapi.h>
#endif
Expand Down Expand Up @@ -235,6 +238,52 @@ bool waitUntil(const void* uaddr,
return timeoutOverflow || errno != ETIMEDOUT;
}

#elif defined(__FreeBSD__)

void notifyOne(const void* uaddr) {
_umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAKE, 1, NULL, NULL);
}

void notifyMany(const void* uaddr, int nToWake) {
_umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAKE, nToWake, NULL, NULL);
}

void notifyAll(const void* uaddr) {
_umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAKE, INT_MAX, NULL, NULL);
}

bool waitUntil(const void* uaddr,
uint32_t old,
boost::optional<system_clock::time_point> deadline) {
struct timespec timeout;
bool timeoutOverflow = false;
if (deadline) {
int64_t micros = durationCount<Microseconds>(*deadline - system_clock::now());
if (micros <= 0) {
return false; // Synthesize a timeout.
}

if (micros > int64_t(std::numeric_limits<uint32_t>::max())) {
// 2**32 micros is a little over an hour. If this happens, we wait as long as we can,
// then return as-if a spurious wakeup happened, rather than a timeout. This will cause
// the caller to loop and we will compute a smaller time each pass, eventually reaching
// a representable timeout.
micros = std::numeric_limits<uint32_t>::max();
timeoutOverflow = true;
}

timeout.tv_sec = micros / 1000;
timeout.tv_nsec = (micros % 1000) * 1000;
}

if (_umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAIT, old, (void*)sizeof(struct timespec), &timeout) != -1)
return true;

// There isn't a good list of possible errors, so assuming that anything other than a timeout
// error is a possible spurious wakeup.
return timeoutOverflow || errno != ETIMEDOUT;
}

#else
#error "Need an implementation of waitUntil(), notifyOne(), notifyMany(), notifyAll() for this OS"
#endif
Expand Down