1 /****************************************************************************
2 **
3 **  This file is part of GAP, a system for computational discrete algebra.
4 **
5 **  Copyright of GAP belongs to its developers, whose names are too numerous
6 **  to list here. Please refer to the COPYRIGHT file for details.
7 **
8 **  SPDX-License-Identifier: GPL-2.0-or-later
9 */
10 
11 #ifndef GAP_TLS_H
12 #define GAP_TLS_H
13 
14 #include "system.h"
15 
16 #if !defined(HPCGAP)
17 #error This header is only meant to be used with HPC-GAP
18 #endif
19 
20 #include "hpc/tlsconfig.h"
21 
22 typedef struct Region Region;
23 
24 typedef struct ThreadLocalStorage
25 {
26   int threadID;
27   void *threadLock;
28   void *threadSignal;
29   void *acquiredMonitor;
30   unsigned multiplexRandomSeed;
31   Region * currentRegion;
32   Region * threadRegion;
33   Obj threadObject;
34   Obj tlRecords;
35   Obj lockStack;
36   int lockStackPointer;
37   Obj copiedObjs;
38   Obj interruptHandlers;
39   void *CurrentHashLock;
40   int DisableGuards;
41 
42   /* From intrprtr.c */
43   UInt PeriodicCheckCount;
44 
45   /* From read.c */
46   syJmp_buf threadExit;
47 
48   /* From scanner.c */
49   Obj DefaultOutput;
50   Obj DefaultInput;
51 
52   /* Profiling */
53   UInt CountActive;
54   UInt LocksAcquired;
55   UInt LocksContended;
56 } ThreadLocalStorage;
57 
58 #ifdef HAVE_NATIVE_TLS
59 extern __thread ThreadLocalStorage *TLSInstance;
60 #endif
61 
62 #if defined(VERBOSE_GUARDS)
GetTLS(void)63 static inline ThreadLocalStorage *GetTLS(void)
64 #else
65 static ALWAYS_INLINE ThreadLocalStorage *GetTLS(void)
66 #endif
67 {
68 #ifdef HAVE_NATIVE_TLS
69     return TLSInstance;
70 #else
71     void *stack;
72   #ifdef __GNUC__
73     stack = __builtin_frame_address(0);
74   #else
75     int dummy[1];
76     stack = dummy;
77   #endif
78     return (ThreadLocalStorage *) (((uintptr_t) stack) & TLS_MASK);
79 #endif
80 }
81 
82 // Convenience helper for accessing TLS members
83 #define TLS(x) GetTLS()->x
84 
85 void InitializeTLS(void);
86 
87 #endif // GAP_TLS_H
88