| 1 | // ********************************************************************** | 
|---|
| 2 | // | 
|---|
| 3 | // Copyright (c) 2000 | 
|---|
| 4 | // Object Oriented Concepts, Inc. | 
|---|
| 5 | // Billerica, MA, USA | 
|---|
| 6 | // | 
|---|
| 7 | // All Rights Reserved | 
|---|
| 8 | // | 
|---|
| 9 | // ********************************************************************** | 
|---|
| 10 |  | 
|---|
| 11 | #include <JTC/Types.h> | 
|---|
| 12 | #include <JTC/Exception.h> | 
|---|
| 13 | #include <JTC/Syscall.h> | 
|---|
| 14 | #include <JTC/Mutex.h> | 
|---|
| 15 | #include <JTC/TSS.h> | 
|---|
| 16 |  | 
|---|
| 17 | #include <TSSManager.h> | 
|---|
| 18 |  | 
|---|
| 19 | #include <errno.h> | 
|---|
| 20 |  | 
|---|
| 21 | #ifdef HAVE_STD_IOSTREAM | 
|---|
| 22 | using namespace std; | 
|---|
| 23 | #endif | 
|---|
| 24 |  | 
|---|
| 25 | // ---------------------------------------------------------------------- | 
|---|
| 26 | // JTCTSS public member implementation | 
|---|
| 27 | // ---------------------------------------------------------------------- | 
|---|
| 28 |  | 
|---|
| 29 | JTCThreadKey | 
|---|
| 30 | JTCTSS::allocate() | 
|---|
| 31 | { | 
|---|
| 32 | JTCThreadKey key; | 
|---|
| 33 | #if defined(HAVE_POSIX_THREADS) | 
|---|
| 34 | JTC_SYSCALL_2(pthread_key_create, &key, 0, != 0) | 
|---|
| 35 | #endif | 
|---|
| 36 | #if defined(HAVE_DCE_THREADS) | 
|---|
| 37 | JTC_SYSCALL_2(pthread_keycreate, &key, 0, != 0) | 
|---|
| 38 | #endif | 
|---|
| 39 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 40 | JTC_SYSCALL_0(key = TlsAlloc, == 0xFFFFFFFF) | 
|---|
| 41 | #endif | 
|---|
| 42 | return key; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | JTCThreadKey | 
|---|
| 46 | JTCTSS::allocate(void (*cleanup)(void*)) | 
|---|
| 47 | { | 
|---|
| 48 | JTCThreadKey key; | 
|---|
| 49 | #if defined(HAVE_POSIX_THREADS) | 
|---|
| 50 | JTC_SYSCALL_2(pthread_key_create, &key, 0, != 0) | 
|---|
| 51 | #endif | 
|---|
| 52 | #if defined(HAVE_DCE_THREADS) | 
|---|
| 53 | JTC_SYSCALL_2(pthread_keycreate, &key, 0, != 0) | 
|---|
| 54 | #endif | 
|---|
| 55 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 56 | JTC_SYSCALL_0(key = TlsAlloc, == 0xFFFFFFFF) | 
|---|
| 57 | #endif | 
|---|
| 58 |  | 
|---|
| 59 | JTCTSSManager::instance() -> allocate(key, cleanup); | 
|---|
| 60 |  | 
|---|
| 61 | return key; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void | 
|---|
| 65 | JTCTSS::release(JTCThreadKey key) | 
|---|
| 66 | { | 
|---|
| 67 | // | 
|---|
| 68 | // It's possible that release is called after the JTCTSS Manager | 
|---|
| 69 | // has already been destroyed -- although this is technically | 
|---|
| 70 | // an error, just ignore it. | 
|---|
| 71 | // | 
|---|
| 72 | JTCTSSManager* manager = JTCTSSManager::instance(); | 
|---|
| 73 | if(manager != 0) | 
|---|
| 74 | manager -> release(key); | 
|---|
| 75 |  | 
|---|
| 76 | #if defined(HAVE_POSIX_THREADS) | 
|---|
| 77 | JTC_SYSCALL_1(pthread_key_delete, key, != 0) | 
|---|
| 78 | #endif | 
|---|
| 79 | #if defined(HAVE_DCE_THREADS) | 
|---|
| 80 | // | 
|---|
| 81 | // DCE threads doesn't have this operation. | 
|---|
| 82 | // | 
|---|
| 83 | #endif | 
|---|
| 84 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 85 | JTC_SYSCALL_1(TlsFree, key, == 0) | 
|---|
| 86 | #endif | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | void* | 
|---|
| 90 | JTCTSS::get(JTCThreadKey key) | 
|---|
| 91 | { | 
|---|
| 92 | void* v; | 
|---|
| 93 | #if defined(HAVE_POSIX_THREADS) | 
|---|
| 94 | // | 
|---|
| 95 | // FSU Threads has bad definition for pthread_getspecific. | 
|---|
| 96 | // | 
|---|
| 97 | #   if defined(HAVE_FSU_THREADS) | 
|---|
| 98 |  | 
|---|
| 99 | pthread_getspecific(key, &v); | 
|---|
| 100 |  | 
|---|
| 101 | #   else | 
|---|
| 102 |  | 
|---|
| 103 | v = pthread_getspecific(key); | 
|---|
| 104 |  | 
|---|
| 105 | #   endif | 
|---|
| 106 | #endif | 
|---|
| 107 | #if defined(HAVE_DCE_THREADS) | 
|---|
| 108 | JTC_SYSCALL_2(pthread_getspecific, key, &v, != 0); | 
|---|
| 109 | #endif | 
|---|
| 110 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 111 | v = TlsGetValue(key); | 
|---|
| 112 | #endif | 
|---|
| 113 | return v; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void | 
|---|
| 117 | JTCTSS::set(JTCThreadKey key, void* value) | 
|---|
| 118 | { | 
|---|
| 119 | #if defined(HAVE_POSIX_THREADS) || defined(HAVE_DCE_THREADS) | 
|---|
| 120 | try | 
|---|
| 121 | { | 
|---|
| 122 | JTC_SYSCALL_2(pthread_setspecific, key, value, != 0) | 
|---|
| 123 | } | 
|---|
| 124 | catch(const JTCSystemCallException& e) | 
|---|
| 125 | { | 
|---|
| 126 | if(e.getError() == EAGAIN || e.getError() == ENOMEM) | 
|---|
| 127 | { | 
|---|
| 128 | throw JTCOutOfMemoryError(e.getMessage()); | 
|---|
| 129 | } | 
|---|
| 130 | throw; | 
|---|
| 131 | } | 
|---|
| 132 | #endif | 
|---|
| 133 | #if defined(HAVE_WIN32_THREADS) | 
|---|
| 134 | try | 
|---|
| 135 | { | 
|---|
| 136 | JTC_SYSCALL_2(TlsSetValue, key, value, == 0) | 
|---|
| 137 | } | 
|---|
| 138 | catch(const JTCSystemCallException& e) | 
|---|
| 139 | { | 
|---|
| 140 | if(e.getError() == ERROR_NOT_ENOUGH_MEMORY || | 
|---|
| 141 | e.getError() == ERROR_OUTOFMEMORY) | 
|---|
| 142 | { | 
|---|
| 143 | throw JTCOutOfMemoryError(e.getMessage()); | 
|---|
| 144 | } | 
|---|
| 145 | throw; | 
|---|
| 146 | } | 
|---|
| 147 | #endif | 
|---|
| 148 | } | 
|---|