|
Packit |
d28291 |
/*
|
|
Packit |
d28291 |
* Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
|
|
Packit |
d28291 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
|
|
Packit |
d28291 |
*
|
|
Packit |
d28291 |
* Permission is hereby granted to use or copy this program
|
|
Packit |
d28291 |
* for any purpose, provided the above notices are retained on all copies.
|
|
Packit |
d28291 |
* Permission to modify the code and to distribute modified code is granted,
|
|
Packit |
d28291 |
* provided the above notices are retained, and a notice that the code was
|
|
Packit |
d28291 |
* modified is included with the above copyright notice.
|
|
Packit |
d28291 |
*/
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#include "private/thread_local_alloc.h"
|
|
Packit |
d28291 |
/* To determine type of tsd impl. */
|
|
Packit |
d28291 |
/* Includes private/specific.h */
|
|
Packit |
d28291 |
/* if needed. */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#if defined(USE_CUSTOM_SPECIFIC)
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
static const tse invalid_tse = {INVALID_QTID, 0, 0, INVALID_THREADID};
|
|
Packit |
d28291 |
/* A thread-specific data entry which will never */
|
|
Packit |
d28291 |
/* appear valid to a reader. Used to fill in empty */
|
|
Packit |
d28291 |
/* cache entries to avoid a check for 0. */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_INNER int GC_key_create_inner(tsd ** key_ptr)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
int i;
|
|
Packit |
d28291 |
int ret;
|
|
Packit |
d28291 |
tsd * result = (tsd *)MALLOC_CLEAR(sizeof(tsd));
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* A quick alignment check, since we need atomic stores */
|
|
Packit |
d28291 |
GC_ASSERT((word)(&invalid_tse.next) % sizeof(tse *) == 0);
|
|
Packit |
d28291 |
if (0 == result) return ENOMEM;
|
|
Packit |
d28291 |
ret = pthread_mutex_init(&result->lock, NULL);
|
|
Packit |
d28291 |
if (ret != 0) return ret;
|
|
Packit |
d28291 |
for (i = 0; i < TS_CACHE_SIZE; ++i) {
|
|
Packit |
d28291 |
result -> cache[i] = (/* no const */ tse *)&invalid_tse;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# ifdef GC_ASSERTIONS
|
|
Packit |
d28291 |
for (i = 0; i < TS_HASH_SIZE; ++i) {
|
|
Packit |
d28291 |
GC_ASSERT(result -> hash[i].p == 0);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
*key_ptr = result;
|
|
Packit |
d28291 |
return 0;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_INNER int GC_setspecific(tsd * key, void * value)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
pthread_t self = pthread_self();
|
|
Packit |
d28291 |
int hash_val = HASH(self);
|
|
Packit |
d28291 |
volatile tse * entry;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(I_HOLD_LOCK());
|
|
Packit |
d28291 |
GC_ASSERT(self != INVALID_THREADID);
|
|
Packit |
d28291 |
GC_dont_gc++; /* disable GC */
|
|
Packit |
d28291 |
entry = (volatile tse *)MALLOC_CLEAR(sizeof(tse));
|
|
Packit |
d28291 |
GC_dont_gc--;
|
|
Packit |
d28291 |
if (0 == entry) return ENOMEM;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
pthread_mutex_lock(&(key -> lock));
|
|
Packit |
d28291 |
/* Could easily check for an existing entry here. */
|
|
Packit |
d28291 |
entry -> next = key->hash[hash_val].p;
|
|
Packit |
d28291 |
entry -> thread = self;
|
|
Packit |
d28291 |
entry -> value = value;
|
|
Packit |
d28291 |
GC_ASSERT(entry -> qtid == INVALID_QTID);
|
|
Packit |
d28291 |
/* There can only be one writer at a time, but this needs to be */
|
|
Packit |
d28291 |
/* atomic with respect to concurrent readers. */
|
|
Packit |
d28291 |
AO_store_release(&key->hash[hash_val].ao, (AO_t)entry);
|
|
Packit |
d28291 |
pthread_mutex_unlock(&(key -> lock));
|
|
Packit |
d28291 |
return 0;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Remove thread-specific data for a given thread. This function is */
|
|
Packit |
d28291 |
/* called at fork from the child process for all threads except for the */
|
|
Packit |
d28291 |
/* survived one. GC_remove_specific() should be called on thread exit. */
|
|
Packit |
d28291 |
GC_INNER void GC_remove_specific_after_fork(tsd * key, pthread_t t)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
unsigned hash_val = HASH(t);
|
|
Packit |
d28291 |
tse *entry;
|
|
Packit |
d28291 |
tse **link = &key->hash[hash_val].p;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
# ifdef CAN_HANDLE_FORK
|
|
Packit |
d28291 |
/* Both GC_setspecific and GC_remove_specific should be called */
|
|
Packit |
d28291 |
/* with the allocation lock held to ensure the consistency of */
|
|
Packit |
d28291 |
/* the hash table in the forked child. */
|
|
Packit |
d28291 |
GC_ASSERT(I_HOLD_LOCK());
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
pthread_mutex_lock(&(key -> lock));
|
|
Packit |
d28291 |
entry = *link;
|
|
Packit |
d28291 |
while (entry != NULL && entry -> thread != t) {
|
|
Packit |
d28291 |
link = &(entry -> next);
|
|
Packit |
d28291 |
entry = *link;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
/* Invalidate qtid field, since qtids may be reused, and a later */
|
|
Packit |
d28291 |
/* cache lookup could otherwise find this entry. */
|
|
Packit |
d28291 |
if (entry != NULL) {
|
|
Packit |
d28291 |
entry -> qtid = INVALID_QTID;
|
|
Packit |
d28291 |
*link = entry -> next;
|
|
Packit |
d28291 |
/* Atomic! concurrent accesses still work. */
|
|
Packit |
d28291 |
/* They must, since readers don't lock. */
|
|
Packit |
d28291 |
/* We shouldn't need a volatile access here, */
|
|
Packit |
d28291 |
/* since both this and the preceding write */
|
|
Packit |
d28291 |
/* should become visible no later than */
|
|
Packit |
d28291 |
/* the pthread_mutex_unlock() call. */
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
/* If we wanted to deallocate the entry, we'd first have to clear */
|
|
Packit |
d28291 |
/* any cache entries pointing to it. That probably requires */
|
|
Packit |
d28291 |
/* additional synchronization, since we can't prevent a concurrent */
|
|
Packit |
d28291 |
/* cache lookup, which should still be examining deallocated memory.*/
|
|
Packit |
d28291 |
/* This can only happen if the concurrent access is from another */
|
|
Packit |
d28291 |
/* thread, and hence has missed the cache, but still... */
|
|
Packit |
d28291 |
# ifdef LINT2
|
|
Packit |
d28291 |
GC_noop1((word)entry);
|
|
Packit |
d28291 |
# endif
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* With GC, we're done, since the pointers from the cache will */
|
|
Packit |
d28291 |
/* be overwritten, all local pointers to the entries will be */
|
|
Packit |
d28291 |
/* dropped, and the entry will then be reclaimed. */
|
|
Packit |
d28291 |
pthread_mutex_unlock(&(key -> lock));
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
/* Note that even the slow path doesn't lock. */
|
|
Packit |
d28291 |
GC_INNER void * GC_slow_getspecific(tsd * key, word qtid,
|
|
Packit |
d28291 |
tse * volatile * cache_ptr)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
pthread_t self = pthread_self();
|
|
Packit |
d28291 |
unsigned hash_val = HASH(self);
|
|
Packit |
d28291 |
tse *entry = key->hash[hash_val].p;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
GC_ASSERT(qtid != INVALID_QTID);
|
|
Packit |
d28291 |
while (entry != NULL && entry -> thread != self) {
|
|
Packit |
d28291 |
entry = entry -> next;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
if (entry == NULL) return NULL;
|
|
Packit |
d28291 |
/* Set cache_entry. */
|
|
Packit |
d28291 |
entry -> qtid = (AO_t)qtid;
|
|
Packit |
d28291 |
/* It's safe to do this asynchronously. Either value */
|
|
Packit |
d28291 |
/* is safe, though may produce spurious misses. */
|
|
Packit |
d28291 |
/* We're replacing one qtid with another one for the */
|
|
Packit |
d28291 |
/* same thread. */
|
|
Packit |
d28291 |
*cache_ptr = entry;
|
|
Packit |
d28291 |
/* Again this is safe since pointer assignments are */
|
|
Packit |
d28291 |
/* presumed atomic, and either pointer is valid. */
|
|
Packit |
d28291 |
return entry -> value;
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#ifdef GC_ASSERTIONS
|
|
Packit |
d28291 |
/* Check that that all elements of the data structure associated */
|
|
Packit |
d28291 |
/* with key are marked. */
|
|
Packit |
d28291 |
void GC_check_tsd_marks(tsd *key)
|
|
Packit |
d28291 |
{
|
|
Packit |
d28291 |
int i;
|
|
Packit |
d28291 |
tse *p;
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
if (!GC_is_marked(GC_base(key))) {
|
|
Packit |
d28291 |
ABORT("Unmarked thread-specific-data table");
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
for (i = 0; i < TS_HASH_SIZE; ++i) {
|
|
Packit |
d28291 |
for (p = key->hash[i].p; p != 0; p = p -> next) {
|
|
Packit |
d28291 |
if (!GC_is_marked(GC_base(p))) {
|
|
Packit |
d28291 |
ABORT_ARG1("Unmarked thread-specific-data entry",
|
|
Packit |
d28291 |
" at %p", (void *)p);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
for (i = 0; i < TS_CACHE_SIZE; ++i) {
|
|
Packit |
d28291 |
p = key -> cache[i];
|
|
Packit |
d28291 |
if (p != &invalid_tse && !GC_is_marked(GC_base(p))) {
|
|
Packit |
d28291 |
ABORT_ARG1("Unmarked cached thread-specific-data entry",
|
|
Packit |
d28291 |
" at %p", (void *)p);
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
}
|
|
Packit |
d28291 |
#endif /* GC_ASSERTIONS */
|
|
Packit |
d28291 |
|
|
Packit |
d28291 |
#endif /* USE_CUSTOM_SPECIFIC */
|