Skip to content

[Concurrency] Use a stable TSD on Darwin to store the current Task #37136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2021
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
19 changes: 16 additions & 3 deletions stdlib/public/Concurrency/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "swift/Runtime/Concurrency.h"

#include "../CompatibilityOverride/CompatibilityOverride.h"
#include "../runtime/ThreadLocalStorage.h"
#include "swift/Runtime/Atomic.h"
#include "swift/Runtime/Casting.h"
#include "swift/Runtime/Once.h"
Expand Down Expand Up @@ -180,6 +181,17 @@ class ExecutorTrackingInfo {
}
};

#ifdef SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
class ActiveTask {
public:
static void set(AsyncTask *task) {
SWIFT_THREAD_SETSPECIFIC(SWIFT_CONCURRENCY_TASK_KEY, task);
}
static AsyncTask *get() {
return (AsyncTask *)SWIFT_THREAD_GETSPECIFIC(SWIFT_CONCURRENCY_TASK_KEY);
}
};
#else
class ActiveTask {
/// A thread-local variable pointing to the active tracking
/// information about the current thread, if any.
Expand All @@ -191,12 +203,13 @@ class ActiveTask {
};

/// Define the thread-locals.
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
Pointer<ExecutorTrackingInfo>,
ExecutorTrackingInfo::ActiveInfoInThread);
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
Pointer<AsyncTask>,
ActiveTask::Value);
#endif
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
Pointer<ExecutorTrackingInfo>,
ExecutorTrackingInfo::ActiveInfoInThread);

} // end anonymous namespace

Expand Down
4 changes: 4 additions & 0 deletions stdlib/public/runtime/ThreadLocalStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ extern "C" int pthread_key_init_np(int key, void (*destructor)(void *));
# ifndef __PTK_FRAMEWORK_SWIFT_KEY2
# define __PTK_FRAMEWORK_SWIFT_KEY2 102
# endif
# ifndef __PTK_FRAMEWORK_SWIFT_KEY3
# define __PTK_FRAMEWORK_SWIFT_KEY3 103
# endif


# define SWIFT_RUNTIME_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY0
# define SWIFT_STDLIB_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY1
# define SWIFT_COMPATIBILITY_50_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY2
# define SWIFT_CONCURRENCY_TASK_KEY __PTK_FRAMEWORK_SWIFT_KEY3

#endif

Expand Down