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

Drop workaround for glibc<2.25 #211

Draft
wants to merge 2 commits into
base: branch-0.38
Choose a base branch
from
Draft
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
61 changes: 14 additions & 47 deletions cpp/src/utils/callback_notifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,35 @@
#include <atomic>
#include <features.h>

#ifdef __GLIBC__
#include <gnu/libc-version.h>
#include <string>
#endif

#include <ucxx/log.h>
#include <ucxx/utils/callback_notifier.h>

namespace ucxx {
namespace utils {

#ifdef __GLIBC__
static const bool _useSpinlock = []() {
auto const libcVersion = std::string{gnu_get_libc_version()};
auto const dot = libcVersion.find(".");
if (dot == std::string::npos) {
return false;
} else {
// See https://sourceware.org/bugzilla/show_bug.cgi?id=13165
auto const glibcMajor = std::stoi(libcVersion.substr(0, dot).data());
auto const glibcMinor = std::stoi(libcVersion.substr(dot + 1).data());
auto const use = glibcMajor < 2 || (glibcMajor == 2 && glibcMinor < 25);
ucxx_debug("glibc version %s detected, spinlock use is %d", libcVersion.c_str(), use);
return use;
}
}();
#else
static constexpr bool _useSpinlock = false;
#endif

void CallbackNotifier::set()
{
if (_useSpinlock) {
_flag.store(true, std::memory_order_release);
} else {
{
std::lock_guard lock(_mutex);
// This can be relaxed because the mutex is providing
// ordering.
_flag.store(true, std::memory_order_relaxed);
}
_conditionVariable.notify_one();
{
std::lock_guard lock(_mutex);
// This can be relaxed because the mutex is providing
// ordering.
_flag.store(true, std::memory_order_relaxed);
}
_conditionVariable.notify_one();
}

bool CallbackNotifier::wait(uint64_t period)
{
if (_useSpinlock) {
while (!_flag.load(std::memory_order_acquire)) {}
std::unique_lock lock(_mutex);
// Likewise here, the mutex provides ordering.
if (period > 0) {
return _conditionVariable.wait_for(lock,
std::chrono::duration<uint64_t, std::nano>(period),
[this]() { return _flag.load(std::memory_order_relaxed); });
} else {
std::unique_lock lock(_mutex);
// Likewise here, the mutex provides ordering.
if (period > 0) {
return _conditionVariable.wait_for(
lock, std::chrono::duration<uint64_t, std::nano>(period), [this]() {
return _flag.load(std::memory_order_relaxed);
});
} else {
_conditionVariable.wait(lock, [this]() { return _flag.load(std::memory_order_relaxed); });
}
_conditionVariable.wait(lock, [this]() { return _flag.load(std::memory_order_relaxed); });
return true;
}
return true;
}

} // namespace utils
Expand Down
Loading