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
|
/*
* libmainloop/select.c
*
* Partly based on a contributed code.
*
* See the included file LICENSE for details.
*/
#include <signal.h>
#include <sys/select.h>
#include <libtu/types.h>
#include <libtu/misc.h>
#include <libtu/dlist.h>
#include "select.h"
#include "signal.h"
/*{{{ File descriptor management */
static WInputFd *input_fds=NULL;
static WInputFd *find_input_fd(int fd)
{
WInputFd *tmp=input_fds;
while(tmp){
if(tmp->fd==fd)
break;
tmp=tmp->next;
}
return tmp;
}
bool mainloop_register_input_fd(int fd, void *data,
void (*callback)(int fd, void *d))
{
WInputFd *tmp;
if(find_input_fd(fd)!=NULL)
return FALSE;
tmp=ALLOC(WInputFd);
if(tmp==NULL)
return FALSE;
tmp->fd=fd;
tmp->data=data;
tmp->process_input_fn=callback;
LINK_ITEM(input_fds, tmp, next, prev);
return TRUE;
}
void mainloop_unregister_input_fd(int fd)
{
WInputFd *tmp=find_input_fd(fd);
if(tmp!=NULL){
UNLINK_ITEM(input_fds, tmp, next, prev);
free(tmp);
}
}
static void set_input_fds(fd_set *rfds, int *nfds)
{
WInputFd *tmp=input_fds;
while(tmp){
FD_SET(tmp->fd, rfds);
if(tmp->fd>*nfds)
*nfds=tmp->fd;
tmp=tmp->next;
}
}
static void check_input_fds(fd_set *rfds)
{
WInputFd *tmp=input_fds, *next=NULL;
while(tmp){
next=tmp->next;
if(FD_ISSET(tmp->fd, rfds))
tmp->process_input_fn(tmp->fd, tmp->data);
tmp=next;
}
}
/*}}}*/
/*{{{ Select */
void mainloop_select()
{
fd_set rfds;
int nfds=0;
int ret=0;
FD_ZERO(&rfds);
set_input_fds(&rfds, &nfds);
#ifdef _POSIX_SELECT
{
sigset_t oldmask;
mainloop_block_signals(&oldmask);
if(!mainloop_unhandled_signals())
ret=pselect(nfds+1, &rfds, NULL, NULL, NULL, &oldmask);
sigprocmask(SIG_SETMASK, &oldmask, NULL);
}
#else
#warning "pselect() unavailable -- using dirty hacks"
{
struct timeval tv_={0, 0}, *tv=&tv_;
/* If there are timers, make sure we return from select with
* some delay, if the timer signal happens right before
* entering select(). Race conditions with other signals
* we'll just have to ignore without pselect().
*/
if(!libmainloop_get_timeout(tv))
tv=NULL;
if(!mainloop_unhandled_signals())
ret=select(nfds+1, &rfds, NULL, NULL, tv);
}
#endif
if(ret>0)
check_input_fds(&rfds);
}
/*}}}*/
|