@@ -75,13 +75,7 @@ struct mevent {
75
75
LIST_ENTRY (mevent ) me_list ;
76
76
};
77
77
78
- struct ctl_event {
79
- int op ;
80
- int fd ;
81
- struct epoll_event ee ;
82
- };
83
-
84
- static LIST_HEAD (listhead , mevent ) global_head , change_head ;
78
+ static LIST_HEAD (listhead , mevent ) global_head ;
85
79
86
80
static void
87
81
mevent_qlock (void )
@@ -141,80 +135,25 @@ mevent_kq_filter(struct mevent *mevp)
141
135
return retval ;
142
136
}
143
137
144
- static int
145
- mevent_kq_flags (struct mevent * mevp )
146
- {
147
- int ret ;
148
-
149
- switch (mevp -> me_state ) {
150
- case MEV_ADD :
151
- ret = EPOLL_CTL_ADD ; /* implicitly enabled */
152
- break ;
153
- case MEV_DEL_PENDING :
154
- ret = EPOLL_CTL_DEL ;
155
- break ;
156
- default :
157
- assert (0 );
158
- break ;
159
- }
160
-
161
- return ret ;
162
- }
163
-
164
- static int
165
- mevent_build (int mfd , struct ctl_event * kev )
166
- {
167
- struct mevent * mevp , * tmpp ;
168
- int i ;
169
-
170
- i = 0 ;
171
-
172
- mevent_qlock ();
173
-
174
- list_foreach_safe (mevp , & change_head , me_list , tmpp ) {
175
- if (mevp -> me_closefd ) {
176
- /*
177
- * A close of the file descriptor will remove the
178
- * event
179
- */
180
- close (mevp -> me_fd );
181
- } else {
182
- kev [i ].fd = mevp -> me_fd ;
183
- kev [i ].ee .events = mevent_kq_filter (mevp );
184
- kev [i ].op = mevent_kq_flags (mevp );
185
- kev [i ].ee .data .ptr = mevp ;
186
- i ++ ;
187
- }
188
-
189
- mevp -> me_cq = 0 ;
190
- LIST_REMOVE (mevp , me_list );
191
-
192
- if (mevp -> me_state == MEV_DEL_PENDING )
193
- free (mevp );
194
- else
195
- LIST_INSERT_HEAD (& global_head , mevp , me_list );
196
-
197
- assert (i < MEVENT_MAX );
198
- }
199
-
200
- mevent_qunlock ();
201
-
202
- return i ;
203
- }
204
-
205
138
static void
206
139
mevent_destroy ()
207
140
{
208
141
struct mevent * mevp , * tmpp ;
142
+ struct epoll_event ee ;
209
143
210
144
mevent_qlock ();
211
145
212
146
list_foreach_safe (mevp , & global_head , me_list , tmpp ) {
147
+ LIST_REMOVE (mevp , me_list );
148
+ ee .events = mevent_kq_filter (mevp );
149
+ ee .data .ptr = mevp ;
150
+ epoll_ctl (epoll_fd , EPOLL_CTL_DEL , mevp -> me_fd , & ee );
151
+
213
152
if ((mevp -> me_type == EVF_READ ||
214
153
mevp -> me_type == EVF_WRITE )
215
154
&& mevp -> me_fd != STDIN_FILENO )
216
155
close (mevp -> me_fd );
217
- LIST_REMOVE ( mevp , me_list );
156
+
218
157
free (mevp );
219
158
}
220
159
@@ -239,6 +178,8 @@ struct mevent *
239
178
mevent_add (int tfd , enum ev_type type ,
240
179
void (* func )(int , enum ev_type , void * ), void * param )
241
180
{
181
+ int ret ;
182
+ struct epoll_event ee ;
242
183
struct mevent * lp , * mevp ;
243
184
244
185
if (tfd < 0 || func == NULL )
@@ -247,44 +188,41 @@ mevent_add(int tfd, enum ev_type type,
247
188
if (type == EVF_TIMER )
248
189
return NULL ;
249
190
250
- mevp = NULL ;
251
-
252
191
mevent_qlock ();
253
-
254
- /*
255
- * Verify that the fd/type tuple is not present in any list
256
- */
192
+ /* Verify that the fd/type tuple is not present in the list */
257
193
LIST_FOREACH (lp , & global_head , me_list ) {
258
- if (lp -> me_fd == tfd && lp -> me_type == type )
259
- goto exit ;
260
- }
261
-
262
- LIST_FOREACH (lp , & change_head , me_list ) {
263
- if (lp -> me_fd == tfd && lp -> me_type == type )
264
- goto exit ;
194
+ if (lp -> me_fd == tfd && lp -> me_type == type ) {
195
+ mevent_qunlock ();
196
+ return lp ;
197
+ }
265
198
}
199
+ mevent_qunlock ();
266
200
267
201
/*
268
- * Allocate an entry, populate it, and add it to the change list.
202
+ * Allocate an entry, populate it, and add it to the list.
269
203
*/
270
204
mevp = calloc (1 , sizeof (struct mevent ));
271
205
if (mevp == NULL )
272
- goto exit ;
206
+ return NULL ;
273
207
274
208
mevp -> me_fd = tfd ;
275
209
mevp -> me_type = type ;
276
210
mevp -> me_func = func ;
277
211
mevp -> me_param = param ;
278
212
279
- LIST_INSERT_HEAD ( & change_head , mevp , me_list );
280
- mevp -> me_cq = 1 ;
281
- mevp -> me_state = MEV_ADD ;
282
- mevent_notify ();
283
-
284
- exit :
285
- mevent_qunlock ();
213
+ ee . events = mevent_kq_filter ( mevp );
214
+ ee . data . ptr = mevp ;
215
+ ret = epoll_ctl ( epoll_fd , EPOLL_CTL_ADD , mevp -> me_fd , & ee ) ;
216
+ if ( ret == 0 ) {
217
+ mevent_qlock ();
218
+ LIST_INSERT_HEAD ( & global_head , mevp , me_list );
219
+ mevent_qunlock ();
286
220
287
- return mevp ;
221
+ return mevp ;
222
+ } else {
223
+ free (mevp );
224
+ return NULL ;
225
+ }
288
226
}
289
227
290
228
int
@@ -302,25 +240,20 @@ mevent_disable(struct mevent *evp)
302
240
static int
303
241
mevent_delete_event (struct mevent * evp , int closefd )
304
242
{
243
+ struct epoll_event ee ;
244
+
305
245
mevent_qlock ();
246
+ LIST_REMOVE (evp , me_list );
247
+ mevent_qunlock ();
306
248
307
- /*
308
- * Place the entry onto the changed list if not already there, and
309
- * mark as to be deleted.
310
- */
311
- if (evp -> me_cq == 0 ) {
312
- evp -> me_cq = 1 ;
313
- LIST_REMOVE (evp , me_list );
314
- LIST_INSERT_HEAD (& change_head , evp , me_list );
315
- mevent_notify ();
316
- }
317
- evp -> me_state = MEV_DEL_PENDING ;
249
+ ee .events = mevent_kq_filter (evp );
250
+ ee .data .ptr = evp ;
251
+ epoll_ctl (epoll_fd , EPOLL_CTL_DEL , evp -> me_fd , & ee );
318
252
319
253
if (closefd )
320
- evp -> me_closefd = 1 ;
321
-
322
- mevent_qunlock ();
254
+ close (evp -> me_fd );
323
255
256
+ free (evp );
324
257
return 0 ;
325
258
}
326
259
@@ -364,11 +297,9 @@ mevent_deinit(void)
364
297
void
365
298
mevent_dispatch (void )
366
299
{
367
- struct ctl_event clist [MEVENT_MAX ];
368
300
struct epoll_event eventlist [MEVENT_MAX ];
369
301
370
302
struct mevent * pipev ;
371
- int numev ;
372
303
int ret ;
373
304
374
305
mevent_tid = pthread_self ();
@@ -392,24 +323,6 @@ mevent_dispatch(void)
392
323
assert (pipev != NULL );
393
324
394
325
for (;;) {
395
- /*
396
- * Build changelist if required.
397
- * XXX the changelist can be put into the blocking call
398
- * to eliminate the extra syscall. Currently better for
399
- * debug.
400
- */
401
- int i ;
402
- struct epoll_event * e ;
403
-
404
- numev = mevent_build (epoll_fd , clist );
405
-
406
- for (i = 0 ; i < numev ; i ++ ) {
407
- e = & clist [i ].ee ;
408
- ret = epoll_ctl (epoll_fd , clist [i ].op , clist [i ].fd , e );
409
- if (ret == -1 )
410
- perror ("Error return from epoll_ctl" );
411
- }
412
-
413
326
/*
414
327
* Block awaiting events
415
328
*/
@@ -425,5 +338,4 @@ mevent_dispatch(void)
425
338
if (vm_get_suspend_mode () != VM_SUSPEND_NONE )
426
339
break ;
427
340
}
428
- mevent_build (epoll_fd , clist );
429
341
}
0 commit comments