Skip to content

Commit ed342a5

Browse files
committed
More fix
1 parent 430c425 commit ed342a5

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/core/lib/promise/activity.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ namespace grpc_core {
2727
///////////////////////////////////////////////////////////////////////////////
2828
// GLOBALS
2929

30-
thread_local Activity* Activity::g_current_activity_{nullptr};
30+
Activity** Activity::GetCurrentActivity() {
31+
static thread_local Activity* current_activity = nullptr;
32+
return &current_activity;
33+
}
3134

3235
namespace promise_detail {
3336

src/core/lib/promise/activity.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class Activity : public Orphanable {
179179
// locked
180180
// - back up that assertation with a runtime check in debug builds (it's
181181
// prohibitively expensive in non-debug builds)
182-
static Activity* current() { return g_current_activity_; }
182+
static Activity* current() { return *GetCurrentActivity(); }
183183

184184
// Produce an activity-owning Waker. The produced waker will keep the activity
185185
// alive until it's awoken or dropped.
@@ -196,17 +196,17 @@ class Activity : public Orphanable {
196196
protected:
197197
// Check if this activity is the current activity executing on the current
198198
// thread.
199-
bool is_current() const { return this == g_current_activity_; }
199+
bool is_current() const { return this == *GetCurrentActivity(); }
200200
// Check if there is an activity executing on the current thread.
201-
static bool have_current() { return g_current_activity_ != nullptr; }
201+
static bool have_current() { return *GetCurrentActivity() != nullptr; }
202202
// Set the current activity at construction, clean it up at destruction.
203203
class ScopedActivity {
204204
public:
205205
explicit ScopedActivity(Activity* activity)
206-
: prior_activity_(g_current_activity_) {
207-
g_current_activity_ = activity;
206+
: prior_activity_(*GetCurrentActivity()) {
207+
*GetCurrentActivity() = activity;
208208
}
209-
~ScopedActivity() { g_current_activity_ = prior_activity_; }
209+
~ScopedActivity() { *GetCurrentActivity() = prior_activity_; }
210210
ScopedActivity(const ScopedActivity&) = delete;
211211
ScopedActivity& operator=(const ScopedActivity&) = delete;
212212

@@ -217,6 +217,7 @@ class Activity : public Orphanable {
217217
private:
218218
// Set during RunLoop to the Activity that's executing.
219219
// Being set implies that mu_ is held.
220+
static Activity** GetCurrentActivity();
220221
static thread_local Activity* g_current_activity_;
221222
};
222223

0 commit comments

Comments
 (0)