| 1 |
/* |
| 2 |
* tclThread.c -- |
| 3 |
* |
| 4 |
* This file implements Platform independent thread operations. Most of |
| 5 |
* the real work is done in the platform dependent files. |
| 6 |
* |
| 7 |
* Copyright (c) 1998 by Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 2008 by George Peter Staplin |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclInt.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* There are three classes of synchronization objects: mutexes, thread data |
| 18 |
* keys, and condition variables. The following are used to record the memory |
| 19 |
* used for these objects so they can be finalized. |
| 20 |
* |
| 21 |
* These statics are guarded by the mutex in the caller of |
| 22 |
* TclRememberThreadData, e.g., TclpThreadDataKeyInit |
| 23 |
*/ |
| 24 |
|
| 25 |
typedef struct { |
| 26 |
int num; /* Number of objects remembered */ |
| 27 |
int max; /* Max size of the array */ |
| 28 |
void **list; /* List of pointers */ |
| 29 |
} SyncObjRecord; |
| 30 |
|
| 31 |
static SyncObjRecord keyRecord = {0, 0, NULL}; |
| 32 |
static SyncObjRecord mutexRecord = {0, 0, NULL}; |
| 33 |
static SyncObjRecord condRecord = {0, 0, NULL}; |
| 34 |
|
| 35 |
/* |
| 36 |
* Prototypes of functions used only in this file. |
| 37 |
*/ |
| 38 |
|
| 39 |
static void ForgetSyncObject(void *objPtr, SyncObjRecord *recPtr); |
| 40 |
static void RememberSyncObject(void *objPtr, |
| 41 |
SyncObjRecord *recPtr); |
| 42 |
|
| 43 |
/* |
| 44 |
* Several functions are #defined to nothing in tcl.h if TCL_THREADS is not |
| 45 |
* specified. Here we undo that so the functions are defined in the stubs |
| 46 |
* table. |
| 47 |
*/ |
| 48 |
|
| 49 |
#ifndef TCL_THREADS |
| 50 |
#undef Tcl_MutexLock |
| 51 |
#undef Tcl_MutexUnlock |
| 52 |
#undef Tcl_MutexFinalize |
| 53 |
#undef Tcl_ConditionNotify |
| 54 |
#undef Tcl_ConditionWait |
| 55 |
#undef Tcl_ConditionFinalize |
| 56 |
#endif |
| 57 |
|
| 58 |
/* |
| 59 |
*---------------------------------------------------------------------- |
| 60 |
* |
| 61 |
* Tcl_GetThreadData -- |
| 62 |
* |
| 63 |
* This function allocates and initializes a chunk of thread local |
| 64 |
* storage. |
| 65 |
* |
| 66 |
* Results: |
| 67 |
* A thread-specific pointer to the data structure. |
| 68 |
* |
| 69 |
* Side effects: |
| 70 |
* Will allocate memory the first time this thread calls for this chunk |
| 71 |
* of storage. |
| 72 |
* |
| 73 |
*---------------------------------------------------------------------- |
| 74 |
*/ |
| 75 |
|
| 76 |
void * |
| 77 |
Tcl_GetThreadData( |
| 78 |
Tcl_ThreadDataKey *keyPtr, /* Identifier for the data chunk */ |
| 79 |
int size) /* Size of storage block */ |
| 80 |
{ |
| 81 |
void *result; |
| 82 |
#ifdef TCL_THREADS |
| 83 |
/* |
| 84 |
* Initialize the key for this thread. |
| 85 |
*/ |
| 86 |
|
| 87 |
result = TclThreadStorageKeyGet(keyPtr); |
| 88 |
|
| 89 |
if (result == NULL) { |
| 90 |
result = ckalloc(size); |
| 91 |
memset(result, 0, size); |
| 92 |
TclThreadStorageKeySet(keyPtr, result); |
| 93 |
} |
| 94 |
#else /* TCL_THREADS */ |
| 95 |
if (*keyPtr == NULL) { |
| 96 |
result = ckalloc(size); |
| 97 |
memset(result, 0, size); |
| 98 |
*keyPtr = result; |
| 99 |
RememberSyncObject(keyPtr, &keyRecord); |
| 100 |
} else { |
| 101 |
result = *keyPtr; |
| 102 |
} |
| 103 |
#endif /* TCL_THREADS */ |
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
*---------------------------------------------------------------------- |
| 109 |
* |
| 110 |
* TclThreadDataKeyGet -- |
| 111 |
* |
| 112 |
* This function returns a pointer to a block of thread local storage. |
| 113 |
* |
| 114 |
* Results: |
| 115 |
* A thread-specific pointer to the data structure, or NULL if the memory |
| 116 |
* has not been assigned to this key for this thread. |
| 117 |
* |
| 118 |
* Side effects: |
| 119 |
* None. |
| 120 |
* |
| 121 |
*---------------------------------------------------------------------- |
| 122 |
*/ |
| 123 |
|
| 124 |
void * |
| 125 |
TclThreadDataKeyGet( |
| 126 |
Tcl_ThreadDataKey *keyPtr) /* Identifier for the data chunk. */ |
| 127 |
|
| 128 |
{ |
| 129 |
#ifdef TCL_THREADS |
| 130 |
return TclThreadStorageKeyGet(keyPtr); |
| 131 |
#else /* TCL_THREADS */ |
| 132 |
return *keyPtr; |
| 133 |
#endif /* TCL_THREADS */ |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
*---------------------------------------------------------------------- |
| 138 |
* |
| 139 |
* RememberSyncObject |
| 140 |
* |
| 141 |
* Keep a list of (mutexes/condition variable/data key) used during |
| 142 |
* finalization. |
| 143 |
* |
| 144 |
* Assume global lock is held. |
| 145 |
* |
| 146 |
* Results: |
| 147 |
* None. |
| 148 |
* |
| 149 |
* Side effects: |
| 150 |
* Add to the appropriate list. |
| 151 |
* |
| 152 |
*---------------------------------------------------------------------- |
| 153 |
*/ |
| 154 |
|
| 155 |
static void |
| 156 |
RememberSyncObject( |
| 157 |
void *objPtr, /* Pointer to sync object */ |
| 158 |
SyncObjRecord *recPtr) /* Record of sync objects */ |
| 159 |
{ |
| 160 |
void **newList; |
| 161 |
int i, j; |
| 162 |
|
| 163 |
|
| 164 |
/* |
| 165 |
* Reuse any free slot in the list. |
| 166 |
*/ |
| 167 |
|
| 168 |
for (i=0 ; i < recPtr->num ; ++i) { |
| 169 |
if (recPtr->list[i] == NULL) { |
| 170 |
recPtr->list[i] = objPtr; |
| 171 |
return; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* Grow the list of pointers if necessary, copying only non-NULL |
| 177 |
* pointers to the new list. |
| 178 |
*/ |
| 179 |
|
| 180 |
if (recPtr->num >= recPtr->max) { |
| 181 |
recPtr->max += 8; |
| 182 |
newList = ckalloc(recPtr->max * sizeof(void *)); |
| 183 |
for (i=0,j=0 ; i<recPtr->num ; i++) { |
| 184 |
if (recPtr->list[i] != NULL) { |
| 185 |
newList[j++] = recPtr->list[i]; |
| 186 |
} |
| 187 |
} |
| 188 |
if (recPtr->list != NULL) { |
| 189 |
ckfree(recPtr->list); |
| 190 |
} |
| 191 |
recPtr->list = newList; |
| 192 |
recPtr->num = j; |
| 193 |
} |
| 194 |
|
| 195 |
recPtr->list[recPtr->num] = objPtr; |
| 196 |
recPtr->num++; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
*---------------------------------------------------------------------- |
| 201 |
* |
| 202 |
* ForgetSyncObject |
| 203 |
* |
| 204 |
* Remove a single object from the list. |
| 205 |
* Assume global lock is held. |
| 206 |
* |
| 207 |
* Results: |
| 208 |
* None. |
| 209 |
* |
| 210 |
* Side effects: |
| 211 |
* Remove from the appropriate list. |
| 212 |
* |
| 213 |
*---------------------------------------------------------------------- |
| 214 |
*/ |
| 215 |
|
| 216 |
static void |
| 217 |
ForgetSyncObject( |
| 218 |
void *objPtr, /* Pointer to sync object */ |
| 219 |
SyncObjRecord *recPtr) /* Record of sync objects */ |
| 220 |
{ |
| 221 |
int i; |
| 222 |
|
| 223 |
for (i=0 ; i<recPtr->num ; i++) { |
| 224 |
if (objPtr == recPtr->list[i]) { |
| 225 |
recPtr->list[i] = NULL; |
| 226 |
return; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/* |
| 232 |
*---------------------------------------------------------------------- |
| 233 |
* |
| 234 |
* TclRememberMutex |
| 235 |
* |
| 236 |
* Keep a list of mutexes used during finalization. |
| 237 |
* Assume global lock is held. |
| 238 |
* |
| 239 |
* Results: |
| 240 |
* None. |
| 241 |
* |
| 242 |
* Side effects: |
| 243 |
* Add to the mutex list. |
| 244 |
* |
| 245 |
*---------------------------------------------------------------------- |
| 246 |
*/ |
| 247 |
|
| 248 |
void |
| 249 |
TclRememberMutex( |
| 250 |
Tcl_Mutex *mutexPtr) |
| 251 |
{ |
| 252 |
RememberSyncObject(mutexPtr, &mutexRecord); |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
*---------------------------------------------------------------------- |
| 257 |
* |
| 258 |
* Tcl_MutexFinalize -- |
| 259 |
* |
| 260 |
* Finalize a single mutex and remove it from the list of remembered |
| 261 |
* objects. |
| 262 |
* |
| 263 |
* Results: |
| 264 |
* None. |
| 265 |
* |
| 266 |
* Side effects: |
| 267 |
* Remove the mutex from the list. |
| 268 |
* |
| 269 |
*---------------------------------------------------------------------- |
| 270 |
*/ |
| 271 |
|
| 272 |
void |
| 273 |
Tcl_MutexFinalize( |
| 274 |
Tcl_Mutex *mutexPtr) |
| 275 |
{ |
| 276 |
#ifdef TCL_THREADS |
| 277 |
TclpFinalizeMutex(mutexPtr); |
| 278 |
#endif |
| 279 |
TclpGlobalLock(); |
| 280 |
ForgetSyncObject(mutexPtr, &mutexRecord); |
| 281 |
TclpGlobalUnlock(); |
| 282 |
} |
| 283 |
|
| 284 |
/* |
| 285 |
*---------------------------------------------------------------------- |
| 286 |
* |
| 287 |
* TclRememberCondition |
| 288 |
* |
| 289 |
* Keep a list of condition variables used during finalization. |
| 290 |
* Assume global lock is held. |
| 291 |
* |
| 292 |
* Results: |
| 293 |
* None. |
| 294 |
* |
| 295 |
* Side effects: |
| 296 |
* Add to the condition variable list. |
| 297 |
* |
| 298 |
*---------------------------------------------------------------------- |
| 299 |
*/ |
| 300 |
|
| 301 |
void |
| 302 |
TclRememberCondition( |
| 303 |
Tcl_Condition *condPtr) |
| 304 |
{ |
| 305 |
RememberSyncObject(condPtr, &condRecord); |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
*---------------------------------------------------------------------- |
| 310 |
* |
| 311 |
* Tcl_ConditionFinalize -- |
| 312 |
* |
| 313 |
* Finalize a single condition variable and remove it from the list of |
| 314 |
* remembered objects. |
| 315 |
* |
| 316 |
* Results: |
| 317 |
* None. |
| 318 |
* |
| 319 |
* Side effects: |
| 320 |
* Remove the condition variable from the list. |
| 321 |
* |
| 322 |
*---------------------------------------------------------------------- |
| 323 |
*/ |
| 324 |
|
| 325 |
void |
| 326 |
Tcl_ConditionFinalize( |
| 327 |
Tcl_Condition *condPtr) |
| 328 |
{ |
| 329 |
#ifdef TCL_THREADS |
| 330 |
TclpFinalizeCondition(condPtr); |
| 331 |
#endif |
| 332 |
TclpGlobalLock(); |
| 333 |
ForgetSyncObject(condPtr, &condRecord); |
| 334 |
TclpGlobalUnlock(); |
| 335 |
} |
| 336 |
|
| 337 |
/* |
| 338 |
*---------------------------------------------------------------------- |
| 339 |
* |
| 340 |
* TclFinalizeThreadData -- |
| 341 |
* |
| 342 |
* This function cleans up the thread-local storage. Secondary, it cleans |
| 343 |
* thread alloc cache. |
| 344 |
* This is called once for each thread before thread exits. |
| 345 |
* |
| 346 |
* Results: |
| 347 |
* None. |
| 348 |
* |
| 349 |
* Side effects: |
| 350 |
* Frees up all thread local storage. |
| 351 |
* |
| 352 |
*---------------------------------------------------------------------- |
| 353 |
*/ |
| 354 |
|
| 355 |
void |
| 356 |
TclFinalizeThreadData(int quick) |
| 357 |
{ |
| 358 |
TclFinalizeThreadDataThread(); |
| 359 |
#if defined(TCL_THREADS) && defined(USE_THREAD_ALLOC) |
| 360 |
if (!quick) { |
| 361 |
/* |
| 362 |
* Quick exit principle makes it useless to terminate allocators |
| 363 |
*/ |
| 364 |
TclFinalizeThreadAllocThread(); |
| 365 |
} |
| 366 |
#endif |
| 367 |
} |
| 368 |
|
| 369 |
/* |
| 370 |
*---------------------------------------------------------------------- |
| 371 |
* |
| 372 |
* TclFinalizeSynchronization -- |
| 373 |
* |
| 374 |
* This function cleans up all synchronization objects: mutexes, |
| 375 |
* condition variables, and thread-local storage. |
| 376 |
* |
| 377 |
* Results: |
| 378 |
* None. |
| 379 |
* |
| 380 |
* Side effects: |
| 381 |
* Frees up the memory. |
| 382 |
* |
| 383 |
*---------------------------------------------------------------------- |
| 384 |
*/ |
| 385 |
|
| 386 |
void |
| 387 |
TclFinalizeSynchronization(void) |
| 388 |
{ |
| 389 |
int i; |
| 390 |
void *blockPtr; |
| 391 |
Tcl_ThreadDataKey *keyPtr; |
| 392 |
#ifdef TCL_THREADS |
| 393 |
Tcl_Mutex *mutexPtr; |
| 394 |
Tcl_Condition *condPtr; |
| 395 |
|
| 396 |
TclpGlobalLock(); |
| 397 |
#endif |
| 398 |
|
| 399 |
/* |
| 400 |
* If we're running unthreaded, the TSD blocks are simply stored inside |
| 401 |
* their thread data keys. Free them here. |
| 402 |
*/ |
| 403 |
|
| 404 |
if (keyRecord.list != NULL) { |
| 405 |
for (i=0 ; i<keyRecord.num ; i++) { |
| 406 |
keyPtr = (Tcl_ThreadDataKey *) keyRecord.list[i]; |
| 407 |
blockPtr = *keyPtr; |
| 408 |
ckfree(blockPtr); |
| 409 |
} |
| 410 |
ckfree(keyRecord.list); |
| 411 |
keyRecord.list = NULL; |
| 412 |
} |
| 413 |
keyRecord.max = 0; |
| 414 |
keyRecord.num = 0; |
| 415 |
|
| 416 |
#ifdef TCL_THREADS |
| 417 |
/* |
| 418 |
* Call thread storage global cleanup. |
| 419 |
*/ |
| 420 |
|
| 421 |
TclFinalizeThreadStorage(); |
| 422 |
|
| 423 |
for (i=0 ; i<mutexRecord.num ; i++) { |
| 424 |
mutexPtr = (Tcl_Mutex *)mutexRecord.list[i]; |
| 425 |
if (mutexPtr != NULL) { |
| 426 |
TclpFinalizeMutex(mutexPtr); |
| 427 |
} |
| 428 |
} |
| 429 |
if (mutexRecord.list != NULL) { |
| 430 |
ckfree(mutexRecord.list); |
| 431 |
mutexRecord.list = NULL; |
| 432 |
} |
| 433 |
mutexRecord.max = 0; |
| 434 |
mutexRecord.num = 0; |
| 435 |
|
| 436 |
for (i=0 ; i<condRecord.num ; i++) { |
| 437 |
condPtr = (Tcl_Condition *) condRecord.list[i]; |
| 438 |
if (condPtr != NULL) { |
| 439 |
TclpFinalizeCondition(condPtr); |
| 440 |
} |
| 441 |
} |
| 442 |
if (condRecord.list != NULL) { |
| 443 |
ckfree(condRecord.list); |
| 444 |
condRecord.list = NULL; |
| 445 |
} |
| 446 |
condRecord.max = 0; |
| 447 |
condRecord.num = 0; |
| 448 |
|
| 449 |
TclpGlobalUnlock(); |
| 450 |
#endif /* TCL_THREADS */ |
| 451 |
} |
| 452 |
|
| 453 |
/* |
| 454 |
*---------------------------------------------------------------------- |
| 455 |
* |
| 456 |
* Tcl_ExitThread -- |
| 457 |
* |
| 458 |
* This function is called to terminate the current thread. This should |
| 459 |
* be used by extensions that create threads with additional interpreters |
| 460 |
* in them. |
| 461 |
* |
| 462 |
* Results: |
| 463 |
* None. |
| 464 |
* |
| 465 |
* Side effects: |
| 466 |
* All thread exit handlers are invoked, then the thread dies. |
| 467 |
* |
| 468 |
*---------------------------------------------------------------------- |
| 469 |
*/ |
| 470 |
|
| 471 |
TCL_NORETURN void |
| 472 |
Tcl_ExitThread( |
| 473 |
int status) |
| 474 |
{ |
| 475 |
Tcl_FinalizeThread(); |
| 476 |
#ifdef TCL_THREADS |
| 477 |
TclpThreadExit(status); |
| 478 |
#else |
| 479 |
exit(status); |
| 480 |
#endif |
| 481 |
} |
| 482 |
|
| 483 |
#ifndef TCL_THREADS |
| 484 |
|
| 485 |
/* |
| 486 |
*---------------------------------------------------------------------- |
| 487 |
* |
| 488 |
* Tcl_ConditionWait, et al. -- |
| 489 |
* |
| 490 |
* These noop functions are provided so the stub table does not have to |
| 491 |
* be conditionalized for threads. The real implementations of these |
| 492 |
* functions live in the platform specific files. |
| 493 |
* |
| 494 |
* Results: |
| 495 |
* None. |
| 496 |
* |
| 497 |
* Side effects: |
| 498 |
* None. |
| 499 |
* |
| 500 |
*---------------------------------------------------------------------- |
| 501 |
*/ |
| 502 |
|
| 503 |
#undef Tcl_ConditionWait |
| 504 |
void |
| 505 |
Tcl_ConditionWait( |
| 506 |
Tcl_Condition *condPtr, /* Really (pthread_cond_t **) */ |
| 507 |
Tcl_Mutex *mutexPtr, /* Really (pthread_mutex_t **) */ |
| 508 |
const Tcl_Time *timePtr) /* Timeout on waiting period */ |
| 509 |
{ |
| 510 |
} |
| 511 |
|
| 512 |
#undef Tcl_ConditionNotify |
| 513 |
void |
| 514 |
Tcl_ConditionNotify( |
| 515 |
Tcl_Condition *condPtr) |
| 516 |
{ |
| 517 |
} |
| 518 |
|
| 519 |
#undef Tcl_MutexLock |
| 520 |
void |
| 521 |
Tcl_MutexLock( |
| 522 |
Tcl_Mutex *mutexPtr) |
| 523 |
{ |
| 524 |
} |
| 525 |
|
| 526 |
#undef Tcl_MutexUnlock |
| 527 |
void |
| 528 |
Tcl_MutexUnlock( |
| 529 |
Tcl_Mutex *mutexPtr) |
| 530 |
{ |
| 531 |
} |
| 532 |
#endif /* !TCL_THREADS */ |
| 533 |
|
| 534 |
/* |
| 535 |
* Local Variables: |
| 536 |
* mode: c |
| 537 |
* c-basic-offset: 4 |
| 538 |
* fill-column: 78 |
| 539 |
* End: |
| 540 |
*/ |