| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "tclInt.h" |
| 16 |
#include "tclPort.h" |
| 17 |
|
| 18 |
#ifdef TCL_THREADS |
| 19 |
|
| 20 |
#include "pthread.h" |
| 21 |
|
| 22 |
typedef struct ThreadSpecificData { |
| 23 |
char nabuf[16]; |
| 24 |
struct tm gtbuf; |
| 25 |
struct tm ltbuf; |
| 26 |
struct { |
| 27 |
Tcl_DirEntry ent; |
| 28 |
char name[MAXNAMLEN+1]; |
| 29 |
} rdbuf; |
| 30 |
} ThreadSpecificData; |
| 31 |
|
| 32 |
static Tcl_ThreadDataKey dataKey; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static pthread_mutex_t masterLock = PTHREAD_MUTEX_INITIALIZER; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static pthread_mutex_t initLock = PTHREAD_MUTEX_INITIALIZER; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
static pthread_mutex_t allocLock = PTHREAD_MUTEX_INITIALIZER; |
| 56 |
static pthread_mutex_t *allocLockPtr = &allocLock; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#define MASTER_LOCK pthread_mutex_lock(&masterLock) |
| 63 |
#define MASTER_UNLOCK pthread_mutex_unlock(&masterLock) |
| 64 |
|
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
int |
| 86 |
Tcl_CreateThread(idPtr, proc, clientData, stackSize, flags) |
| 87 |
Tcl_ThreadId *idPtr; |
| 88 |
Tcl_ThreadCreateProc proc; |
| 89 |
ClientData clientData; |
| 90 |
int stackSize; |
| 91 |
int flags; |
| 92 |
|
| 93 |
{ |
| 94 |
#ifdef TCL_THREADS |
| 95 |
pthread_attr_t attr; |
| 96 |
int result; |
| 97 |
|
| 98 |
pthread_attr_init(&attr); |
| 99 |
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); |
| 100 |
|
| 101 |
#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE |
| 102 |
if (stackSize != TCL_THREAD_STACK_DEFAULT) { |
| 103 |
pthread_attr_setstacksize(&attr, (size_t) stackSize); |
| 104 |
#ifdef TCL_THREAD_STACK_MIN |
| 105 |
} else { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
size_t size; |
| 119 |
result = pthread_attr_getstacksize(&attr, &size); |
| 120 |
if (!result && (size < TCL_THREAD_STACK_MIN)) { |
| 121 |
pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN); |
| 122 |
} |
| 123 |
#endif |
| 124 |
} |
| 125 |
#endif |
| 126 |
if (! (flags & TCL_THREAD_JOINABLE)) { |
| 127 |
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
if (pthread_create((pthread_t *)idPtr, &attr, |
| 132 |
(void * (*)())proc, (void *)clientData) && |
| 133 |
pthread_create((pthread_t *)idPtr, NULL, |
| 134 |
(void * (*)())proc, (void *)clientData)) { |
| 135 |
result = TCL_ERROR; |
| 136 |
} else { |
| 137 |
result = TCL_OK; |
| 138 |
} |
| 139 |
pthread_attr_destroy(&attr); |
| 140 |
return result; |
| 141 |
#else |
| 142 |
return TCL_ERROR; |
| 143 |
#endif |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
int |
| 164 |
Tcl_JoinThread(threadId, state) |
| 165 |
Tcl_ThreadId threadId; |
| 166 |
int* state; |
| 167 |
|
| 168 |
|
| 169 |
{ |
| 170 |
#ifdef TCL_THREADS |
| 171 |
int result; |
| 172 |
|
| 173 |
result = pthread_join ((pthread_t) threadId, (VOID**) state); |
| 174 |
return (result == 0) ? TCL_OK : TCL_ERROR; |
| 175 |
#else |
| 176 |
return TCL_ERROR; |
| 177 |
#endif |
| 178 |
} |
| 179 |
|
| 180 |
#ifdef TCL_THREADS |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
void |
| 198 |
TclpThreadExit(status) |
| 199 |
int status; |
| 200 |
{ |
| 201 |
pthread_exit((VOID *)status); |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
Tcl_ThreadId |
| 222 |
Tcl_GetCurrentThread() |
| 223 |
{ |
| 224 |
#ifdef TCL_THREADS |
| 225 |
return (Tcl_ThreadId) pthread_self(); |
| 226 |
#else |
| 227 |
return (Tcl_ThreadId) 0; |
| 228 |
#endif |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
void |
| 252 |
TclpInitLock() |
| 253 |
{ |
| 254 |
#ifdef TCL_THREADS |
| 255 |
pthread_mutex_lock(&initLock); |
| 256 |
#endif |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
void |
| 278 |
TclpInitUnlock() |
| 279 |
{ |
| 280 |
#ifdef TCL_THREADS |
| 281 |
pthread_mutex_unlock(&initLock); |
| 282 |
#endif |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
void |
| 308 |
TclpMasterLock() |
| 309 |
{ |
| 310 |
#ifdef TCL_THREADS |
| 311 |
pthread_mutex_lock(&masterLock); |
| 312 |
#endif |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
void |
| 334 |
TclpMasterUnlock() |
| 335 |
{ |
| 336 |
#ifdef TCL_THREADS |
| 337 |
pthread_mutex_unlock(&masterLock); |
| 338 |
#endif |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
Tcl_Mutex * |
| 362 |
Tcl_GetAllocMutex() |
| 363 |
{ |
| 364 |
#ifdef TCL_THREADS |
| 365 |
return (Tcl_Mutex *)&allocLockPtr; |
| 366 |
#else |
| 367 |
return NULL; |
| 368 |
#endif |
| 369 |
} |
| 370 |
|
| 371 |
#ifdef TCL_THREADS |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
void |
| 395 |
Tcl_MutexLock(mutexPtr) |
| 396 |
Tcl_Mutex *mutexPtr; |
| 397 |
{ |
| 398 |
pthread_mutex_t *pmutexPtr; |
| 399 |
if (*mutexPtr == NULL) { |
| 400 |
MASTER_LOCK; |
| 401 |
if (*mutexPtr == NULL) { |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
pmutexPtr = (pthread_mutex_t *)ckalloc(sizeof(pthread_mutex_t)); |
| 407 |
pthread_mutex_init(pmutexPtr, NULL); |
| 408 |
*mutexPtr = (Tcl_Mutex)pmutexPtr; |
| 409 |
TclRememberMutex(mutexPtr); |
| 410 |
} |
| 411 |
MASTER_UNLOCK; |
| 412 |
} |
| 413 |
pmutexPtr = *((pthread_mutex_t **)mutexPtr); |
| 414 |
pthread_mutex_lock(pmutexPtr); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
void |
| 436 |
Tcl_MutexUnlock(mutexPtr) |
| 437 |
Tcl_Mutex *mutexPtr; |
| 438 |
{ |
| 439 |
pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **)mutexPtr; |
| 440 |
pthread_mutex_unlock(pmutexPtr); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
void |
| 464 |
TclpFinalizeMutex(mutexPtr) |
| 465 |
Tcl_Mutex *mutexPtr; |
| 466 |
{ |
| 467 |
pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **)mutexPtr; |
| 468 |
if (pmutexPtr != NULL) { |
| 469 |
ckfree((char *)pmutexPtr); |
| 470 |
*mutexPtr = NULL; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
void |
| 501 |
TclpThreadDataKeyInit(keyPtr) |
| 502 |
Tcl_ThreadDataKey *keyPtr; |
| 503 |
|
| 504 |
{ |
| 505 |
pthread_key_t *pkeyPtr; |
| 506 |
|
| 507 |
MASTER_LOCK; |
| 508 |
if (*keyPtr == NULL) { |
| 509 |
pkeyPtr = (pthread_key_t *)ckalloc(sizeof(pthread_key_t)); |
| 510 |
pthread_key_create(pkeyPtr, NULL); |
| 511 |
*keyPtr = (Tcl_ThreadDataKey)pkeyPtr; |
| 512 |
TclRememberDataKey(keyPtr); |
| 513 |
} |
| 514 |
MASTER_UNLOCK; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
VOID * |
| 535 |
TclpThreadDataKeyGet(keyPtr) |
| 536 |
Tcl_ThreadDataKey *keyPtr; |
| 537 |
|
| 538 |
{ |
| 539 |
pthread_key_t *pkeyPtr = *(pthread_key_t **)keyPtr; |
| 540 |
if (pkeyPtr == NULL) { |
| 541 |
return NULL; |
| 542 |
} else { |
| 543 |
return (VOID *)pthread_getspecific(*pkeyPtr); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
void |
| 566 |
TclpThreadDataKeySet(keyPtr, data) |
| 567 |
Tcl_ThreadDataKey *keyPtr; |
| 568 |
|
| 569 |
VOID *data; |
| 570 |
{ |
| 571 |
pthread_key_t *pkeyPtr = *(pthread_key_t **)keyPtr; |
| 572 |
pthread_setspecific(*pkeyPtr, data); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
void |
| 593 |
TclpFinalizeThreadData(keyPtr) |
| 594 |
Tcl_ThreadDataKey *keyPtr; |
| 595 |
{ |
| 596 |
VOID *result; |
| 597 |
pthread_key_t *pkeyPtr; |
| 598 |
|
| 599 |
if (*keyPtr != NULL) { |
| 600 |
pkeyPtr = *(pthread_key_t **)keyPtr; |
| 601 |
result = (VOID *)pthread_getspecific(*pkeyPtr); |
| 602 |
if (result != NULL) { |
| 603 |
ckfree((char *)result); |
| 604 |
pthread_setspecific(*pkeyPtr, (void *)NULL); |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
void |
| 630 |
TclpFinalizeThreadDataKey(keyPtr) |
| 631 |
Tcl_ThreadDataKey *keyPtr; |
| 632 |
{ |
| 633 |
pthread_key_t *pkeyPtr; |
| 634 |
if (*keyPtr != NULL) { |
| 635 |
pkeyPtr = *(pthread_key_t **)keyPtr; |
| 636 |
pthread_key_delete(*pkeyPtr); |
| 637 |
ckfree((char *)pkeyPtr); |
| 638 |
*keyPtr = NULL; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
void |
| 666 |
Tcl_ConditionWait(condPtr, mutexPtr, timePtr) |
| 667 |
Tcl_Condition *condPtr; |
| 668 |
Tcl_Mutex *mutexPtr; |
| 669 |
Tcl_Time *timePtr; |
| 670 |
{ |
| 671 |
pthread_cond_t *pcondPtr; |
| 672 |
pthread_mutex_t *pmutexPtr; |
| 673 |
struct timespec ptime; |
| 674 |
|
| 675 |
if (*condPtr == NULL) { |
| 676 |
MASTER_LOCK; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
if (*condPtr == NULL) { |
| 684 |
pcondPtr = (pthread_cond_t *)ckalloc(sizeof(pthread_cond_t)); |
| 685 |
pthread_cond_init(pcondPtr, NULL); |
| 686 |
*condPtr = (Tcl_Condition)pcondPtr; |
| 687 |
TclRememberCondition(condPtr); |
| 688 |
} |
| 689 |
MASTER_UNLOCK; |
| 690 |
} |
| 691 |
pmutexPtr = *((pthread_mutex_t **)mutexPtr); |
| 692 |
pcondPtr = *((pthread_cond_t **)condPtr); |
| 693 |
if (timePtr == NULL) { |
| 694 |
pthread_cond_wait(pcondPtr, pmutexPtr); |
| 695 |
} else { |
| 696 |
Tcl_Time now; |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
Tcl_GetTime(&now); |
| 704 |
ptime.tv_sec = timePtr->sec + now.sec + |
| 705 |
(timePtr->usec + now.usec) / 1000000; |
| 706 |
ptime.tv_nsec = 1000 * ((timePtr->usec + now.usec) % 1000000); |
| 707 |
pthread_cond_timedwait(pcondPtr, pmutexPtr, &ptime); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
void |
| 731 |
Tcl_ConditionNotify(condPtr) |
| 732 |
Tcl_Condition *condPtr; |
| 733 |
{ |
| 734 |
pthread_cond_t *pcondPtr = *((pthread_cond_t **)condPtr); |
| 735 |
if (pcondPtr != NULL) { |
| 736 |
pthread_cond_broadcast(pcondPtr); |
| 737 |
} else { |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
void |
| 765 |
TclpFinalizeCondition(condPtr) |
| 766 |
Tcl_Condition *condPtr; |
| 767 |
{ |
| 768 |
pthread_cond_t *pcondPtr = *(pthread_cond_t **)condPtr; |
| 769 |
if (pcondPtr != NULL) { |
| 770 |
pthread_cond_destroy(pcondPtr); |
| 771 |
ckfree((char *)pcondPtr); |
| 772 |
*condPtr = NULL; |
| 773 |
} |
| 774 |
} |
| 775 |
#endif |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
#if defined(TCL_THREADS) && !defined(HAVE_READDIR_R) |
| 795 |
TCL_DECLARE_MUTEX( rdMutex ) |
| 796 |
#undef readdir |
| 797 |
#endif |
| 798 |
|
| 799 |
Tcl_DirEntry * |
| 800 |
TclpReaddir(DIR * dir) |
| 801 |
{ |
| 802 |
Tcl_DirEntry *ent; |
| 803 |
#ifdef TCL_THREADS |
| 804 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 805 |
|
| 806 |
#ifdef HAVE_READDIR_R |
| 807 |
ent = &tsdPtr->rdbuf.ent; |
| 808 |
if (TclOSreaddir_r(dir, ent, &ent) != 0) { |
| 809 |
ent = NULL; |
| 810 |
} |
| 811 |
|
| 812 |
#else |
| 813 |
|
| 814 |
Tcl_MutexLock(&rdMutex); |
| 815 |
# ifdef HAVE_STRUCT_DIRENT64 |
| 816 |
ent = readdir64(dir); |
| 817 |
# else |
| 818 |
ent = readdir(dir); |
| 819 |
# endif |
| 820 |
if (ent != NULL) { |
| 821 |
memcpy((VOID *) &tsdPtr->rdbuf.ent, (VOID *) ent, |
| 822 |
sizeof(tsdPtr->rdbuf)); |
| 823 |
ent = &tsdPtr->rdbuf.ent; |
| 824 |
} |
| 825 |
Tcl_MutexUnlock(&rdMutex); |
| 826 |
|
| 827 |
#endif |
| 828 |
#else |
| 829 |
# ifdef HAVE_STRUCT_DIRENT64 |
| 830 |
ent = readdir64(dir); |
| 831 |
# else |
| 832 |
ent = readdir(dir); |
| 833 |
# endif |
| 834 |
#endif |
| 835 |
return ent; |
| 836 |
} |
| 837 |
|
| 838 |
#if defined(TCL_THREADS) && (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)) |
| 839 |
TCL_DECLARE_MUTEX( tmMutex ) |
| 840 |
#undef localtime |
| 841 |
#undef gmtime |
| 842 |
#endif |
| 843 |
|
| 844 |
struct tm * |
| 845 |
TclpLocaltime(time_t * clock) |
| 846 |
{ |
| 847 |
#ifdef TCL_THREADS |
| 848 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 849 |
|
| 850 |
#ifdef HAVE_LOCALTIME_R |
| 851 |
return localtime_r(clock, &tsdPtr->ltbuf); |
| 852 |
#else |
| 853 |
Tcl_MutexLock( &tmMutex ); |
| 854 |
memcpy( (VOID *) &tsdPtr->ltbuf, (VOID *) localtime( clock ), sizeof (struct tm) ); |
| 855 |
Tcl_MutexUnlock( &tmMutex ); |
| 856 |
return &tsdPtr->ltbuf; |
| 857 |
#endif |
| 858 |
#else |
| 859 |
return localtime(clock); |
| 860 |
#endif |
| 861 |
} |
| 862 |
|
| 863 |
struct tm * |
| 864 |
TclpGmtime(time_t * clock) |
| 865 |
{ |
| 866 |
#ifdef TCL_THREADS |
| 867 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 868 |
|
| 869 |
#ifdef HAVE_GMTIME_R |
| 870 |
return gmtime_r(clock, &tsdPtr->gtbuf); |
| 871 |
#else |
| 872 |
Tcl_MutexLock( &tmMutex ); |
| 873 |
memcpy( (VOID *) &tsdPtr->gtbuf, (VOID *) gmtime( clock ), sizeof (struct tm) ); |
| 874 |
Tcl_MutexUnlock( &tmMutex ); |
| 875 |
return &tsdPtr->gtbuf; |
| 876 |
#endif |
| 877 |
#else |
| 878 |
return gmtime(clock); |
| 879 |
#endif |
| 880 |
} |
| 881 |
|
| 882 |
char * |
| 883 |
TclpInetNtoa(struct in_addr addr) |
| 884 |
{ |
| 885 |
#ifdef TCL_THREADS |
| 886 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 887 |
union { |
| 888 |
unsigned long l; |
| 889 |
unsigned char b[4]; |
| 890 |
} u; |
| 891 |
|
| 892 |
u.l = (unsigned long) addr.s_addr; |
| 893 |
sprintf(tsdPtr->nabuf, "%u.%u.%u.%u", u.b[0], u.b[1], u.b[2], u.b[3]); |
| 894 |
return tsdPtr->nabuf; |
| 895 |
#else |
| 896 |
return inet_ntoa(addr); |
| 897 |
#endif |
| 898 |
} |
| 899 |
|
| 900 |
#ifdef TCL_THREADS |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
#ifdef USE_THREAD_ALLOC |
| 905 |
static int initialized = 0; |
| 906 |
static pthread_key_t key; |
| 907 |
static pthread_once_t once = PTHREAD_ONCE_INIT; |
| 908 |
|
| 909 |
Tcl_Mutex * |
| 910 |
TclpNewAllocMutex(void) |
| 911 |
{ |
| 912 |
struct lock { |
| 913 |
Tcl_Mutex tlock; |
| 914 |
pthread_mutex_t plock; |
| 915 |
} *lockPtr; |
| 916 |
|
| 917 |
lockPtr = malloc(sizeof(struct lock)); |
| 918 |
if (lockPtr == NULL) { |
| 919 |
panic("could not allocate lock"); |
| 920 |
} |
| 921 |
lockPtr->tlock = (Tcl_Mutex) &lockPtr->plock; |
| 922 |
pthread_mutex_init(&lockPtr->plock, NULL); |
| 923 |
return &lockPtr->tlock; |
| 924 |
} |
| 925 |
|
| 926 |
static void |
| 927 |
InitKey(void) |
| 928 |
{ |
| 929 |
extern void TclFreeAllocCache(void *); |
| 930 |
|
| 931 |
pthread_key_create(&key, TclFreeAllocCache); |
| 932 |
initialized = 1; |
| 933 |
} |
| 934 |
|
| 935 |
void * |
| 936 |
TclpGetAllocCache(void) |
| 937 |
{ |
| 938 |
if (!initialized) { |
| 939 |
pthread_once(&once, InitKey); |
| 940 |
} |
| 941 |
return pthread_getspecific(key); |
| 942 |
} |
| 943 |
|
| 944 |
void |
| 945 |
TclpSetAllocCache(void *arg) |
| 946 |
{ |
| 947 |
pthread_setspecific(key, arg); |
| 948 |
} |
| 949 |
|
| 950 |
#endif |
| 951 |
#endif |