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
|
/*
* tclEpollNotfy.c --
*
* This file contains the implementation of the epoll()-based
* Linux-specific notifier, which is the lowest-level part of the
* Tcl event loop. This file works together with generic/tclNotify.c.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
* Copyright (c) 2016 Lucio Andrés Illanes Albornoz <l.illanes@gmx.de>
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#ifdef NOTIFIER_EPOLL
#define _GNU_SOURCE /* For pipe2(2) */
#include "tclInt.h"
#ifndef HAVE_COREFOUNDATION /* Darwin/Mac OS X CoreFoundation notifier is
* in tclMacOSXNotify.c */
#include <fcntl.h>
#include <signal.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/queue.h>
#include <unistd.h>
/*
* This structure is used to keep track of the notifier info for a registered
* file.
*/
struct PlatformEventData;
typedef struct FileHandler {
int fd;
int mask; /* Mask of desired events: TCL_READABLE,
* etc. */
int readyMask; /* Mask of events that have been seen since
* the last time file handlers were invoked
* for this file. */
Tcl_FileProc *proc; /* Function to call, in the style of
* Tcl_CreateFileHandler. */
ClientData clientData; /* Argument to pass to proc. */
struct FileHandler *nextPtr;/* Next in list of all files we care about. */
LIST_ENTRY(FileHandler) readyNode;
/* Next/previous in list of FileHandlers asso-
* ciated with regular files (S_IFREG) that are
* ready for I/O. */
struct PlatformEventData *pedPtr;
/* Pointer to PlatformEventData associating this
* FileHandler with epoll(7) events. */
} FileHandler;
/*
* The following structure associates a FileHandler and the thread that owns it
* with the file descriptors of interest and their event masks passed to epoll_ctl(2)
* and their corresponding event(s) returned by epoll_wait(2).
*/
struct ThreadSpecificData;
struct PlatformEventData {
FileHandler *filePtr;
struct ThreadSpecificData *tsdPtr;
};
/*
* The following structure is what is added to the Tcl event queue when file
* handlers are ready to fire.
*/
typedef struct {
Tcl_Event header; /* Information that is standard for all
* events. */
int fd; /* File descriptor that is ready. Used to find
* the FileHandler structure for the file
* (can't point directly to the FileHandler
* structure because it could go away while
* the event is queued). */
} FileHandlerEvent;
/*
* The following static structure contains the state information for the
* epoll based implementation of the Tcl notifier. One of these structures is
* created for each thread that is using the notifier.
*/
LIST_HEAD(PlatformReadyFileHandlerList, FileHandler);
typedef struct ThreadSpecificData {
FileHandler *firstFileHandlerPtr;
/* Pointer to head of file handler list. */
struct PlatformReadyFileHandlerList firstReadyFileHandlerPtr;
/* Pointer to head of list of FileHandlers
* associated with regular files (S_IFREG)
* that are ready for I/O. */
pthread_mutex_t notifierMutex;
/* Mutex protecting notifier termination in
* PlatformEventsFinalize. */
#ifdef HAVE_EVENTFD
int triggerEventFd; /* eventfd(2) used by other threads to wake
* up this thread for inter-thread IPC. */
#else
int triggerPipe[2]; /* pipe(2) used by other threads to wake
* up this thread for inter-thread IPC. */
#endif /* HAVE_EVENTFD */
int eventsFd; /* epoll(7) file descriptor used to wait for fds */
struct epoll_event *readyEvents;
/* Pointer to at most maxReadyEvents events
* returned by epoll_wait(2). */
size_t maxReadyEvents; /* Count of epoll_events in readyEvents. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
void PlatformEventsControl(FileHandler *filePtr, ThreadSpecificData *tsdPtr, int op, int isNew);
static void PlatformEventsFinalize(void);
void PlatformEventsInit(void);
static int PlatformEventsTranslate(struct epoll_event *event);
static int PlatformEventsWait(struct epoll_event *events, size_t numEvents, struct timeval *timePtr);
#include "tclUnixNotfy.c"
/*
*----------------------------------------------------------------------
*
* Tcl_InitNotifier --
*
* Initializes the platform specific notifier state.
*
* Results:
* Returns a handle to the notifier state for this thread.
*
* Side effects:
* If no initNotifierProc notifier hook exists, PlatformEventsInit
* is called.
*
*----------------------------------------------------------------------
*/
ClientData
Tcl_InitNotifier(void)
{
if (tclNotifierHooks.initNotifierProc) {
return tclNotifierHooks.initNotifierProc();
} else {
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
PlatformEventsInit();
return tsdPtr;
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_FinalizeNotifier --
*
* This function is called to cleanup the notifier state before a thread
* is terminated.
*
* Results:
* None.
*
* Side effects:
* If no finalizeNotifierProc notifier hook exists, PlatformEvents-
* Finalize is called.
*
*----------------------------------------------------------------------
*/
void
Tcl_FinalizeNotifier(
ClientData clientData) /* Not used. */
{
if (tclNotifierHooks.finalizeNotifierProc) {
tclNotifierHooks.finalizeNotifierProc(clientData);
return;
} else {
PlatformEventsFinalize();
}
}
/*
*----------------------------------------------------------------------
*
* PlatformEventsControl --
*
* This function registers interest for the file descriptor and the mask
* of TCL_* bits associated with filePtr on the epoll file descriptor
* associated with tsdPtr.
* Future calls to epoll_wait will return filePtr and tsdPtr alongside with
* the event registered here via the PlatformEventData struct.
*
* Results:
* None.
*
* Side effects:
* If adding a new file descriptor, a PlatformEventData struct will be
* allocated and associated with filePtr.
* fstat is called on the file descriptor; if it is associated with
* a regular file (S_IFREG,) filePtr is considered to be ready for I/O
* and added to or deleted from the corresponding list in tsdPtr.
* If it is not associated with a regular file, the file descriptor is
* added, modified concerning its mask of events of interest, or deleted
* from the epoll file descriptor of the calling thread.
*
*----------------------------------------------------------------------
*/
void
PlatformEventsControl(
FileHandler *filePtr,
ThreadSpecificData *tsdPtr,
int op,
int isNew)
{
struct epoll_event newEvent;
struct PlatformEventData *newPedPtr;
struct stat fdStat;
newEvent.events = 0;
if (filePtr->mask & (TCL_READABLE | TCL_EXCEPTION)) {
newEvent.events |= EPOLLIN;
}
if (filePtr->mask & TCL_WRITABLE) {
newEvent.events |= EPOLLOUT;
}
if (isNew) {
newPedPtr = ckalloc(sizeof(*newPedPtr));
newPedPtr->filePtr = filePtr;
newPedPtr->tsdPtr = tsdPtr;
filePtr->pedPtr = newPedPtr;
}
newEvent.data.ptr = filePtr->pedPtr;
/*
* N.B. As discussed in Tcl_WaitForEvent(), epoll(7) does not sup-
* port regular files (S_IFREG.) Therefore, filePtr is in these
* cases simply added or deleted from the list of FileHandlers
* associated with regular files belonging to tsdPtr.
*/
if (fstat(filePtr->fd, &fdStat) == -1) {
Tcl_Panic("fstat: %s", strerror(errno));
} else if ((fdStat.st_mode & S_IFMT) == S_IFREG) {
switch (op) {
case EPOLL_CTL_ADD:
if (isNew) {
LIST_INSERT_HEAD(&tsdPtr->firstReadyFileHandlerPtr, filePtr, readyNode);
}
break;
case EPOLL_CTL_DEL:
LIST_REMOVE(filePtr, readyNode);
break;
}
return;
} else if (epoll_ctl(tsdPtr->eventsFd, op, filePtr->fd, &newEvent) == -1) {
Tcl_Panic("epoll_ctl: %s", strerror(errno));
}
}
/*
*----------------------------------------------------------------------
*
* PlatformEventsFinalize --
*
* This function closes the eventfd and the epoll file descriptor and
* frees the epoll_event structs owned by the thread of the caller.
* The above operations are protected by tsdPtr->notifierMutex, which
* is destroyed thereafter.
*
* Results:
* None.
*
* Side effects:
* While tsdPtr->notifierMutex is held:
* The per-thread eventfd(2) is closed, if non-zero, and set to -1.
* The per-thread epoll(7) fd is closed, if non-zero, and set to 0.
* The per-thread epoll_event structs are freed, if any, and set to 0.
*
* tsdPtr->notifierMutex is destroyed.
*
*----------------------------------------------------------------------
*/
void
PlatformEventsFinalize(
void)
{
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
pthread_mutex_lock(&tsdPtr->notifierMutex);
#ifdef HAVE_EVENTFD
if (tsdPtr->triggerEventFd) {
close(tsdPtr->triggerEventFd);
tsdPtr->triggerEventFd = -1;
}
#else
if (tsdPtr->triggerPipe[0]) {
close(tsdPtr->triggerPipe[0]);
tsdPtr->triggerPipe[0] = -1;
}
if (tsdPtr->triggerPipe[1]) {
close(tsdPtr->triggerPipe[1]);
tsdPtr->triggerPipe[1] = -1;
}
#endif /* HAVE_EVENTFD */
if (tsdPtr->eventsFd > 0) {
close(tsdPtr->eventsFd);
tsdPtr->eventsFd = 0;
}
if (tsdPtr->readyEvents) {
ckfree(tsdPtr->readyEvents);
tsdPtr->maxReadyEvents = 0;
}
pthread_mutex_unlock(&tsdPtr->notifierMutex);
if ((errno = pthread_mutex_destroy(&tsdPtr->notifierMutex))) {
Tcl_Panic("pthread_mutex_destroy: %s", strerror(errno));
}
}
/*
*----------------------------------------------------------------------
*
* PlatformEventsInit --
*
* This function abstracts creating a kqueue fd via the epoll_create
* system call and allocating memory for the epoll_event structs in
* tsdPtr for the thread of the caller.
*
* Results:
* None.
*
* Side effects:
* The following per-thread entities are initialised:
* notifierMutex is initialised.
* The eventfd(2) is created w/ EFD_CLOEXEC and EFD_NONBLOCK.
* The epoll(7) fd is created w/ EPOLL_CLOEXEC.
* A FileHandler struct is allocated and initialised for the event-
* fd(2), registering interest for TCL_READABLE on it via Platform-
* EventsControl().
* readyEvents and maxReadyEvents are initialised with 512 epoll_events.
*
*----------------------------------------------------------------------
*/
void
PlatformEventsInit(
void)
{
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
FileHandler *filePtr;
errno = pthread_mutex_init(&tsdPtr->notifierMutex, NULL);
if (errno) {
Tcl_Panic("Tcl_InitNotifier: %s", "could not create mutex");
}
filePtr = ckalloc(sizeof(*filePtr));
#ifdef HAVE_EVENTFD
if ((tsdPtr->triggerEventFd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) <= 0) {
Tcl_Panic("Tcl_InitNotifier: %s", "could not create trigger eventfd");
}
filePtr->fd = tsdPtr->triggerEventFd;
#else
if (pipe2(tsdPtr->triggerPipe, O_CLOEXEC | O_NONBLOCK) != 0) {
Tcl_Panic("Tcl_InitNotifier: %s", "could not create trigger pipe");
}
filePtr->fd = tsdPtr->triggerPipe[0];
#endif
if ((tsdPtr->eventsFd = epoll_create1(EPOLL_CLOEXEC)) == -1) {
Tcl_Panic("epoll_create1: %s", strerror(errno));
}
filePtr->mask = TCL_READABLE;
PlatformEventsControl(filePtr, tsdPtr, EPOLL_CTL_ADD, 1);
if (!tsdPtr->readyEvents) {
tsdPtr->maxReadyEvents = 512;
tsdPtr->readyEvents = ckalloc(tsdPtr->maxReadyEvents
* sizeof(tsdPtr->readyEvents[0]));
}
LIST_INIT(&tsdPtr->firstReadyFileHandlerPtr);
}
/*
*----------------------------------------------------------------------
*
* PlatformEventsTranslate --
*
* This function translates the platform-specific mask of returned
* events in eventPtr to a mask of TCL_* bits.
*
* Results:
* Returns the translated mask.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
PlatformEventsTranslate(
struct epoll_event *eventPtr)
{
int mask;
mask = 0;
if (eventPtr->events & (EPOLLIN | EPOLLHUP)) {
mask |= TCL_READABLE;
}
if (eventPtr->events & EPOLLOUT) {
mask |= TCL_WRITABLE;
}
if (eventPtr->events & EPOLLERR) {
mask |= TCL_EXCEPTION;
}
return mask;
}
/*
*----------------------------------------------------------------------
*
* PlatformEventsWait --
*
* This function abstracts waiting for I/O events via epoll_wait.
*
* Results:
* Returns -1 if epoll_wait failed. Returns 0 if polling and if no
* events became available whilst polling. Returns a pointer to and
* the count of all returned events in all other cases.
*
* Side effects:
* gettimeofday(2), epoll_wait(2), and gettimeofday(2) are called,
* in the specified order.
* If timePtr specifies a positive value, it is updated to reflect
* the amount of time that has passed; if its value would {under,
* over}flow, it is set to zero.
*
*----------------------------------------------------------------------
*/
int
PlatformEventsWait(
struct epoll_event *events,
size_t numEvents,
struct timeval *timePtr)
{
int numFound;
struct timeval tv0, tv1, tv_delta;
int timeout;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* If timePtr is NULL, epoll_wait(2) will wait indefinitely. If it
* specifies a timeout of {0,0}, epoll_wait(2) will poll. Otherwise,
* the timeout will simply be converted to milliseconds.
*/
if (!timePtr) {
timeout = -1;
} else if (!timePtr->tv_sec && !timePtr->tv_usec) {
timeout = 0;
} else {
timeout = (int)timePtr->tv_sec;
}
/*
* Call (and possibly block on) epoll_wait(2) and substract the delta
* of gettimeofday(2) before and after the call from timePtr if the
* latter is not NULL. Return the number of events returned by epoll_wait(2).
*/
gettimeofday(&tv0, NULL);
numFound = epoll_wait(tsdPtr->eventsFd, events, (int)numEvents, timeout);
gettimeofday(&tv1, NULL);
if (timePtr && (timePtr->tv_sec && timePtr->tv_usec)) {
timersub(&tv1, &tv0, &tv_delta);
if (!timercmp(&tv_delta, timePtr, >)) {
timersub(timePtr, &tv_delta, timePtr);
} else {
timePtr->tv_sec = 0;
timePtr->tv_usec = 0;
}
}
return numFound;
}
/*
*----------------------------------------------------------------------
*
* Tcl_CreateFileHandler --
*
* This function registers a file handler with the epoll notifier
* of the thread of the caller.
*
* Results:
* None.
*
* Side effects:
* Creates a new file handler structure.
* PlatformEventsControl() is called for the new file handler structure.
*
*----------------------------------------------------------------------
*/
void
Tcl_CreateFileHandler(
int fd, /* Handle of stream to watch. */
int mask, /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, and TCL_EXCEPTION: indicates
* conditions under which proc should be
* called. */
Tcl_FileProc *proc, /* Function to call for each selected
* event. */
ClientData clientData) /* Arbitrary data to pass to proc. */
{
int isNew;
if (tclNotifierHooks.createFileHandlerProc) {
tclNotifierHooks.createFileHandlerProc(fd, mask, proc, clientData);
return;
} else {
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
FileHandler *filePtr;
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
if (filePtr->fd == fd) {
break;
}
}
if (filePtr == NULL) {
filePtr = ckalloc(sizeof(FileHandler));
filePtr->fd = fd;
filePtr->readyMask = 0;
filePtr->nextPtr = tsdPtr->firstFileHandlerPtr;
tsdPtr->firstFileHandlerPtr = filePtr;
isNew = 1;
} else {
isNew = 0;
}
filePtr->proc = proc;
filePtr->clientData = clientData;
filePtr->mask = mask;
PlatformEventsControl(filePtr, tsdPtr, isNew ?
EPOLL_CTL_ADD : EPOLL_CTL_MOD, isNew);
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_DeleteFileHandler --
*
* Cancel a previously-arranged callback arrangement for a file on
* the epoll file descriptor of the thread of the caller.
*
* Results:
* None.
*
* Side effects:
* If a callback was previously registered on file, remove it.
* PlatformEventsControl() is called for the file handler structure.
* The PlatformEventData struct associated with the new file handler
* structure is freed.
*
*----------------------------------------------------------------------
*/
void
Tcl_DeleteFileHandler(
int fd) /* Stream id for which to remove callback
* function. */
{
if (tclNotifierHooks.deleteFileHandlerProc) {
tclNotifierHooks.deleteFileHandlerProc(fd);
return;
} else {
FileHandler *filePtr, *prevPtr;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
/*
* Find the entry for the given file (and return if there isn't one).
*/
for (prevPtr = NULL, filePtr = tsdPtr->firstFileHandlerPtr; ;
prevPtr = filePtr, filePtr = filePtr->nextPtr) {
if (filePtr == NULL) {
return;
}
if (filePtr->fd == fd) {
break;
}
}
/*
* Update the check masks for this file.
*/
PlatformEventsControl(filePtr, tsdPtr, EPOLL_CTL_DEL, 0);
if (filePtr->pedPtr) {
ckfree(filePtr->pedPtr);
}
/*
* Clean up information in the callback record.
*/
if (prevPtr == NULL) {
tsdPtr->firstFileHandlerPtr = filePtr->nextPtr;
} else {
prevPtr->nextPtr = filePtr->nextPtr;
}
ckfree(filePtr);
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_WaitForEvent --
*
* This function is called by Tcl_DoOneEvent to wait for new events on
* the message queue. If the block time is 0, then Tcl_WaitForEvent just
* polls without blocking.
* The waiting logic is implemented in PlatformEventsWait.
*
* Results:
* Returns -1 if PlatformEventsWait() would block forever, otherwise
* returns 0.
*
* Side effects:
* Queues file events that are detected by PlatformEventsWait().
*
*----------------------------------------------------------------------
*/
int
Tcl_WaitForEvent(
const Tcl_Time *timePtr) /* Maximum block time, or NULL. */
{
if (tclNotifierHooks.waitForEventProc) {
return tclNotifierHooks.waitForEventProc(timePtr);
} else {
FileHandler *filePtr;
int mask;
Tcl_Time vTime;
/*
* Impl. notes: timeout & timeoutPtr are used if, and only if threads
* are not enabled. They are the arguments for the regular epoll_wait()
* used when the core is not thread-enabled.
*/
struct timeval timeout, *timeoutPtr;
int numFound, numEvent;
struct PlatformEventData *pedPtr;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
int numQueued;
ssize_t i;
/*
* Set up the timeout structure. Note that if there are no events to
* check for, we return with a negative result rather than blocking
* forever.
*/
if (timePtr != NULL) {
/*
* TIP #233 (Virtualized Time). Is virtual time in effect? And do
* we actually have something to scale? If yes to both then we
* call the handler to do this scaling.
*/
if (timePtr->sec != 0 || timePtr->usec != 0) {
vTime = *timePtr;
tclScaleTimeProcPtr(&vTime, tclTimeClientData);
timePtr = &vTime;
}
timeout.tv_sec = timePtr->sec;
timeout.tv_usec = timePtr->usec;
timeoutPtr = &timeout;
} else {
timeoutPtr = NULL;
}
/*
* Walk the list of FileHandlers associated with regular files
* (S_IFREG) belonging to tsdPtr, queue Tcl events for them, and
* update their mask of events of interest.
* As epoll(7) does not support regular files, the behaviour of
* {select,poll}(2) is simply simulated here: fds associated with
* regular files are added to this list by PlatformEventsControl()
* and processed here before calling (and possibly blocking) on
* PlatformEventsWait().
*/
numQueued = 0;
LIST_FOREACH(filePtr, &tsdPtr->firstReadyFileHandlerPtr, readyNode) {
mask = 0;
if (filePtr->mask & TCL_READABLE) {
mask |= TCL_READABLE;
}
if (filePtr->mask & TCL_WRITABLE) {
mask |= TCL_WRITABLE;
}
/*
* Don't bother to queue an event if the mask was previously
* non-zero since an event must still be on the queue.
*/
if (filePtr->readyMask == 0) {
FileHandlerEvent *fileEvPtr =
ckalloc(sizeof(FileHandlerEvent));
fileEvPtr->header.proc = FileHandlerEventProc;
fileEvPtr->fd = filePtr->fd;
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
numQueued++;
}
filePtr->readyMask = mask;
}
/*
* If any events were queued in the above loop, force PlatformEvents-
* Wait() to poll as there already are events that need to be processed
* at this point.
*/
if (numQueued) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
timeoutPtr = &timeout;
}
/*
* Wait or poll for new events, queue Tcl events for the FileHandlers
* corresponding to them, and update the FileHandlers' mask of events
* of interest registered by the last call to Tcl_CreateFileHandler().
*
* Events for the eventfd(2)/trigger pipe are processed here in order
* to facilitate inter-thread IPC. If another thread intends to wake
* up this thread whilst it's blocking on PlatformEventsWait(), it
* write(2)s to the eventfd(2)/trigger pipe (see Tcl_AlertNotifier(),)
* which in turn will cause PlatformEventsWait() to return immediately.
*/
numFound = PlatformEventsWait(tsdPtr->readyEvents, tsdPtr->maxReadyEvents, timeoutPtr);
for (numEvent = 0; numEvent < numFound; numEvent++) {
pedPtr = tsdPtr->readyEvents[numEvent].data.ptr;
filePtr = pedPtr->filePtr;
mask = PlatformEventsTranslate(&tsdPtr->readyEvents[numEvent]);
#ifdef HAVE_EVENTFD
if (filePtr->fd == tsdPtr->triggerEventFd) {
uint64_t eventFdVal;
i = read(tsdPtr->triggerEventFd, &eventFdVal, sizeof(eventFdVal));
if ((i != sizeof(eventFdVal)) && (errno != EAGAIN)) {
#else
if (filePtr->fd == tsdPtr->triggerPipe[0]) {
char triggerPipeVal;
i = read(tsdPtr->triggerPipe[0], &triggerPipeVal, sizeof(triggerPipeVal));
if ((i != sizeof(triggerPipeVal)) && (errno != EAGAIN)) {
#endif
Tcl_Panic("Tcl_WaitForEvent: "
"read from %p->triggerEventFd: %s",
(void *)tsdPtr, strerror(errno));
} else {
continue;
}
}
if (!mask) {
continue;
}
/*
* Don't bother to queue an event if the mask was previously
* non-zero since an event must still be on the queue.
*/
if (filePtr->readyMask == 0) {
FileHandlerEvent *fileEvPtr =
ckalloc(sizeof(FileHandlerEvent));
fileEvPtr->header.proc = FileHandlerEventProc;
fileEvPtr->fd = filePtr->fd;
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
}
filePtr->readyMask = mask;
}
return 0;
}
}
#endif /* !HAVE_COREFOUNDATION */
#endif /* NOTIFIER_EPOLL */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|