|
41 | 41 | #include <netdb.h>
|
42 | 42 |
|
43 | 43 | #include <time.h>
|
| 44 | +#include <map> |
44 | 45 | #include "co_routine.h"
|
45 | 46 | #include "co_routine_inner.h"
|
46 | 47 | #include "co_routine_specific.h"
|
@@ -575,15 +576,48 @@ extern int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int
|
575 | 576 |
|
576 | 577 | int poll(struct pollfd fds[], nfds_t nfds, int timeout)
|
577 | 578 | {
|
578 |
| - |
579 | 579 | HOOK_SYS_FUNC( poll );
|
580 | 580 |
|
581 |
| - if( !co_is_enable_sys_hook() ) |
582 |
| - { |
583 |
| - return g_sys_poll_func( fds,nfds,timeout ); |
| 581 | + if (!co_is_enable_sys_hook() || timeout == 0) { |
| 582 | + return g_sys_poll_func(fds, nfds, timeout); |
| 583 | + } |
| 584 | + pollfd *fds_merge = NULL; |
| 585 | + nfds_t nfds_merge = 0; |
| 586 | + std::map<int, int> m; // fd --> idx |
| 587 | + std::map<int, int>::iterator it; |
| 588 | + if (nfds > 1) { |
| 589 | + fds_merge = (pollfd *)malloc(sizeof(pollfd) * nfds); |
| 590 | + for (size_t i = 0; i < nfds; i++) { |
| 591 | + if ((it = m.find(fds[i].fd)) == m.end()) { |
| 592 | + fds_merge[nfds_merge] = fds[i]; |
| 593 | + m[fds[i].fd] = nfds_merge; |
| 594 | + nfds_merge++; |
| 595 | + } else { |
| 596 | + int j = it->second; |
| 597 | + fds_merge[j].events |= fds[i].events; // merge in j slot |
| 598 | + } |
| 599 | + } |
584 | 600 | }
|
585 | 601 |
|
586 |
| - return co_poll_inner( co_get_epoll_ct(),fds,nfds,timeout, g_sys_poll_func); |
| 602 | + int ret = 0; |
| 603 | + if (nfds_merge == nfds || nfds == 1) { |
| 604 | + ret = co_poll_inner(co_get_epoll_ct(), fds, nfds, timeout, g_sys_poll_func); |
| 605 | + } else { |
| 606 | + ret = co_poll_inner(co_get_epoll_ct(), fds_merge, nfds_merge, timeout, |
| 607 | + g_sys_poll_func); |
| 608 | + if (ret > 0) { |
| 609 | + for (size_t i = 0; i < nfds; i++) { |
| 610 | + it = m.find(fds[i].fd); |
| 611 | + if (it != m.end()) { |
| 612 | + int j = it->second; |
| 613 | + fds[i].revents = fds_merge[j].revents & fds[i].events; |
| 614 | + } |
| 615 | + } |
| 616 | + } |
| 617 | + } |
| 618 | + free(fds_merge); |
| 619 | + return ret; |
| 620 | + |
587 | 621 |
|
588 | 622 | }
|
589 | 623 | int setsockopt(int fd, int level, int option_name,
|
|
0 commit comments