|
38 | 38 | #include <arpa/inet.h> |
39 | 39 | #include <sys/syscall.h> |
40 | 40 | #include <unistd.h> |
| 41 | +#include <limits.h> |
41 | 42 |
|
42 | 43 | extern "C" |
43 | 44 | { |
@@ -395,14 +396,15 @@ int AddTimeout( stTimeout_t *apTimeout,stTimeoutItem_t *apItem ,unsigned long lo |
395 | 396 |
|
396 | 397 | return __LINE__; |
397 | 398 | } |
398 | | - int diff = apItem->ullExpireTime - apTimeout->ullStart; |
| 399 | + unsigned long long diff = apItem->ullExpireTime - apTimeout->ullStart; |
399 | 400 |
|
400 | | - if( diff >= apTimeout->iItemSize ) |
| 401 | + if( diff >= (unsigned long long)apTimeout->iItemSize ) |
401 | 402 | { |
| 403 | + diff = apTimeout->iItemSize - 1; |
402 | 404 | co_log_err("CO_ERR: AddTimeout line %d diff %d", |
403 | 405 | __LINE__,diff); |
404 | 406 |
|
405 | | - return __LINE__; |
| 407 | + //return __LINE__; |
406 | 408 | } |
407 | 409 | AddTail( apTimeout->pItems + ( apTimeout->llStartIdx + diff ) % apTimeout->iItemSize , apItem ); |
408 | 410 |
|
@@ -802,6 +804,16 @@ void co_eventloop( stCoEpoll_t *ctx,pfn_co_eventloop_t pfn,void *arg ) |
802 | 804 | { |
803 | 805 |
|
804 | 806 | 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 | + } |
805 | 817 | if( lp->pfnProcess ) |
806 | 818 | { |
807 | 819 | lp->pfnProcess( lp ); |
@@ -868,10 +880,13 @@ stCoRoutine_t *GetCurrThreadCo( ) |
868 | 880 | typedef int (*poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout); |
869 | 881 | int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeout, poll_pfn_t pollfunc) |
870 | 882 | { |
871 | | - |
872 | | - if( timeout > stTimeoutItem_t::eMaxTimeout ) |
| 883 | + if (timeout == 0) |
| 884 | + { |
| 885 | + return pollfunc(fds, nfds, timeout); |
| 886 | + } |
| 887 | + if (timeout < 0) |
873 | 888 | { |
874 | | - timeout = stTimeoutItem_t::eMaxTimeout; |
| 889 | + timeout = INT_MAX; |
875 | 890 | } |
876 | 891 | int epfd = ctx->iEpollFd; |
877 | 892 | stCoRoutine_t* self = co_self(); |
@@ -934,47 +949,45 @@ int co_poll_inner( stCoEpoll_t *ctx,struct pollfd fds[], nfds_t nfds, int timeou |
934 | 949 | unsigned long long now = GetTickMS(); |
935 | 950 | arg.ullExpireTime = now + timeout; |
936 | 951 | int ret = AddTimeout( ctx->pTimeout,&arg,now ); |
| 952 | + int iRaiseCnt = 0; |
937 | 953 | if( ret != 0 ) |
938 | 954 | { |
939 | 955 | co_log_err("CO_ERR: AddTimeout ret %d now %lld timeout %d arg.ullExpireTime %lld", |
940 | 956 | ret,now,timeout,arg.ullExpireTime); |
941 | 957 | errno = EINVAL; |
| 958 | + iRaiseCnt = -1; |
942 | 959 |
|
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++) |
944 | 971 | { |
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; |
947 | 978 | } |
948 | | - free(arg.fds); |
949 | | - free(&arg); |
950 | 979 |
|
951 | | - return -__LINE__; |
952 | | - } |
953 | | - |
954 | | - co_yield_env( co_get_curr_thread_env() ); |
955 | 980 |
|
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 ) |
961 | 982 | { |
962 | | - co_epoll_ctl( epfd,EPOLL_CTL_DEL,fd,&arg.pPollItems[i].stEvent ); |
| 983 | + free( arg.pPollItems ); |
| 984 | + arg.pPollItems = NULL; |
963 | 985 | } |
964 | | - fds[i].revents = arg.fds[i].revents; |
965 | | - } |
966 | | - |
967 | 986 |
|
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); |
973 | 989 | } |
974 | 990 |
|
975 | | - free(arg.fds); |
976 | | - free(&arg); |
977 | | - |
978 | 991 | return iRaiseCnt; |
979 | 992 | } |
980 | 993 |
|
@@ -1145,5 +1158,3 @@ stCoCondItem_t *co_cond_pop( stCoCond_t *link ) |
1145 | 1158 | } |
1146 | 1159 | return p; |
1147 | 1160 | } |
1148 | | - |
1149 | | - |
|
0 commit comments