1 | /* |
1 | /* |
|
|
2 | * libev select 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 |
… | |
… | |
31 | #include <sys/time.h> |
33 | #include <sys/time.h> |
32 | #include <sys/types.h> |
34 | #include <sys/types.h> |
33 | #include <unistd.h> |
35 | #include <unistd.h> |
34 | |
36 | |
35 | /* for unix systems */ |
37 | /* for unix systems */ |
|
|
38 | #ifndef WIN32 |
36 | #include <sys/select.h> |
39 | # include <sys/select.h> |
|
|
40 | #endif |
37 | |
41 | |
38 | #include <string.h> |
42 | #include <string.h> |
39 | #include <inttypes.h> |
43 | #include <inttypes.h> |
40 | |
44 | |
41 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
42 | static int vec_max; |
|
|
43 | |
|
|
44 | static void |
45 | static void |
45 | select_modify (int fd, int oev, int nev) |
46 | select_modify (EV_P_ int fd, int oev, int nev) |
46 | { |
47 | { |
47 | int offs = fd >> 3; |
48 | int offs = fd >> 3; |
48 | int mask = 1 << (fd & 7); |
49 | int mask = 1 << (fd & 7); |
|
|
50 | |
|
|
51 | if (oev == nev) |
|
|
52 | return; |
49 | |
53 | |
50 | if (vec_max < (fd >> 5) + 1) |
54 | if (vec_max < (fd >> 5) + 1) |
51 | { |
55 | { |
52 | int new_max = (fd >> 5) + 1; |
56 | int new_max = (fd >> 5) + 1; |
53 | |
57 | |
54 | vec_ri = (unsigned char *)realloc (vec_ri, new_max * 4); |
58 | vec_ri = (unsigned char *)ev_realloc (vec_ri, new_max * 4); |
55 | vec_ro = (unsigned char *)realloc (vec_ro, new_max * 4); /* could free/malloc */ |
59 | vec_ro = (unsigned char *)ev_realloc (vec_ro, new_max * 4); /* could free/malloc */ |
56 | vec_wi = (unsigned char *)realloc (vec_wi, new_max * 4); |
60 | vec_wi = (unsigned char *)ev_realloc (vec_wi, new_max * 4); |
57 | vec_wo = (unsigned char *)realloc (vec_wo, new_max * 4); /* could free/malloc */ |
61 | vec_wo = (unsigned char *)ev_realloc (vec_wo, new_max * 4); /* could free/malloc */ |
58 | |
62 | |
59 | for (; vec_max < new_max; ++vec_max) |
63 | for (; vec_max < new_max; ++vec_max) |
60 | ((uint32_t *)vec_ri)[vec_max] = |
64 | ((uint32_t *)vec_ri)[vec_max] = |
61 | ((uint32_t *)vec_wi)[vec_max] = 0; |
65 | ((uint32_t *)vec_wi)[vec_max] = 0; |
62 | } |
66 | } |
… | |
… | |
69 | if (!(nev & EV_WRITE)) |
73 | if (!(nev & EV_WRITE)) |
70 | vec_wi [offs] &= ~mask; |
74 | vec_wi [offs] &= ~mask; |
71 | } |
75 | } |
72 | |
76 | |
73 | static void |
77 | static void |
74 | select_poll (ev_tstamp timeout) |
78 | select_poll (EV_P_ ev_tstamp timeout) |
75 | { |
79 | { |
|
|
80 | int word, offs; |
76 | struct timeval tv; |
81 | struct timeval tv; |
77 | int res; |
82 | int res; |
78 | |
83 | |
79 | memcpy (vec_ro, vec_ri, vec_max * 4); |
84 | memcpy (vec_ro, vec_ri, vec_max * 4); |
80 | memcpy (vec_wo, vec_wi, vec_max * 4); |
85 | memcpy (vec_wo, vec_wi, vec_max * 4); |
… | |
… | |
82 | tv.tv_sec = (long)timeout; |
87 | tv.tv_sec = (long)timeout; |
83 | tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); |
88 | tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); |
84 | |
89 | |
85 | res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); |
90 | res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); |
86 | |
91 | |
87 | if (res > 0) |
92 | if (res < 0) |
88 | { |
|
|
89 | int word, offs; |
|
|
90 | |
|
|
91 | for (word = vec_max; word--; ) |
|
|
92 | { |
|
|
93 | if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word]) |
|
|
94 | for (offs = 4; offs--; ) |
|
|
95 | { |
|
|
96 | int idx = word * 4 + offs; |
|
|
97 | unsigned char byte_r = vec_ro [idx]; |
|
|
98 | unsigned char byte_w = vec_wo [idx]; |
|
|
99 | int bit; |
|
|
100 | |
|
|
101 | if (byte_r | byte_w) |
|
|
102 | for (bit = 8; bit--; ) |
|
|
103 | { |
|
|
104 | int events = 0; |
|
|
105 | events |= byte_r & (1 << bit) ? EV_READ : 0; |
|
|
106 | events |= byte_w & (1 << bit) ? EV_WRITE : 0; |
|
|
107 | |
|
|
108 | if (events) |
|
|
109 | fd_event (idx * 8 + bit, events); |
|
|
110 | } |
|
|
111 | } |
|
|
112 | } |
|
|
113 | } |
|
|
114 | else if (res < 0) |
|
|
115 | { |
93 | { |
116 | if (errno == EBADF) |
94 | if (errno == EBADF) |
117 | fd_recheck (); |
95 | fd_ebadf (EV_A); |
|
|
96 | else if (errno == ENOMEM && !syserr_cb) |
|
|
97 | fd_enomem (EV_A); |
|
|
98 | else if (errno != EINTR) |
|
|
99 | syserr ("(libev) select"); |
|
|
100 | |
|
|
101 | return; |
|
|
102 | } |
|
|
103 | |
|
|
104 | for (word = vec_max; word--; ) |
|
|
105 | { |
|
|
106 | if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word]) |
|
|
107 | for (offs = 4; offs--; ) |
|
|
108 | { |
|
|
109 | int idx = word * 4 + offs; |
|
|
110 | unsigned char byte_r = vec_ro [idx]; |
|
|
111 | unsigned char byte_w = vec_wo [idx]; |
|
|
112 | int bit; |
|
|
113 | |
|
|
114 | if (byte_r | byte_w) |
|
|
115 | for (bit = 8; bit--; ) |
|
|
116 | { |
|
|
117 | int events = 0; |
|
|
118 | events |= byte_r & (1 << bit) ? EV_READ : 0; |
|
|
119 | events |= byte_w & (1 << bit) ? EV_WRITE : 0; |
|
|
120 | |
|
|
121 | if (events) |
|
|
122 | fd_event (EV_A_ idx * 8 + bit, events); |
|
|
123 | } |
|
|
124 | } |
118 | } |
125 | } |
119 | } |
126 | } |
120 | |
127 | |
121 | static void |
128 | static int |
122 | select_init (int flags) |
129 | select_init (EV_P_ int flags) |
123 | { |
130 | { |
124 | ev_method = EVMETHOD_SELECT; |
|
|
125 | method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */ |
131 | method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */ |
126 | method_modify = select_modify; |
132 | method_modify = select_modify; |
127 | method_poll = select_poll; |
133 | method_poll = select_poll; |
|
|
134 | |
|
|
135 | vec_max = 0; |
|
|
136 | vec_ri = 0; |
|
|
137 | vec_ri = 0; |
|
|
138 | vec_wo = 0; |
|
|
139 | vec_wo = 0; |
|
|
140 | |
|
|
141 | return EVMETHOD_SELECT; |
128 | } |
142 | } |
129 | |
143 | |
|
|
144 | static void |
|
|
145 | select_destroy (EV_P) |
|
|
146 | { |
|
|
147 | ev_free (vec_ri); |
|
|
148 | ev_free (vec_ro); |
|
|
149 | ev_free (vec_wi); |
|
|
150 | ev_free (vec_wo); |
|
|
151 | } |
|
|
152 | |
|
|
153 | |