Debian Codegraph @

external/bdwgc/include/private/specific.h

raw
1 /*
2 * This is a reimplementation of a subset of the pthread_getspecific/setspecific
3 * interface. This appears to outperform the standard linuxthreads one
4 * by a significant margin.
5 * The major restriction is that each thread may only make a single
6 * pthread_setspecific call on a single key. (The current data structure
7 * doesn't really require that. The restriction should be easily removable.)
8 * We don't currently support the destruction functions, though that
9 * could be done.
10 * We also currently assume that only one pthread_setspecific call
11 * can be executed at a time, though that assumption would be easy to remove
12 * by adding a lock.
13 */
14
15 #include <errno.h>
16
17 EXTERN_C_BEGIN
18
19 /* Called during key creation or setspecific. */
20 /* For the GC we already hold lock. */
21 /* Currently allocated objects leak on thread exit. */
22 /* That's hard to fix, but OK if we allocate garbage */
23 /* collected memory. */
24 #define MALLOC_CLEAR(n) GC_INTERNAL_MALLOC(n, NORMAL)
25
26 #define TS_CACHE_SIZE 1024
27 #define CACHE_HASH(n) ((((n) >> 8) ^ (n)) & (TS_CACHE_SIZE - 1))
28
29 #define TS_HASH_SIZE 1024
30 #define HASH(p) \
31 ((unsigned)((((word)(p)) >> 8) ^ (word)(p)) & (TS_HASH_SIZE - 1))
32
33 /* An entry describing a thread-specific value for a given thread. */
34 /* All such accessible structures preserve the invariant that if either */
35 /* thread is a valid pthread id or qtid is a valid "quick thread id" */
36 /* for a thread, then value holds the corresponding thread specific */
37 /* value. This invariant must be preserved at ALL times, since */
38 /* asynchronous reads are allowed. */
39 typedef struct thread_specific_entry {
40 volatile AO_t qtid; /* quick thread id, only for cache */
41 void * value;
42 struct thread_specific_entry *next;
43 pthread_t thread;
44 } tse;
45
46 /* We represent each thread-specific datum as two tables. The first is */
47 /* a cache, indexed by a "quick thread identifier". The "quick" thread */
48 /* identifier is an easy to compute value, which is guaranteed to */
49 /* determine the thread, though a thread may correspond to more than */
50 /* one value. We typically use the address of a page in the stack. */
51 /* The second is a hash table, indexed by pthread_self(). It is used */
52 /* only as a backup. */
53
54 /* Return the "quick thread id". Default version. Assumes page size, */
55 /* or at least thread stack separation, is at least 4K. */
56 /* Must be defined so that it never returns 0. (Page 0 can't really be */
57 /* part of any stack, since that would make 0 a valid stack pointer.) */
58 #define quick_thread_id() (((word)GC_approx_sp()) >> 12)
59
60 #define INVALID_QTID ((word)0)
61 #define INVALID_THREADID ((pthread_t)0)
62
63 union ptse_ao_u {
64 tse *p;
65 volatile AO_t ao;
66 };
67
68 typedef struct thread_specific_data {
69 tse * volatile cache[TS_CACHE_SIZE];
70 /* A faster index to the hash table */
71 union ptse_ao_u hash[TS_HASH_SIZE];
72 pthread_mutex_t lock;
73 } tsd;
74
75 typedef tsd * GC_key_t;
76
77 #define GC_key_create(key, d) GC_key_create_inner(key)
78 GC_INNER int GC_key_create_inner(tsd ** key_ptr);
79 GC_INNER int GC_setspecific(tsd * key, void * value);
80 #define GC_remove_specific(key) \
81 GC_remove_specific_after_fork(key, pthread_self())
82 GC_INNER void GC_remove_specific_after_fork(tsd * key, pthread_t t);
83
84 /* An internal version of getspecific that assumes a cache miss. */
85 GC_INNER void * GC_slow_getspecific(tsd * key, word qtid,
86 tse * volatile * cache_entry);
87
88 /* GC_INLINE is defined in gc_priv.h. */
89 GC_INLINE void * GC_getspecific(tsd * key)
90 {
91 word qtid = quick_thread_id();
92 tse * volatile * entry_ptr = &key->cache[CACHE_HASH(qtid)];
93 tse * entry = *entry_ptr; /* Must be loaded only once. */
94
95 GC_ASSERT(qtid != INVALID_QTID);
96 if (EXPECT(entry -> qtid == qtid, TRUE)) {
97 GC_ASSERT(entry -> thread == pthread_self());
98 return entry -> value;
99 }
100 return GC_slow_getspecific(key, qtid, entry_ptr);
101 }
102
103 EXTERN_C_END