| 879 | |
| 880 | typedef int (*poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout); |
| 881 | int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc) |
| 882 | { |
| 883 | if (timeout == 0) |
| 884 | { |
| 885 | return pollfunc(fds, nfds, timeout); |
| 886 | } |
| 887 | if (timeout < 0) |
| 888 | { |
| 889 | timeout = INT_MAX; |
| 890 | } |
| 891 | int epfd = ctx->iEpollFd; |
| 892 | stCoRoutine_t* self = co_self(); |
| 893 | |
| 894 | //1.struct change |
| 895 | stPoll_t& arg = *((stPoll_t*)malloc(sizeof(stPoll_t))); |
| 896 | memset( &arg,0,sizeof(arg) ); |
| 897 | |
| 898 | arg.iEpollFd = epfd; |
| 899 | arg.fds = (pollfd*)calloc(nfds, sizeof(pollfd)); |
| 900 | arg.nfds = nfds; |
| 901 | |
| 902 | stPollItem_t arr[2]; |
| 903 | if( nfds < sizeof(arr) / sizeof(arr[0]) && !self->cIsShareStack) |
| 904 | { |
| 905 | arg.pPollItems = arr; |
| 906 | } |
| 907 | else |
| 908 | { |
| 909 | arg.pPollItems = (stPollItem_t*)malloc( nfds * sizeof( stPollItem_t ) ); |
| 910 | } |
| 911 | memset( arg.pPollItems,0,nfds * sizeof(stPollItem_t) ); |
| 912 | |
| 913 | arg.pfnProcess = OnPollProcessEvent; |
| 914 | arg.pArg = GetCurrCo( co_get_curr_thread_env() ); |
| 915 | |
| 916 | |
| 917 | //2. add epoll |
| 918 | for(nfds_t i=0;i<nfds;i++) |
| 919 | { |
| 920 | arg.pPollItems[i].pSelf = arg.fds + i; |
| 921 | arg.pPollItems[i].pPoll = &arg; |
| 922 | |
| 923 | arg.pPollItems[i].pfnPrepare = OnPollPreparePfn; |
| 924 | struct epoll_event &ev = arg.pPollItems[i].stEvent; |
| 925 | |
| 926 | if( fds[i].fd > -1 ) |
| 927 | { |
| 928 | ev.data.ptr = arg.pPollItems + i; |
| 929 | ev.events = PollEvent2Epoll( fds[i].events ); |
| 930 | |
| 931 | int ret = co_epoll_ctl( epfd,EPOLL_CTL_ADD, fds[i].fd, &ev ); |
| 932 | if (ret < 0 && errno == EPERM && nfds == 1 && pollfunc != NULL) |
| 933 | { |
| 934 | if( arg.pPollItems != arr ) |
| 935 | { |
| 936 | free( arg.pPollItems ); |
| 937 | arg.pPollItems = NULL; |
| 938 | } |
no test coverage detected