@@ -852,7 +852,8 @@ stCoRoutine_t *GetCurrThreadCo( )
852
852
853
853
854
854
855
- int co_poll ( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
855
+ typedef int (*poll_pfn_t )(struct pollfd fds[], nfds_t nfds, int timeout);
856
+ int co_poll_inner ( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc)
856
857
{
857
858
858
859
if ( timeout > stTimeoutItem_t::eMaxTimeout )
@@ -884,7 +885,38 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
884
885
arg.pfnProcess = OnPollProcessEvent;
885
886
arg.pArg = GetCurrCo ( co_get_curr_thread_env () );
886
887
887
- // 2.add timeout
888
+
889
+ // 2. add epoll
890
+ for (nfds_t i=0 ;i<nfds;i++)
891
+ {
892
+ arg.pPollItems [i].pSelf = arg.fds + i;
893
+ arg.pPollItems [i].pPoll = &arg;
894
+
895
+ arg.pPollItems [i].pfnPrepare = OnPollPreparePfn;
896
+ struct epoll_event &ev = arg.pPollItems [i].stEvent ;
897
+
898
+ if ( fds[i].fd > -1 )
899
+ {
900
+ ev.data .ptr = arg.pPollItems + i;
901
+ ev.events = PollEvent2Epoll ( fds[i].events );
902
+
903
+ int ret = co_epoll_ctl ( epfd,EPOLL_CTL_ADD, fds[i].fd , &ev );
904
+ if (ret < 0 && errno == EPERM && nfds == 1 && pollfunc != NULL )
905
+ {
906
+ if ( arg.pPollItems != arr )
907
+ {
908
+ free ( arg.pPollItems );
909
+ arg.pPollItems = NULL ;
910
+ }
911
+ free (arg.fds );
912
+ free (&arg);
913
+ return pollfunc (fds, nfds, timeout);
914
+ }
915
+ }
916
+ // if fail,the timeout would work
917
+ }
918
+
919
+ // 3.add timeout
888
920
889
921
unsigned long long now = GetTickMS ();
890
922
arg.ullExpireTime = now + timeout;
@@ -905,26 +937,6 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
905
937
906
938
return -__LINE__;
907
939
}
908
- // 3. add epoll
909
-
910
- for (nfds_t i=0 ;i<nfds;i++)
911
- {
912
- arg.pPollItems [i].pSelf = arg.fds + i;
913
- arg.pPollItems [i].pPoll = &arg;
914
-
915
- arg.pPollItems [i].pfnPrepare = OnPollPreparePfn;
916
- struct epoll_event &ev = arg.pPollItems [i].stEvent ;
917
-
918
- if ( fds[i].fd > -1 )
919
- {
920
- ev.data .ptr = arg.pPollItems + i;
921
- ev.events = PollEvent2Epoll ( fds[i].events );
922
-
923
- co_epoll_ctl ( epfd,EPOLL_CTL_ADD, fds[i].fd , &ev );
924
- }
925
- // if fail,the timeout would work
926
-
927
- }
928
940
929
941
co_yield_env ( co_get_curr_thread_env () );
930
942
@@ -940,6 +952,7 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
940
952
}
941
953
942
954
955
+ int iRaiseCnt = arg.iRaiseCnt ;
943
956
if ( arg.pPollItems != arr )
944
957
{
945
958
free ( arg.pPollItems );
@@ -949,7 +962,12 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
949
962
free (arg.fds );
950
963
free (&arg);
951
964
952
- return arg.iRaiseCnt ;
965
+ return iRaiseCnt;
966
+ }
967
+
968
+ int co_poll ( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout_ms )
969
+ {
970
+ return co_poll_inner (ctx, fds, nfds, timeout_ms, NULL );
953
971
}
954
972
955
973
void SetEpoll ( stCoRoutineEnv_t *env,stCoEpoll_t *ev )
@@ -1115,3 +1133,4 @@ stCoCondItem_t *co_cond_pop( stCoCond_t *link )
1115
1133
return p;
1116
1134
}
1117
1135
1136
+
0 commit comments