@@ -157,16 +157,8 @@ static_assert(sizeof(Access) <= sizeof(ValueBuffer) &&
157
157
" Access doesn't fit in a value buffer!" );
158
158
159
159
// / 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.
162
160
class AccessSet {
163
161
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 ;
170
162
public:
171
163
constexpr AccessSet () {}
172
164
@@ -229,21 +221,18 @@ class AccessSet {
229
221
}
230
222
}
231
223
#endif
224
+ };
232
225
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;
238
230
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 ;
247
236
};
248
237
249
238
} // end anonymous namespace
@@ -254,40 +243,40 @@ class AccessSet {
254
243
#if SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
255
244
// Use the reserved TSD key if possible.
256
245
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 ;
262
251
263
252
static OnceToken_t setupToken;
264
253
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);
267
256
});
268
257
}, nullptr );
269
258
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 ;
273
262
}
274
263
275
264
#elif SWIFT_TLS_HAS_THREADLOCAL
276
265
// Second choice is direct language support for thread-locals.
277
266
278
- static LLVM_THREAD_LOCAL AccessSet ExclusivityAccessSet ;
267
+ static LLVM_THREAD_LOCAL SwiftTLSContext TLSContext ;
279
268
280
- static AccessSet & getAccessSet () {
281
- return ExclusivityAccessSet ;
269
+ static SwiftTLSContext & getTLSContext () {
270
+ return TLSContext ;
282
271
}
283
272
284
273
#else
285
274
// Use the platform thread-local data API.
286
275
287
- static __swift_thread_key_t createAccessSetThreadKey () {
276
+ static __swift_thread_key_t createSwiftThreadKey () {
288
277
__swift_thread_key_t key;
289
278
int result = SWIFT_THREAD_KEY_CREATE (&key, [](void *pointer) {
290
- delete static_cast <AccessSet *>(pointer);
279
+ delete static_cast <SwiftTLSContext *>(pointer);
291
280
});
292
281
293
282
if (result != 0 ) {
@@ -297,15 +286,15 @@ static __swift_thread_key_t createAccessSetThreadKey() {
297
286
return key;
298
287
}
299
288
300
- static AccessSet & getAccessSet () {
301
- static __swift_thread_key_t key = createAccessSetThreadKey ();
289
+ static SwiftTLSContext & getTLSContext () {
290
+ static __swift_thread_key_t key = createSwiftThreadKey ();
302
291
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 );
307
296
}
308
- return *set ;
297
+ return *ctx ;
309
298
}
310
299
311
300
#endif
@@ -332,7 +321,7 @@ void swift::swift_beginAccess(void *pointer, ValueBuffer *buffer,
332
321
if (!pc)
333
322
pc = get_return_address ();
334
323
335
- if (!getAccessSet () .insert (access, pc, pointer, flags))
324
+ if (!getTLSContext (). accessSet .insert (access, pc, pointer, flags))
336
325
access->Pointer = nullptr ;
337
326
}
338
327
@@ -347,22 +336,24 @@ void swift::swift_endAccess(ValueBuffer *buffer) {
347
336
return ;
348
337
}
349
338
350
- getAccessSet () .remove (access);
339
+ getTLSContext (). accessSet .remove (access);
351
340
}
352
341
353
342
char *swift::swift_getFunctionReplacement (char **ReplFnPtr, char *CurrFn) {
354
343
char *ReplFn = *ReplFnPtr;
355
344
if (ReplFn == CurrFn)
356
345
return nullptr ;
357
- if (getAccessSet ().shouldCallOriginalOfReplacedFunction ()) {
346
+ SwiftTLSContext &ctx = getTLSContext ();
347
+ if (ctx.CallOriginalOfReplacedFunction ) {
348
+ ctx.CallOriginalOfReplacedFunction = false ;
358
349
return nullptr ;
359
350
}
360
351
return ReplFn;
361
352
}
362
353
363
354
char *swift::swift_getOrigOfReplaceable (char **OrigFnPtr) {
364
355
char *OrigFn = *OrigFnPtr;
365
- getAccessSet ().aboutToCallOriginalOfReplacedFunction () ;
356
+ getTLSContext ().CallOriginalOfReplacedFunction = true ;
366
357
return OrigFn;
367
358
}
368
359
@@ -372,7 +363,7 @@ char *swift::swift_getOrigOfReplaceable(char **OrigFnPtr) {
372
363
//
373
364
// This is only intended to be used in the debugger.
374
365
void swift::swift_dumpTrackedAccesses () {
375
- getAccessSet () .forEach ([](Access *a) {
366
+ getTLSContext (). accessSet .forEach ([](Access *a) {
376
367
fprintf (stderr, " Access. Pointer: %p. PC: %p. AccessAction: %s\n " ,
377
368
a->Pointer , a->PC , getAccessName (a->getAccessAction ()));
378
369
});
0 commit comments