| 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 <assert.h> | 
|---|
| 20 |  | 
|---|
| 21 | // | 
|---|
| 22 | // Static members | 
|---|
| 23 | // | 
|---|
| 24 | JTCTSSManager* JTCTSSManager::instance_ = 0; | 
|---|
| 25 |  | 
|---|
| 26 | // ---------------------------------------------------------------------- | 
|---|
| 27 | // JTCTSSManager constructor/destructor | 
|---|
| 28 | // ---------------------------------------------------------------------- | 
|---|
| 29 |  | 
|---|
| 30 | JTCTSSManager::JTCTSSManager() | 
|---|
| 31 | : head_(0) | 
|---|
| 32 | { | 
|---|
| 33 | assert(instance_ == 0); | 
|---|
| 34 | instance_ = this; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | JTCTSSManager::~JTCTSSManager() | 
|---|
| 38 | { | 
|---|
| 39 | assert(instance_ != 0); | 
|---|
| 40 | instance_ = 0; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | // ---------------------------------------------------------------------- | 
|---|
| 44 | // JTCTSSManager public member implementation | 
|---|
| 45 | // ---------------------------------------------------------------------- | 
|---|
| 46 |  | 
|---|
| 47 | JTCTSSManager* | 
|---|
| 48 | JTCTSSManager::instance() | 
|---|
| 49 | { | 
|---|
| 50 | return instance_; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | // | 
|---|
| 54 | // Associate a key and a cleanup function | 
|---|
| 55 | // | 
|---|
| 56 | void | 
|---|
| 57 | JTCTSSManager::allocate(JTCThreadKey key, void(*release)(void*)) | 
|---|
| 58 | { | 
|---|
| 59 | mut_.lock(); | 
|---|
| 60 |  | 
|---|
| 61 | KeyCleanup* node = new KeyCleanup; | 
|---|
| 62 | node -> key = key; | 
|---|
| 63 | node -> release = release; | 
|---|
| 64 | node -> next = head_; | 
|---|
| 65 | head_ = node; | 
|---|
| 66 |  | 
|---|
| 67 | mut_.unlock(); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | // | 
|---|
| 71 | // Remove the key from our internal table | 
|---|
| 72 | // | 
|---|
| 73 | void | 
|---|
| 74 | JTCTSSManager::release(JTCThreadKey key) | 
|---|
| 75 | { | 
|---|
| 76 | mut_.lock(); | 
|---|
| 77 |  | 
|---|
| 78 | KeyCleanup** node = &head_; | 
|---|
| 79 | while(*node != 0) | 
|---|
| 80 | { | 
|---|
| 81 | if((*node) -> key == key) | 
|---|
| 82 | break; | 
|---|
| 83 | node = &(*node) -> next; | 
|---|
| 84 | } | 
|---|
| 85 | if(*node != 0) | 
|---|
| 86 | { | 
|---|
| 87 | KeyCleanup* tmp = *node; | 
|---|
| 88 | *node = (*node) -> next; | 
|---|
| 89 | delete tmp; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | mut_.unlock(); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | // | 
|---|
| 96 | // Run the cleanup functions | 
|---|
| 97 | // | 
|---|
| 98 | void | 
|---|
| 99 | JTCTSSManager::cleanup() | 
|---|
| 100 | { | 
|---|
| 101 | mut_.lock(); | 
|---|
| 102 |  | 
|---|
| 103 | KeyCleanup* node = head_; | 
|---|
| 104 | while(node != 0) | 
|---|
| 105 | { | 
|---|
| 106 | void* data = JTCTSS::get(node -> key); | 
|---|
| 107 | (*node -> release)(data); | 
|---|
| 108 | JTCTSS::set(node -> key, 0); | 
|---|
| 109 | node = node -> next; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | mut_.unlock(); | 
|---|
| 113 | } | 
|---|