Lines Matching defs:fde
62 static void dump_fde(fdevent *fde, const char *info)
65 fprintf(stderr,"FDE #%03d %c%c%c %s\n", fde->fd,
66 fde->state & FDE_READ ? 'R' : ' ',
67 fde->state & FDE_WRITE ? 'W' : ' ',
68 fde->state & FDE_ERROR ? 'E' : ' ',
73 #define dump_fde(fde, info) do { } while(0)
117 static void fdevent_connect(fdevent *fde)
123 ev.data.ptr = fde;
126 if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) {
133 static void fdevent_disconnect(fdevent *fde)
139 ev.data.ptr = fde;
146 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fde->fd, &ev);
149 static void fdevent_update(fdevent *fde, unsigned events)
154 active = (fde->state & FDE_EVENTMASK) != 0;
158 ev.data.ptr = fde;
164 fde->state = (fde->state & FDE_STATEMASK) | events;
172 if(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fde->fd, &ev)) {
177 if(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fde->fd, &ev)) {
187 if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) {
198 fdevent *fde;
211 fde = ev->data.ptr;
214 fde->events |= FDE_READ;
217 fde->events |= FDE_WRITE;
220 fde->events |= FDE_ERROR;
222 if(fde->events) {
223 if(fde->state & FDE_PENDING) continue;
224 fde->state |= FDE_PENDING;
225 fdevent_plist_enqueue(fde);
251 static void fdevent_connect(fdevent *fde)
253 if(fde->fd >= select_n) {
254 select_n = fde->fd + 1;
258 static void fdevent_disconnect(fdevent *fde)
262 FD_CLR(fde->fd, &read_fds);
263 FD_CLR(fde->fd, &write_fds);
264 FD_CLR(fde->fd, &error_fds);
272 static void fdevent_update(fdevent *fde, unsigned events)
275 FD_SET(fde->fd, &read_fds);
277 FD_CLR(fde->fd, &read_fds);
280 FD_SET(fde->fd, &write_fds);
282 FD_CLR(fde->fd, &write_fds);
285 FD_SET(fde->fd, &error_fds);
287 FD_CLR(fde->fd, &error_fds);
290 fde->state = (fde->state & FDE_STATEMASK) | events;
299 fdevent *fde;
302 fde = fd_table[i];
303 if(fde == 0) continue;
307 // fde->state |= FDE_DONT_CLOSE;
320 fdevent *fde;
341 fde = fd_table[i];
343 if(fde == 0) {
359 fdevent *fde;
402 fde = fd_table[i];
403 if(fde == 0)
404 FATAL("missing fde for fd %d\n", i);
406 fde->events |= events;
408 D("got events fde->fd=%d events=%04x, state=%04x\n",
409 fde->fd, fde->events, fde->state);
410 if(fde->state & FDE_PENDING) continue;
411 fde->state |= FDE_PENDING;
412 fdevent_plist_enqueue(fde);
419 static void fdevent_register(fdevent *fde)
421 if(fde->fd < 0) {
422 FATAL("bogus negative fd (%d)\n", fde->fd);
425 if(fde->fd >= fd_table_max) {
427 if(fde->fd > 32000) {
428 FATAL("bogus huuuuge fd (%d)\n", fde->fd);
434 while(fd_table_max <= fde->fd) {
444 fd_table[fde->fd] = fde;
447 static void fdevent_unregister(fdevent *fde)
449 if((fde->fd < 0) || (fde->fd >= fd_table_max)) {
450 FATAL("fd out of range (%d)\n", fde->fd);
453 if(fd_table[fde->fd] != fde) {
454 FATAL("fd_table out of sync [%d]\n", fde->fd);
457 fd_table[fde->fd] = 0;
459 if(!(fde->state & FDE_DONT_CLOSE)) {
460 dump_fde(fde, "close");
461 adb_close(fde->fd);
498 static void fdevent_call_fdfunc(fdevent* fde)
500 unsigned events = fde->events;
501 fde->events = 0;
502 if(!(fde->state & FDE_PENDING)) return;
503 fde->state &= (~FDE_PENDING);
504 dump_fde(fde, "callback");
505 fde->func(fde->fd, events, fde->arg);
513 // Hook oneself back into the fde's suitable for select() on read.
517 fdevent *fde = fd_table[fd];
518 fdevent_add(fde, FDE_READ);
568 fdevent *fde = (fdevent*) malloc(sizeof(fdevent));
569 if(fde == 0) return 0;
570 fdevent_install(fde, fd, func, arg);
571 fde->state |= FDE_CREATED;
572 return fde;
575 void fdevent_destroy(fdevent *fde)
577 if(fde == 0) return;
578 if(!(fde->state & FDE_CREATED)) {
579 FATAL("fde %p not created by fdevent_create()\n", fde);
581 fdevent_remove(fde);
584 void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg)
586 memset(fde, 0, sizeof(fdevent));
587 fde->state = FDE_ACTIVE;
588 fde->fd = fd;
589 fde->force_eof = 0;
590 fde->func = func;
591 fde->arg = arg;
596 fdevent_register(fde);
597 dump_fde(fde, "connect");
598 fdevent_connect(fde);
599 fde->state |= FDE_ACTIVE;
602 void fdevent_remove(fdevent *fde)
604 if(fde->state & FDE_PENDING) {
605 fdevent_plist_remove(fde);
608 if(fde->state & FDE_ACTIVE) {
609 fdevent_disconnect(fde);
610 dump_fde(fde, "disconnect");
611 fdevent_unregister(fde);
614 fde->state = 0;
615 fde->events = 0;
619 void fdevent_set(fdevent *fde, unsigned events)
623 if((fde->state & FDE_EVENTMASK) == events) return;
625 if(fde->state & FDE_ACTIVE) {
626 fdevent_update(fde, events);
627 dump_fde(fde, "update");
630 fde->state = (fde->state & FDE_STATEMASK) | events;
632 if(fde->state & FDE_PENDING) {
637 fde->events &= (~events);
638 if(fde
639 fdevent_plist_remove(fde);
640 fde->state &= (~FDE_PENDING);
645 void fdevent_add(fdevent *fde, unsigned events)
648 fde, (fde->state & FDE_EVENTMASK) | (events & FDE_EVENTMASK));
651 void fdevent_del(fdevent *fde, unsigned events)
654 fde, (fde->state & FDE_EVENTMASK) & (~(events & FDE_EVENTMASK)));
665 fdevent *fde;
666 fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
667 if(!fde)
669 fdevent_add(fde, FDE_READ);
674 fdevent *fde;
682 while((fde = fdevent_plist_dequeue())) {
683 fdevent_call_fdfunc(fde);