root/juggler/tags/1.0.7/Threads/vjThreadKeyWin32.h
| Revision 8789, 9.5 kB (checked in by patrickh, 7 years ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /*************** <auto-copyright.pl BEGIN do not edit this line> ************** |
| 2 | * |
| 3 | * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002 |
| 4 | * by Iowa State University |
| 5 | * |
| 6 | * Original Authors: |
| 7 | * Allen Bierbaum, Christopher Just, |
| 8 | * Patrick Hartling, Kevin Meinert, |
| 9 | * Carolina Cruz-Neira, Albert Baker |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Library General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Library General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Library General Public |
| 22 | * License along with this library; if not, write to the |
| 23 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 24 | * Boston, MA 02111-1307, USA. |
| 25 | * |
| 26 | * ----------------------------------------------------------------- |
| 27 | * File: $RCSfile$ |
| 28 | * Date modified: $Date$ |
| 29 | * Version: $Revision$ |
| 30 | * ----------------------------------------------------------------- |
| 31 | * |
| 32 | *************** <auto-copyright.pl END do not edit this line> ***************/ |
| 33 | |
| 34 | |
| 35 | /* |
| 36 | * -------------------------------------------------------------------------- |
| 37 | * NOTES: |
| 38 | * - This file (vjThreadKeyWin32.h) MUST be included by vjThread.h, not |
| 39 | * the other way around. |
| 40 | * -------------------------------------------------------------------------- |
| 41 | */ |
| 42 | |
| 43 | #ifndef _VJ_THREAD_KEY_WIN_32_H_ |
| 44 | #define _VJ_THREAD_KEY_WIN_32_H_ |
| 45 | |
| 46 | #include <vjConfig.h> |
| 47 | |
| 48 | #include <stdio.h> |
| 49 | |
| 50 | #include <Threads/vjThreadFunctor.h> |
| 51 | |
| 52 | |
| 53 | // Key destructor function type. |
| 54 | typedef vj_thread_func_t vjKeyDestructor; |
| 55 | |
| 56 | class vjThreadKeyWin32 { |
| 57 | public: |
| 58 | // ----------------------------------------------------------------------- |
| 59 | //: Constructor. Allocate a <keyp> that is used to identify data that is |
| 60 | //+ specific to each thread in the process, is global to all threads in |
| 61 | //+ the process and is destroyed using the spcefied destructor function |
| 62 | //+ that takes a single argument. |
| 63 | // |
| 64 | //! PRE: None. |
| 65 | //! POST: A key is created and is associated with the specified |
| 66 | //+ destructor function and argument. |
| 67 | // |
| 68 | //! ARGS: dest_func - The destructor function for the key. This uses |
| 69 | //+ the functor data structure. |
| 70 | //! ARGS: arg - Argument to be passed to destructor (optional). |
| 71 | // ----------------------------------------------------------------------- |
| 72 | vjThreadKeyWin32 (vj_thread_func_t destructor, void* arg = 0) |
| 73 | : mKeyID(0xffffffff), mDestroyData(NULL) |
| 74 | { |
| 75 | mDestroyData = new vjThreadNonMemberFunctor(destructor, arg); |
| 76 | keycreate(); |
| 77 | } |
| 78 | |
| 79 | // ----------------------------------------------------------------------- |
| 80 | //: Constructor. Allocates a <keyp> that is used to identify data that |
| 81 | //+ is specific to each thread in the process, is global to all threads |
| 82 | //+ in the process and is destroyed by the specified destructor function. |
| 83 | // |
| 84 | //! PRE: None. |
| 85 | //! POST: A key is created and is associated with the specified |
| 86 | //+ destructor function. |
| 87 | // |
| 88 | //! ARGS: desctructor - Procedure to be called to destroy a data value |
| 89 | //+ associated with the key when the thread |
| 90 | //+ terminates. |
| 91 | // ----------------------------------------------------------------------- |
| 92 | vjThreadKeyWin32 (vjBaseThreadFunctor* destructor) |
| 93 | : mKeyID(0xffffffff), mDestroyData(NULL) |
| 94 | { |
| 95 | mDestroyData = destructor; |
| 96 | keycreate(); |
| 97 | } |
| 98 | |
| 99 | // ----------------------------------------------------------------------- |
| 100 | //: Destructor. |
| 101 | // ----------------------------------------------------------------------- |
| 102 | ~vjThreadKeyWin32 (void) { |
| 103 | keyfree(); |
| 104 | } |
| 105 | |
| 106 | // ----------------------------------------------------------------------- |
| 107 | //: Allocate a <keyp> that is used to identify data that is specific to |
| 108 | //+ each thread in the process, is global to all threads in the process |
| 109 | //+ and is destroyed using the spcefied destructor function that takes a |
| 110 | //+ single argument. |
| 111 | // |
| 112 | //! PRE: None. |
| 113 | //! POST: A key is created and is associated with the specified |
| 114 | //+ destructor function and argument. |
| 115 | // |
| 116 | //! ARGS: dest_func - The destructor function for the key. This uses |
| 117 | //+ the functor data structure. |
| 118 | //! ARGS: arg - Argument to be passed to destructor (optional). |
| 119 | // |
| 120 | //! RETURNS: 0 - Successful completion |
| 121 | //! RETURNS: -1 - Error |
| 122 | // |
| 123 | //! NOTE: Use this routine to construct the key destructor function if |
| 124 | //+ it requires arguments. Otherwise, use the two-argument |
| 125 | //+ version of keycreate(). |
| 126 | // ----------------------------------------------------------------------- |
| 127 | inline int |
| 128 | keycreate (vj_thread_func_t destructor, void* arg = 0) { |
| 129 | if ( mDestroyData != NULL ) { |
| 130 | delete mDestroyData; |
| 131 | } |
| 132 | |
| 133 | mDestroyData = new vjThreadNonMemberFunctor(destructor, arg); |
| 134 | return keycreate(); |
| 135 | } |
| 136 | |
| 137 | // ----------------------------------------------------------------------- |
| 138 | //: Allocates a <keyp> that is used to identify data that is specific to |
| 139 | //+ each thread in the process, is global to all threads in the process |
| 140 | //+ and is destroyed by the specified destructor function. |
| 141 | // |
| 142 | //! PRE: None. |
| 143 | //! POST: A key is created and is associated with the specified |
| 144 | //+ destructor function. |
| 145 | // |
| 146 | //! ARGS: desctructor - Procedure to be called to destroy a data value |
| 147 | //+ associated with the key when the thread |
| 148 | //+ terminates. |
| 149 | // |
| 150 | //! RETURNS: 0 - Successful completion |
| 151 | //! RETURNS: -1 - Error |
| 152 | // ----------------------------------------------------------------------- |
| 153 | inline int |
| 154 | keycreate (vjBaseThreadFunctor* destructor) { |
| 155 | mDestroyData = destructor; |
| 156 | return keycreate(); |
| 157 | } |
| 158 | |
| 159 | // ----------------------------------------------------------------------- |
| 160 | //: Free up the key so that other threads may reuse it. |
| 161 | // |
| 162 | //! PRE: The specified key must have been properly created using the |
| 163 | //+ keycreate() member function. |
| 164 | //! POST: The specified key is destroyed using the destructor function |
| 165 | //+ previously associated with it, and its resources are freed. |
| 166 | // |
| 167 | //! RETURNS: 0 - Successful completion |
| 168 | //! RETURNS: -1 - Error |
| 169 | // ----------------------------------------------------------------------- |
| 170 | inline int |
| 171 | keyfree (void) { |
| 172 | int retval; |
| 173 | |
| 174 | if ( mDestroyData != NULL ) { |
| 175 | (*mDestroyData)(); |
| 176 | } |
| 177 | |
| 178 | retval = 0; |
| 179 | |
| 180 | if ( mKeyID != 0xffffffff ) { |
| 181 | if ( ! TlsFree(mKeyID) ) { |
| 182 | perror("Could not free thread local storage"); |
| 183 | retval = -1; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return retval; |
| 188 | } |
| 189 | |
| 190 | // ----------------------------------------------------------------------- |
| 191 | //: Bind value to the thread-specific data key, <key>, for the calling |
| 192 | //+ thread. |
| 193 | // |
| 194 | //! PRE: The specified key must have been properly created using the |
| 195 | //+ keycreate() member function. |
| 196 | //! POST: The specified value is associated with the key for the calling |
| 197 | //+ thread. |
| 198 | // |
| 199 | //! ARGS: value - Address containing data to be associated with the |
| 200 | //+ specified key for the current thread. |
| 201 | // |
| 202 | //! RETURNS: 0 - Successful completion |
| 203 | //! RETURNS: -1 - Error |
| 204 | // ----------------------------------------------------------------------- |
| 205 | inline int |
| 206 | setspecific (void* value) { |
| 207 | int retval; |
| 208 | |
| 209 | retval = 0; |
| 210 | if ( ! TlsSetValue(mKeyID, value) ) { |
| 211 | perror("Could not set value for thread local storage"); |
| 212 | retval = -1; |
| 213 | } |
| 214 | |
| 215 | return retval; |
| 216 | } |
| 217 | |
| 218 | // ----------------------------------------------------------------------- |
| 219 | //: Stores the current value bound to <key> for the calling thread into |
| 220 | //+ the location pointed to by <valuep>. |
| 221 | // |
| 222 | //! PRE: The specified key must have been properly created using the |
| 223 | //+ keycreate() member function. |
| 224 | //! POST: The value associated with the key is obtained and stored in the |
| 225 | //+ pointer valuep so that the caller may work with it. |
| 226 | // |
| 227 | //! ARGS: valuep - Address of the current data value associated with the |
| 228 | //+ key. |
| 229 | // |
| 230 | //! RETURNS: 0 - Successful completion |
| 231 | //! RETURNS: -1 - Error |
| 232 | // ----------------------------------------------------------------------- |
| 233 | inline int |
| 234 | getspecific (void** valuep) { |
| 235 | *valuep = TlsGetValue(mKeyID); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | private: |
| 241 | // ----------------------------------------------------------------------- |
| 242 | //: Allocate a <keyp> that is used to identify data that is specific to |
| 243 | //+ each thread in the process, is global to all threads in the process. |
| 244 | // ----------------------------------------------------------------------- |
| 245 | inline int |
| 246 | keycreate (void) { |
| 247 | int retval; |
| 248 | |
| 249 | retval = 0; |
| 250 | mKeyID = TlsAlloc(); |
| 251 | |
| 252 | if ( mKeyID == 0xffffffff ) { |
| 253 | perror("Could not create thread local storage id"); |
| 254 | retval = -1; |
| 255 | } |
| 256 | |
| 257 | return retval; |
| 258 | } |
| 259 | |
| 260 | DWORD mKeyID; //: Thread local storage ID |
| 261 | vjBaseThreadFunctor* mDestroyData; //: Destructor for TLS data |
| 262 | }; |
| 263 | |
| 264 | #endif /* _VJ_THREAD_KEY_WIN_32_H_ */ |
Note: See TracBrowser for help on using the browser.
