Skip to content

Commit 7a5de47

Browse files
committed
runtime: change the naming of TLS related stuff to make it more general and not specific to exclusivity checking.
Just a refactoring, NFC
1 parent 2ea531c commit 7a5de47

File tree

2 files changed

+40
-49
lines changed

2 files changed

+40
-49
lines changed

stdlib/public/runtime/Exclusivity.cpp

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,8 @@ static_assert(sizeof(Access) <= sizeof(ValueBuffer) &&
157157
"Access doesn't fit in a value buffer!");
158158

159159
/// A set of accesses that we're tracking. Just a singly-linked list.
160-
/// TODO: rename this class to something like SwiftTLSContext, because it also
161-
/// contains fields not related to access tracking.
162160
class AccessSet {
163161
Access *Head = nullptr;
164-
165-
// Not related to access tracking: The "implicit" boolean parameter which is
166-
// passed to a dynamically replaceable function.
167-
// If true, the original function should be executed instead of the
168-
// replacement function.
169-
bool CallOriginalOfReplacedFunction = false;
170162
public:
171163
constexpr AccessSet() {}
172164

@@ -229,21 +221,18 @@ class AccessSet {
229221
}
230222
}
231223
#endif
224+
};
232225

233-
/// Called immediately before a replacement function calls its original
234-
/// function.
235-
void aboutToCallOriginalOfReplacedFunction() {
236-
CallOriginalOfReplacedFunction = true;
237-
}
226+
class SwiftTLSContext {
227+
public:
228+
/// The set of tracked accesses.
229+
AccessSet accessSet;
238230

239-
/// Checked in the prolog of a replaceable function. Returns true if the
240-
/// original function should be called instead of the replacement function.
241-
/// Also clears the CallOriginalOfReplacedFunction flag.
242-
bool shouldCallOriginalOfReplacedFunction() {
243-
bool callOrig = CallOriginalOfReplacedFunction;
244-
CallOriginalOfReplacedFunction = false;
245-
return callOrig;
246-
}
231+
// The "implicit" boolean parameter which is passed to a dynamically
232+
// replaceable function.
233+
// If true, the original function should be executed instead of the
234+
// replacement function.
235+
bool CallOriginalOfReplacedFunction = false;
247236
};
248237

249238
} // end anonymous namespace
@@ -254,40 +243,40 @@ class AccessSet {
254243
#if SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
255244
// Use the reserved TSD key if possible.
256245

257-
static AccessSet &getAccessSet() {
258-
AccessSet *set = static_cast<AccessSet*>(
259-
SWIFT_THREAD_GETSPECIFIC(SWIFT_EXCLUSIVITY_TLS_KEY));
260-
if (set)
261-
return *set;
246+
static SwiftTLSContext &getTLSContext() {
247+
SwiftTLSContext *ctx = static_cast<SwiftTLSContext*>(
248+
SWIFT_THREAD_GETSPECIFIC(SWIFT_RUNTIME_TLS_KEY));
249+
if (ctx)
250+
return *ctx;
262251

263252
static OnceToken_t setupToken;
264253
SWIFT_ONCE_F(setupToken, [](void *) {
265-
pthread_key_init_np(SWIFT_EXCLUSIVITY_TLS_KEY, [](void *pointer) {
266-
delete static_cast<AccessSet*>(pointer);
254+
pthread_key_init_np(SWIFT_RUNTIME_TLS_KEY, [](void *pointer) {
255+
delete static_cast<SwiftTLSContext*>(pointer);
267256
});
268257
}, nullptr);
269258

270-
set = new AccessSet();
271-
SWIFT_THREAD_SETSPECIFIC(SWIFT_EXCLUSIVITY_TLS_KEY, set);
272-
return *set;
259+
ctx = new SwiftTLSContext();
260+
SWIFT_THREAD_SETSPECIFIC(SWIFT_RUNTIME_TLS_KEY, ctx);
261+
return *ctx;
273262
}
274263

275264
#elif SWIFT_TLS_HAS_THREADLOCAL
276265
// Second choice is direct language support for thread-locals.
277266

278-
static LLVM_THREAD_LOCAL AccessSet ExclusivityAccessSet;
267+
static LLVM_THREAD_LOCAL SwiftTLSContext TLSContext;
279268

280-
static AccessSet &getAccessSet() {
281-
return ExclusivityAccessSet;
269+
static SwiftTLSContext &getTLSContext() {
270+
return TLSContext;
282271
}
283272

284273
#else
285274
// Use the platform thread-local data API.
286275

287-
static __swift_thread_key_t createAccessSetThreadKey() {
276+
static __swift_thread_key_t createSwiftThreadKey() {
288277
__swift_thread_key_t key;
289278
int result = SWIFT_THREAD_KEY_CREATE(&key, [](void *pointer) {
290-
delete static_cast<AccessSet*>(pointer);
279+
delete static_cast<SwiftTLSContext*>(pointer);
291280
});
292281

293282
if (result != 0) {
@@ -297,15 +286,15 @@ static __swift_thread_key_t createAccessSetThreadKey() {
297286
return key;
298287
}
299288

300-
static AccessSet &getAccessSet() {
301-
static __swift_thread_key_t key = createAccessSetThreadKey();
289+
static SwiftTLSContext &getTLSContext() {
290+
static __swift_thread_key_t key = createSwiftThreadKey();
302291

303-
AccessSet *set = static_cast<AccessSet*>(SWIFT_THREAD_GETSPECIFIC(key));
304-
if (!set) {
305-
set = new AccessSet();
306-
SWIFT_THREAD_SETSPECIFIC(key, set);
292+
SwiftTLSContext *ctx = static_cast<SwiftTLSContext*>(SWIFT_THREAD_GETSPECIFIC(key));
293+
if (!ctx) {
294+
ctx = new SwiftTLSContext();
295+
SWIFT_THREAD_SETSPECIFIC(key, ctx);
307296
}
308-
return *set;
297+
return *ctx;
309298
}
310299

311300
#endif
@@ -332,7 +321,7 @@ void swift::swift_beginAccess(void *pointer, ValueBuffer *buffer,
332321
if (!pc)
333322
pc = get_return_address();
334323

335-
if (!getAccessSet().insert(access, pc, pointer, flags))
324+
if (!getTLSContext().accessSet.insert(access, pc, pointer, flags))
336325
access->Pointer = nullptr;
337326
}
338327

@@ -347,22 +336,24 @@ void swift::swift_endAccess(ValueBuffer *buffer) {
347336
return;
348337
}
349338

350-
getAccessSet().remove(access);
339+
getTLSContext().accessSet.remove(access);
351340
}
352341

353342
char *swift::swift_getFunctionReplacement(char **ReplFnPtr, char *CurrFn) {
354343
char *ReplFn = *ReplFnPtr;
355344
if (ReplFn == CurrFn)
356345
return nullptr;
357-
if (getAccessSet().shouldCallOriginalOfReplacedFunction()) {
346+
SwiftTLSContext &ctx = getTLSContext();
347+
if (ctx.CallOriginalOfReplacedFunction) {
348+
ctx.CallOriginalOfReplacedFunction = false;
358349
return nullptr;
359350
}
360351
return ReplFn;
361352
}
362353

363354
char *swift::swift_getOrigOfReplaceable(char **OrigFnPtr) {
364355
char *OrigFn = *OrigFnPtr;
365-
getAccessSet().aboutToCallOriginalOfReplacedFunction();
356+
getTLSContext().CallOriginalOfReplacedFunction = true;
366357
return OrigFn;
367358
}
368359

@@ -372,7 +363,7 @@ char *swift::swift_getOrigOfReplaceable(char **OrigFnPtr) {
372363
//
373364
// This is only intended to be used in the debugger.
374365
void swift::swift_dumpTrackedAccesses() {
375-
getAccessSet().forEach([](Access *a) {
366+
getTLSContext().accessSet.forEach([](Access *a) {
376367
fprintf(stderr, "Access. Pointer: %p. PC: %p. AccessAction: %s\n",
377368
a->Pointer, a->PC, getAccessName(a->getAccessAction()));
378369
});

stdlib/public/runtime/ThreadLocalStorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern "C" int pthread_key_init_np(int key, void (*destructor)(void *));
5454
# define __PTK_FRAMEWORK_SWIFT_KEY1 101
5555
# endif
5656

57-
# define SWIFT_EXCLUSIVITY_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY0
57+
# define SWIFT_RUNTIME_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY0
5858
# define SWIFT_STDLIB_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY1
5959

6060
#endif

0 commit comments

Comments
 (0)