Skip to content

Commit 773987b

Browse files
authored
Merge pull request #3048 from cloudflare/kenton/event-loop-local
Pull in and use kj::EventLoopLocal.
2 parents e789b3a + ade8460 commit 773987b

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

build/deps/gen/dep_capnp_cpp.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
load("@//:build/http.bzl", "http_archive")
44

5-
URL = "https://github.com/capnproto/capnproto/tarball/6e071e34d88a8fc489638535899cd9d02e55bf76"
6-
STRIP_PREFIX = "capnproto-capnproto-6e071e3/c++"
7-
SHA256 = "78bad43b723d3b5a21e50424ad20b0d045f5d963c59d677cd5035ef5c68aabaa"
5+
URL = "https://github.com/capnproto/capnproto/tarball/14132442b125d0383285e36809e467c6b6a759aa"
6+
STRIP_PREFIX = "capnproto-capnproto-1413244/c++"
7+
SHA256 = "d1e1ff677a53aaf840a8ae624af4e2fed2b08e324ad82b0efd821926d16d6ce6"
88
TYPE = "tgz"
9-
COMMIT = "6e071e34d88a8fc489638535899cd9d02e55bf76"
9+
COMMIT = "14132442b125d0383285e36809e467c6b6a759aa"
1010

1111
def dep_capnp_cpp():
1212
http_archive(

src/workerd/io/io-context.c++

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
namespace workerd {
1919

2020
static thread_local IoContext* threadLocalRequest = nullptr;
21-
static thread_local void* threadId = nullptr;
21+
22+
static const kj::EventLoopLocal<int> threadId;
2223

2324
static void* getThreadId() {
24-
if (threadId == nullptr) threadId = new int;
25-
return threadId;
25+
return threadId.get();
2626
}
2727

2828
class IoContext::TimeoutManagerImpl final: public TimeoutManager {

src/workerd/io/worker.c++

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private:
371371
kj::Maybe<AsyncWaiter&> next;
372372
kj::Maybe<AsyncWaiter&>* prev;
373373

374-
static thread_local AsyncWaiter* threadCurrentWaiter;
374+
static const kj::EventLoopLocal<AsyncWaiter*> threadCurrentWaiter;
375375

376376
friend class Worker::Isolate;
377377
friend class Worker::AsyncLock;
@@ -2228,7 +2228,7 @@ void Worker::Lock::validateHandlers(ValidationErrorReporter& errorReporter) {
22282228
// =======================================================================================
22292229
// AsyncLock implementation
22302230

2231-
thread_local Worker::AsyncWaiter* Worker::AsyncWaiter::threadCurrentWaiter = nullptr;
2231+
const kj::EventLoopLocal<Worker::AsyncWaiter*> Worker::AsyncWaiter::threadCurrentWaiter;
22322232

22332233
Worker::Isolate::AsyncWaiterList::~AsyncWaiterList() noexcept {
22342234
// It should be impossible for this list to be non-empty since each member of the list holds a
@@ -2257,7 +2257,7 @@ kj::Promise<Worker::AsyncLock> Worker::Isolate::takeAsyncLockImpl(
22572257
}
22582258

22592259
for (uint threadWaitingDifferentLockCount = 0;; ++threadWaitingDifferentLockCount) {
2260-
AsyncWaiter* waiter = AsyncWaiter::threadCurrentWaiter;
2260+
AsyncWaiter* waiter = *AsyncWaiter::threadCurrentWaiter;
22612261

22622262
if (waiter == nullptr) {
22632263
// Thread is not currently waiting on a lock.
@@ -2327,7 +2327,7 @@ Worker::AsyncWaiter::AsyncWaiter(kj::Own<const Isolate> isolateParam)
23272327
*lock->tail = this;
23282328
lock->tail = &next;
23292329

2330-
threadCurrentWaiter = this;
2330+
*threadCurrentWaiter = this;
23312331

23322332
__atomic_add_fetch(&isolate->impl->lockAttemptGauge, 1, __ATOMIC_RELAXED);
23332333
}
@@ -2358,20 +2358,22 @@ Worker::AsyncWaiter::~AsyncWaiter() noexcept {
23582358
}
23592359
}
23602360

2361-
KJ_ASSERT(threadCurrentWaiter == this);
2362-
threadCurrentWaiter = nullptr;
2361+
auto& w = *threadCurrentWaiter;
2362+
KJ_ASSERT(w == this);
2363+
w = nullptr;
23632364
}
23642365

23652366
kj::Promise<void> Worker::AsyncLock::whenThreadIdle() {
2367+
AsyncWaiter*& currentWaiter = *AsyncWaiter::threadCurrentWaiter;
23662368
for (;;) {
2367-
if (auto waiter = AsyncWaiter::threadCurrentWaiter; waiter != nullptr) {
2368-
co_await waiter->releasePromise;
2369+
if (currentWaiter != nullptr) {
2370+
co_await currentWaiter->releasePromise;
23692371
continue;
23702372
}
23712373

23722374
co_await kj::yieldUntilQueueEmpty();
23732375

2374-
if (AsyncWaiter::threadCurrentWaiter == nullptr) {
2376+
if (currentWaiter == nullptr) {
23752377
co_return;
23762378
}
23772379
// Whoops, a new lock attempt appeared, loop.

src/workerd/util/wait-list.c++

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace workerd {
1111
namespace {
1212
// Optimization: If the same wait list is waited multiple times in the same thread, we want to
1313
// share the signal rather than send two cross-thread signals.
14-
thread_local CrossThreadWaitList::WaiterMap threadLocalWaiters;
14+
static const kj::EventLoopLocal<CrossThreadWaitList::WaiterMap> threadLocalWaiters;
1515

1616
void END_WAIT_LIST_CANCELER_STACK_START_CANCELEE_STACK() {}
1717
} // namespace
@@ -50,9 +50,9 @@ CrossThreadWaitList::Waiter::~Waiter() noexcept(false) {
5050
}
5151

5252
if (state->useThreadLocalOptimization) {
53-
auto& entry = KJ_ASSERT_NONNULL(threadLocalWaiters.findEntry(state.get()));
53+
auto& entry = KJ_ASSERT_NONNULL(threadLocalWaiters->findEntry(state.get()));
5454
KJ_ASSERT(entry.value == this);
55-
threadLocalWaiters.erase(entry);
55+
threadLocalWaiters->erase(entry);
5656
}
5757
}
5858

@@ -68,8 +68,8 @@ kj::Promise<void> CrossThreadWaitList::addWaiter() const {
6868
if (state->useThreadLocalOptimization) {
6969
kj::Own<Waiter> ownWaiter;
7070

71-
auto& waiter =
72-
threadLocalWaiters.findOrCreate(state.get(), [&]() -> decltype(threadLocalWaiters)::Entry {
71+
auto& waiter = threadLocalWaiters->findOrCreate(
72+
state.get(), [&]() -> CrossThreadWaitList::WaiterMap::Entry {
7373
auto paf = kj::newPromiseAndCrossThreadFulfiller<void>();
7474
ownWaiter = kj::refcounted<Waiter>(*state, kj::mv(paf.fulfiller));
7575
ownWaiter->forkedPromise = paf.promise.fork();

0 commit comments

Comments
 (0)