Lines Matching defs:fde
70 static void dump_fde(fdevent *fde, const char *info)
73 fprintf(stderr,"FDE #%03d %c%c%c %s\n", fde->fd,
74 fde->state & FDE_READ ? 'R' : ' ',
75 fde->state & FDE_WRITE ? 'W' : ' ',
76 fde->state & FDE_ERROR ? 'E' : ' ',
82 #define dump_fde(fde, info) do { } while(0)
126 static void fdevent_connect(fdevent *fde)
132 ev.data.ptr = fde;
135 if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) {
142 static void fdevent_disconnect(fdevent *fde)
148 ev.data.ptr = fde;
155 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fde->fd, &ev);
158 static void fdevent_update(fdevent *fde, unsigned events)
163 active = (fde->state & FDE_EVENTMASK) != 0;
167 ev.data.ptr = fde;
173 fde->state = (fde->state & FDE_STATEMASK) | events;
181 if(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fde->fd, &ev)) {
186 if(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fde->fd, &ev)) {
196 if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) {
207 fdevent *fde;
220 fde = ev->data.ptr;
223 fde->events |= FDE_READ;
226 fde->events |= FDE_WRITE;
229 fde->events |= FDE_ERROR;
231 if(fde->events) {
232 if(fde->state & FDE_PENDING) continue;
233 fde->state |= FDE_PENDING;
234 fdevent_plist_enqueue(fde);
260 static void fdevent_connect(fdevent *fde)
262 if(fde->fd >= select_n) {
263 select_n = fde->fd + 1;
267 static void fdevent_disconnect(fdevent *fde)
271 FD_CLR(fde->fd, &read_fds);
272 FD_CLR(fde->fd, &write_fds);
273 FD_CLR(fde->fd, &error_fds);
281 static void fdevent_update(fdevent *fde, unsigned events)
284 FD_SET(fde->fd, &read_fds);
286 FD_CLR(fde->fd, &read_fds);
289 FD_SET(fde->fd, &write_fds);
291 FD_CLR(fde->fd, &write_fds);
294 FD_SET(fde->fd, &error_fds);
296 FD_CLR(fde->fd, &error_fds);
299 fde->state = (fde->state & FDE_STATEMASK) | events;
308 fdevent *fde;
311 fde = fd_table[i];
312 if(fde == 0) continue;
316 // fde->state |= FDE_DONT_CLOSE;
329 fdevent *fde;
350 fde = fd_table[i];
352 if(fde == 0) {
368 fdevent *fde;
411 fde = fd_table[i];
412 if(fde == 0)
413 FATAL("missing fde for fd %d\n", i);
415 fde->events |= events;
417 D("got events fde->fd=%d events=%04x, state=%04x\n",
418 fde->fd, fde->events, fde->state);
419 if(fde->state & FDE_PENDING) continue;
420 fde->state |= FDE_PENDING;
421 fdevent_plist_enqueue(fde);
428 static void fdevent_register(fdevent *fde)
430 if(fde->fd < 0) {
431 FATAL("bogus negative fd (%d)\n", fde->fd);
434 if(fde->fd >= fd_table_max) {
436 if(fde->fd > 32000) {
437 FATAL("bogus huuuuge fd (%d)\n", fde->fd);
443 while(fd_table_max <= fde->fd) {
453 fd_table[fde->fd] = fde;
456 static void fdevent_unregister(fdevent *fde)
458 if((fde->fd < 0) || (fde->fd >= fd_table_max)) {
459 FATAL("fd out of range (%d)\n", fde->fd);
462 if(fd_table[fde->fd] != fde) {
463 FATAL("fd_table out of sync [%d]\n", fde->fd);
466 fd_table[fde->fd] = 0;
468 if(!(fde->state & FDE_DONT_CLOSE)) {
469 dump_fde(fde, "close");
470 adb_close(fde->fd);
507 static void fdevent_call_fdfunc(fdevent* fde)
509 unsigned events = fde->events;
510 fde->events = 0;
511 if(!(fde->state & FDE_PENDING)) return;
512 fde->state &= (~FDE_PENDING);
513 dump_fde(fde, "callback");
514 fde->func(fde->fd, events, fde->arg);
522 // Hook oneself back into the fde's suitable for select() on read.
526 fdevent *fde = fd_table[fd];
527 fdevent_add(fde, FDE_READ);
577 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
578 if(fde == 0) return 0;
579 fdevent_install(fde, fd, func, arg);
580 fde->state |= FDE_CREATED;
581 return fde;
584 void fdevent_destroy(fdevent *fde)
586 if(fde == 0) return;
587 if(!(fde->state & FDE_CREATED)) {
588 FATAL("fde %p not created by fdevent_create()\n", fde);
590 fdevent_remove(fde);
593 void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg)
595 memset(fde, 0, sizeof(fdevent));
596 fde->state = FDE_ACTIVE;
597 fde->fd = fd;
598 fde->force_eof = 0;
599 fde->func = func;
600 fde->arg = arg;
605 fdevent_register(fde);
606 dump_fde(fde, "connect");
607 fdevent_connect(fde);
608 fde->state |= FDE_ACTIVE;
611 void fdevent_remove(fdevent *fde)
613 if(fde->state & FDE_PENDING) {
614 fdevent_plist_remove(fde);
617 if(fde->state & FDE_ACTIVE) {
618 fdevent_disconnect(fde);
619 dump_fde(fde, "disconnect");
620 fdevent_unregister(fde);
623 fde->state = 0;
624 fde->events = 0;
628 void fdevent_set(fdevent *fde, unsigned events)
632 if((fde->state & FDE_EVENTMASK) == events) return;
634 if(fde
635 fdevent_update(fde, events);
636 dump_fde(fde, "update");
639 fde->state = (fde->state & FDE_STATEMASK) | events;
641 if(fde->state & FDE_PENDING) {
646 fde->events &= (~events);
647 if(fde->events == 0) {
648 fdevent_plist_remove(fde);
649 fde->state &= (~FDE_PENDING);
654 void fdevent_add(fdevent *fde, unsigned events)
657 fde, (fde->state & FDE_EVENTMASK) | (events & FDE_EVENTMASK));
660 void fdevent_del(fdevent *fde, unsigned events)
663 fde, (fde->state & FDE_EVENTMASK) & (~(events & FDE_EVENTMASK)));
674 fdevent *fde;
675 fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
676 if(!fde)
678 fdevent_add(fde, FDE_READ);
683 fdevent *fde;
691 while((fde = fdevent_plist_dequeue())) {
692 fdevent_call_fdfunc(fde);