new Stack memory allocation, use of unused arg regs as temp disabled
[cacao.git] / src / 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 #if defined(GC_LINUX_THREADS)
17
18 #include "private/gc_priv.h" /* For GC_compare_and_exchange, GC_memory_barrier */
19 #include "private/specific.h"
20
21 static tse invalid_tse = {INVALID_QTID, 0, 0, INVALID_THREADID};
22                         /* A thread-specific data entry which will never        */
23                         /* appear valid to a reader.  Used to fill in empty     */
24                         /* cache entries to avoid a check for 0.                */
25
26 int PREFIXED(key_create) (tsd ** key_ptr, void (* destructor)(void *)) {
27     int i;
28     tsd * result = (tsd *)MALLOC_CLEAR(sizeof (tsd));
29
30     /* A quick alignment check, since we need atomic stores */
31       GC_ASSERT((unsigned long)(&invalid_tse.next) % sizeof(tse *) == 0);
32     if (0 == result) return ENOMEM;
33     pthread_mutex_init(&(result -> lock), NULL);
34     for (i = 0; i < TS_CACHE_SIZE; ++i) {
35         result -> cache[i] = &invalid_tse;
36     }
37 #   ifdef GC_ASSERTIONS
38       for (i = 0; i < TS_HASH_SIZE; ++i) {
39         GC_ASSERT(result -> hash[i] == 0);
40       }
41 #   endif
42     *key_ptr = result;
43     return 0;
44 }
45
46 int PREFIXED(setspecific) (tsd * key, void * value) {
47     pthread_t self = pthread_self();
48     int hash_val = HASH(self);
49     volatile tse * entry = (volatile tse *)MALLOC_CLEAR(sizeof (tse));
50     
51     GC_ASSERT(self != INVALID_THREADID);
52     if (0 == entry) return ENOMEM;
53     pthread_mutex_lock(&(key -> lock));
54     /* Could easily check for an existing entry here.   */
55     entry -> next = key -> hash[hash_val];
56     entry -> thread = self;
57     entry -> value = value;
58     GC_ASSERT(entry -> qtid == INVALID_QTID);
59     /* There can only be one writer at a time, but this needs to be     */
60     /* atomic with respect to concurrent readers.                       */ 
61     *(volatile tse **)(key -> hash + hash_val) = entry;
62     pthread_mutex_unlock(&(key -> lock));
63     return 0;
64 }
65
66 /* Remove thread-specific data for this thread.  Should be called on    */
67 /* thread exit.                                                         */
68 void PREFIXED(remove_specific) (tsd * key) {
69     pthread_t self = pthread_self();
70     unsigned hash_val = HASH(self);
71     tse *entry;
72     tse **link = key -> hash + hash_val;
73
74     pthread_mutex_lock(&(key -> lock));
75     entry = *link;
76     while (entry != NULL && entry -> thread != self) {
77         link = &(entry -> next);
78         entry = *link;
79     }
80     /* Invalidate qtid field, since qtids may be reused, and a later    */
81     /* cache lookup could otherwise find this entry.                    */
82         entry -> qtid = INVALID_QTID;
83     if (entry != NULL) {
84         *link = entry -> next;
85         /* Atomic! concurrent accesses still work.      */
86         /* They must, since readers don't lock.         */
87         /* We shouldn't need a volatile access here,    */
88         /* since both this and the preceding write      */
89         /* should become visible no later than          */
90         /* the pthread_mutex_unlock() call.             */
91     }
92     /* If we wanted to deallocate the entry, we'd first have to clear   */
93     /* any cache entries pointing to it.  That probably requires        */
94     /* additional synchronization, since we can't prevent a concurrent  */
95     /* cache lookup, which should still be examining deallocated memory.*/
96     /* This can only happen if the concurrent access is from another    */
97     /* thread, and hence has missed the cache, but still...             */
98
99     /* With GC, we're done, since the pointers from the cache will      */
100     /* be overwritten, all local pointers to the entries will be        */
101     /* dropped, and the entry will then be reclaimed.                   */
102     pthread_mutex_unlock(&(key -> lock));
103 }
104
105 /* Note that even the slow path doesn't lock.   */
106 void *  PREFIXED(slow_getspecific) (tsd * key, unsigned long qtid,
107                                     tse * volatile * cache_ptr) {
108     pthread_t self = pthread_self();
109     unsigned hash_val = HASH(self);
110     tse *entry = key -> hash[hash_val];
111
112     GC_ASSERT(qtid != INVALID_QTID);
113     while (entry != NULL && entry -> thread != self) {
114         entry = entry -> next;
115     } 
116     if (entry == NULL) return NULL;
117     /* Set cache_entry.         */
118         entry -> qtid = qtid;
119                 /* It's safe to do this asynchronously.  Either value   */
120                 /* is safe, though may produce spurious misses.         */
121                 /* We're replacing one qtid with another one for the    */
122                 /* same thread.                                         */
123         *cache_ptr = entry;
124                 /* Again this is safe since pointer assignments are     */
125                 /* presumed atomic, and either pointer is valid.        */
126     return entry -> value;
127 }
128
129 #endif /* GC_LINUX_THREADS */