/libs/headers/gc/private/specific.h

http://github.com/nddrylliog/ooc · C++ Header · 96 lines · 42 code · 17 blank · 37 comment · 3 complexity · 050b80fa85106917c5109c61fbe18c36 MD5 · raw file

  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. #include "atomic_ops.h"
  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 PREFIXED(name) GC_##name
  23. #define TS_CACHE_SIZE 1024
  24. #define CACHE_HASH(n) (((((long)n) >> 8) ^ (long)n) & (TS_CACHE_SIZE - 1))
  25. #define TS_HASH_SIZE 1024
  26. #define HASH(n) (((((long)n) >> 8) ^ (long)n) & (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 tread 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 */
  49. /* be part of any stack, since that would make 0 a valid stack pointer.)*/
  50. static __inline__ unsigned long quick_thread_id() {
  51. int dummy;
  52. return (unsigned long)(&dummy) >> 12;
  53. }
  54. #define INVALID_QTID ((unsigned long)0)
  55. #define INVALID_THREADID ((pthread_t)0)
  56. typedef struct thread_specific_data {
  57. tse * volatile cache[TS_CACHE_SIZE];
  58. /* A faster index to the hash table */
  59. tse * hash[TS_HASH_SIZE];
  60. pthread_mutex_t lock;
  61. } tsd;
  62. typedef tsd * PREFIXED(key_t);
  63. extern int PREFIXED(key_create) (tsd ** key_ptr, void (* destructor)(void *));
  64. extern int PREFIXED(setspecific) (tsd * key, void * value);
  65. extern void PREFIXED(remove_specific) (tsd * key);
  66. /* An internal version of getspecific that assumes a cache miss. */
  67. void * PREFIXED(slow_getspecific) (tsd * key, unsigned long qtid,
  68. tse * volatile * cache_entry);
  69. static __inline__ void * PREFIXED(getspecific) (tsd * key) {
  70. long qtid = quick_thread_id();
  71. unsigned hash_val = CACHE_HASH(qtid);
  72. tse * volatile * entry_ptr = key -> cache + hash_val;
  73. tse * entry = *entry_ptr; /* Must be loaded only once. */
  74. if (EXPECT(entry -> qtid == qtid, 1)) {
  75. GC_ASSERT(entry -> thread == pthread_self());
  76. return entry -> value;
  77. }
  78. return PREFIXED(slow_getspecific) (key, qtid, entry_ptr);
  79. }