1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 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 630 631 632 633 634 635 636 637 638 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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 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 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 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 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
|
/**
* The GSRunLoopCtxt stores context information to handle polling for
* events. This information is associated with a particular runloop
* mode, and persists throughout the life of the runloop instance.
*
* NB. This class is private to NSRunLoop and must not be subclassed.
*/
#import "common.h"
#import "Foundation/NSError.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSNotificationQueue.h"
#import "Foundation/NSPort.h"
#import "Foundation/NSStream.h"
#import "../GSRunLoopCtxt.h"
#import "../GSRunLoopWatcher.h"
#import "../GSPrivate.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_POLL_F
#include <poll.h>
#endif
#define FDCOUNT 1024
static SEL wRelSel;
static SEL wRetSel;
static IMP wRelImp;
static IMP wRetImp;
static void
wRelease(NSMapTable* t, void* w)
{
(*wRelImp)((id)w, wRelSel);
}
static void
wRetain(NSMapTable* t, const void* w)
{
(*wRetImp)((id)w, wRetSel);
}
static const NSMapTableValueCallBacks WatcherMapValueCallBacks =
{
wRetain,
wRelease,
0
};
@implementation GSRunLoopCtxt
+ (void) initialize
{
wRelSel = @selector(release);
wRetSel = @selector(retain);
wRelImp = [[GSRunLoopWatcher class] instanceMethodForSelector: wRelSel];
wRetImp = [[GSRunLoopWatcher class] instanceMethodForSelector: wRetSel];
}
- (void) dealloc
{
RELEASE(mode);
GSIArrayEmpty(performers);
NSZoneFree(performers->zone, (void*)performers);
GSIArrayEmpty(timers);
NSZoneFree(timers->zone, (void*)timers);
GSIArrayEmpty(watchers);
NSZoneFree(watchers->zone, (void*)watchers);
if (_efdMap != 0)
{
NSFreeMapTable(_efdMap);
}
if (_rfdMap != 0)
{
NSFreeMapTable(_rfdMap);
}
if (_wfdMap != 0)
{
NSFreeMapTable(_wfdMap);
}
GSIArrayEmpty(_trigger);
NSZoneFree(_trigger->zone, (void*)_trigger);
#ifdef HAVE_POLL_F
if (pollfds != 0)
{
NSZoneFree(NSDefaultMallocZone(), pollfds);
}
#endif
[super dealloc];
}
/**
* Remove any callback for the specified event which is set for an
* uncompleted poll operation.<br />
* This is called by nested event loops on contexts in outer loops
* when they handle an event ... removing the event from the outer
* loop ensures that it won't get handled twice, once by the inner
* loop and once by the outer one.
*/
- (void) endEvent: (void*)data
for: (GSRunLoopWatcher*)watcher
{
if (completed == NO)
{
unsigned i = GSIArrayCount(_trigger);
while (i-- > 0)
{
GSIArrayItem item = GSIArrayItemAtIndex(_trigger, i);
if (item.obj == (id)watcher)
{
GSIArrayRemoveItemAtIndex(_trigger, i);
return;
}
}
switch (watcher->type)
{
case ET_RPORT:
case ET_RDESC:
NSMapRemove(_rfdMap, data);
break;
case ET_WDESC:
NSMapRemove(_wfdMap, data);
break;
case ET_EDESC:
NSMapRemove(_efdMap, data);
break;
case ET_TRIGGER:
// Already handled
break;
default:
NSLog(@"Ending an event of unexpected type (%d)", watcher->type);
break;
}
}
}
/**
* Mark this poll context as having completed, so that if we are
* executing a re-entrant poll, the enclosing poll operations
* know they can stop what they are doing because an inner
* operation has done the job.
*/
- (void) endPoll
{
completed = YES;
}
- (id) init
{
[NSException raise: NSInternalInconsistencyException
format: @"-init may not be called for GSRunLoopCtxt"];
return nil;
}
- (id) initWithMode: (NSString*)theMode extra: (void*)e
{
self = [super init];
if (self != nil)
{
NSZone *z;
mode = [theMode copy];
extra = e;
z = [self zone];
performers = NSZoneMalloc(z, sizeof(GSIArray_t));
timers = NSZoneMalloc(z, sizeof(GSIArray_t));
watchers = NSZoneMalloc(z, sizeof(GSIArray_t));
_trigger = NSZoneMalloc(z, sizeof(GSIArray_t));
GSIArrayInitWithZoneAndCapacity(performers, z, 8);
GSIArrayInitWithZoneAndCapacity(timers, z, 8);
GSIArrayInitWithZoneAndCapacity(watchers, z, 8);
GSIArrayInitWithZoneAndCapacity(_trigger, z, 8);
_efdMap = NSCreateMapTable (NSIntegerMapKeyCallBacks,
WatcherMapValueCallBacks, 0);
_rfdMap = NSCreateMapTable (NSIntegerMapKeyCallBacks,
WatcherMapValueCallBacks, 0);
_wfdMap = NSCreateMapTable (NSIntegerMapKeyCallBacks,
WatcherMapValueCallBacks, 0);
}
return self;
}
#ifdef HAVE_POLL_F
static void setPollfd(int fd, int event, GSRunLoopCtxt *ctxt)
{
int index;
struct pollfd *pollfds = ctxt->pollfds;
pollextra *pe = (pollextra*)ctxt->extra;
if (fd >= pe->limit)
{
int oldfd_limit = pe->limit;
pe->limit = fd + 1;
if (pe->index == 0)
{
pe->index = NSZoneMalloc(NSDefaultMallocZone(),
pe->limit * sizeof(*(pe->index)));
}
else
{
pe->index = NSZoneRealloc(NSDefaultMallocZone(),
pe->index, pe->limit * sizeof(*(pe->index)));
}
do
{
pe->index[oldfd_limit++] = -1;
}
while (oldfd_limit < pe->limit);
}
index = pe->index[fd];
if (index == -1)
{
if (ctxt->pollfds_count >= ctxt->pollfds_capacity)
{
ctxt->pollfds_capacity += 8;
pollfds = NSZoneRealloc(NSDefaultMallocZone(),
pollfds, ctxt->pollfds_capacity * sizeof (*pollfds));
ctxt->pollfds = pollfds;
}
index = ctxt->pollfds_count++;
pe->index[fd] = index;
pollfds[index].fd = fd;
pollfds[index].events = 0;
pollfds[index].revents = 0;
}
pollfds[index].events |= event;
}
/**
* Perform a poll for the specified runloop context.
* If the method has been called re-entrantly, the contexts stack
* will list all the contexts with polls in progress
* and this method must tell those outer contexts not to handle events
* which are handled by this context.
*/
- (BOOL) pollUntil: (int)milliseconds within: (NSArray*)contexts
{
GSRunLoopThreadInfo *threadInfo = GSRunLoopInfoForThread(nil);
int poll_return;
int fdEnd; /* Number of descriptors being monitored. */
int fdIndex;
int fdFinish;
unsigned count;
unsigned int i;
BOOL immediate = NO;
i = GSIArrayCount(watchers);
/*
* Get ready to listen to file descriptors.
* The maps will not have been emptied by any previous call.
*/
NSResetMapTable(_efdMap);
NSResetMapTable(_rfdMap);
NSResetMapTable(_wfdMap);
GSIArrayRemoveAllItems(_trigger);
/*
* Do the pre-listening set-up for the file descriptors of this mode.
*/
if (pollfds_capacity < i + 2)
{
pollfds_capacity = i + 2;
if (pollfds == 0)
{
pollfds = NSZoneMalloc(NSDefaultMallocZone(),
pollfds_capacity * sizeof(*pollfds));
}
else
{
pollfds = NSZoneRealloc(NSDefaultMallocZone(),
pollfds, pollfds_capacity * sizeof(*pollfds));
}
}
pollfds_count = 0;
((pollextra*)extra)->limit = 0;
/* Watch for signals from other threads.
*/
setPollfd(threadInfo->inputFd, POLLIN, self);
while (i-- > 0)
{
GSRunLoopWatcher *info;
BOOL trigger;
info = GSIArrayItemAtIndex(watchers, i).obj;
if (info->_invalidated == YES)
{
GSIArrayRemoveItemAtIndex(watchers, i);
}
else if ([info runLoopShouldBlock: &trigger] == NO)
{
if (trigger == YES)
{
immediate = YES;
GSIArrayAddItem(_trigger, (GSIArrayItem)(id)info);
}
}
else
{
int fd;
switch (info->type)
{
case ET_EDESC:
fd = (int)(intptr_t)info->data;
setPollfd(fd, POLLPRI, self);
NSMapInsert(_efdMap, (void*)(intptr_t)fd, info);
break;
case ET_RDESC:
fd = (int)(intptr_t)info->data;
setPollfd(fd, POLLIN, self);
NSMapInsert(_rfdMap, (void*)(intptr_t)fd, info);
break;
case ET_WDESC:
fd = (int)(intptr_t)info->data;
setPollfd(fd, POLLOUT, self);
NSMapInsert(_wfdMap, (void*)(intptr_t)fd, info);
break;
case ET_TRIGGER:
break;
case ET_RPORT:
{
id port = info->receiver;
NSInteger port_fd_size = FDCOUNT;
NSInteger port_fd_count = FDCOUNT;
NSInteger port_fd_buffer[FDCOUNT];
NSInteger *port_fd_array = port_fd_buffer;
[port getFds: port_fd_array count: &port_fd_count];
while (port_fd_count > port_fd_size)
{
if (port_fd_array != port_fd_buffer) free(port_fd_array);
port_fd_size = port_fd_count;
port_fd_count = port_fd_size;
port_fd_array = malloc(sizeof(NSInteger)*port_fd_size);
[port getFds: port_fd_array count: &port_fd_count];
}
NSDebugMLLog(@"NSRunLoop",
@"listening to %"PRIdPTR" port handles\n", port_fd_count);
while (port_fd_count--)
{
fd = port_fd_array[port_fd_count];
setPollfd(fd, POLLIN, self);
NSMapInsert(_rfdMap, (void*)(intptr_t)fd, info);
}
if (port_fd_array != port_fd_buffer) free(port_fd_array);
}
break;
}
}
}
/*
* If there are notifications in the 'idle' queue, we try an
* instantaneous select so that, if there is no input pending,
* we can service the queue. Similarly, if a task has completed,
* we need to deliver its notifications.
*/
if (GSPrivateCheckTasks() || GSPrivateNotifyMore(mode) || immediate == YES)
{
milliseconds = 0;
}
#if 0
{
unsigned int i;
fprintf(stderr, "poll %d %d:", milliseconds, pollfds_count);
for (i = 0; i < pollfds_count; i++)
fprintf(stderr, " %d,%x", pollfds[i].fd, pollfds[i].events);
fprintf(stderr, "\n");
}
#endif
poll_return = poll (pollfds, pollfds_count, milliseconds);
#if 0
{
unsigned int i;
fprintf(stderr, "ret %d %d:", poll_return, pollfds_count);
for (i = 0; i < pollfds_count; i++)
fprintf(stderr, " %d,%x", pollfds[i].fd, pollfds[i].revents);
fprintf(stderr, "\n");
}
#endif
NSDebugMLLog(@"NSRunLoop", @"poll returned %d\n", poll_return);
if (poll_return < 0)
{
if (errno == EINTR)
{
GSPrivateCheckTasks();
poll_return = 0;
}
else if (errno == 0)
{
/* Some systems returns an errno == 0. Not sure why */
poll_return = 0;
}
else
{
/* Some exceptional condition happened. */
/* xxx We can do something with exception_fds, instead of
aborting here. */
NSLog (@"poll() error in -acceptInputForMode:beforeDate: '%@'",
[NSError _last]);
abort ();
}
}
/*
* Trigger any watchers which are set up to for every runloop wait.
*/
count = GSIArrayCount(_trigger);
while (count-- > 0)
{
GSRunLoopWatcher *watcher;
watcher = (GSRunLoopWatcher*)GSIArrayItemAtIndex(_trigger, count).obj;
if (watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
{
[c endEvent: (void*)watcher for: watcher];
}
}
/*
* The watcher is still valid - so call its
* receivers event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: watcher->data
forMode: mode];
}
GSPrivateNotifyASAP(mode);
}
/*
* If the poll returned no descriptors with events, we have no more to do.
*/
if (poll_return == 0)
{
completed = YES;
return NO;
}
/*
* Look at all the file descriptors poll() says are ready for action;
* notify the corresponding object for each of the ready fd's.
* NB. It is possible for a watcher to be missing from the map - if
* the event handler of a previous watcher has 'run' the loop again
* before returning.
* NB. Each time this loop is entered, the starting position (fairStart)
* is incremented - this is to ensure a fair distribution over all
* inputs where multiple inputs are in use. Note - fairStart can be
* modified while we are in the loop (by recursive calls).
*/
fdEnd = pollfds_count;
if (++fairStart >= fdEnd)
{
fairStart = 0;
fdIndex = 0;
fdFinish = 0;
}
else
{
fdIndex = fairStart;
fdFinish = fairStart;
}
completed = NO;
while (completed == NO)
{
if (pollfds[fdIndex].revents != 0)
{
int fd = pollfds[fdIndex].fd;
GSRunLoopWatcher *watcher;
BOOL found = NO;
/*
* The poll() call supports various error conditions - all
* errors should be handled by any available handler.
* The ET_EDSEC handler is the primary handler for exceptions
* though it is more generally used to deal with out-of-band data.
*/
if (pollfds[fdIndex].revents & (POLLPRI|POLLERR|POLLHUP|POLLNVAL))
{
watcher
= (GSRunLoopWatcher*)NSMapGet(_efdMap, (void*)(intptr_t)fd);
if (watcher != nil && watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
{
[c endEvent: (void*)(intptr_t)fd for: watcher];
}
}
/*
* The watcher is still valid - so call its
* receivers event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: (void*)(uintptr_t)fd
forMode: mode];
}
GSPrivateNotifyASAP(mode);
if (completed == YES)
{
break; // A nested poll has done the job.
}
found = YES;
}
if (pollfds[fdIndex].revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL))
{
watcher
= (GSRunLoopWatcher*)NSMapGet(_wfdMap, (void*)(intptr_t)fd);
if (watcher != nil && watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
{
[c endEvent: (void*)(intptr_t)fd for: watcher];
}
}
/*
* The watcher is still valid - so call its
* receivers event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: (void*)(uintptr_t)fd
forMode: mode];
}
GSPrivateNotifyASAP(mode);
if (completed == YES)
{
break; // A nested poll has done the job.
}
found = YES;
}
if (pollfds[fdIndex].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL))
{
if (fd == threadInfo->inputFd)
{
NSDebugMLLog(@"NSRunLoop", @"Fire perform on thread");
[threadInfo fire];
watcher = nil;
}
else
{
watcher = (GSRunLoopWatcher*)
NSMapGet(_rfdMap, (void*)(intptr_t)fd);
}
if (watcher != nil && watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
{
[c endEvent: (void*)(intptr_t)fd for: watcher];
}
}
/*
* The watcher is still valid - so call its
* receivers event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: (void*)(uintptr_t)fd
forMode: mode];
}
GSPrivateNotifyASAP(mode);
if (completed == YES)
{
break; // A nested poll has done the job.
}
found = YES;
}
if (found == YES && --poll_return == 0)
{
completed = YES;
}
}
if (++fdIndex >= fdEnd)
{
fdIndex = 0;
}
if (fdIndex == fdFinish)
{
completed = YES;
}
}
completed = YES;
return YES;
}
+ (BOOL) awakenedBefore: (NSDate*)when
{
GSRunLoopThreadInfo *threadInfo = GSRunLoopInfoForThread(nil);
NSTimeInterval ti = (when == nil) ? 0.0 : [when timeIntervalSinceNow];
int milliseconds = (ti <= 0.0) ? 0 : (int)(ti*1000);
struct pollfd pollfds;
/* Watch for signals from other threads.
*/
pollfds.fd = threadInfo->inputFd;
pollfds.events = POLLIN;
pollfds.revents = 0;
if (poll(&pollfds, 1, milliseconds) == 1)
{
NSDebugMLLog(@"NSRunLoop", @"Fire perform on thread");
[threadInfo fire];
return YES;
}
return NO;
}
#else
- (BOOL) pollUntil: (int)milliseconds within: (NSArray*)contexts
{
GSRunLoopThreadInfo *threadInfo = GSRunLoopInfoForThread(nil);
struct timeval timeout;
void *select_timeout;
int select_return;
int fdIndex;
int fdFinish;
fd_set read_fds; // Mask for read-ready fds.
fd_set exception_fds; // Mask for exception fds.
fd_set write_fds; // Mask for write-ready fds.
int fd;
int fdEnd = -1;
unsigned count;
unsigned i;
BOOL immediate = NO;
i = GSIArrayCount(watchers);
/* Find out how much time we should wait, and set SELECT_TIMEOUT. */
if (milliseconds == 0)
{
/* Don't wait at all. */
timeout.tv_sec = 0;
timeout.tv_usec = 0;
select_timeout = &timeout;
}
else if (milliseconds > 0)
{
timeout.tv_sec = milliseconds/1000;
timeout.tv_usec = (milliseconds - 1000 * timeout.tv_sec) * 1000;
select_timeout = &timeout;
}
else
{
timeout.tv_sec = -1;
timeout.tv_usec = -1;
select_timeout = NULL;
}
/*
* Get ready to listen to file descriptors.
* Initialize the set of FDS we'll pass to select(), and make sure we
* have empty maps for keeping track of which watcher is associated
* with which file descriptor.
* The maps may not have been emptied if a previous call to this
* method was terminated by an exception.
*/
memset(&exception_fds, '\0', sizeof(exception_fds));
memset(&read_fds, '\0', sizeof(read_fds));
memset(&write_fds, '\0', sizeof(write_fds));
NSResetMapTable(_efdMap);
NSResetMapTable(_rfdMap);
NSResetMapTable(_wfdMap);
GSIArrayRemoveAllItems(_trigger);
/* Watch for signals from other threads.
*/
fd = threadInfo->inputFd;
if (fd > fdEnd)
fdEnd = fd;
FD_SET (fd, &read_fds);
while (i-- > 0)
{
GSRunLoopWatcher *info;
int fd;
BOOL trigger;
info = GSIArrayItemAtIndex(watchers, i).obj;
if (info->_invalidated == YES)
{
GSIArrayRemoveItemAtIndex(watchers, i);
}
else if ([info runLoopShouldBlock: &trigger] == NO)
{
if (trigger == YES)
{
immediate = YES;
GSIArrayAddItem(_trigger, (GSIArrayItem)(id)info);
}
}
else
{
switch (info->type)
{
case ET_EDESC:
fd = (int)(intptr_t)info->data;
if (fd > fdEnd)
fdEnd = fd;
FD_SET (fd, &exception_fds);
NSMapInsert(_efdMap, (void*)(intptr_t)fd, info);
break;
case ET_RDESC:
fd = (int)(intptr_t)info->data;
if (fd > fdEnd)
fdEnd = fd;
FD_SET (fd, &read_fds);
NSMapInsert(_rfdMap, (void*)(intptr_t)fd, info);
break;
case ET_WDESC:
fd = (int)(intptr_t)info->data;
if (fd > fdEnd)
fdEnd = fd;
FD_SET (fd, &write_fds);
NSMapInsert(_wfdMap, (void*)(intptr_t)fd, info);
break;
case ET_RPORT:
{
id port = info->receiver;
NSInteger port_fd_size = FDCOUNT;
NSInteger port_fd_count = FDCOUNT;
NSInteger port_fd_buffer[FDCOUNT];
NSInteger *port_fd_array = port_fd_buffer;
[port getFds: port_fd_array count: &port_fd_count];
while (port_fd_count > port_fd_size)
{
if (port_fd_array != port_fd_buffer) free(port_fd_array);
port_fd_size = port_fd_count;
port_fd_count = port_fd_size;
port_fd_array = malloc(sizeof(NSInteger)*port_fd_size);
[port getFds: port_fd_array count: &port_fd_count];
}
NSDebugMLLog(@"NSRunLoop", @"listening to %d port sockets",
port_fd_count);
while (port_fd_count--)
{
fd = port_fd_array[port_fd_count];
if (fd > fdEnd)
fdEnd = fd;
FD_SET (fd, &read_fds);
NSMapInsert(_rfdMap, (void*)(intptr_t)fd, info);
}
if (port_fd_array != port_fd_buffer) free(port_fd_array);
}
break;
case ET_TRIGGER:
break;
}
}
}
fdEnd++;
/*
* If there are notifications in the 'idle' queue, we try an
* instantaneous select so that, if there is no input pending,
* we can service the queue. Similarly, if a task has completed,
* we need to deliver its notifications.
*/
if (GSPrivateCheckTasks() || GSPrivateNotifyMore(mode) || immediate == YES)
{
timeout.tv_sec = 0;
timeout.tv_usec = 0;
select_timeout = &timeout;
}
// NSDebugMLLog(@"NSRunLoop", @"select timeout %d,%d", timeout.tv_sec, timeout.tv_usec);
select_return = select (fdEnd, &read_fds, &write_fds,
&exception_fds, select_timeout);
NSDebugMLLog(@"NSRunLoop", @"select returned %d", select_return);
if (select_return < 0)
{
if (errno == EINTR)
{
GSPrivateCheckTasks();
select_return = 0;
}
else if (errno == 0)
{
/* Some systems return an errno == 0. Not sure why */
select_return = 0;
}
else
{
/* Some exceptional condition happened. */
/* xxx We can do something with exception_fds, instead of
aborting here. */
NSLog (@"select() error in -acceptInputForMode:beforeDate: '%@'",
[NSError _last]);
abort ();
}
}
/*
* Trigger any watchers which are set up to for every runloop wait.
*/
count = GSIArrayCount(_trigger);
while (count-- > 0)
{
GSRunLoopWatcher *watcher;
watcher = (GSRunLoopWatcher*)GSIArrayItemAtIndex(_trigger, count).obj;
if (watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
{
[c endEvent: (void*)watcher for: watcher];
}
}
/*
* The watcher is still valid - so call its
* receivers event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: watcher->data
forMode: mode];
}
GSPrivateNotifyASAP(mode);
}
/*
* If the select returned no descriptors with events, we have no more to do.
*/
if (select_return == 0)
{
completed = YES;
return NO;
}
/*
* Look at all the file descriptors select() says are ready for action;
* notify the corresponding object for each of the ready fd's.
* NB. Each time this loop is entered, the starting position (fairStart)
* is incremented - this is to ensure a fair distribution over all
* inputs where multiple inputs are in use. Note - fairStart can be
* modified while we are in the loop (by recursive calls).
*/
if (++fairStart >= fdEnd)
{
fairStart = 0;
fdIndex = 0;
fdFinish = 0;
}
else
{
fdIndex = fairStart;
fdFinish = fairStart;
}
completed = NO;
while (completed == NO)
{
BOOL found = NO;
if (FD_ISSET (fdIndex, &exception_fds))
{
GSRunLoopWatcher *watcher;
watcher
= (GSRunLoopWatcher*)NSMapGet(_efdMap, (void*)(intptr_t)fdIndex);
if (watcher != nil && watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
[c endEvent: (void*)(intptr_t)fdIndex for: watcher];
}
/*
* The watcher is still valid - so call its receivers
* event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: (void*)(intptr_t)fdIndex
forMode: mode];
}
GSPrivateNotifyASAP(mode);
if (completed == YES)
{
break;
}
found = YES;
}
if (FD_ISSET (fdIndex, &write_fds))
{
GSRunLoopWatcher *watcher;
watcher
= (GSRunLoopWatcher*)NSMapGet(_wfdMap, (void*)(intptr_t)fdIndex);
if (watcher != nil && watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
[c endEvent: (void*)(intptr_t)fdIndex for: watcher];
}
/*
* The watcher is still valid - so call its receivers
* event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: (void*)(intptr_t)fdIndex
forMode: mode];
}
GSPrivateNotifyASAP(mode);
if (completed == YES)
{
break;
}
found = YES;
}
if (FD_ISSET (fdIndex, &read_fds))
{
GSRunLoopWatcher *watcher;
if (fdIndex == threadInfo->inputFd)
{
NSDebugMLLog(@"NSRunLoop", @"Fire perform on thread");
[threadInfo fire];
watcher = nil;
}
else
{
watcher = (GSRunLoopWatcher*)
NSMapGet(_rfdMap, (void*)(intptr_t)fdIndex);
}
if (watcher != nil && watcher->_invalidated == NO)
{
i = [contexts count];
while (i-- > 0)
{
GSRunLoopCtxt *c = [contexts objectAtIndex: i];
if (c != self)
[c endEvent: (void*)(intptr_t)fdIndex for: watcher];
}
/*
* The watcher is still valid - so call its receivers
* event handling method.
*/
[watcher->receiver receivedEvent: watcher->data
type: watcher->type
extra: (void*)(intptr_t)fdIndex
forMode: mode];
}
GSPrivateNotifyASAP(mode);
if (completed == YES)
{
break;
}
found = YES;
}
if (found == YES && --select_return == 0)
{
completed = YES;
}
if (++fdIndex >= fdEnd)
{
fdIndex = 0;
}
if (fdIndex == fdFinish)
{
completed = YES;
}
}
completed = YES;
return YES;
}
+ (BOOL) awakenedBefore: (NSDate*)when
{
GSRunLoopThreadInfo *threadInfo = GSRunLoopInfoForThread(nil);
NSTimeInterval ti = (when == nil) ? 0.0 : [when timeIntervalSinceNow];
int milliseconds = (ti <= 0.0) ? 0 : (int)(ti*1000);
struct timeval timeout;
fd_set read_fds; // Mask for read-ready fds.
fd_set exception_fds; // Mask for exception fds.
fd_set write_fds; // Mask for write-ready fds.
memset(&exception_fds, '\0', sizeof(exception_fds));
memset(&read_fds, '\0', sizeof(read_fds));
memset(&write_fds, '\0', sizeof(write_fds));
timeout.tv_sec = milliseconds/1000;
timeout.tv_usec = (milliseconds - 1000 * timeout.tv_sec) * 1000;
FD_SET (threadInfo->inputFd, &read_fds);
if (select (threadInfo->inputFd, &read_fds, &write_fds,
&exception_fds, &timeout) > 0)
{
NSDebugMLLog(@"NSRunLoop", @"Fire perform on thread");
[threadInfo fire];
return YES;
}
return NO;
}
#endif
@end
|