Skip to content
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
11 changes: 11 additions & 0 deletions include/silk/util/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ static inline uint64_t getTimeNanoseconds() noexcept
return static_cast<uint64_t>(ts.tv_sec) * 1'000'000'000 + static_cast<uint64_t>(ts.tv_nsec);
}

/** 64-bit integer hash (Murmur3 fmix64 finalizer). */
static constexpr uint64_t intHash(uint64_t key) noexcept
{
key ^= key >> 33;
key *= 0xff51afd7ed558ccdULL;
key ^= key >> 33;
key *= 0xc4ceb9fe1a85ec53ULL;
key ^= key >> 33;
return key;
}

/**
* Initialize librseq. Must be called before any rseq critical sections run.
* Idempotent -- safe to call multiple times.
Expand Down
7 changes: 4 additions & 3 deletions src/fibers/fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <cerrno>
#include <cstdint>
#include <format>
#include <functional>
#include <memory>
#include <mutex>
#include <random>
Expand Down Expand Up @@ -58,6 +57,8 @@ static constexpr uint64_t CQE_TAG_CANCEL = 0;
static constexpr uint64_t CQE_TAG_TIMEOUT = 1;
static constexpr uint64_t CQE_TAG_DOORBELL = 2;

static_assert((WAITER_TABLE_SIZE & (WAITER_TABLE_SIZE - 1)) == 0);

// clang-format off
#define FIBER_SIMPLE_COUNTERS(x) \
x(FIBER_STARTED, "FiberStarted") \
Expand Down Expand Up @@ -1192,7 +1193,7 @@ void FiberScheduler::suspend(SuspendCallback * callback, void * context) noexcep

void FiberScheduler::enqueueWaiter(uint64_t key, Fiber * fiber) noexcept
{
uint64_t index = std::hash<uint64_t>{}(key) % WAITER_TABLE_SIZE;
uint64_t index = intHash(key) & (WAITER_TABLE_SIZE - 1);
scheduler->waiterTable[index].push(fiber);

// seq_cst fence pairs with the one in releaseWaiters to prevent the
Expand All @@ -1207,7 +1208,7 @@ void FiberScheduler::releaseWaiters(uint64_t key) noexcept
// seq_cst fence pairs with the one in enqueueWaiter.
std::atomic_thread_fence(std::memory_order_seq_cst);

uint64_t index = std::hash<uint64_t>{}(key) % WAITER_TABLE_SIZE;
uint64_t index = intHash(key) & (WAITER_TABLE_SIZE - 1);
Fiber * fiber = scheduler->waiterTable[index].popAll();
while (fiber)
{
Expand Down
Loading