specific.h 4.1 KB

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