⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ss_queue.c

📁 中国石油二期加油站IC系统后台通讯软件
💻 C
📖 第 1 页 / 共 3 页
字号:
SsDmndQ *dQueue                        /* demand Queue */
)
#else
PUBLIC S16 ssDestroyDmndQ(dQueue)
SsDmndQ *dQueue;                       /* demand Queue */
#endif
{
    U8     i;
    Buffer *tBuf;
    S16    ret;

    TRC0(ssDestroyDmndQ)

#if (ERRCLASS & ERRCLS_INT_PAR)
    if (dQueue == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS404, ERRZERO, "NULL DQ Pointer");
        RETVALUE(RFAILED);
    }
#endif

    for (i = 0; i < SS_DQ_BIT_MASK_LEN; i++)
    {
        ret = SDestroyLock(&dQueue->dmndQLock[i]);
        if (ret != ROK)
        {
#if (ERRCLASS & ERRCLS_DEBUG)
            SSLOGERROR(ERRCLS_DEBUG, ESS405, (ErrVal)ret, "Failed to destroy lock");
#endif
            RETVALUE(RFAILED);
        }
    }
    for (i = 0; i < SS_MAX_NUM_DQ; i++)
    {
        while (dQueue->queue[i].head != NULLP)
        {
            tBuf = dQueue->queue[i].head;
            dQueue->queue[i].head = dQueue->queue[i].head->b_next;
            SPutMsg(tBuf);
        }
    }
    ssDestroySema(&dQueue->dmndQSema);
    RETVALUE (ROK);

} /* end of ssDestroyDmndQ */


/*
*
*       Fun:   ssDmndQPut
*
*       Desc:  This function adds a message to the head or tail of the 
*              priority queue specified. The priority specified is the 
*              destination Q index i.e 
*              ((dst_Tsk_pri * SS_MAX_MSG_PRI) + msg_pri)
*
*       Ret:   ROK      - ok
*              RFAILED  - failed
*
*       Notes: 
*
*       File:  ss_queue.c
*
*/
#ifdef ANSI
PUBLIC S16 ssDmndQPut
(
SsDmndQ *dQueue,                       /* demand Queue */
Buffer  *mBuf,                         /* message buffer */
Prior   priority,                      /* priority */
Order   order                          /* position */
)
#else
PUBLIC S16 ssDmndQPut(dQueue, mBuf, priority, order)
SsDmndQ *dQueue;                       /* demand Queue */
Buffer  *mBuf;                         /* message buffer */
Prior   priority;                      /* priority */
Order   order;                         /* position */
#endif
{
    U8     maskIndex;                   /* mask Index */
    U8     bitPosition;                 /* bit position in index */
    Queue *queue;                       /* queue in demand queue */
    S16    ret;                         /* return value */

    TRC0(ssDmndQPut)

#if (ERRCLASS & ERRCLS_INT_PAR)
    if (dQueue == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS406, ERRZERO, "NULL DQ Pointer");
        RETVALUE(RFAILED);
    }

    if (mBuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS407, ERRZERO, "NULL mBuf Pointer");
        RETVALUE(RFAILED);
    }

    if ((priority == PRIORNC) || (priority > SS_MAX_DQ_PRIOR))
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS408, ERRZERO, "invalid priority ");
        RETVALUE(RFAILED);
    }

    if ((order != SS_DQ_FIRST) && (order != SS_DQ_LAST))
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS409, ERRZERO, "invalid order ");
        RETVALUE(RFAILED);
    }
#endif

    maskIndex   = priority >> 3;
    bitPosition = 7 - (priority % 8);
    queue       = &dQueue->queue[priority];

    ret = SLock(&dQueue->dmndQLock[maskIndex]);
    if (ret != ROK)
    {
#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS410, (ErrVal)ret, "Failed to get lock");
#endif
        RETVALUE (RFAILED);
    }

    if (queue->crntSize == 0)
    {
        queue->head   = mBuf;
        queue->tail   = mBuf;
        mBuf->b_next  = NULLP;
        mBuf->b_prev  = NULLP;

        /* Set the corresponding bit in bit mask */
        dQueue->bitMask[maskIndex] |= (1 << bitPosition);
    } else
    {
        if (order == SS_DQ_LAST)
        {
            mBuf->b_prev        = queue->tail;
            mBuf->b_next        = NULLP;
            queue->tail->b_next = mBuf;
            queue->tail         = mBuf;
        } else
        {
            mBuf->b_next        = queue->head;
            mBuf->b_prev        = NULLP;
            queue->head->b_prev = mBuf;
            queue->head         = mBuf;
        }
    }
    queue->crntSize++;

    ret = SUnlock(&dQueue->dmndQLock[maskIndex]);
    if (ret != ROK)
    {
#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS411, (ErrVal)ret, "Failed to release lock");
#endif
        RETVALUE (RFAILED);
    }

    /* increment the counting semaphore */
    ssPostSema(&dQueue->dmndQSema);

    RETVALUE(ROK);

} /* End of ssDmndQPut */


/*
*
*       Fun:   ssDmndQGet
*
*       Desc:  This function removes a message from head or tail of the 
*              highest non-empty priority queue message. 
*
*       Ret:   ROK      - ok
*              RFAILED  - failed
*              ROKDNA   - ok, no data available in queue
*
*       Notes:  This is a blocking call
*
*       File:  ss_queue.c
*
*/
#ifdef ANSI
PUBLIC S16 ssDmndQGet
(
SsDmndQ *dQueue,                          /* demand queue */
Buffer  **mBuf,                           /* message buffer */
Order   order                             /* position */ 
)
#else
PUBLIC S16 ssDmndQGet(dQueue, mBuf, order)
SsDmndQ *dQueue;                          /* demand queue */
Buffer  **mBuf;                           /* message buffer */
Order   order;                            /* position */
#endif
{
    Queue *queue;
    S16   ret;
    S16   i;
    U8    bitPosition;
    U8    qIndex;

    TRC0(ssDmndQGet)

#if (ERRCLASS & ERRCLS_INT_PAR)
    if (dQueue == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS412, ERRZERO, "NULL DQ Pointer");
        RETVALUE(RFAILED);
    }

    if (mBuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS413, ERRZERO, "NULL mBuf Pointer");
        RETVALUE(RFAILED);
    }

    if ((order != SS_DQ_FIRST) && (order != SS_DQ_LAST))
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS414, ERRZERO, "invalid order ");
        RETVALUE(RFAILED);
    }
#endif

    ret = ssWaitSema(&dQueue->dmndQSema);
    if (ret != ROK)
    {
#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS415, (ErrVal)ret, "Failed to get semaphore");
#endif
        RETVALUE (RFAILED);
    }

    for (i = 0; i < SS_DQ_BIT_MASK_LEN; i++)
    {
        ret = SLock(&dQueue->dmndQLock[i]);
        if (ret != ROK)
        {
#if (ERRCLASS & ERRCLS_DEBUG)
            SSLOGERROR(ERRCLS_DEBUG, ESS416, (ErrVal)ret, "Failed to get lock");
#endif
            RETVALUE (RFAILED);
        }

        bitPosition = osCp.dmndQLookupTbl[dQueue->bitMask[i]];
        if (bitPosition != 255)
            break;

        ret = SUnlock(&dQueue->dmndQLock[i]);
        if (ret != ROK)
        {
#if (ERRCLASS & ERRCLS_DEBUG)
            SSLOGERROR(ERRCLS_DEBUG, ESS417, ret, "Failed to release lock");
#endif
            RETVALUE (RFAILED);
        }
    }

    if (i >= SS_DQ_BIT_MASK_LEN)
    {
        /* Demand Queue is empty */
        *mBuf = NULLP;
        RETVALUE (ROKDNA);
    }

    qIndex = (i * 8) +  (7 - bitPosition);
    queue = &dQueue->queue[qIndex];

    if (queue->crntSize == 1)
    {
        *mBuf = queue->head;
        queue->head = NULLP;
        queue->tail = NULLP;

        /* Reset the corresponding bit in bit mask */
        dQueue->bitMask[i] &= (~( 1 << (bitPosition)));
    } else
    {
        if (order == SS_DQ_FIRST)
        {
            *mBuf = queue->head;
            queue->head = queue->head->b_next;
            queue->head->b_prev = NULLP;
        } else
        {
            *mBuf = queue->tail;
            queue->tail = queue->tail->b_prev;
            queue->tail->b_next = NULLP;
        }
    }
    queue->crntSize--;

    ret = SUnlock(&dQueue->dmndQLock[i]); 
    if (ret != ROK)
    {
#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS418, (ErrVal)ret, "Failed to release lock");
#endif
        RETVALUE (RFAILED);
    }

    RETVALUE (ROK);

} /* End of ssDmndQGet */


/*
*
*       Fun:   ssFndLenDmndQ 
*
*       Desc:  This function returns the number of messages in a queue
*              If priority is specified, length of the associated Q is 
*              returned otherwise total length of all Qs is returned.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed
*
*       Notes: 
*
*       File:  ss_queue.c
*
*/
#ifdef ANSI
PUBLIC S16 ssFndLenDmndQ
(
SsDmndQ *dQueue,                               /* demand queue */
Prior   priority,                              /* priority */
QLen    *len                                   /* queue length */
)
#else
PUBLIC S16 ssFndLenDmndQ(dQueue, priority, len)
SsDmndQ *dQueue;                               /* demand queue */
Prior   priority;                              /* priority */
QLen    *len;                                  /* queue length */
#endif
{

    S16  ret;                                   /* return value */
    U8   i;

    TRC0(ssFndLenDmndQ)

#if (ERRCLASS & ERRCLS_INT_PAR)
    if ((dQueue == NULLP) || (len == NULLP))
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS419, ERRZERO, "NULL Pointer");
        RETVALUE(RFAILED);
    }
#endif

    *len = 0;
    if (priority != PRIORNC)
    {
        i = priority >> 3; 
        ret = SLock(&dQueue->dmndQLock[i]);
        if (ret != ROK)
        {
#if (ERRCLASS & ERRCLS_DEBUG)
            SSLOGERROR(ERRCLS_DEBUG, ESS420, (ErrVal)ret, "Failed to get lock");
#endif
            RETVALUE (RFAILED);
        }

        *len = dQueue->queue[priority].crntSize;

        ret = SUnlock(&dQueue->dmndQLock[i]);
        if (ret != ROK)
        {
#if (ERRCLASS & ERRCLS_DEBUG)
            SSLOGERROR(ERRCLS_DEBUG, ESS421, (ErrVal)ret,  \
                       "Failed to release lock");
#endif
            RETVALUE (RFAILED);
        }
    } else
    {
        for (i = 0; i < SS_DQ_BIT_MASK_LEN; i++)
        {
            ret = SLock(&dQueue->dmndQLock[i]);
            if (ret != ROK)
            {
#if (ERRCLASS & ERRCLS_DEBUG)
                SSLOGERROR(ERRCLS_DEBUG, ESS422, (ErrVal)ret, "Failed to get lock");
#endif

                /* Release all the locks aquired */
                while (i > 0)
                {
                    SUnlock(&dQueue->dmndQLock[i-1]);
                    i--;
                }

                RETVALUE (RFAILED);
            }
        }

        for (i = 0; i  < SS_MAX_NUM_DQ; i++)
            *len  += dQueue->queue[i].crntSize;

        for ( i = 0; i < SS_DQ_BIT_MASK_LEN; i++)
        {
            ret = SUnlock(&dQueue->dmndQLock[i]);
            if (ret != ROK)
            {
#if (ERRCLASS & ERRCLS_DEBUG)
                SSLOGERROR(ERRCLS_DEBUG, ESS423, (ErrVal)ret, "Failed to get lock");
#endif
                RETVALUE (RFAILED);
            }
        }
    }
    RETVALUE(ROK);

} /* End of ssFndLenDmndQ */



/********************************************************************30**
  
         End of file: ss_queue.c 1.3  -  10/14/98 14:17:28
  
*********************************************************************31*/

  
/********************************************************************40**
  
        Notes: 
  
*********************************************************************41*/

/********************************************************************50**

*********************************************************************51*/

   
/********************************************************************60**
  
        Revision history:
  
*********************************************************************61*/

/********************************************************************90**
 
     ver       pat    init                  description
------------ -------- ---- ----------------------------------------------
1.1          ---      bsr  1. initial release.
  
1.2          ---      kr   1. defined functions SQueueFirst, 
                              SDequeueFirst, SQueueLast and SDequeueLast
                              under SS_ENABLE_MACROS
             ---      kp   2. Cosmetic changes

1.3          ---      kp   1. Bug fix in ssDmndQGet()

*********************************************************************91*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -