add lPopup function time parameter

pull/1/head
deffpuzzl 2018-06-22 13:13:02 +08:00
parent d1f7dac14c
commit 69233e7092
2 changed files with 53 additions and 23 deletions

View File

@ -29,7 +29,7 @@ int main(int argc, char *argv[])
lTime = time(NULL); lTime = time(NULL);
queuenull(pstSavm, sizeof(dbUser), QUEUE_USER_INFO); queuenull(pstSavm, sizeof(dbUser), QUEUE_USER_INFO);
if(RC_SUCC != lPopup(pstSavm, 3, 5, &lRows, (void **)&pstUser)) if(RC_SUCC != lPopup(pstSavm, 3, 0, &lRows, (void **)&pstUser))
{ {
fprintf(stderr, "Pop error:(%u)(%s), time:%ld, effect:%d\n", pstSavm->m_lErrno, fprintf(stderr, "Pop error:(%u)(%s), time:%ld, effect:%d\n", pstSavm->m_lErrno,
sGetTError(pstSavm->m_lErrno), time(NULL) - lTime, pstSavm->m_lEffect); sGetTError(pstSavm->m_lErrno), time(NULL) - lTime, pstSavm->m_lEffect);

View File

@ -198,7 +198,7 @@ retry:
true --success true --success
false --time out false --time out
*************************************************************************************************/ *************************************************************************************************/
bool bIsTimeOut(Timesp *tm, Timesp *tb) bool bTimeOut(Timesp *tm, Timesp *tb)
{ {
Timesp tms; Timesp tms;
@ -248,13 +248,13 @@ bool bIsTimeOut(Timesp *tm, Timesp *tb)
RC_SUCC --success RC_SUCC --success
RC_FAIL --failure RC_FAIL --failure
*************************************************************************************************/ *************************************************************************************************/
long _lPops(SATvm *pstSavm, void *pvAddr, size_t lExpect, Timesp *tm, size_t *plOut, long _lPops(SATvm *pstSavm, void *pvAddr, size_t lExpect, time_t lTime, size_t *plOut,
void **ppsvOut) void **ppsvOut)
{ {
Timesp tms;
int nPos, i = 0;
SHTruck *ps = NULL;
extern int errno; extern int errno;
SHTruck *ps = NULL;
int nPos, i = 0;
Timesp tms, tm = {0};
TblDef *pv = (TblDef *)pvAddr; TblDef *pv = (TblDef *)pvAddr;
if(NULL == (*ppsvOut = (char *)malloc(lExpect * pv->m_lReSize))) if(NULL == (*ppsvOut = (char *)malloc(lExpect * pv->m_lReSize)))
@ -263,9 +263,9 @@ long _lPops(SATvm *pstSavm, void *pvAddr, size_t lExpect, Timesp *tm, size_t
return RC_FAIL; return RC_FAIL;
} }
for (*plOut = 0, clock_gettime(CLOCK_REALTIME, &tms); *plOut < lExpect; ) for (tm.tv_sec = lTime, *plOut = 0, clock_gettime(CLOCK_REALTIME, &tms); *plOut < lExpect; )
{ {
if(!bIsTimeOut(tm, &tms)) if(lTime > 0 && !bTimeOut(&tm, &tms))
{ {
pstSavm->m_lEffect = *plOut; pstSavm->m_lEffect = *plOut;
if(0 == pstSavm->m_lEffect) if(0 == pstSavm->m_lEffect)
@ -278,7 +278,7 @@ long _lPops(SATvm *pstSavm, void *pvAddr, size_t lExpect, Timesp *tm, size_t
return RC_FAIL; return RC_FAIL;
} }
if(0 != Futex(&pv->m_lValid, FUTEX_WAIT, 0, tm)) if(0 != Futex(&pv->m_lValid, FUTEX_WAIT, 0, &tm))
{ {
if(ETIMEDOUT == errno) if(ETIMEDOUT == errno)
{ {
@ -301,6 +301,38 @@ long _lPops(SATvm *pstSavm, void *pvAddr, size_t lExpect, Timesp *tm, size_t
; ;
} }
if(lTime == 0)
{
if(0 == pv->m_lValid)
{
pstSavm->m_lEffect = *plOut;
if(0 == pstSavm->m_lEffect)
{
TFree(*ppsvOut);
pstSavm->m_lErrno = NO_DATA_FOUND;
return RC_FAIL;
}
return RC_SUCC;
}
if(0 > (int)__sync_sub_and_fetch(&pv->m_lValid, 1))
{
__sync_fetch_and_add(&pv->m_lValid, 1);
pstSavm->m_lEffect = *plOut;
if(0 == pstSavm->m_lEffect)
{
TFree(*ppsvOut);
pstSavm->m_lErrno = NO_DATA_FOUND;
return RC_FAIL;
}
return RC_SUCC;
}
}
else
{
if(0 == pv->m_lValid) if(0 == pv->m_lValid)
continue; continue;
@ -309,7 +341,7 @@ long _lPops(SATvm *pstSavm, void *pvAddr, size_t lExpect, Timesp *tm, size_t
__sync_fetch_and_add(&pv->m_lValid, 1); __sync_fetch_and_add(&pv->m_lValid, 1);
continue; continue;
} }
}
retrys: retrys:
/* at least cost one vaild */ /* at least cost one vaild */
if(pv->m_lMaxRow > (nPos = __sync_add_and_fetch(&pv->m_lListOfs, 1))) if(pv->m_lMaxRow > (nPos = __sync_add_and_fetch(&pv->m_lListOfs, 1)))
@ -342,7 +374,7 @@ retrys:
} }
/************************************************************************************************* /*************************************************************************************************
descriptionpop data from queue descriptionpop data from queue use tvmpop
parameters: parameters:
pstSavm --stvm handle pstSavm --stvm handle
psvOut --out data psvOut --out data
@ -398,7 +430,7 @@ long lPop(SATvm *pstSavm, void *pvOut, Uenum eWait)
{ {
long lRet; long lRet;
RunTime *pstRun = NULL; RunTime *pstRun = NULL;
static Timesp tm = {0, 1}; static Timesp tm = {0, 0};
if(!pstSavm) if(!pstSavm)
{ {
@ -445,7 +477,6 @@ long lPop(SATvm *pstSavm, void *pvOut, Uenum eWait)
long lPopup(SATvm *pstSavm, size_t lExpect, time_t lTime, size_t *plOut, void **ppsvOut) long lPopup(SATvm *pstSavm, size_t lExpect, time_t lTime, size_t *plOut, void **ppsvOut)
{ {
long lRet; long lRet;
Timesp tm = {0};
RunTime *pstRun = NULL; RunTime *pstRun = NULL;
if(!pstSavm) if(!pstSavm)
@ -470,8 +501,7 @@ long lPopup(SATvm *pstSavm, size_t lExpect, time_t lTime, size_t *plOut, void
return _lPopupByRt(pstSavm, lExpect, lTime, plOut, ppsvOut); return _lPopupByRt(pstSavm, lExpect, lTime, plOut, ppsvOut);
} }
tm.tv_sec = lTime; lRet = _lPops(pstSavm, pstRun->m_pvAddr, lExpect, lTime, plOut, ppsvOut);
lRet = _lPops(pstSavm, pstRun->m_pvAddr, lExpect, &tm, plOut, ppsvOut);
vTblDisconnect(pstSavm, pstSavm->tblName); vTblDisconnect(pstSavm, pstSavm->tblName);
return lRet; return lRet;
} }