Back to index

plt-scheme  4.2.1
specific.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2000 by Hewlett-Packard Company.  All rights reserved.
00003  *
00004  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00005  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00006  *
00007  * Permission is hereby granted to use or copy this program
00008  * for any purpose,  provided the above notices are retained on all copies.
00009  * Permission to modify the code and to distribute modified code is granted,
00010  * provided the above notices are retained, and a notice that the code was
00011  * modified is included with the above copyright notice.
00012  */
00013 
00014 #include "private/gc_priv.h" /* For GC_compare_and_exchange, GC_memory_barrier */
00015 
00016 #if defined(GC_LINUX_THREADS)
00017 
00018 #include "private/specific.h"
00019 
00020 static tse invalid_tse = {INVALID_QTID, 0, 0, INVALID_THREADID};
00021                      /* A thread-specific data entry which will never */
00022                      /* appear valid to a reader.  Used to fill in empty     */
00023                      /* cache entries to avoid a check for 0.         */
00024 
00025 int PREFIXED(key_create) (tsd ** key_ptr, void (* destructor)(void *)) {
00026     int i;
00027     tsd * result = (tsd *)MALLOC_CLEAR(sizeof (tsd));
00028 
00029     /* A quick alignment check, since we need atomic stores */
00030       GC_ASSERT((unsigned long)(&invalid_tse.next) % sizeof(tse *) == 0);
00031     if (0 == result) return ENOMEM;
00032     pthread_mutex_init(&(result -> lock), NULL);
00033     for (i = 0; i < TS_CACHE_SIZE; ++i) {
00034        result -> cache[i] = &invalid_tse;
00035     }
00036 #   ifdef GC_ASSERTIONS
00037       for (i = 0; i < TS_HASH_SIZE; ++i) {
00038        GC_ASSERT(result -> hash[i] == 0);
00039       }
00040 #   endif
00041     *key_ptr = result;
00042     return 0;
00043 }
00044 
00045 int PREFIXED(setspecific) (tsd * key, void * value) {
00046     pthread_t self = pthread_self();
00047     int hash_val = HASH(self);
00048     volatile tse * entry = (volatile tse *)MALLOC_CLEAR(sizeof (tse));
00049     
00050     GC_ASSERT(self != INVALID_THREADID);
00051     if (0 == entry) return ENOMEM;
00052     pthread_mutex_lock(&(key -> lock));
00053     /* Could easily check for an existing entry here.   */
00054     entry -> next = key -> hash[hash_val];
00055     entry -> thread = self;
00056     entry -> value = value;
00057     GC_ASSERT(entry -> qtid == INVALID_QTID);
00058     /* There can only be one writer at a time, but this needs to be   */
00059     /* atomic with respect to concurrent readers.                     */ 
00060     *(volatile tse **)(key -> hash + hash_val) = entry;
00061     pthread_mutex_unlock(&(key -> lock));
00062     return 0;
00063 }
00064 
00065 /* Remove thread-specific data for this thread.  Should be called on  */
00066 /* thread exit.                                                       */
00067 void PREFIXED(remove_specific) (tsd * key) {
00068     pthread_t self = pthread_self();
00069     unsigned hash_val = HASH(self);
00070     tse *entry;
00071     tse **link = key -> hash + hash_val;
00072 
00073     pthread_mutex_lock(&(key -> lock));
00074     entry = *link;
00075     while (entry != NULL && entry -> thread != self) {
00076        link = &(entry -> next);
00077         entry = *link;
00078     }
00079     /* Invalidate qtid field, since qtids may be reused, and a later  */
00080     /* cache lookup could otherwise find this entry.                  */
00081         entry -> qtid = INVALID_QTID;
00082     if (entry != NULL) {
00083        *link = entry -> next;
00084        /* Atomic! concurrent accesses still work.       */
00085        /* They must, since readers don't lock.          */
00086        /* We shouldn't need a volatile access here,     */
00087        /* since both this and the preceding write       */
00088        /* should become visible no later than           */
00089        /* the pthread_mutex_unlock() call.              */
00090     }
00091     /* If we wanted to deallocate the entry, we'd first have to clear        */
00092     /* any cache entries pointing to it.  That probably requires      */
00093     /* additional synchronization, since we can't prevent a concurrent       */
00094     /* cache lookup, which should still be examining deallocated memory.*/
00095     /* This can only happen if the concurrent access is from another  */
00096     /* thread, and hence has missed the cache, but still...           */
00097 
00098     /* With GC, we're done, since the pointers from the cache will    */
00099     /* be overwritten, all local pointers to the entries will be      */
00100     /* dropped, and the entry will then be reclaimed.                 */
00101     pthread_mutex_unlock(&(key -> lock));
00102 }
00103 
00104 /* Note that even the slow path doesn't lock.    */
00105 void *  PREFIXED(slow_getspecific) (tsd * key, unsigned long qtid,
00106                                 tse * volatile * cache_ptr) {
00107     pthread_t self = pthread_self();
00108     unsigned hash_val = HASH(self);
00109     tse *entry = key -> hash[hash_val];
00110 
00111     GC_ASSERT(qtid != INVALID_QTID);
00112     while (entry != NULL && entry -> thread != self) {
00113        entry = entry -> next;
00114     } 
00115     if (entry == NULL) return NULL;
00116     /* Set cache_entry.            */
00117         entry -> qtid = qtid;
00118               /* It's safe to do this asynchronously.  Either value   */
00119               /* is safe, though may produce spurious misses.         */
00120               /* We're replacing one qtid with another one for the    */
00121               /* same thread.                                         */
00122        *cache_ptr = entry;
00123               /* Again this is safe since pointer assignments are     */
00124               /* presumed atomic, and either pointer is valid. */
00125     return entry -> value;
00126 }
00127 
00128 #endif /* GC_LINUX_THREADS */