ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_epoll.c
(Generate patch)

Comparing libev/ev_epoll.c (file contents):
Revision 1.5 by root, Wed Oct 31 14:44:15 2007 UTC vs.
Revision 1.11 by root, Sat Nov 3 10:37:29 2007 UTC

1/* 1/*
2 * libev epoll fd activity backend
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
35epoll_modify (int fd, int oev, int nev) 37epoll_modify (int fd, int oev, int nev)
36{ 38{
37 int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL; 39 int mode = nev ? oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD : EPOLL_CTL_DEL;
38 40
39 struct epoll_event ev; 41 struct epoll_event ev;
40 ev.data.fd = fd; 42 ev.data.u64 = fd; /* use u64 to fully initialise the struct, for nicer strace etc. */
41 ev.events = 43 ev.events =
42 (nev & EV_READ ? EPOLLIN : 0) 44 (nev & EV_READ ? EPOLLIN : 0)
43 | (nev & EV_WRITE ? EPOLLOUT : 0); 45 | (nev & EV_WRITE ? EPOLLOUT : 0);
44 46
45 epoll_ctl (epoll_fd, mode, fd, &ev); 47 epoll_ctl (epoll_fd, mode, fd, &ev);
46} 48}
47 49
50static void
48void epoll_postfork_child (void) 51epoll_postfork_child (void)
49{ 52{
50 int fd; 53 int fd;
51 54
52 epoll_fd = epoll_create (256); 55 epoll_fd = epoll_create (256);
53 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC); 56 fcntl (epoll_fd, F_SETFD, FD_CLOEXEC);
54 57
55 /* re-register interest in fds */ 58 /* re-register interest in fds */
56 for (fd = 0; fd < anfdmax; ++fd) 59 for (fd = 0; fd < anfdmax; ++fd)
57 if (anfds [fd].wev) 60 if (anfds [fd].events)//D
58 epoll_modify (fd, EV_NONE, anfds [fd].wev); 61 epoll_modify (fd, EV_NONE, anfds [fd].events);
59} 62}
60 63
61static struct epoll_event *events; 64static struct epoll_event *events;
62static int eventmax; 65static int eventmax;
63 66
67static void
64static void epoll_poll (ev_tstamp timeout) 68epoll_poll (ev_tstamp timeout)
65{ 69{
66 int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.)); 70 int eventcnt = epoll_wait (epoll_fd, events, eventmax, ceil (timeout * 1000.));
67 int i; 71 int i;
68 72
69 if (eventcnt < 0) 73 if (eventcnt < 0)
75 (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) 79 (events [i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0)
76 | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0) 80 | (events [i].events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0)
77 ); 81 );
78 82
79 /* if the receive array was full, increase its size */ 83 /* if the receive array was full, increase its size */
80 if (eventcnt == eventmax) 84 if (expect_false (eventcnt == eventmax))
81 { 85 {
82 free (events); 86 free (events);
83 eventmax += eventmax >> 1; 87 eventmax = array_roundsize (events, eventmax << 1);
84 events = malloc (sizeof (struct epoll_event) * eventmax); 88 events = malloc (sizeof (struct epoll_event) * eventmax);
85 } 89 }
86} 90}
87 91
92static void
88void epoll_init (int flags) 93epoll_init (int flags)
89{ 94{
90 epoll_fd = epoll_create (256); 95 epoll_fd = epoll_create (256);
91 96
92 if (epoll_fd < 0) 97 if (epoll_fd < 0)
93 return; 98 return;
100 method_poll = epoll_poll; 105 method_poll = epoll_poll;
101 106
102 eventmax = 64; /* intiial number of events receivable per poll */ 107 eventmax = 64; /* intiial number of events receivable per poll */
103 events = malloc (sizeof (struct epoll_event) * eventmax); 108 events = malloc (sizeof (struct epoll_event) * eventmax);
104} 109}
110

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines