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

📄 queue.c

📁 介绍ROCK OS操作系统.一般用于汽车电子,类似OCVX.里面是个DEMO文档,内附说明.
💻 C
📖 第 1 页 / 共 2 页
字号:
            g_queueCB[qid].head = g_basicElementPool[element].next;
        }

        /* free it to pool. */
        tail = g_basicFreeQ.tail;
        g_basicElementPool[tail].next = element;
        g_basicElementPool[element].next = (U32)(-1);
        g_basicFreeQ.tail = element;
        g_basicFreeQ.current++;

        nret = OS_SUCCESS;
        break;

    case OS_QUEUE_PRIO:
        /* get the data and clear it. */
        element = g_queueCB[qid].head;
        if (phandle != NULL)
        {
            * phandle = g_prioElementPool[element].handle;
        }

        g_prioElementPool[element].handle = NULL_HANDLE;
        g_prioElementPool[element].priority = (U16)(-1);

        /* remove it from queue. */
        g_queueCB[qid].current--;
        if (g_queueCB[qid].current == 0)
        {
            g_queueCB[qid].head = (U32)(-1);
        }
        else
        {
            g_queueCB[qid].head = g_prioElementPool[element].next;
        }

        /* free it to pool. */
        tail = g_priorityFreeQ.tail;
        g_prioElementPool[tail].next = element;
        g_prioElementPool[element].next = (U32)(-1);
        g_priorityFreeQ.tail = element;
        g_priorityFreeQ.current++;
        nret = OS_SUCCESS;
        break;

    case OS_QUEUE_SET:
        OS_error("q_enum(): qid [%d] is a set, can't be enummed.!!!\n", qid);
        nret = OS_FAIL;
        break;

    default:
        OS_error("q_enum(): qid [%d] state is [%d]!!!\n", qid, g_queueCB[qid].state);
        nret = OS_FAIL;
        break;
    }

    return nret;
}

/******************************************************************************
Function    : STATUS q_head (HQUEUE qid, void ** pData)
Params      : qid   - the queue which to be enumed.
            : pData - to save the element's data from queue.
            : 
            : 
Return      : OS_SUCCESS or OS_FAIL if the queue is empty.
Description : inquire the first element but not delete it from queue.
******************************************************************************/
STATUS q_head (HQUEUE qid, HANDLE * phandle)
{
    U32 element;

    /* params check. */
    if (qid > MAX_QUEUE_NUM)
    {
        OS_error("q_head(): qid [%d] out of range!!!\n", qid);
        return OS_FAIL;
    }

    if (g_queueCB[qid].state == OS_QUEUE_FREE)
    {
        OS_error("q_head(): qid [%d] is free!!!\n", qid);
        return OS_FAIL;
    }

    /* if queue is empty. */
    if (g_queueCB[qid].current == 0)
    {
        return OS_FAIL;
    }

    switch(g_queueCB[qid].state)
    {
    case OS_QUEUE_FIFO:
    case OS_QUEUE_LIFO:
        /* get the data. */
        element = g_queueCB[qid].head;
        * phandle = g_basicElementPool[element].handle;
        break;

    case OS_QUEUE_PRIO:
        /* get the data. */
        element = g_queueCB[qid].head;
        if (phandle != NULL)
        {
            * phandle = g_prioElementPool[element].handle;
        }
        break;

    case OS_QUEUE_SET:
        OS_error("q_head(): qid [%d] is a set, can't be headed.!!!\n", qid);
        break;

    default:
        OS_error("q_head(): qid [%d] state is [%d]!!!\n", qid, g_queueCB[qid].state);
        break;
    }

    return OS_SUCCESS;
}

/******************************************************************************
Function    : STATUS q_del (HQUEUE qid, void * pData)
Params      : qid   - the queue.
            : pData - the data in queue.
            : 
            : 
Return      : OS_SUCCESS or OS_FAIL if the pData is not in the queue.
Description : This function is a reverse action with q_add(). 
******************************************************************************/
STATUS q_del (HQUEUE qid, HANDLE handle)
{
    STATUS nret;

    U32 prev, current;
    U32 tail;

    /* params check. */
    if (qid > MAX_QUEUE_NUM)
    {
        OS_error("q_del(): qid [%d] out of range!!!\n", qid);
        return OS_FAIL;
    }

    if (g_queueCB[qid].state == OS_QUEUE_FREE)
    {
        OS_error("q_del(): qid [%d] is free!!!\n", qid);
        return OS_FAIL;
    }

    /* if queue is empty. */
    if (g_queueCB[qid].current == 0)
    {
        OS_error("q_del(): qid [%d] is empty!!!\n", qid);
        return OS_FAIL;
    }

    switch(g_queueCB[qid].state)
    {
    case OS_QUEUE_FIFO:
    case OS_QUEUE_LIFO:
    case OS_QUEUE_SET:
        current = g_queueCB[qid].head;
        prev = current;

        while ((current != (U32)(-1))
                &&(g_basicElementPool[current].handle != handle))
        {
            prev = current;
            current  = g_basicElementPool[current].next;
        }
        if (current != (U32)(-1))
        {
            /* found, remove it from queue. */
            g_queueCB[qid].current--;
            if (g_queueCB[qid].current == 0)
            {
                g_queueCB[qid].head = (U32)(-1);
            }
            else if (prev == current)
            {
                /* remove the first element. */
                g_queueCB[qid].head = g_basicElementPool[current].next;
            }
            else
            {
                /* remove the second or later element. */
                g_basicElementPool[prev].next = g_basicElementPool[current].next;
            }

            /* clear its data. */
            g_basicElementPool[current].handle = NULL_HANDLE;

            /* free it to pool. */
            tail = g_basicFreeQ.tail;
            g_basicElementPool[tail].next = current;
            g_basicElementPool[current].next = (U32)(-1);
            g_basicFreeQ.tail = current;
            g_basicFreeQ.current++;
            
            nret = OS_SUCCESS;
        }
        else
        {
            /* not found. */
            nret = OS_FAIL;
        }
        break;

    case OS_QUEUE_PRIO:
        current = g_queueCB[qid].head;
        prev = current;

        while ((current != (U32)(-1))
                &&(g_prioElementPool[current].handle != handle))
        {
            prev = current;
            current  = g_prioElementPool[current].next;
        }
        if (current != (U32)(-1))
        {
            /* found, remove it from queue. */
            g_queueCB[qid].current--;
            if (g_queueCB[qid].current == 0)
            {
                g_queueCB[qid].head = (U32)(-1);
            }
            else if (prev == current)
            {
                /* remove the first element. */
                g_queueCB[qid].head = g_prioElementPool[current].next;
            }
            else
            {
                /* remove the second or later element. */
                g_prioElementPool[prev].next = g_prioElementPool[current].next;
            }

            /* clear its data. */
            g_prioElementPool[current].handle = NULL_HANDLE;
            g_prioElementPool[current].priority = (U16)(-1);

            /* free it to pool. */
            tail = g_priorityFreeQ.tail;
            g_prioElementPool[tail].next = current;
            g_prioElementPool[current].next = (U32)(-1);
            g_priorityFreeQ.tail = current;
            g_priorityFreeQ.current++;
            
            nret = OS_SUCCESS;
        }
        else
        {
            /* not found. */
            OS_error("q_del(): element [%d] not found in queue [%d]!!!\n", handle, qid);
            q_showSingle(qid);
            nret = OS_FAIL;
        }
        break;

    default:
        OS_error("q_del(): qid [%d] state is [%d]!!!\n", qid, g_queueCB[qid].state);
        nret = OS_FAIL;
        break;
    }

    return nret;
}

/******************************************************************************
Function    : U32 q_count (HQUEUE qid)
Params      : qid - the queue.
            : 
            : 
            : 
Return      : the number of elements in queue
Description : get how many elements is in queue
******************************************************************************/
U32 q_count (HQUEUE qid)
{
    U32 nret;

    /* params check. */
    if (qid > MAX_QUEUE_NUM)
    {
        OS_error("q_count(): qid [%d] out of range!!!\n", qid);
        return OS_FAIL;
    }

    if (g_queueCB[qid].state == OS_QUEUE_FREE)
    {
        OS_error("q_count(): qid [%d] is free!!!\n", qid);
        return OS_FAIL;
    }

    switch(g_queueCB[qid].state)
    {
    case OS_QUEUE_FIFO:
    case OS_QUEUE_LIFO:
    case OS_QUEUE_PRIO:
    case OS_QUEUE_SET:
        nret = g_queueCB[qid].current;
        break;

    default:
        OS_error("q_count(): qid [%d] state is [%d]!!!\n", qid, g_queueCB[qid].state);
        nret = 0;
        break;
    }
    
    return nret;
}

/******************************************************************************
Function    : void q_showAll(void)
Params      : N/A.
            : 
            : 
            : 
Return      : N/A
Description : display all queues' state and elements count.
            : 
******************************************************************************/
void q_showAll()
{
    HQUEUE qid;

    OS_printf("QID   Type      Name                  TotalSize   CurrentSize\n");
    OS_printf("====  ========  ====================  ==========  ===========\n");
    for (qid = 0; qid < MAX_QUEUE_NUM; qid++)
    {
        switch (g_queueCB[qid].state)
        {
        case OS_QUEUE_FIFO:
            OS_printf ("%-4d  FIFO      %-20s  %-8d    %-8d\n", 
                        qid, g_queueCB[qid].name, g_queueCB[qid].total, g_queueCB[qid].current);
            break;
        case OS_QUEUE_LIFO:
            OS_printf ("%-4d  LIFO      %-20s  %-8d    %-8d\n", 
                        qid, g_queueCB[qid].name, g_queueCB[qid].total, g_queueCB[qid].current);
            break;
        case OS_QUEUE_PRIO:
            OS_printf ("%-4d  PRIO      %-20s  %-8d    %-8d\n", 
                        qid, g_queueCB[qid].name, g_queueCB[qid].total, g_queueCB[qid].current);
            break;
        case OS_QUEUE_SET:
            OS_printf ("%-4d  SET       %-20s  %-8d    %-8d\n", 
                        qid, g_queueCB[qid].name, g_queueCB[qid].total, g_queueCB[qid].current);
            break;
        default:
            break;
        }
    }
    OS_printf("=============================================================\n");
}

/******************************************************************************
Function    : void q_showSingle(HQUEUE qid)
Params      : qid - the queue.
            : 
            : 
            : 
Return      : N/A
Description : display a queue's detail information, called by q_show().
            : 
******************************************************************************/
void q_showSingle(HQUEUE qid)
{
    U32 element;
    U32 counter;

    if (qid >= MAX_QUEUE_NUM)
    {
        OS_printf("Qid [%d] out of range, valid is [%d..%d]\n", qid, 0, MAX_QUEUE_NUM-1);
        return;
    }

    OS_printf ("============================================================\n");
    switch(g_queueCB[qid].state)
    {
    case OS_QUEUE_FREE:
        OS_printf ("Qid [%d] is a free queue...\n", qid);
        break;
    case OS_QUEUE_FIFO:
    case OS_QUEUE_SET:
        OS_printf ("Qid : [%d]\n", qid);
        OS_printf ("Queue name : %s\n", &g_queueCB[qid].name[0]);
        OS_printf ("Queue type : FIFO\n");
        OS_printf ("Total queue size : [%d]\n", g_queueCB[qid].total);
        OS_printf ("Current queue size : [%d]\n", g_queueCB[qid].current);
        OS_printf ("Data in queue:\n");

        counter = 0;
        element = g_queueCB[qid].head;
        while((g_queueCB[qid].current!=0)&&(element != (U32)(-1)))
        {
            OS_printf ("\t%d", g_basicElementPool[element].handle);
            element = g_basicElementPool[element].next;
            counter ++;
            if ((counter % 4) == 0)
            {
                OS_printf ("\n");
            }
        }
        break;

    case OS_QUEUE_LIFO:
        OS_printf ("Qid : [%d]\n", qid);
        OS_printf ("Queue name : %s\n", &g_queueCB[qid].name[0]);
        OS_printf ("Queue type : LIFO\n");
        OS_printf ("Total queue size : [%d]\n", g_queueCB[qid].total);
        OS_printf ("Current queue size : [%d]\n", g_queueCB[qid].current);
        OS_printf ("Data in queue:\n");

        counter = 0;
        element = g_queueCB[qid].head;
        while((g_queueCB[qid].current!=0)&&(element != (U32)(-1)))
        {
            OS_printf ("\t%d", g_basicElementPool[element].handle);
            element = g_basicElementPool[element].next;
            counter ++;
            if ((counter % 4) == 0)
            {
                OS_printf ("\n");
            }
        }
        break;

    case OS_QUEUE_PRIO:
        OS_printf ("Qid : [%d]\n", qid);
        OS_printf ("Queue name : %s\n", &g_queueCB[qid].name[0]);
        OS_printf ("Queue type : Priority based.\n");
        OS_printf ("Total queue size : [%d]\n", g_queueCB[qid].total);
        OS_printf ("Current queue size : [%d]\n", g_queueCB[qid].current);
        OS_printf ("Data in queue:\n");

        element = g_queueCB[qid].head;
        while((g_queueCB[qid].current!=0)&&(element != (U32)(-1)))
        {
            OS_printf("\tpriority [%d], data [%d]\n", 
                    g_prioElementPool[element].priority,g_prioElementPool[element].handle);
            element = g_prioElementPool[element].next;
        }
        break;

    default:
        break;
    }
    OS_printf ("\n============================================================\n");
}

⌨️ 快捷键说明

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