| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#if 0 |
| 18 |
#include "tclInt.h" |
| 19 |
#include "tclPort.h" |
| 20 |
#else |
| 21 |
#include "tkPort.h" |
| 22 |
#include "Lang.h" |
| 23 |
#endif |
| 24 |
#include <signal.h> |
| 25 |
#ifdef _LANG |
| 26 |
#include <errno.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef TCL_EVENT_IMPLEMENT |
| 30 |
|
| 31 |
#ifndef _LANG |
| 32 |
extern TclStubs tclStubs; |
| 33 |
#else |
| 34 |
#include "tkEvent_f.h" |
| 35 |
#endif |
| 36 |
extern Tcl_NotifierProcs tclOriginalNotifier; |
| 37 |
|
| 38 |
#ifndef MASK_SIZE |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#ifdef HAVE_SYS_SELECT_H |
| 43 |
# include <sys/select.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#ifndef FD_SETSIZE |
| 51 |
# ifdef OPEN_MAX |
| 52 |
# define FD_SETSIZE OPEN_MAX |
| 53 |
# else |
| 54 |
# define FD_SETSIZE 256 |
| 55 |
# endif |
| 56 |
#endif |
| 57 |
#if !defined(howmany) |
| 58 |
# define howmany(x, y) (((x)+((y)-1))/(y)) |
| 59 |
#endif |
| 60 |
#ifndef NFDBITS |
| 61 |
# define NFDBITS NBBY*sizeof(fd_mask) |
| 62 |
#endif |
| 63 |
#define MASK_SIZE howmany(FD_SETSIZE, NFDBITS) |
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
typedef struct FileHandler { |
| 72 |
int fd; |
| 73 |
int mask; |
| 74 |
|
| 75 |
int readyMask; |
| 76 |
|
| 77 |
|
| 78 |
Tcl_FileProc *proc; |
| 79 |
|
| 80 |
ClientData clientData; |
| 81 |
struct FileHandler *nextPtr; |
| 82 |
} FileHandler; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
typedef struct FileHandlerEvent { |
| 90 |
Tcl_Event header; |
| 91 |
|
| 92 |
int fd; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
} FileHandlerEvent; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
typedef struct SelectMasks { |
| 106 |
fd_set readable; |
| 107 |
fd_set writable; |
| 108 |
fd_set exceptional; |
| 109 |
} SelectMasks; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
typedef struct ThreadSpecificData { |
| 118 |
FileHandler *firstFileHandlerPtr; |
| 119 |
|
| 120 |
|
| 121 |
SelectMasks checkMasks; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
SelectMasks readyMasks; |
| 126 |
|
| 127 |
|
| 128 |
int numFdBits; |
| 129 |
|
| 130 |
|
| 131 |
#ifdef TCL_THREADS |
| 132 |
int onList; |
| 133 |
unsigned int pollState; |
| 134 |
|
| 135 |
|
| 136 |
struct ThreadSpecificData *nextPtr, *prevPtr; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
Tcl_Condition waitCV; |
| 144 |
|
| 145 |
|
| 146 |
int eventReady; |
| 147 |
|
| 148 |
|
| 149 |
#endif |
| 150 |
} ThreadSpecificData; |
| 151 |
|
| 152 |
static Tcl_ThreadDataKey dataKey; |
| 153 |
|
| 154 |
#ifdef TCL_THREADS |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static int notifierCount = 0; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
static ThreadSpecificData *waitingListPtr = NULL; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static int triggerPipe = -1; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
TCL_DECLARE_MUTEX(notifierMutex) |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static Tcl_Condition notifierCV; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
#define POLL_WANT 0x1 |
| 215 |
#define POLL_DONE 0x2 |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static Tcl_ThreadId notifierThread; |
| 221 |
|
| 222 |
#endif |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
#ifdef TCL_THREADS |
| 229 |
static void NotifierThreadProc _ANSI_ARGS_((ClientData clientData)); |
| 230 |
#endif |
| 231 |
static int FileHandlerEventProc _ANSI_ARGS_((Tcl_Event *evPtr, |
| 232 |
int flags)); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
ClientData |
| 251 |
Tcl_InitNotifier() |
| 252 |
{ |
| 253 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 254 |
|
| 255 |
#ifdef TCL_THREADS |
| 256 |
tsdPtr->eventReady = 0; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
Tcl_MutexLock(¬ifierMutex); |
| 263 |
if (notifierCount == 0) { |
| 264 |
if (Tcl_CreateThread(¬ifierThread, NotifierThreadProc, NULL, |
| 265 |
TCL_THREAD_STACK_DEFAULT, TCL_THREAD_NOFLAGS) != TCL_OK) { |
| 266 |
panic("Tcl_InitNotifier: unable to start notifier thread"); |
| 267 |
} |
| 268 |
} |
| 269 |
notifierCount++; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
while (triggerPipe < 0) { |
| 276 |
Tcl_ConditionWait(¬ifierCV, ¬ifierMutex, NULL); |
| 277 |
} |
| 278 |
|
| 279 |
Tcl_MutexUnlock(¬ifierMutex); |
| 280 |
#endif |
| 281 |
return (ClientData) tsdPtr; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
void |
| 303 |
Tcl_FinalizeNotifier(clientData) |
| 304 |
ClientData clientData; |
| 305 |
{ |
| 306 |
#ifdef TCL_THREADS |
| 307 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 308 |
|
| 309 |
Tcl_MutexLock(¬ifierMutex); |
| 310 |
notifierCount--; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if (notifierCount == 0) { |
| 318 |
if (triggerPipe < 0) { |
| 319 |
panic("Tcl_FinalizeNotifier: notifier pipe not initialized"); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
write(triggerPipe, "q", 1); |
| 333 |
close(triggerPipe); |
| 334 |
|
| 335 |
Tcl_ConditionWait(¬ifierCV, ¬ifierMutex, NULL); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
Tcl_ConditionFinalize(&(tsdPtr->waitCV)); |
| 343 |
|
| 344 |
Tcl_MutexUnlock(¬ifierMutex); |
| 345 |
#endif |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
void |
| 370 |
Tcl_AlertNotifier(clientData) |
| 371 |
ClientData clientData; |
| 372 |
{ |
| 373 |
#ifdef TCL_THREADS |
| 374 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData; |
| 375 |
Tcl_MutexLock(¬ifierMutex); |
| 376 |
tsdPtr->eventReady = 1; |
| 377 |
Tcl_ConditionNotify(&tsdPtr->waitCV); |
| 378 |
Tcl_MutexUnlock(¬ifierMutex); |
| 379 |
#endif |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
void |
| 401 |
Tcl_SetTimer(timePtr) |
| 402 |
Tcl_Time *timePtr; |
| 403 |
{ |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
if (tclStubs.tcl_SetTimer != tclOriginalNotifier.setTimerProc) { |
| 411 |
tclStubs.tcl_SetTimer(timePtr); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
void |
| 432 |
Tcl_ServiceModeHook(mode) |
| 433 |
int mode; |
| 434 |
|
| 435 |
{ |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
void |
| 455 |
Tcl_CreateFileHandler(fd, mask, proc, clientData) |
| 456 |
int fd; |
| 457 |
int mask; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
Tcl_FileProc *proc; |
| 462 |
|
| 463 |
ClientData clientData; |
| 464 |
{ |
| 465 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 466 |
FileHandler *filePtr; |
| 467 |
|
| 468 |
if (tclStubs.tcl_CreateFileHandler != tclOriginalNotifier.createFileHandlerProc) { |
| 469 |
tclStubs.tcl_CreateFileHandler(fd, mask, proc, clientData); |
| 470 |
return; |
| 471 |
} |
| 472 |
|
| 473 |
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL; |
| 474 |
filePtr = filePtr->nextPtr) { |
| 475 |
if (filePtr->fd == fd) { |
| 476 |
break; |
| 477 |
} |
| 478 |
} |
| 479 |
if (filePtr == NULL) { |
| 480 |
filePtr = (FileHandler*) ckalloc(sizeof(FileHandler)); |
| 481 |
filePtr->fd = fd; |
| 482 |
filePtr->readyMask = 0; |
| 483 |
filePtr->nextPtr = tsdPtr->firstFileHandlerPtr; |
| 484 |
tsdPtr->firstFileHandlerPtr = filePtr; |
| 485 |
} |
| 486 |
filePtr->proc = proc; |
| 487 |
filePtr->clientData = clientData; |
| 488 |
filePtr->mask = mask; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
if ( mask & TCL_READABLE ) { |
| 495 |
FD_SET( fd, &(tsdPtr->checkMasks.readable) ); |
| 496 |
} else { |
| 497 |
FD_CLR( fd, &(tsdPtr->checkMasks.readable) ); |
| 498 |
} |
| 499 |
if ( mask & TCL_WRITABLE ) { |
| 500 |
FD_SET( fd, &(tsdPtr->checkMasks.writable) ); |
| 501 |
} else { |
| 502 |
FD_CLR( fd, &(tsdPtr->checkMasks.writable) ); |
| 503 |
} |
| 504 |
if ( mask & TCL_EXCEPTION ) { |
| 505 |
FD_SET( fd, &(tsdPtr->checkMasks.exceptional) ); |
| 506 |
} else { |
| 507 |
FD_CLR( fd, &(tsdPtr->checkMasks.exceptional) ); |
| 508 |
} |
| 509 |
if (tsdPtr->numFdBits <= fd) { |
| 510 |
tsdPtr->numFdBits = fd+1; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
void |
| 532 |
Tcl_DeleteFileHandler(fd) |
| 533 |
int fd; |
| 534 |
{ |
| 535 |
FileHandler *filePtr, *prevPtr; |
| 536 |
int i; |
| 537 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 538 |
|
| 539 |
if (tclStubs.tcl_DeleteFileHandler != tclOriginalNotifier.deleteFileHandlerProc) { |
| 540 |
tclStubs.tcl_DeleteFileHandler(fd); |
| 541 |
return; |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
for (prevPtr = NULL, filePtr = tsdPtr->firstFileHandlerPtr; ; |
| 549 |
prevPtr = filePtr, filePtr = filePtr->nextPtr) { |
| 550 |
if (filePtr == NULL) { |
| 551 |
return; |
| 552 |
} |
| 553 |
if (filePtr->fd == fd) { |
| 554 |
break; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
if (filePtr->mask & TCL_READABLE) { |
| 563 |
FD_CLR( fd, &(tsdPtr->checkMasks.readable) ); |
| 564 |
} |
| 565 |
if (filePtr->mask & TCL_WRITABLE) { |
| 566 |
FD_CLR( fd, &(tsdPtr->checkMasks.writable) ); |
| 567 |
} |
| 568 |
if (filePtr->mask & TCL_EXCEPTION) { |
| 569 |
FD_CLR( fd, &(tsdPtr->checkMasks.exceptional) ); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
if (fd+1 == tsdPtr->numFdBits) { |
| 577 |
tsdPtr->numFdBits = 0; |
| 578 |
for (i = fd-1; i >= 0; i--) { |
| 579 |
if ( FD_ISSET( i, &(tsdPtr->checkMasks.readable) ) |
| 580 |
|| FD_ISSET( i, &(tsdPtr->checkMasks.writable) ) |
| 581 |
|| FD_ISSET( i, &(tsdPtr->checkMasks.exceptional ) ) ) { |
| 582 |
tsdPtr->numFdBits = i+1; |
| 583 |
break; |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
if (prevPtr == NULL) { |
| 593 |
tsdPtr->firstFileHandlerPtr = filePtr->nextPtr; |
| 594 |
} else { |
| 595 |
prevPtr->nextPtr = filePtr->nextPtr; |
| 596 |
} |
| 597 |
ckfree((char *) filePtr); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
#if defined(__EMX__) && (defined(__WIN32__) || defined(__PM__)) |
| 602 |
|
| 603 |
int (*Tcl_WaitForEventProc)(int, fd_mask *, Tcl_Time *) = NULL; |
| 604 |
#endif |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
static int |
| 628 |
FileHandlerEventProc(evPtr, flags) |
| 629 |
Tcl_Event *evPtr; |
| 630 |
int flags; |
| 631 |
|
| 632 |
{ |
| 633 |
int mask; |
| 634 |
FileHandler *filePtr; |
| 635 |
FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr; |
| 636 |
ThreadSpecificData *tsdPtr; |
| 637 |
|
| 638 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 650 |
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL; |
| 651 |
filePtr = filePtr->nextPtr) { |
| 652 |
if (filePtr->fd != fileEvPtr->fd) { |
| 653 |
continue; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
mask = filePtr->readyMask & filePtr->mask; |
| 669 |
filePtr->readyMask = 0; |
| 670 |
if (mask != 0) { |
| 671 |
(*filePtr->proc)(filePtr->clientData, mask); |
| 672 |
} |
| 673 |
break; |
| 674 |
} |
| 675 |
return 1; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
int |
| 698 |
Tcl_WaitForEvent(timePtr) |
| 699 |
Tcl_Time *timePtr; |
| 700 |
{ |
| 701 |
FileHandler *filePtr; |
| 702 |
FileHandlerEvent *fileEvPtr; |
| 703 |
struct timeval timeout, *timeoutPtr; |
| 704 |
int mask; |
| 705 |
#ifdef TCL_THREADS |
| 706 |
int waitForFiles; |
| 707 |
#else |
| 708 |
int numFound; |
| 709 |
#endif |
| 710 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 711 |
|
| 712 |
if (tclStubs.tcl_WaitForEvent != tclOriginalNotifier.waitForEventProc) { |
| 713 |
return tclStubs.tcl_WaitForEvent(timePtr); |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
if (timePtr) { |
| 723 |
timeout.tv_sec = timePtr->sec; |
| 724 |
timeout.tv_usec = timePtr->usec; |
| 725 |
timeoutPtr = &timeout; |
| 726 |
#ifndef TCL_THREADS |
| 727 |
} else if (tsdPtr->numFdBits == 0) { |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
return -1; |
| 737 |
#endif |
| 738 |
} else { |
| 739 |
timeoutPtr = NULL; |
| 740 |
} |
| 741 |
|
| 742 |
#ifdef TCL_THREADS |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
Tcl_MutexLock(¬ifierMutex); |
| 749 |
|
| 750 |
waitForFiles = (tsdPtr->numFdBits > 0); |
| 751 |
if (timePtr != NULL && timePtr->sec == 0 && timePtr->usec == 0) { |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
waitForFiles = 1; |
| 761 |
tsdPtr->pollState = POLL_WANT; |
| 762 |
timePtr = NULL; |
| 763 |
} else { |
| 764 |
tsdPtr->pollState = 0; |
| 765 |
} |
| 766 |
|
| 767 |
if (waitForFiles) { |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
tsdPtr->nextPtr = waitingListPtr; |
| 776 |
if (waitingListPtr) { |
| 777 |
waitingListPtr->prevPtr = tsdPtr; |
| 778 |
} |
| 779 |
tsdPtr->prevPtr = 0; |
| 780 |
waitingListPtr = tsdPtr; |
| 781 |
tsdPtr->onList = 1; |
| 782 |
|
| 783 |
write(triggerPipe, "", 1); |
| 784 |
} |
| 785 |
|
| 786 |
FD_ZERO( &(tsdPtr->readyMasks.readable) ); |
| 787 |
FD_ZERO( &(tsdPtr->readyMasks.writable) ); |
| 788 |
FD_ZERO( &(tsdPtr->readyMasks.exceptional) ); |
| 789 |
|
| 790 |
if (!tsdPtr->eventReady) { |
| 791 |
Tcl_ConditionWait(&tsdPtr->waitCV, ¬ifierMutex, timePtr); |
| 792 |
} |
| 793 |
tsdPtr->eventReady = 0; |
| 794 |
|
| 795 |
if (waitForFiles && tsdPtr->onList) { |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
if (tsdPtr->prevPtr) { |
| 804 |
tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr; |
| 805 |
} else { |
| 806 |
waitingListPtr = tsdPtr->nextPtr; |
| 807 |
} |
| 808 |
if (tsdPtr->nextPtr) { |
| 809 |
tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr; |
| 810 |
} |
| 811 |
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; |
| 812 |
tsdPtr->onList = 0; |
| 813 |
write(triggerPipe, "", 1); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
#else |
| 818 |
tsdPtr->readyMasks = tsdPtr->checkMasks; |
| 819 |
numFound = select( tsdPtr->numFdBits, |
| 820 |
&(tsdPtr->readyMasks.readable), |
| 821 |
&(tsdPtr->readyMasks.writable), |
| 822 |
&(tsdPtr->readyMasks.exceptional), |
| 823 |
timeoutPtr ); |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
if (numFound == -1) { |
| 831 |
FD_ZERO( &(tsdPtr->readyMasks.readable ) ); |
| 832 |
FD_ZERO( &(tsdPtr->readyMasks.writable ) ); |
| 833 |
FD_ZERO( &(tsdPtr->readyMasks.exceptional ) ); |
| 834 |
} |
| 835 |
|
| 836 |
#ifdef _LANG |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
if (numFound == -1 && errno == EINTR) { |
| 841 |
LangAsyncCheck(); |
| 842 |
} |
| 843 |
#endif |
| 844 |
|
| 845 |
#endif |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
for (filePtr = tsdPtr->firstFileHandlerPtr; (filePtr != NULL); |
| 852 |
filePtr = filePtr->nextPtr) { |
| 853 |
|
| 854 |
mask = 0; |
| 855 |
if ( FD_ISSET( filePtr->fd, &(tsdPtr->readyMasks.readable) ) ) { |
| 856 |
mask |= TCL_READABLE; |
| 857 |
} |
| 858 |
if ( FD_ISSET( filePtr->fd, &(tsdPtr->readyMasks.writable) ) ) { |
| 859 |
mask |= TCL_WRITABLE; |
| 860 |
} |
| 861 |
if ( FD_ISSET( filePtr->fd, &(tsdPtr->readyMasks.exceptional) ) ) { |
| 862 |
mask |= TCL_EXCEPTION; |
| 863 |
} |
| 864 |
|
| 865 |
if (!mask) { |
| 866 |
continue; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
if (filePtr->readyMask == 0) { |
| 875 |
fileEvPtr = (FileHandlerEvent *) ckalloc( |
| 876 |
sizeof(FileHandlerEvent)); |
| 877 |
fileEvPtr->header.proc = FileHandlerEventProc; |
| 878 |
fileEvPtr->fd = filePtr->fd; |
| 879 |
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL); |
| 880 |
} |
| 881 |
filePtr->readyMask = mask; |
| 882 |
} |
| 883 |
#ifdef TCL_THREADS |
| 884 |
Tcl_MutexUnlock(¬ifierMutex); |
| 885 |
#endif |
| 886 |
return 0; |
| 887 |
} |
| 888 |
|
| 889 |
#ifdef TCL_THREADS |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
static void |
| 916 |
NotifierThreadProc(clientData) |
| 917 |
ClientData clientData; |
| 918 |
{ |
| 919 |
ThreadSpecificData *tsdPtr; |
| 920 |
fd_set readableMask; |
| 921 |
fd_set writableMask; |
| 922 |
fd_set exceptionalMask; |
| 923 |
int fds[2]; |
| 924 |
int i, status, numFdBits, receivePipe; |
| 925 |
long found; |
| 926 |
struct timeval poll = {0., 0.}, *timePtr; |
| 927 |
char buf[2]; |
| 928 |
|
| 929 |
if (pipe(fds) != 0) { |
| 930 |
panic("NotifierThreadProc: could not create trigger pipe."); |
| 931 |
} |
| 932 |
|
| 933 |
receivePipe = fds[0]; |
| 934 |
|
| 935 |
#ifndef USE_FIONBIO |
| 936 |
status = fcntl(receivePipe, F_GETFL); |
| 937 |
status |= O_NONBLOCK; |
| 938 |
if (fcntl(receivePipe, F_SETFL, status) < 0) { |
| 939 |
panic("NotifierThreadProc: could not make receive pipe non blocking."); |
| 940 |
} |
| 941 |
status = fcntl(fds[1], F_GETFL); |
| 942 |
status |= O_NONBLOCK; |
| 943 |
if (fcntl(fds[1], F_SETFL, status) < 0) { |
| 944 |
panic("NotifierThreadProc: could not make trigger pipe non blocking."); |
| 945 |
} |
| 946 |
#else |
| 947 |
if (ioctl(receivePipe, (int) FIONBIO, &status) < 0) { |
| 948 |
panic("NotifierThreadProc: could not make receive pipe non blocking."); |
| 949 |
} |
| 950 |
if (ioctl(fds[1], (int) FIONBIO, &status) < 0) { |
| 951 |
panic("NotifierThreadProc: could not make trigger pipe non blocking."); |
| 952 |
} |
| 953 |
#endif |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
Tcl_MutexLock(¬ifierMutex); |
| 960 |
triggerPipe = fds[1]; |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
Tcl_ConditionNotify(¬ifierCV); |
| 967 |
Tcl_MutexUnlock(¬ifierMutex); |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
while (1) { |
| 974 |
|
| 975 |
FD_ZERO( &readableMask ); |
| 976 |
FD_ZERO( &writableMask ); |
| 977 |
FD_ZERO( &exceptionalMask ); |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
Tcl_MutexLock(¬ifierMutex); |
| 985 |
timePtr = NULL; |
| 986 |
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) { |
| 987 |
for ( i = tsdPtr->numFdBits-1; i >= 0; --i ) { |
| 988 |
if ( FD_ISSET( i, &(tsdPtr->checkMasks.readable) ) ) { |
| 989 |
FD_SET( i, &readableMask ); |
| 990 |
} |
| 991 |
if ( FD_ISSET( i, &(tsdPtr->checkMasks.writable) ) ) { |
| 992 |
FD_SET( i, &writableMask ); |
| 993 |
} |
| 994 |
if ( FD_ISSET( i, &(tsdPtr->checkMasks.exceptional) ) ) { |
| 995 |
FD_SET( i, &exceptionalMask ); |
| 996 |
} |
| 997 |
} |
| 998 |
if ( tsdPtr->numFdBits > numFdBits ) { |
| 999 |
numFdBits = tsdPtr->numFdBits; |
| 1000 |
} |
| 1001 |
if (tsdPtr->pollState & POLL_WANT) { |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
tsdPtr->pollState |= POLL_DONE; |
| 1008 |
timePtr = &poll; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
Tcl_MutexUnlock(¬ifierMutex); |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
if ( receivePipe >= numFdBits ) { |
| 1018 |
numFdBits = receivePipe + 1; |
| 1019 |
} |
| 1020 |
FD_SET( receivePipe, &readableMask ); |
| 1021 |
|
| 1022 |
if ( select( numFdBits, &readableMask, &writableMask, |
| 1023 |
&exceptionalMask, timePtr) == -1 ) { |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
continue; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
Tcl_MutexLock(¬ifierMutex); |
| 1036 |
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) { |
| 1037 |
found = 0; |
| 1038 |
|
| 1039 |
for ( i = tsdPtr->numFdBits-1; i >= 0; --i ) { |
| 1040 |
if ( FD_ISSET( i, &(tsdPtr->checkMasks.readable) ) |
| 1041 |
&& FD_ISSET( i, &readableMask ) ) { |
| 1042 |
FD_SET( i, &(tsdPtr->readyMasks.readable) ); |
| 1043 |
found = 1; |
| 1044 |
} |
| 1045 |
if ( FD_ISSET( i, &(tsdPtr->checkMasks.writable) ) |
| 1046 |
&& FD_ISSET( i, &writableMask ) ) { |
| 1047 |
FD_SET( i, &(tsdPtr->readyMasks.writable) ); |
| 1048 |
found = 1; |
| 1049 |
} |
| 1050 |
if ( FD_ISSET( i, &(tsdPtr->checkMasks.exceptional) ) |
| 1051 |
&& FD_ISSET( i, &exceptionalMask ) ) { |
| 1052 |
FD_SET( i, &(tsdPtr->readyMasks.exceptional) ); |
| 1053 |
found = 1; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
if (found || (tsdPtr->pollState & POLL_DONE)) { |
| 1058 |
tsdPtr->eventReady = 1; |
| 1059 |
if (tsdPtr->onList) { |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
if (tsdPtr->prevPtr) { |
| 1068 |
tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr; |
| 1069 |
} else { |
| 1070 |
waitingListPtr = tsdPtr->nextPtr; |
| 1071 |
} |
| 1072 |
if (tsdPtr->nextPtr) { |
| 1073 |
tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr; |
| 1074 |
} |
| 1075 |
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL; |
| 1076 |
tsdPtr->onList = 0; |
| 1077 |
tsdPtr->pollState = 0; |
| 1078 |
} |
| 1079 |
Tcl_ConditionNotify(&tsdPtr->waitCV); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
Tcl_MutexUnlock(¬ifierMutex); |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
if ( FD_ISSET( receivePipe, &readableMask ) ) { |
| 1091 |
i = read(receivePipe, buf, 1); |
| 1092 |
|
| 1093 |
if ((i == 0) || ((i == 1) && (buf[0] == 'q'))) { |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
break; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
close(receivePipe); |
| 1111 |
Tcl_MutexLock(¬ifierMutex); |
| 1112 |
triggerPipe = -1; |
| 1113 |
Tcl_ConditionNotify(¬ifierCV); |
| 1114 |
Tcl_MutexUnlock(¬ifierMutex); |
| 1115 |
} |
| 1116 |
#endif |
| 1117 |
#endif |
| 1118 |
|
| 1119 |
|