| 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 | /* Called during key creation or setspecific. */
|
|---|
| 18 | /* For the GC we already hold lock. */
|
|---|
| 19 | /* Currently allocated objects leak on thread exit. */
|
|---|
| 20 | /* That's hard to fix, but OK if we allocate garbage */
|
|---|
| 21 | /* collected memory. */
|
|---|
| 22 | #define MALLOC_CLEAR(n) GC_INTERNAL_MALLOC(n, NORMAL)
|
|---|
| 23 | #define PREFIXED(name) GC_##name
|
|---|
| 24 |
|
|---|
| 25 | #define TS_CACHE_SIZE 1024
|
|---|
| 26 | #define CACHE_HASH(n) (((((long)n) >> 8) ^ (long)n) & (TS_CACHE_SIZE - 1))
|
|---|
| 27 | #define TS_HASH_SIZE 1024
|
|---|
| 28 | #define HASH(n) (((((long)n) >> 8) ^ (long)n) & (TS_HASH_SIZE - 1))
|
|---|
| 29 |
|
|---|
| 30 | /* An entry describing a thread-specific value for a given thread. */
|
|---|
| 31 | /* All such accessible structures preserve the invariant that if either */
|
|---|
| 32 | /* thread is a valid pthread id or qtid is a valid "quick tread id" */
|
|---|
| 33 | /* for a thread, then value holds the corresponding thread specific */
|
|---|
| 34 | /* value. This invariant must be preserved at ALL times, since */
|
|---|
| 35 | /* asynchronous reads are allowed. */
|
|---|
| 36 | typedef struct thread_specific_entry {
|
|---|
| 37 | unsigned long qtid; /* quick thread id, only for cache */
|
|---|
| 38 | void * value;
|
|---|
| 39 | struct thread_specific_entry *next;
|
|---|
| 40 | pthread_t thread;
|
|---|
| 41 | } tse;
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | /* We represent each thread-specific datum as two tables. The first is */
|
|---|
| 45 | /* a cache, indexed by a "quick thread identifier". The "quick" thread */
|
|---|
| 46 | /* identifier is an easy to compute value, which is guaranteed to */
|
|---|
| 47 | /* determine the thread, though a thread may correspond to more than */
|
|---|
| 48 | /* one value. We typically use the address of a page in the stack. */
|
|---|
| 49 | /* The second is a hash table, indexed by pthread_self(). It is used */
|
|---|
| 50 | /* only as a backup. */
|
|---|
| 51 |
|
|---|
| 52 | /* Return the "quick thread id". Default version. Assumes page size, */
|
|---|
| 53 | /* or at least thread stack separation, is at least 4K. */
|
|---|
| 54 | /* Must be defined so that it never returns 0. (Page 0 can't really */
|
|---|
| 55 | /* be part of any stack, since that would make 0 a valid stack pointer.)*/
|
|---|
| 56 | static __inline__ unsigned long quick_thread_id() {
|
|---|
| 57 | int dummy;
|
|---|
| 58 | return (unsigned long)(&dummy) >> 12;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | #define INVALID_QTID ((unsigned long)0)
|
|---|
| 62 | #define INVALID_THREADID ((pthread_t)0)
|
|---|
| 63 |
|
|---|
| 64 | typedef struct thread_specific_data {
|
|---|
| 65 | tse * volatile cache[TS_CACHE_SIZE];
|
|---|
| 66 | /* A faster index to the hash table */
|
|---|
| 67 | tse * hash[TS_HASH_SIZE];
|
|---|
| 68 | pthread_mutex_t lock;
|
|---|
| 69 | } tsd;
|
|---|
| 70 |
|
|---|
| 71 | typedef tsd * PREFIXED(key_t);
|
|---|
| 72 |
|
|---|
| 73 | extern int PREFIXED(key_create) (tsd ** key_ptr, void (* destructor)(void *));
|
|---|
| 74 |
|
|---|
| 75 | extern int PREFIXED(setspecific) (tsd * key, void * value);
|
|---|
| 76 |
|
|---|
| 77 | extern void PREFIXED(remove_specific) (tsd * key);
|
|---|
| 78 |
|
|---|
| 79 | /* An internal version of getspecific that assumes a cache miss. */
|
|---|
| 80 | void * PREFIXED(slow_getspecific) (tsd * key, unsigned long qtid,
|
|---|
| 81 | tse * volatile * cache_entry);
|
|---|
| 82 |
|
|---|
| 83 | static __inline__ void * PREFIXED(getspecific) (tsd * key) {
|
|---|
| 84 | long qtid = quick_thread_id();
|
|---|
| 85 | unsigned hash_val = CACHE_HASH(qtid);
|
|---|
| 86 | tse * volatile * entry_ptr = key -> cache + hash_val;
|
|---|
| 87 | tse * entry = *entry_ptr; /* Must be loaded only once. */
|
|---|
| 88 | if (EXPECT(entry -> qtid == qtid, 1)) {
|
|---|
| 89 | GC_ASSERT(entry -> thread == pthread_self());
|
|---|
| 90 | return entry -> value;
|
|---|
| 91 | }
|
|---|
| 92 | return PREFIXED(slow_getspecific) (key, qtid, entry_ptr);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|