2 * Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
16 #include "private/gc_priv.h" /* For configuration, pthreads.h. */
17 #include "private/thread_local_alloc.h"
18 /* To determine type of tsd impl. */
19 /* Includes private/specific.h */
22 #if defined(USE_CUSTOM_SPECIFIC)
24 #include "atomic_ops.h"
26 static tse invalid_tse = {INVALID_QTID, 0, 0, INVALID_THREADID};
27 /* A thread-specific data entry which will never */
28 /* appear valid to a reader. Used to fill in empty */
29 /* cache entries to avoid a check for 0. */
31 int PREFIXED(key_create) (tsd ** key_ptr, void (* destructor)(void *)) {
33 tsd * result = (tsd *)MALLOC_CLEAR(sizeof (tsd));
35 /* A quick alignment check, since we need atomic stores */
36 GC_ASSERT((unsigned long)(&invalid_tse.next) % sizeof(tse *) == 0);
37 if (0 == result) return ENOMEM;
38 pthread_mutex_init(&(result -> lock), NULL);
39 for (i = 0; i < TS_CACHE_SIZE; ++i) {
40 result -> cache[i] = &invalid_tse;
43 for (i = 0; i < TS_HASH_SIZE; ++i) {
44 GC_ASSERT(result -> hash[i] == 0);
51 int PREFIXED(setspecific) (tsd * key, void * value) {
52 pthread_t self = pthread_self();
53 int hash_val = HASH(self);
54 volatile tse * entry = (volatile tse *)MALLOC_CLEAR(sizeof (tse));
56 GC_ASSERT(self != INVALID_THREADID);
57 if (0 == entry) return ENOMEM;
58 pthread_mutex_lock(&(key -> lock));
59 /* Could easily check for an existing entry here. */
60 entry -> next = key -> hash[hash_val];
61 entry -> thread = self;
62 entry -> value = value;
63 GC_ASSERT(entry -> qtid == INVALID_QTID);
64 /* There can only be one writer at a time, but this needs to be */
65 /* atomic with respect to concurrent readers. */
66 AO_store_release((volatile AO_t *)(key -> hash + hash_val), (AO_t)entry);
67 pthread_mutex_unlock(&(key -> lock));
71 /* Remove thread-specific data for this thread. Should be called on */
73 void PREFIXED(remove_specific) (tsd * key) {
74 pthread_t self = pthread_self();
75 unsigned hash_val = HASH(self);
77 tse **link = key -> hash + hash_val;
79 pthread_mutex_lock(&(key -> lock));
81 while (entry != NULL && entry -> thread != self) {
82 link = &(entry -> next);
85 /* Invalidate qtid field, since qtids may be reused, and a later */
86 /* cache lookup could otherwise find this entry. */
87 entry -> qtid = INVALID_QTID;
89 *link = entry -> next;
90 /* Atomic! concurrent accesses still work. */
91 /* They must, since readers don't lock. */
92 /* We shouldn't need a volatile access here, */
93 /* since both this and the preceding write */
94 /* should become visible no later than */
95 /* the pthread_mutex_unlock() call. */
97 /* If we wanted to deallocate the entry, we'd first have to clear */
98 /* any cache entries pointing to it. That probably requires */
99 /* additional synchronization, since we can't prevent a concurrent */
100 /* cache lookup, which should still be examining deallocated memory.*/
101 /* This can only happen if the concurrent access is from another */
102 /* thread, and hence has missed the cache, but still... */
104 /* With GC, we're done, since the pointers from the cache will */
105 /* be overwritten, all local pointers to the entries will be */
106 /* dropped, and the entry will then be reclaimed. */
107 pthread_mutex_unlock(&(key -> lock));
110 /* Note that even the slow path doesn't lock. */
111 void * PREFIXED(slow_getspecific) (tsd * key, unsigned long qtid,
112 tse * volatile * cache_ptr) {
113 pthread_t self = pthread_self();
114 unsigned hash_val = HASH(self);
115 tse *entry = key -> hash[hash_val];
117 GC_ASSERT(qtid != INVALID_QTID);
118 while (entry != NULL && entry -> thread != self) {
119 entry = entry -> next;
121 if (entry == NULL) return NULL;
122 /* Set cache_entry. */
123 entry -> qtid = qtid;
124 /* It's safe to do this asynchronously. Either value */
125 /* is safe, though may produce spurious misses. */
126 /* We're replacing one qtid with another one for the */
129 /* Again this is safe since pointer assignments are */
130 /* presumed atomic, and either pointer is valid. */
131 return entry -> value;
136 /* Check that that all elements of the data structure associated */
137 /* with key are marked. */
138 void PREFIXED(check_tsd_marks) (tsd *key)
143 if (!GC_is_marked(GC_base(key))) {
144 ABORT("Unmarked thread-specific-data table");
146 for (i = 0; i < TS_HASH_SIZE; ++i) {
147 for (p = key -> hash[i]; p != 0; p = p -> next) {
148 if (!GC_is_marked(GC_base(p))) {
150 "Thread-specific-data entry at %p not marked\n",p);
151 ABORT("Unmarked tse");
155 for (i = 0; i < TS_CACHE_SIZE; ++i) {
157 if (p != &invalid_tse && !GC_is_marked(GC_base(p))) {
159 "Cached thread-specific-data entry at %p not marked\n",p);
160 ABORT("Unmarked cached tse");
167 #endif /* USE_CUSTOM_SPECIFIC */