Skip to content

Instantly share code, notes, and snippets.

@kennyyu
Last active October 13, 2023 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennyyu/f49c3f5d09930d30f98c5f53fec520c6 to your computer and use it in GitHub Desktop.
Save kennyyu/f49c3f5d09930d30f98c5f53fec520c6 to your computer and use it in GitHub Desktop.
folly coro changes proof of concept
diff --git a/folly/folly/experimental/coro/Task.h b/folly/folly/experimental/coro/Task.h
--- a/folly/folly/experimental/coro/Task.h
+++ b/folly/folly/experimental/coro/Task.h
@@ -43,13 +43,37 @@
#include <folly/futures/Future.h>
#include <folly/io/async/Request.h>
#include <folly/lang/Assume.h>
+#include <folly/synchronization/SanitizeThread.h>
#include <folly/tracing/AsyncStack.h>
#if FOLLY_HAS_COROUTINES
+#ifdef FOLLY_SANITIZE_THREAD
+namespace __tsan {
+struct ThreadState;
+
+extern __thread char cur_thread_placeholder[];
+ThreadState* cur_thread2() {
+ return reinterpret_cast<ThreadState*>(&cur_thread_placeholder);
+}
+
+void ThreadStateCheckNoLocks(ThreadState* thr);
+} // namespace __tsan
+
+#endif
+
namespace folly {
namespace coro {
+void ensureNoLocks() {
+ if constexpr (kIsSanitizeThread) {
+ auto thr = __tsan::cur_thread2();
+ __tsan::ThreadStateCheckNoLocks(thr);
+ }
+}
+
template <typename T = void>
class Task;
@@ -66,6 +90,8 @@
template <typename Promise>
FOLLY_CORO_AWAIT_SUSPEND_NONTRIVIAL_ATTRIBUTES coroutine_handle<>
await_suspend(coroutine_handle<Promise> coro) noexcept {
+ ensureNoLocks();
+
auto& promise = coro.promise();
// If the continuation has been exchanged, then we expect that the
// exchanger will handle the lifetime of the async stack. See
@@ -510,6 +536,8 @@
}
}
+ ensureNoLocks();
+
auto& calleeFrame = promise.getAsyncFrame();
calleeFrame.setReturnAddress();
@@ -566,6 +594,8 @@
DCHECK(!promise.continuation_);
DCHECK(promise.executor_);
+ ensureNoLocks();
+
promise.continuation_ = continuation;
auto& calleeFrame = promise.getAsyncFrame();
@@ -771,6 +801,9 @@
FOLLY_NOINLINE auto await_suspend(
coroutine_handle<Promise> continuation) noexcept {
DCHECK(coro_);
+
+ ensureNoLocks();
+
auto& promise = coro_.promise();
promise.continuation_ = continuation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment