root/platform/mysql/mycached/trunk/picoev_select.c @ 35002

Revision 35002, 3.2 kB (checked in by kazuho, 4 years ago)

update picoev

Line 
1/*
2 * Copyright (c) 2009, Cybozu Labs, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 *   this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 *   this list of conditions and the following disclaimer in the documentation
12 *   and/or other materials provided with the distribution.
13 * * Neither the name of the <ORGANIZATION> nor the names of its contributors
14 *   may be used to endorse or promote products derived from this software
15 *   without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/select.h>
31#include "picoev.h"
32
33picoev_globals picoev;
34
35int picoev_update_events_internal(picoev_loop* loop, int fd, int events)
36{
37  picoev.fds[fd].events = events;
38  return 0;
39}
40
41picoev_loop* picoev_create_loop(int max_timeout)
42{
43  picoev_loop* loop;
44 
45  assert(PICOEV_IS_INITED);
46  if ((loop = (picoev_loop*)malloc(sizeof(picoev_loop))) == NULL) {
47    return NULL;
48  }
49  if (picoev_init_loop_internal(loop, max_timeout) != 0) {
50    free(loop);
51    return NULL;
52  }
53 
54  return loop;
55}
56
57int picoev_destroy_loop(picoev_loop* loop)
58{
59  picoev_deinit_loop_internal(loop);
60  free(loop);
61  return 0;
62}
63
64int picoev_poll_once_internal(picoev_loop* loop, int max_wait)
65{
66  fd_set readfds, writefds, errorfds;
67  struct timeval tv;
68  int i, r, maxfd = 0;
69 
70  /* setup */
71  FD_ZERO(&readfds);
72  FD_ZERO(&writefds);
73  FD_ZERO(&errorfds);
74  for (i = 0; i < picoev.max_fd; ++i) {
75    picoev_fd* fd = picoev.fds + i;
76    if (fd->loop_id == loop->loop_id) {
77      if ((fd->events & PICOEV_READ) != 0) {
78        FD_SET(i, &readfds);
79        if (maxfd < i) {
80          maxfd = i;
81        }
82      }
83      if ((fd->events & PICOEV_WRITE) != 0) {
84        FD_SET(i, &writefds);
85        if (maxfd < i) {
86          maxfd = i;
87        }
88      }
89    }
90  }
91 
92  /* select and handle if any */
93  tv.tv_sec = max_wait;
94  tv.tv_usec = 0;
95  r = select(maxfd + 1, &readfds, &writefds, &errorfds, &tv);
96  if (r == -1) {
97    return -1;
98  } else if (r > 0) {
99    for (i = 0; i < picoev.max_fd; ++i) {
100      picoev_fd* target = picoev.fds + i;
101      if (target->loop_id == loop->loop_id) {
102        int revents = (FD_ISSET(i, &readfds) ? PICOEV_READ : 0)
103          | (FD_ISSET(i, &writefds) ? PICOEV_WRITE : 0);
104        if (revents != 0) {
105          (*target->callback)(loop, i, revents, target->cb_arg);
106        }
107      }
108    }
109  }
110 
111  return 0;
112}
Note: See TracBrowser for help on using the browser.