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