821dc02e0f7aeedc0547d8f8d948434e80b572ba
[cacao.git] / src / mm / boehm-gc / specific.c
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
14 #include "config.h"
15
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    */
20                                 /* if needed.                     */
21
22 #if defined(USE_CUSTOM_SPECIFIC)
23
24 #include "atomic_ops.h"
25
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.            */
30
31 int PREFIXED(key_create) (tsd ** key_ptr, void (* destructor)(void *)) {
32     int i;
33     tsd * result = (tsd *)MALLOC_CLEAR(sizeof (tsd));
34
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;
41     }
42 #   ifdef GC_ASSERTIONS
43       for (i = 0; i < TS_HASH_SIZE; ++i) {
44         GC_ASSERT(result -> hash[i] == 0);
45       }
46 #   endif
47     *key_ptr = result;
48     return 0;
49 }
50
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));
55     
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));
68     return 0;
69 }
70
71 /* Remove thread-specific data for this thread.  Should be called on    */
72 /* thread exit.                                                         */
73 void PREFIXED(remove_specific) (tsd * key) {
74     pthread_t self = pthread_self();
75     unsigned hash_val = HASH(self);
76     tse *entry;
77     tse **link = key -> hash + hash_val;
78
79     pthread_mutex_lock(&(key -> lock));
80     entry = *link;
81     while (entry != NULL && entry -> thread != self) {
82         link = &(entry -> next);
83         entry = *link;
84     }
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;
88     if (entry != NULL) {
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.             */
96     }
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...             */
103
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));
108 }
109
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];
116
117     GC_ASSERT(qtid != INVALID_QTID);
118     while (entry != NULL && entry -> thread != self) {
119         entry = entry -> next;
120     } 
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    */
127                 /* same thread.                                         */
128         *cache_ptr = entry;
129                 /* Again this is safe since pointer assignments are     */
130                 /* presumed atomic, and either pointer is valid.        */
131     return entry -> value;
132 }
133
134 #ifdef GC_ASSERTIONS
135
136 /* Check that that all elements of the data structure associated        */
137 /* with key are marked.                                                 */
138 void PREFIXED(check_tsd_marks) (tsd *key)
139 {
140     int i;
141     tse *p;
142
143     if (!GC_is_marked(GC_base(key))) {
144         ABORT("Unmarked thread-specific-data table");
145     }
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))) {
149                 GC_err_printf(
150                         "Thread-specific-data entry at %p not marked\n",p);
151                 ABORT("Unmarked tse");
152             }
153         }
154     }
155     for (i = 0; i < TS_CACHE_SIZE; ++i) {
156         p = key -> cache[i];
157         if (p != &invalid_tse && !GC_is_marked(GC_base(p))) {
158             GC_err_printf(
159                 "Cached thread-specific-data entry at %p not marked\n",p);
160             ABORT("Unmarked cached tse");
161         }
162     }
163 }
164
165 #endif
166
167 #endif /* USE_CUSTOM_SPECIFIC */