Back to home page

Redis cross reference

 
 

    


0001 /* Select()-based ae.c module.
0002  *
0003  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
0004  * All rights reserved.
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions are met:
0008  *
0009  *   * Redistributions of source code must retain the above copyright notice,
0010  *     this list of conditions and the following disclaimer.
0011  *   * Redistributions in binary form must reproduce the above copyright
0012  *     notice, this list of conditions and the following disclaimer in the
0013  *     documentation and/or other materials provided with the distribution.
0014  *   * Neither the name of Redis nor the names of its contributors may be used
0015  *     to endorse or promote products derived from this software without
0016  *     specific prior written permission.
0017  *
0018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0028  * POSSIBILITY OF SUCH DAMAGE.
0029  */
0030 
0031 
0032 #include <string.h>
0033 
0034 typedef struct aeApiState {
0035     fd_set rfds, wfds;
0036     /* We need to have a copy of the fd sets as it's not safe to reuse
0037      * FD sets after select(). */
0038     fd_set _rfds, _wfds;
0039 } aeApiState;
0040 
0041 static int aeApiCreate(aeEventLoop *eventLoop) {
0042     aeApiState *state = zmalloc(sizeof(aeApiState));
0043 
0044     if (!state) return -1;
0045     FD_ZERO(&state->rfds);
0046     FD_ZERO(&state->wfds);
0047     eventLoop->apidata = state;
0048     return 0;
0049 }
0050 
0051 static void aeApiFree(aeEventLoop *eventLoop) {
0052     zfree(eventLoop->apidata);
0053 }
0054 
0055 static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
0056     aeApiState *state = eventLoop->apidata;
0057 
0058     if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
0059     if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
0060     return 0;
0061 }
0062 
0063 static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
0064     aeApiState *state = eventLoop->apidata;
0065 
0066     if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);
0067     if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);
0068 }
0069 
0070 static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
0071     aeApiState *state = eventLoop->apidata;
0072     int retval, j, numevents = 0;
0073 
0074     memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
0075     memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));
0076 
0077     retval = select(eventLoop->maxfd+1,
0078                 &state->_rfds,&state->_wfds,NULL,tvp);
0079     if (retval > 0) {
0080         for (j = 0; j <= eventLoop->maxfd; j++) {
0081             int mask = 0;
0082             aeFileEvent *fe = &eventLoop->events[j];
0083 
0084             if (fe->mask == AE_NONE) continue;
0085             if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
0086                 mask |= AE_READABLE;
0087             if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
0088                 mask |= AE_WRITABLE;
0089             eventLoop->fired[numevents].fd = j;
0090             eventLoop->fired[numevents].mask = mask;
0091             numevents++;
0092         }
0093     }
0094     return numevents;
0095 }
0096 
0097 static char *aeApiName(void) {
0098     return "select";
0099 }