]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/src/engine/boehm_gc/specific.c
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 configuration, pthreads.h. */
15 #include "private/thread_local_alloc.h"
16 /* To determine type of tsd impl. */
17 /* Includes private/specific.h */
20 #if defined(USE_CUSTOM_SPECIFIC)
22 #include "atomic_ops.h"
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. */
29 int PREFIXED(key_create
) (tsd
** key_ptr
, void (* destructor
)(void *)) {
31 tsd
* result
= (tsd
*)MALLOC_CLEAR(sizeof (tsd
));
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
;
41 for (i
= 0; i
< TS_HASH_SIZE
; ++i
) {
42 GC_ASSERT(result
-> hash
[i
] == 0);
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
));
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
));
69 /* Remove thread-specific data for this thread. Should be called on */
71 void PREFIXED(remove_specific
) (tsd
* key
) {
72 pthread_t self
= pthread_self();
73 unsigned hash_val
= HASH(self
);
75 tse
**link
= key
-> hash
+ hash_val
;
77 pthread_mutex_lock(&(key
-> lock
));
79 while (entry
!= NULL
&& entry
-> thread
!= self
) {
80 link
= &(entry
-> next
);
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
;
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. */
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... */
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
));
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
];
115 GC_ASSERT(qtid
!= INVALID_QTID
);
116 while (entry
!= NULL
&& entry
-> thread
!= self
) {
117 entry
= entry
-> next
;
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 */
127 /* Again this is safe since pointer assignments are */
128 /* presumed atomic, and either pointer is valid. */
129 return entry
-> value
;
134 /* Check that that all elements of the data structure associated */
135 /* with key are marked. */
136 void PREFIXED(check_tsd_marks
) (tsd
*key
)
141 if (!GC_is_marked(GC_base(key
))) {
142 ABORT("Unmarked thread-specific-data table");
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
))) {
148 "Thread-specific-data entry at %p not marked\n",p
);
149 ABORT("Unmarked tse");
153 for (i
= 0; i
< TS_CACHE_SIZE
; ++i
) {
155 if (p
!= &invalid_tse
&& !GC_is_marked(GC_base(p
))) {
157 "Cached thread-specific-data entry at %p not marked\n",p
);
158 ABORT("Unmarked cached tse");
165 #endif /* USE_CUSTOM_SPECIFIC */