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

📄 test_osapi_semaphore.c

📁 这是DVD中伺服部分的核心代码
💻 C
字号:
/*****************************************************************************
******************************************************************************
**                                                                          **
**  Copyright (c) 2006 Videon Central, Inc.                                 **
**  All rights reserved.                                                    **
**                                                                          **
**  The computer program contained herein contains proprietary information  **
**  which is the property of Videon Central, Inc.  The program may be used  **
**  and/or copied only with the written permission of Videon Central, Inc.  **
**  or in accordance with the terms and conditions stipulated in the        **
**  agreement/contract under which the programs have been supplied.         **
**                                                                          **
******************************************************************************
*****************************************************************************/
/**
 * @file test_osapi_semaphore.c
 *
 * $Revision: 1.3 $ 
 *
 * Operating System API (OSAPI) test file. 
 *
 */

#include "vdvd_types.h"
#include "osapi.h"
#include "dbgprint.h"
#include "test_osapi.h"

#define STACK_SIZE  8*1024


#define COUNTING_SEMAPHORE_TEST_COUNT 13
static volatile OS_SEM_ID   testSemaphoreHandle = 0;

ULONG SemaphoreTestThreadEntryPoint( void )
{
    OS_STATUS   status;

    DbgPrint(("[running] Test 10: wait for semaphore to be released by another thread\n"));
    status = OS_SemTake( testSemaphoreHandle, 5*OS_WAIT_1S ); // Wait for 5 seconds...

    DbgPrint(("\n[running] Test 10: wait for semaphore to be released by another thread"));
    PassFail( (status==OS_OK) );

    DbgPrint(("SemaphoreTestThread exiting\n"));

    return 0;
}


void OSAPISemaphoreTests ( void )
{
    OS_SEM_ID   handle;
    OS_STATUS   status;
    ULONG       iSemaphoreTestThread1ID;
    int         index;

    DbgPrint(("\n\nOSAPI SEMAPHORE TESTS\n"));

    DbgPrint(("[running] Test 1: create full binary semaphore"));
    handle = OS_SemBCreate(OS_SEM_Q_FIFO, OS_SEM_FULL);
    PassFail( (handle!=0) );

    DbgPrint(("[running] Test 2: obtain full binary semaphore"));
    status = OS_SemTake( handle, OS_WAIT_FOREVER );
    PassFail( (status==OS_OK) );

    DbgPrint(("[running] Test 3: obtain empty binary semaphore with timeout"));
    status = OS_SemTake( handle, 10 );
    PassFail( (status==OS_TIMEOUT) );

    DbgPrint(("[running] Test 4: obtain empty binary semaphore with 0 timeout"));
    status = OS_SemTake( handle, OS_NO_WAIT );
    PassFail( (status==OS_TIMEOUT) );

    DbgPrint(("[running] Test 5: release empty binary semaphore"));
    status = OS_SemGive( handle );
    PassFail( (status==OS_OK) );

    DbgPrint(("[running] Test 6: release full binary semaphore"));
    status = OS_SemGive( handle );
    PassFail( (status==OS_FAILURE) );

    DbgPrint(("[running] Test 7: obtain full binary semaphore with timeout"));
    status = OS_SemTake( handle, 10 );
    PassFail( (status==OS_OK) );

    DbgPrint(("[running] Test 8: release empty binary semaphore"));
    status = OS_SemGive( handle );
    PassFail( (status==OS_OK) );


    // Obtain full binary semaphore (the semaphore is now not available)

    DbgPrint(("[running] Test 9: obtain full binary semaphore"));
    status = OS_SemTake( handle, OS_WAIT_FOREVER );
    PassFail( (status==OS_OK) );

    
    // Create a thread that will wait to obtain the semaphore

    testSemaphoreHandle = handle;
    iSemaphoreTestThread1ID = OS_TaskSpawn("Task1", 4, STACK_SIZE, SemaphoreTestThreadEntryPoint);

    PassFail( (iSemaphoreTestThread1ID!=0) );

    // Let thread run and block on semaphore
    OS_TaskDelay( 2*OS_WAIT_1S );

    DbgPrint(("[running] Test 11: release empty binary semaphore"));
    status = OS_SemGive( handle );
    PassFail( (status==OS_OK) );

    // Let thread get semaphore and exit
    OS_TaskDelay( 5*OS_WAIT_1S );


    DbgPrint(("[running] Test 12: delete binary semaphore"));
    status = OS_SemDelete(handle);
    PassFail( (status==OS_OK) );


    DbgPrint(("[running] Test 1: create empty counting semaphore"));
    handle = OS_SemCCreate(OS_SEM_Q_FIFO, 0);
    PassFail( (handle!=0) );

    DbgPrint(("[running] Test 2: obtain empty counting semaphore (timeout=NO_WAIT)"));
    status = OS_SemTake( handle, OS_NO_WAIT );
    PassFail( (status==OS_TIMEOUT) );

    DbgPrint(("[running] Test 3: obtain empty counting semaphore (timeout=100ms)"));
    status = OS_SemTake( handle, 100 );
    PassFail( (status==OS_TIMEOUT) );

    status = 0;
    DbgPrint(("[running] Test 4: release counting semaphore %d times", COUNTING_SEMAPHORE_TEST_COUNT));
    for ( index = 0; index < COUNTING_SEMAPHORE_TEST_COUNT; index++ )
    {
        status |= OS_SemGive( handle );
    }
    PassFail( (status==OS_OK) );

    status = 0;
    DbgPrint(("[running] Test 5: obtain counting semaphore %d times", COUNTING_SEMAPHORE_TEST_COUNT));
    for ( index = 0; index < COUNTING_SEMAPHORE_TEST_COUNT; index++ )
    {
        status |= OS_SemTake( handle, 100 );
    }
    PassFail( (status==OS_OK) );

    DbgPrint(("[running] Test 6: obtain empty counting semaphore with (timeout=10ms)"));
    status = OS_SemTake( handle, 10 );
    PassFail( (status==OS_TIMEOUT) );

    DbgPrint(("[running] Test 9: delete counting semaphore"));
    status = OS_SemDelete(handle);
    PassFail( (status==OS_OK) );

    DbgPrint(("\nOSAPI SEMAPHORE TEST FINISHED\n"));
}


⌨️ 快捷键说明

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