Skip to content

Commit 8ce6dfe

Browse files
add infinite timeout and fix epoll_ctl leak bug; #46
1 parent 34b2d51 commit 8ce6dfe

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

co_routine.cpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <arpa/inet.h>
3939
#include <sys/syscall.h>
4040
#include <unistd.h>
41+
#include <limits.h>
4142

4243
extern "C"
4344
{
@@ -395,14 +396,15 @@ int AddTimeout( stTimeout_t *apTimeout,stTimeoutItem_t *apItem ,unsigned long lo
395396

396397
return __LINE__;
397398
}
398-
int diff = apItem->ullExpireTime - apTimeout->ullStart;
399+
unsigned long long diff = apItem->ullExpireTime - apTimeout->ullStart;
399400

400-
if( diff >= apTimeout->iItemSize )
401+
if( diff >= (unsigned long long)apTimeout->iItemSize )
401402
{
403+
diff = apTimeout->iItemSize - 1;
402404
co_log_err("CO_ERR: AddTimeout line %d diff %d",
403405
__LINE__,diff);
404406

405-
return __LINE__;
407+
//return __LINE__;
406408
}
407409
AddTail( apTimeout->pItems + ( apTimeout->llStartIdx + diff ) % apTimeout->iItemSize , apItem );
408410

@@ -802,6 +804,16 @@ void co_eventloop( stCoEpoll_t *ctx,pfn_co_eventloop_t pfn,void *arg )
802804
{
803805

804806
PopHead<stTimeoutItem_t,stTimeoutItemLink_t>( active );
807+
if (lp->bTimeout && now < lp->ullExpireTime)
808+
{
809+
int ret = AddTimeout(ctx->pTimeout, lp, now);
810+
if (!ret)
811+
{
812+
lp->bTimeout = false;
813+
lp = active->head;
814+
continue;
815+
}
816+
}
805817
if( lp->pfnProcess )
806818
{
807819
lp->pfnProcess( lp );
@@ -868,10 +880,13 @@ stCoRoutine_t *GetCurrThreadCo( )
868880
typedef int (*poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout);
869881
int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc)
870882
{
871-
872-
if( timeout > stTimeoutItem_t::eMaxTimeout )
883+
if (timeout == 0)
884+
{
885+
return pollfunc(fds, nfds, timeout);
886+
}
887+
if (timeout < 0)
873888
{
874-
timeout = stTimeoutItem_t::eMaxTimeout;
889+
timeout = INT_MAX;
875890
}
876891
int epfd = ctx->iEpollFd;
877892
stCoRoutine_t* self = co_self();
@@ -934,47 +949,45 @@ int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeou
934949
unsigned long long now = GetTickMS();
935950
arg.ullExpireTime = now + timeout;
936951
int ret = AddTimeout( ctx->pTimeout,&arg,now );
952+
int iRaiseCnt = 0;
937953
if( ret != 0 )
938954
{
939955
co_log_err("CO_ERR: AddTimeout ret %d now %lld timeout %d arg.ullExpireTime %lld",
940956
ret,now,timeout,arg.ullExpireTime);
941957
errno = EINVAL;
958+
iRaiseCnt = -1;
942959

943-
if( arg.pPollItems != arr )
960+
}
961+
else
962+
{
963+
co_yield_env( co_get_curr_thread_env() );
964+
iRaiseCnt = arg.iRaiseCnt;
965+
}
966+
967+
{
968+
//clear epoll status and memory
969+
RemoveFromLink<stTimeoutItem_t,stTimeoutItemLink_t>( &arg );
970+
for(nfds_t i = 0;i < nfds;i++)
944971
{
945-
free( arg.pPollItems );
946-
arg.pPollItems = NULL;
972+
int fd = fds[i].fd;
973+
if( fd > -1 )
974+
{
975+
co_epoll_ctl( epfd,EPOLL_CTL_DEL,fd,&arg.pPollItems[i].stEvent );
976+
}
977+
fds[i].revents = arg.fds[i].revents;
947978
}
948-
free(arg.fds);
949-
free(&arg);
950979

951-
return -__LINE__;
952-
}
953-
954-
co_yield_env( co_get_curr_thread_env() );
955980

956-
RemoveFromLink<stTimeoutItem_t,stTimeoutItemLink_t>( &arg );
957-
for(nfds_t i = 0;i < nfds;i++)
958-
{
959-
int fd = fds[i].fd;
960-
if( fd > -1 )
981+
if( arg.pPollItems != arr )
961982
{
962-
co_epoll_ctl( epfd,EPOLL_CTL_DEL,fd,&arg.pPollItems[i].stEvent );
983+
free( arg.pPollItems );
984+
arg.pPollItems = NULL;
963985
}
964-
fds[i].revents = arg.fds[i].revents;
965-
}
966-
967986

968-
int iRaiseCnt = arg.iRaiseCnt;
969-
if( arg.pPollItems != arr )
970-
{
971-
free( arg.pPollItems );
972-
arg.pPollItems = NULL;
987+
free(arg.fds);
988+
free(&arg);
973989
}
974990

975-
free(arg.fds);
976-
free(&arg);
977-
978991
return iRaiseCnt;
979992
}
980993

@@ -1145,5 +1158,3 @@ stCoCondItem_t *co_cond_pop( stCoCond_t *link )
11451158
}
11461159
return p;
11471160
}
1148-
1149-

0 commit comments

Comments
 (0)