Asterisk Source/Select Poll
AsteriskSource/SelectPoll ¶* select
If the readfs , writefs , and errorfds arguments are all null pointers and the timeout argument is a null pointer, select() blocks until interrupted by a signal. The call returns the number of `ready' FDs found, and the three fdsets are modified in-place, with only the ready FDs left in the sets. Use the `FD_ISSET' macro to test the returned sets. * poll
#define POLLIN 0x0001 // ÀÐÀ» µ¥ÀÌŸ°¡ ÀÖ´Ù. #define POLLPRI 0x0002 // ±ä±ÞÇÑ ÀÐÀ» µ¥ÀÌŸ°¡ ÀÖ´Ù. #define POLLOUT 0x0004 // ¾²±â°¡ ºÀ¼â(block)°¡ ¾Æ´Ï´Ù. #define POLLERR 0x0008 // ¿¡·¯¹ß»ý #define POLLHUP 0x0010 // ¿¬°áÀÌ ²÷°åÀ½ #define POLLNVAL 0x0020 // ÆÄÀÏÁö½ÃÀÚ°¡ ¿¸®Áö ¾ÊÀº°Í°°Àº // Invalid request (À߸øµÈ ¿äû)
If none of the defined events have occurred on any selected file descriptor, poll() shall wait at least timeout milliseconds for an event to occur on any of the selected file descriptors. If the value of timeout is 0, poll() shall return immediately. If the value of timeout is -1, poll() shall block until a requested event occurs or until the call is interrupted. RETURN value Upon successful completion, poll() shall return a non-negative value. A positive value indicates the total number of file descriptors that have been selected (that is, file descriptors for which the revents member is non-zero). A value of 0 indicates that the call timed out and no file descriptors have been selected. Upon failure, poll() shall return -1 and set errno to indicate the error. schedule.c ¶* chan_sip.c
struct sched_context *sched_context_create(void) { struct sched_context *tmp; tmp = malloc(sizeof(struct sched_context)); if (tmp) { tmp->eventcnt = 1; no. of events processed tmp->schedcnt = 0; no. of outstanding schedule events tmp->schedq = NULL; schedule entry and main queue #ifdef SCHED_MAX_CACHE tmp->schedc = NULL; tmp->schedccnt = 0; #endif } return tmp; }
int ast_sched_wait(struct sched_context *con) { /* * Return the number of milliseconds * until the next scheduled event */ struct timeval tv; int ms; DEBUG(ast_log(LOG_DEBUG, "ast_sched_wait()\n")); if (!con->schedq) return -1; if (gettimeofday(&tv, NULL) < 0) { /* This should never happen */ return 0; }; ms = (con->schedq->when.tv_sec - tv.tv_sec) * 1000; ms += (con->schedq->when.tv_usec - tv.tv_usec) / 1000; if (ms < 0) ms = 0; return ms; }
static struct sched *sched_alloc(struct sched_context *con) { /* * We keep a small cache of schedule entries * to minimize the number of necessary malloc()'s */ struct sched *tmp; #ifdef SCHED_MAX_CACHE if (con->schedc) { tmp = con->schedc; con->schedc = con->schedc->next; con->schedccnt--; } else #endif tmp = malloc(sizeof(struct sched)); return tmp; }* static inline int sched_settime(struct timeval *tv, int when) static inline int sched_settime(struct timeval *tv, int when) { if (gettimeofday(tv, NULL) < 0) { /* This shouldn't ever happen, but let's be sure */ ast_log(LOG_NOTICE, "gettimeofday() failed!\n"); return -1; } tv->tv_sec += when/1000; tv->tv_usec += (when % 1000) * 1000; if (tv->tv_usec > 1000000) { tv->tv_sec++; tv->tv_usec-= 1000000; } return 0; }* static void schedule(struct sched_context *con, struct sched *s) static void schedule(struct sched_context *con, struct sched *s) { /* * Take a sched structure and put it in the * queue, such that the soonest event is * first in the list. */ struct sched *last=NULL; struct sched *current=con->schedq; while(current) { if (SOONER(s->when, current->when)) break; last = current; current = current->next; } /* Insert this event into the schedule */ s->next = current; if (last) last->next = s; else con->schedq = s; con->schedcnt++; }* static void sched_release(struct sched_context *con, struct sched *tmp) static void sched_release(struct sched_context *con, struct sched *tmp) { /* * Add to the cache, or just free() if we * already have too many cache entries */ #ifdef SCHED_MAX_CACHE if (con->schedccnt < SCHED_MAX_CACHE) { tmp->next = con->schedc; con->schedc = tmp; con->schedccnt++; } else #endif free(tmp); } |