Skip to content

Commit cf0961b

Browse files
and handle epoll_ctl return value
1 parent a4a9675 commit cf0961b

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

co_hook_sys_call.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ ssize_t recv( int socket, void *buffer, size_t length, int flags )
517517

518518
}
519519

520+
extern int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc);
521+
520522
int poll(struct pollfd fds[], nfds_t nfds, int timeout)
521523
{
522524

@@ -527,7 +529,7 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout)
527529
return g_sys_poll_func( fds,nfds,timeout );
528530
}
529531

530-
return co_poll( co_get_epoll_ct(),fds,nfds,timeout );
532+
return co_poll_inner( co_get_epoll_ct(),fds,nfds,timeout, g_sys_poll_func);
531533

532534
}
533535
int setsockopt(int fd, int level, int option_name,
@@ -907,3 +909,6 @@ struct hostent *co_gethostbyname(const char *name)
907909
co->cEnableSysHook = 1;
908910
}
909911
}
912+
913+
914+

co_routine.cpp

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,8 @@ stCoRoutine_t *GetCurrThreadCo( )
852852

853853

854854

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)
856857
{
857858

858859
if( timeout > stTimeoutItem_t::eMaxTimeout )
@@ -884,7 +885,38 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
884885
arg.pfnProcess = OnPollProcessEvent;
885886
arg.pArg = GetCurrCo( co_get_curr_thread_env() );
886887

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
888920

889921
unsigned long long now = GetTickMS();
890922
arg.ullExpireTime = now + timeout;
@@ -905,26 +937,6 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
905937

906938
return -__LINE__;
907939
}
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-
}
928940

929941
co_yield_env( co_get_curr_thread_env() );
930942

@@ -940,6 +952,7 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
940952
}
941953

942954

955+
int iRaiseCnt = arg.iRaiseCnt;
943956
if( arg.pPollItems != arr )
944957
{
945958
free( arg.pPollItems );
@@ -949,7 +962,12 @@ int co_poll( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout )
949962
free(arg.fds);
950963
free(&arg);
951964

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);
953971
}
954972

955973
void SetEpoll( stCoRoutineEnv_t *env,stCoEpoll_t *ev )
@@ -1115,3 +1133,4 @@ stCoCondItem_t *co_cond_pop( stCoCond_t *link )
11151133
return p;
11161134
}
11171135

1136+

0 commit comments

Comments
 (0)