specific.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "private/thread_local_alloc.h"
  14. /* To determine type of tsd impl. */
  15. /* Includes private/specific.h */
  16. /* if needed. */
  17. #if defined(USE_CUSTOM_SPECIFIC)
  18. static const tse invalid_tse = {INVALID_QTID, 0, 0, INVALID_THREADID};
  19. /* A thread-specific data entry which will never */
  20. /* appear valid to a reader. Used to fill in empty */
  21. /* cache entries to avoid a check for 0. */
  22. GC_INNER int GC_key_create_inner(tsd ** key_ptr)
  23. {
  24. int i;
  25. int ret;
  26. tsd * result;
  27. GC_ASSERT(I_HOLD_LOCK());
  28. /* A quick alignment check, since we need atomic stores */
  29. GC_ASSERT((word)(&invalid_tse.next) % sizeof(tse *) == 0);
  30. result = (tsd *)MALLOC_CLEAR(sizeof(tsd));
  31. if (NULL == result) return ENOMEM;
  32. ret = pthread_mutex_init(&result->lock, NULL);
  33. if (ret != 0) return ret;
  34. for (i = 0; i < TS_CACHE_SIZE; ++i) {
  35. result -> cache[i] = (/* no const */ tse *)&invalid_tse;
  36. }
  37. # ifdef GC_ASSERTIONS
  38. for (i = 0; i < TS_HASH_SIZE; ++i) {
  39. GC_ASSERT(result -> hash[i].p == 0);
  40. }
  41. # endif
  42. *key_ptr = result;
  43. return 0;
  44. }
  45. GC_INNER int GC_setspecific(tsd * key, void * value)
  46. {
  47. pthread_t self = pthread_self();
  48. int hash_val = HASH(self);
  49. volatile tse * entry;
  50. GC_ASSERT(I_HOLD_LOCK());
  51. GC_ASSERT(self != INVALID_THREADID);
  52. GC_dont_gc++; /* disable GC */
  53. entry = (volatile tse *)MALLOC_CLEAR(sizeof(tse));
  54. GC_dont_gc--;
  55. if (0 == entry) return ENOMEM;
  56. pthread_mutex_lock(&(key -> lock));
  57. /* Could easily check for an existing entry here. */
  58. entry -> next = key->hash[hash_val].p;
  59. entry -> thread = self;
  60. entry -> value = value;
  61. GC_ASSERT(entry -> qtid == INVALID_QTID);
  62. /* There can only be one writer at a time, but this needs to be */
  63. /* atomic with respect to concurrent readers. */
  64. AO_store_release(&key->hash[hash_val].ao, (AO_t)entry);
  65. GC_dirty((/* no volatile */ void *)entry);
  66. GC_dirty(key->hash + hash_val);
  67. pthread_mutex_unlock(&(key -> lock));
  68. return 0;
  69. }
  70. /* Remove thread-specific data for a given thread. This function is */
  71. /* called at fork from the child process for all threads except for the */
  72. /* survived one. GC_remove_specific() should be called on thread exit. */
  73. GC_INNER void GC_remove_specific_after_fork(tsd * key, pthread_t t)
  74. {
  75. unsigned hash_val = HASH(t);
  76. tse *entry;
  77. tse *prev = NULL;
  78. # ifdef CAN_HANDLE_FORK
  79. /* Both GC_setspecific and GC_remove_specific should be called */
  80. /* with the allocation lock held to ensure the consistency of */
  81. /* the hash table in the forked child. */
  82. GC_ASSERT(I_HOLD_LOCK());
  83. # endif
  84. pthread_mutex_lock(&(key -> lock));
  85. entry = key->hash[hash_val].p;
  86. while (entry != NULL && !THREAD_EQUAL(entry->thread, t)) {
  87. prev = entry;
  88. entry = entry->next;
  89. }
  90. /* Invalidate qtid field, since qtids may be reused, and a later */
  91. /* cache lookup could otherwise find this entry. */
  92. if (entry != NULL) {
  93. entry -> qtid = INVALID_QTID;
  94. if (NULL == prev) {
  95. key->hash[hash_val].p = entry->next;
  96. GC_dirty(key->hash + hash_val);
  97. } else {
  98. prev->next = entry->next;
  99. GC_dirty(prev);
  100. }
  101. /* Atomic! concurrent accesses still work. */
  102. /* They must, since readers don't lock. */
  103. /* We shouldn't need a volatile access here, */
  104. /* since both this and the preceding write */
  105. /* should become visible no later than */
  106. /* the pthread_mutex_unlock() call. */
  107. }
  108. /* If we wanted to deallocate the entry, we'd first have to clear */
  109. /* any cache entries pointing to it. That probably requires */
  110. /* additional synchronization, since we can't prevent a concurrent */
  111. /* cache lookup, which should still be examining deallocated memory.*/
  112. /* This can only happen if the concurrent access is from another */
  113. /* thread, and hence has missed the cache, but still... */
  114. # ifdef LINT2
  115. GC_noop1((word)entry);
  116. # endif
  117. /* With GC, we're done, since the pointers from the cache will */
  118. /* be overwritten, all local pointers to the entries will be */
  119. /* dropped, and the entry will then be reclaimed. */
  120. pthread_mutex_unlock(&(key -> lock));
  121. }
  122. /* Note that even the slow path doesn't lock. */
  123. GC_INNER void * GC_slow_getspecific(tsd * key, word qtid,
  124. tse * volatile * cache_ptr)
  125. {
  126. pthread_t self = pthread_self();
  127. unsigned hash_val = HASH(self);
  128. tse *entry = key->hash[hash_val].p;
  129. GC_ASSERT(qtid != INVALID_QTID);
  130. while (entry != NULL && !THREAD_EQUAL(entry->thread, self)) {
  131. entry = entry -> next;
  132. }
  133. if (entry == NULL) return NULL;
  134. /* Set cache_entry. */
  135. entry -> qtid = (AO_t)qtid;
  136. /* It's safe to do this asynchronously. Either value */
  137. /* is safe, though may produce spurious misses. */
  138. /* We're replacing one qtid with another one for the */
  139. /* same thread. */
  140. *cache_ptr = entry;
  141. /* Again this is safe since pointer assignments are */
  142. /* presumed atomic, and either pointer is valid. */
  143. return entry -> value;
  144. }
  145. #ifdef GC_ASSERTIONS
  146. /* Check that that all elements of the data structure associated */
  147. /* with key are marked. */
  148. void GC_check_tsd_marks(tsd *key)
  149. {
  150. int i;
  151. tse *p;
  152. if (!GC_is_marked(GC_base(key))) {
  153. ABORT("Unmarked thread-specific-data table");
  154. }
  155. for (i = 0; i < TS_HASH_SIZE; ++i) {
  156. for (p = key->hash[i].p; p != 0; p = p -> next) {
  157. if (!GC_is_marked(GC_base(p))) {
  158. ABORT_ARG1("Unmarked thread-specific-data entry",
  159. " at %p", (void *)p);
  160. }
  161. }
  162. }
  163. for (i = 0; i < TS_CACHE_SIZE; ++i) {
  164. p = key -> cache[i];
  165. if (p != &invalid_tse && !GC_is_marked(GC_base(p))) {
  166. ABORT_ARG1("Unmarked cached thread-specific-data entry",
  167. " at %p", (void *)p);
  168. }
  169. }
  170. }
  171. #endif /* GC_ASSERTIONS */
  172. #endif /* USE_CUSTOM_SPECIFIC */