Debian Codegraph @

gc/include/private/specific.h

raw
1 /*
2 * Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /*
15 * This is a reimplementation of a subset of the pthread_getspecific/setspecific
16 * interface. This appears to outperform the standard linuxthreads one
17 * by a significant margin.
18 * The major restriction is that each thread may only make a single
19 * pthread_setspecific call on a single key. (The current data structure
20 * doesn't really require that. The restriction should be easily removable.)
21 * We don't currently support the destruction functions, though that
22 * could be done.
23 * We also currently assume that only one pthread_setspecific call
24 * can be executed at a time, though that assumption would be easy to remove
25 * by adding a lock.
26 */
27
28 #ifndef GC_SPECIFIC_H
29 #define GC_SPECIFIC_H
30
31 #include <errno.h>
32
33 EXTERN_C_BEGIN
34
35 /* Called during key creation or setspecific. */
36 /* For the GC we already hold lock. */
37 /* Currently allocated objects leak on thread exit. */
38 /* That's hard to fix, but OK if we allocate garbage */
39 /* collected memory. */
40 #define MALLOC_CLEAR(n) GC_INTERNAL_MALLOC(n, NORMAL)
41
42 #define TS_CACHE_SIZE 1024
43 #define CACHE_HASH(n) ((((n) >> 8) ^ (n)) & (TS_CACHE_SIZE - 1))
44
45 #define TS_HASH_SIZE 1024
46 #define HASH(p) \
47 ((unsigned)((((word)(p)) >> 8) ^ (word)(p)) & (TS_HASH_SIZE - 1))
48
49 #ifdef GC_ASSERTIONS
50 /* Thread-local storage is not guaranteed to be scanned by GC. */
51 /* We hide values stored in "specific" entries for a test purpose. */
52 typedef GC_hidden_pointer ts_entry_value_t;
53 # define TS_HIDE_VALUE(p) GC_HIDE_POINTER(p)
54 # define TS_REVEAL_PTR(p) GC_REVEAL_POINTER(p)
55 #else
56 typedef void * ts_entry_value_t;
57 # define TS_HIDE_VALUE(p) (p)
58 # define TS_REVEAL_PTR(p) (p)
59 #endif
60
61 /* An entry describing a thread-specific value for a given thread. */
62 /* All such accessible structures preserve the invariant that if either */
63 /* thread is a valid pthread id or qtid is a valid "quick thread id" */
64 /* for a thread, then value holds the corresponding thread specific */
65 /* value. This invariant must be preserved at ALL times, since */
66 /* asynchronous reads are allowed. */
67 typedef struct thread_specific_entry {
68 volatile AO_t qtid; /* quick thread id, only for cache */
69 ts_entry_value_t value;
70 struct thread_specific_entry *next;
71 pthread_t thread;
72 } tse;
73
74 /* We represent each thread-specific datum as two tables. The first is */
75 /* a cache, indexed by a "quick thread identifier". The "quick" thread */
76 /* identifier is an easy to compute value, which is guaranteed to */
77 /* determine the thread, though a thread may correspond to more than */
78 /* one value. We typically use the address of a page in the stack. */
79 /* The second is a hash table, indexed by pthread_self(). It is used */
80 /* only as a backup. */
81
82 /* Return the "quick thread id". Default version. Assumes page size, */
83 /* or at least thread stack separation, is at least 4 KB. */
84 /* Must be defined so that it never returns 0. (Page 0 can't really be */
85 /* part of any stack, since that would make 0 a valid stack pointer.) */
86 #define quick_thread_id() (((word)GC_approx_sp()) >> 12)
87
88 #define INVALID_QTID ((word)0)
89 #define INVALID_THREADID ((pthread_t)0)
90
91 union ptse_ao_u {
92 tse *p;
93 volatile AO_t ao;
94 };
95
96 typedef struct thread_specific_data {
97 tse * volatile cache[TS_CACHE_SIZE];
98 /* A faster index to the hash table */
99 union ptse_ao_u hash[TS_HASH_SIZE];
100 pthread_mutex_t lock;
101 } tsd;
102
103 typedef tsd * GC_key_t;
104
105 #define GC_key_create(key, d) GC_key_create_inner(key)
106 GC_INNER int GC_key_create_inner(tsd ** key_ptr);
107 GC_INNER int GC_setspecific(tsd * key, void * value);
108 #define GC_remove_specific(key) \
109 GC_remove_specific_after_fork(key, pthread_self())
110 GC_INNER void GC_remove_specific_after_fork(tsd * key, pthread_t t);
111
112 /* An internal version of getspecific that assumes a cache miss. */
113 GC_INNER void * GC_slow_getspecific(tsd * key, word qtid,
114 tse * volatile * cache_entry);
115
116 /* GC_INLINE is defined in gc_priv.h. */
117 GC_INLINE void * GC_getspecific(tsd * key)
118 {
119 word qtid = quick_thread_id();
120 tse * volatile * entry_ptr = &key->cache[CACHE_HASH(qtid)];
121 tse * entry = *entry_ptr; /* Must be loaded only once. */
122
123 GC_ASSERT(qtid != INVALID_QTID);
124 if (EXPECT(entry -> qtid == qtid, TRUE)) {
125 GC_ASSERT(entry -> thread == pthread_self());
126 return TS_REVEAL_PTR(entry -> value);
127 }
128 return GC_slow_getspecific(key, qtid, entry_ptr);
129 }
130
131 EXTERN_C_END
132
133 #endif /* GC_SPECIFIC_H */