1 /* exp_poll.c - This file contains UNIX specific procedures for
2 * poll-based notifier, which is the lowest-level part of the Tcl
3 * event loop. This file works together with ../generic/tclNotify.c.
5 * Design and implementation of this program was paid for by U.S. tax
6 * dollars. Therefore it is public domain. However, the author and
7 * NIST would appreciate credit if this program or parts of it are
10 * Written by Don Libes, NIST, 2/6/90
11 * Rewritten by Don Libes, 2/96 for new Tcl notifier paradigm.
12 * Rewritten again by Don Libes, 8/97 for yet another Tcl notifier paradigm.
20 #include <sys/types.h>
26 /* Some systems require that the poll array be non-empty so provide a
27 * 1-elt array for starters. It will be ignored as soon as it grows
31 static struct pollfd initialFdArray;
32 static struct pollfd *fdArray = &initialFdArray;
33 static int fdsInUse = 0; /* space in use */
34 static int fdsMaxSpace = 1; /* space that has actually been allocated */
39 * This file contains the implementation of the select-based
40 * Unix-specific notifier, which is the lowest-level part of the
41 * Tcl event loop. This file works together with
42 * ../generic/tclNotify.c.
44 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
46 * See the file "license.terms" for information on usage and redistribution
47 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
49 * SCCS: @(#) tclUnixNotfy.c 1.42 97/07/02 20:55:44
53 * This structure is used to keep track of the notifier info for a
57 typedef struct FileHandler {
59 int mask; /* Mask of desired events: TCL_READABLE,
61 int readyMask; /* Mask of events that have been seen since the
62 * last time file handlers were invoked for
64 Tcl_FileProc *proc; /* Procedure to call, in the style of
65 * Tcl_CreateFileHandler. */
66 ClientData clientData; /* Argument to pass to proc. */
67 int pollArrayIndex; /* index into poll array */
68 struct FileHandler *nextPtr;/* Next in list of all files we care about. */
72 * The following structure is what is added to the Tcl event queue when
73 * file handlers are ready to fire.
76 typedef struct FileHandlerEvent {
77 Tcl_Event header; /* Information that is standard for
79 int fd; /* File descriptor that is ready. Used
80 * to find the FileHandler structure for
81 * the file (can't point directly to the
82 * FileHandler structure because it could
83 * go away while the event is queued). */
87 * The following static structure contains the state information for the
88 * select based implementation of the Tcl notifier.
92 FileHandler *firstFileHandlerPtr;
93 /* Pointer to head of file handler list. */
94 fd_mask checkMasks[3*MASK_SIZE];
95 /* This array is used to build up the masks
96 * to be used in the next call to select.
97 * Bits are set in response to calls to
98 * Tcl_CreateFileHandler. */
99 fd_mask readyMasks[3*MASK_SIZE];
100 /* This array reflects the readable/writable
101 * conditions that were found to exist by the
102 * last call to select. */
103 int numFdBits; /* Number of valid bits in checkMasks
104 * (one more than highest fd for which
105 * Tcl_WatchFile has been called). */
109 * The following static indicates whether this module has been initialized.
112 static int initialized = 0;
115 * Static routines defined in this file.
118 static void InitNotifier _ANSI_ARGS_((void));
119 static void NotifierExitHandler _ANSI_ARGS_((
120 ClientData clientData));
121 static int FileHandlerEventProc _ANSI_ARGS_((Tcl_Event *evPtr,
125 *----------------------------------------------------------------------
129 * Initializes the notifier state.
135 * Creates a new exit handler.
137 *----------------------------------------------------------------------
144 memset(¬ifier, 0, sizeof(notifier));
145 Tcl_CreateExitHandler(NotifierExitHandler, NULL);
149 *----------------------------------------------------------------------
151 * NotifierExitHandler --
153 * This function is called to cleanup the notifier state before
160 * Destroys the notifier window.
162 *----------------------------------------------------------------------
166 NotifierExitHandler(clientData)
167 ClientData clientData; /* Not used. */
173 *----------------------------------------------------------------------
177 * This procedure sets the current notifier timer value. This
178 * interface is not implemented in this notifier because we are
179 * always running inside of Tcl_DoOneEvent.
187 *----------------------------------------------------------------------
191 Tcl_SetTimer(timePtr)
192 Tcl_Time *timePtr; /* Timeout value, may be NULL. */
195 * The interval timer doesn't do anything in this implementation,
196 * because the only event loop is via Tcl_DoOneEvent, which passes
197 * timeout values to Tcl_WaitForEvent.
202 *----------------------------------------------------------------------
204 * Tcl_CreateFileHandler --
206 * This procedure registers a file handler with the Xt notifier.
212 * Creates a new file handler structure and registers one or more
213 * input procedures with Xt.
215 *----------------------------------------------------------------------
219 Tcl_CreateFileHandler(fd, mask, proc, clientData)
220 int fd; /* Handle of stream to watch. */
221 int mask; /* OR'ed combination of TCL_READABLE,
222 * TCL_WRITABLE, and TCL_EXCEPTION:
223 * indicates conditions under which
224 * proc should be called. */
225 Tcl_FileProc *proc; /* Procedure to call for each
227 ClientData clientData; /* Arbitrary data to pass to proc. */
229 FileHandler *filePtr;
237 for (filePtr = notifier.firstFileHandlerPtr; filePtr != NULL;
238 filePtr = filePtr->nextPtr) {
239 if (filePtr->fd == fd) {
243 if (filePtr == NULL) {
244 filePtr = (FileHandler*) ckalloc(sizeof(FileHandler)); /* MLK */
246 filePtr->readyMask = 0;
247 filePtr->nextPtr = notifier.firstFileHandlerPtr;
248 notifier.firstFileHandlerPtr = filePtr;
250 filePtr->proc = proc;
251 filePtr->clientData = clientData;
252 filePtr->pollArrayIndex = fdsInUse;
253 cur_fd_index = fdsInUse;
256 if (fdsInUse > fdsMaxSpace) {
257 if (fdArray != &initialFdArray) ckfree((char *)fdArray);
258 fdArray = (struct pollfd *)ckalloc(fdsInUse*sizeof(struct pollfd));
259 fdsMaxSpace = fdsInUse;
262 fdArray[cur_fd_index].fd = fd;
264 /* I know that POLLIN/OUT is right. But I have no idea if POLLPRI
265 * corresponds well to TCL_EXCEPTION.
268 if (mask & TCL_READABLE) {
269 fdArray[cur_fd_index].events = POLLIN;
271 if (mask & TCL_WRITABLE) {
272 fdArray[cur_fd_index].events = POLLOUT;
274 if (mask & TCL_EXCEPTION) {
275 fdArray[cur_fd_index].events = POLLPRI;
280 *----------------------------------------------------------------------
282 * Tcl_DeleteFileHandler --
284 * Cancel a previously-arranged callback arrangement for
291 * If a callback was previously registered on file, remove it.
293 *----------------------------------------------------------------------
297 Tcl_DeleteFileHandler(fd)
298 int fd; /* Stream id for which to remove callback procedure. */
300 FileHandler *filePtr, *prevPtr, *lastPtr;
301 int index, bit, mask, i;
309 * Find the entry for the given file (and return if there
313 for (prevPtr = NULL, filePtr = notifier.firstFileHandlerPtr; ;
314 prevPtr = filePtr, filePtr = filePtr->nextPtr) {
315 if (filePtr == NULL) {
318 if (filePtr->fd == fd) {
324 * Clean up information in the callback record.
327 if (prevPtr == NULL) {
328 notifier.firstFileHandlerPtr = filePtr->nextPtr;
330 prevPtr->nextPtr = filePtr->nextPtr;
333 /* back to poll-specific code - DEL */
335 cur_fd_index = filePtr->pollArrayIndex;
338 /* if this one is last, do nothing special */
339 /* else swap with one at end of array */
341 if (cur_fd_index != fdsInUse) {
342 int lastfd_in_array = fdArray[fdsInUse].fd;
343 memcpy(&fdArray[cur_fd_index],&fdArray[fdsInUse],sizeof(struct pollfd));
345 /* update index to reflect new location in array */
346 /* first find link corresponding to last element in array */
348 for (lastPtr = notifier.firstFileHandlerPtr; filePtr; lastPtr = lastPtr->nextPtr) {
349 if (lastPtr->fd == lastfd_in_array) {
350 lastPtr->pollArrayIndex = cur_fd_index;
358 ckfree((char *) filePtr);
362 *----------------------------------------------------------------------
364 * FileHandlerEventProc --
366 * This procedure is called by Tcl_ServiceEvent when a file event
367 * reaches the front of the event queue. This procedure is
368 * responsible for actually handling the event by invoking the
369 * callback for the file handler.
372 * Returns 1 if the event was handled, meaning it should be removed
373 * from the queue. Returns 0 if the event was not handled, meaning
374 * it should stay on the queue. The only time the event isn't
375 * handled is if the TCL_FILE_EVENTS flag bit isn't set.
378 * Whatever the file handler's callback procedure does.
380 *----------------------------------------------------------------------
384 FileHandlerEventProc(evPtr, flags)
385 Tcl_Event *evPtr; /* Event to service. */
386 int flags; /* Flags that indicate what events to
387 * handle, such as TCL_FILE_EVENTS. */
389 FileHandler *filePtr;
390 FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr;
393 if (!(flags & TCL_FILE_EVENTS)) {
398 * Search through the file handlers to find the one whose handle matches
399 * the event. We do this rather than keeping a pointer to the file
400 * handler directly in the event, so that the handler can be deleted
401 * while the event is queued without leaving a dangling pointer.
404 for (filePtr = notifier.firstFileHandlerPtr; filePtr != NULL;
405 filePtr = filePtr->nextPtr) {
406 if (filePtr->fd != fileEvPtr->fd) {
411 * The code is tricky for two reasons:
412 * 1. The file handler's desired events could have changed
413 * since the time when the event was queued, so AND the
414 * ready mask with the desired mask.
415 * 2. The file could have been closed and re-opened since
416 * the time when the event was queued. This is why the
417 * ready mask is stored in the file handler rather than
418 * the queued event: it will be zeroed when a new
419 * file handler is created for the newly opened file.
422 mask = filePtr->readyMask & filePtr->mask;
423 filePtr->readyMask = 0;
425 (*filePtr->proc)(filePtr->clientData, mask);
433 *----------------------------------------------------------------------
435 * Tcl_WaitForEvent --
437 * This function is called by Tcl_DoOneEvent to wait for new
438 * events on the message queue. If the block time is 0, then
439 * Tcl_WaitForEvent just polls without blocking.
442 * Returns -1 if the select would block forever, otherwise
446 * Queues file events that are detected by the select.
448 *----------------------------------------------------------------------
452 Tcl_WaitForEvent(timePtr)
453 Tcl_Time *timePtr; /* Maximum block time, or NULL. */
455 FileHandler *filePtr;
456 FileHandlerEvent *fileEvPtr;
458 struct timeval *timeoutPtr;
460 int bit, index, mask, numFound;
467 * Set up the timeout structure. Note that if there are no events to
468 * check for, we return with a negative result rather than blocking
473 timeout = timePtr->sec*1000 + timePtr->usec/1000;
475 } else if (notifier.numFdBits == 0) {
481 numFound = poll(fdArray,fdsInUse,timeout);
484 * Queue all detected file events before returning.
487 for (filePtr = notifier.firstFileHandlerPtr;
488 (filePtr != NULL) && (numFound > 0);
489 filePtr = filePtr->nextPtr) {
490 index = filePtr->pollArrayIndex;
493 if (fdArray[index].revents & POLLIN) {
494 mask |= TCL_READABLE;
496 if (fdArray[index].revents & POLLOUT) {
497 mask |= TCL_WRITABLE;
499 /* I have no idea if this is right ... */
500 if (fdArray[index].revents & (POLLPRI|POLLERR|POLLHUP|POLLNVAL)) {
501 mask |= TCL_EXCEPTION;
511 * Don't bother to queue an event if the mask was previously
512 * non-zero since an event must still be on the queue.
515 if (filePtr->readyMask == 0) {
516 fileEvPtr = (FileHandlerEvent *) ckalloc(
517 sizeof(FileHandlerEvent));
518 fileEvPtr->header.proc = FileHandlerEventProc;
519 fileEvPtr->fd = filePtr->fd;
520 Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
522 filePtr->readyMask = mask;