Skip to content

Commit cdc2e29

Browse files
author
git apple-llvm automerger
committed
Merge commit 'a2223b09b10a' from llvm.org/main into apple/main
2 parents 34edefe + a2223b0 commit cdc2e29

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

mlir/lib/ExecutionEngine/AsyncRuntime.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,14 @@ struct AsyncToken : public RefCounted {
136136
// asynchronously executed task. If the caller immediately will drop its
137137
// reference we must ensure that the token will be alive until the
138138
// asynchronous operation is completed.
139-
AsyncToken(AsyncRuntime *runtime) : RefCounted(runtime, /*count=*/2) {}
139+
AsyncToken(AsyncRuntime *runtime)
140+
: RefCounted(runtime, /*count=*/2), ready(false) {}
140141

141-
// Internal state below guarded by a mutex.
142+
std::atomic<bool> ready;
143+
144+
// Pending awaiters are guarded by a mutex.
142145
std::mutex mu;
143146
std::condition_variable cv;
144-
145-
bool ready = false;
146147
std::vector<std::function<void()>> awaiters;
147148
};
148149

@@ -152,17 +153,17 @@ struct AsyncToken : public RefCounted {
152153
struct AsyncValue : public RefCounted {
153154
// AsyncValue similar to an AsyncToken created with a reference count of 2.
154155
AsyncValue(AsyncRuntime *runtime, int32_t size)
155-
: RefCounted(runtime, /*count=*/2), storage(size) {}
156-
157-
// Internal state below guarded by a mutex.
158-
std::mutex mu;
159-
std::condition_variable cv;
156+
: RefCounted(runtime, /*count=*/2), ready(false), storage(size) {}
160157

161-
bool ready = false;
162-
std::vector<std::function<void()>> awaiters;
158+
std::atomic<bool> ready;
163159

164160
// Use vector of bytes to store async value payload.
165161
std::vector<int8_t> storage;
162+
163+
// Pending awaiters are guarded by a mutex.
164+
std::mutex mu;
165+
std::condition_variable cv;
166+
std::vector<std::function<void()>> awaiters;
166167
};
167168

168169
// Async group provides a mechanism to group together multiple async tokens or
@@ -175,10 +176,9 @@ struct AsyncGroup : public RefCounted {
175176
std::atomic<int> pendingTokens;
176177
std::atomic<int> rank;
177178

178-
// Internal state below guarded by a mutex.
179+
// Pending awaiters are guarded by a mutex.
179180
std::mutex mu;
180181
std::condition_variable cv;
181-
182182
std::vector<std::function<void()>> awaiters;
183183
};
184184

@@ -291,13 +291,13 @@ extern "C" void mlirAsyncRuntimeEmplaceValue(AsyncValue *value) {
291291
extern "C" void mlirAsyncRuntimeAwaitToken(AsyncToken *token) {
292292
std::unique_lock<std::mutex> lock(token->mu);
293293
if (!token->ready)
294-
token->cv.wait(lock, [token] { return token->ready; });
294+
token->cv.wait(lock, [token] { return token->ready.load(); });
295295
}
296296

297297
extern "C" void mlirAsyncRuntimeAwaitValue(AsyncValue *value) {
298298
std::unique_lock<std::mutex> lock(value->mu);
299299
if (!value->ready)
300-
value->cv.wait(lock, [value] { return value->ready; });
300+
value->cv.wait(lock, [value] { return value->ready.load(); });
301301
}
302302

303303
extern "C" void mlirAsyncRuntimeAwaitAllInGroup(AsyncGroup *group) {
@@ -319,34 +319,37 @@ extern "C" void mlirAsyncRuntimeExecute(CoroHandle handle, CoroResume resume) {
319319
extern "C" void mlirAsyncRuntimeAwaitTokenAndExecute(AsyncToken *token,
320320
CoroHandle handle,
321321
CoroResume resume) {
322-
std::unique_lock<std::mutex> lock(token->mu);
323322
auto execute = [handle, resume]() { (*resume)(handle); };
324-
if (token->ready)
323+
if (token->ready) {
325324
execute();
326-
else
325+
} else {
326+
std::unique_lock<std::mutex> lock(token->mu);
327327
token->awaiters.push_back([execute]() { execute(); });
328+
}
328329
}
329330

330331
extern "C" void mlirAsyncRuntimeAwaitValueAndExecute(AsyncValue *value,
331332
CoroHandle handle,
332333
CoroResume resume) {
333-
std::unique_lock<std::mutex> lock(value->mu);
334334
auto execute = [handle, resume]() { (*resume)(handle); };
335-
if (value->ready)
335+
if (value->ready) {
336336
execute();
337-
else
337+
} else {
338+
std::unique_lock<std::mutex> lock(value->mu);
338339
value->awaiters.push_back([execute]() { execute(); });
340+
}
339341
}
340342

341343
extern "C" void mlirAsyncRuntimeAwaitAllInGroupAndExecute(AsyncGroup *group,
342344
CoroHandle handle,
343345
CoroResume resume) {
344-
std::unique_lock<std::mutex> lock(group->mu);
345346
auto execute = [handle, resume]() { (*resume)(handle); };
346-
if (group->pendingTokens == 0)
347+
if (group->pendingTokens == 0) {
347348
execute();
348-
else
349+
} else {
350+
std::unique_lock<std::mutex> lock(group->mu);
349351
group->awaiters.push_back([execute]() { execute(); });
352+
}
350353
}
351354

352355
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)