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