📄 validate.c
字号:
/*****************************************************************************
* validate.c - validation suite for testing the implementation of a VxWorks
* kernel API in a POSIX Threads environment.
*
* Copyright (C) 2000 Monta Vista Software Inc.
*
* Author : Gary S. Robertson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "vxwk2pthread.h"
#include "vxw_hdrs.h"
typedef struct qmessage
{
char qname[4];
ulong nullterm;
int t_cycle;
int msg_no;
} my_qmsg_t;
typedef union
{
char blk[16];
my_qmsg_t msg;
} msgblk_t;
extern vxwk2pthread_cb_t *
my_tcb( void );
extern vxwk2pthread_cb_t *
tcb_for( int taskid );
extern void *
ts_malloc( size_t blksize );
/*****************************************************************************
** demo program global data structures
*****************************************************************************/
static int temp_taskid;
static int task1_id;
static int task2_id;
static int task3_id;
static int task4_id;
static int task5_id;
static int task6_id;
static int task7_id;
static int task8_id;
static int task9_id;
static int task10_id;
static MSG_Q_ID queue1_id;
static MSG_Q_ID queue2_id;
static MSG_Q_ID queue3_id;
static SEM_ID mutex1_id;
static SEM_ID mutex2_id;
static SEM_ID mutex3_id;
static SEM_ID sema41_id;
static SEM_ID sema42_id;
static SEM_ID sema43_id;
static SEM_ID enable1;
static SEM_ID enable2;
static SEM_ID enable3;
static SEM_ID enable4;
static SEM_ID enable5;
static SEM_ID enable6;
static SEM_ID enable7;
static SEM_ID enable8;
static SEM_ID enable9;
static SEM_ID enable10;
static SEM_ID complt1;
static SEM_ID complt2;
static SEM_ID complt3;
static SEM_ID complt4;
static SEM_ID complt5;
static SEM_ID complt6;
static SEM_ID complt7;
static SEM_ID complt8;
static SEM_ID complt9;
static SEM_ID complt10;
static WDOG_ID wdog1_id;
static WDOG_ID wdog2_id;
static int test_cycle;
static int wdog1_cycle;
static int wdog2_cycle;
static unsigned char task5_restarted = 0;
/*****************************************************************************
** display_tcb
*****************************************************************************/
void display_tcb( ulong tid )
{
int policy;
vxwk2pthread_cb_t *cur_tcb;
cur_tcb = tcb_for( tid );
if ( cur_tcb == (vxwk2pthread_cb_t *)NULL )
return;
printf(
"\r\nTask Name: %s Task ID: %d Thread ID: %ld Vxworks priority: %d",
cur_tcb->taskname, cur_tcb->taskid, cur_tcb->pthrid,
cur_tcb->vxw_priority );
policy = (cur_tcb->attr).__schedpolicy;
switch (policy )
{
case SCHED_FIFO:
printf( "\r\n schedpolicy: SCHED_FIFO " );
break;
case SCHED_RR:
printf( "\r\n schedpolicy: SCHED_RR " );
break;
case SCHED_OTHER:
printf( "\r\n schedpolicy: SCHED_OTHER " );
break;
default :
printf( "\r\n schedpolicy: %d ", policy );
}
printf( " priority %d ", ((cur_tcb->attr).__schedparam).sched_priority );
printf( " prv_priority %d ", (cur_tcb->prv_priority).sched_priority );
printf( " detachstate %d ", (cur_tcb->attr).__detachstate );
}
/*****************************************************************************
** validate_binary_semaphores
** This function sequences through a series of actions to exercise
** the various features and characteristics of VxWorks semaphores
**
*****************************************************************************/
void validate_binary_semaphores( void )
{
STATUS err;
puts( "\r\n********** Binary Semaphore validation:" );
/************************************************************************
** Non-Mutex Semaphore Flush Test
************************************************************************/
puts( "\n.......... Next we enable Tasks 4, 7, and 10 to wait for" );
puts( " a token from enable1 in reverse-priority order." );
puts( " Then we flush enable1, waking all waiting tasks" );
puts( "Task 1 enabling Tasks 4, 7, and 10 to consume enable1 tokens.");
errno = 0;
err = semGive( enable4 );
if ( err != OK )
{
printf( "semGive of enable4 returned error %x\r\n", errno );
}
taskDelay( 2 );
errno = 0;
err = semGive( enable7 );
if ( err != OK )
{
printf( "semGive of enable7 returned error %x\r\n", errno );
}
taskDelay( 2 );
errno = 0;
err = semGive( enable10 );
if ( err != OK )
{
printf( "semGive of enable10 returned error %x\r\n", errno );
}
taskDelay( 2 );
puts( "Task 1 flushing semaphore enable1." );
errno = 0;
err = semFlush( enable1 );
if ( err != OK )
printf( "\nTask 1 semFlush of enable1 returned error %x\r\n", errno );
else
printf( "\r\n" );
puts( "Task 1 blocking until Tasks 4, 7, and 10 complete semFlush test." );
errno = 0;
err = semTake( complt4, WAIT_FOREVER );
err = semTake( complt7, WAIT_FOREVER );
err = semTake( complt10, WAIT_FOREVER );
}
/*****************************************************************************
** validate_counting_semaphores
** This function sequences through a series of actions to exercise
** the various features and characteristics of VxWorks semaphores
**
*****************************************************************************/
void validate_counting_semaphores( void )
{
STATUS err;
int i;
puts( "\r\n********** Counting Semaphore validation:" );
/************************************************************************
** Semaphore Creation Test
************************************************************************/
puts( "\n.......... First we create three semaphores:" );
puts( "\nCreating Counting Semaphore SEM1, FIFO queuing and 'locked'" );
errno = 0;
sema41_id = semCCreate( SEM_Q_FIFO, 0 );
if ( errno != OK )
{
printf( "... returned error %x\r\n", errno );
}
puts( "Creating Counting Semaphore SEM2, FIFO queuing with 2 tokens" );
errno = 0;
sema42_id = semCCreate( SEM_Q_FIFO, 2 );
if ( errno != OK )
{
printf( "... returned error %x\r\n", errno );
}
puts( "Creating Counting Semaphore SEM3, PRIORITY queuing and 'locked'" );
errno = 0;
sema43_id = semCCreate( SEM_Q_PRIORITY, 0 );
if ( errno != OK )
{
printf( "... returned error %x\r\n", errno );
}
/************************************************************************
** Semaphore Waiting and Task Queueing Order Test
************************************************************************/
puts( "\n.......... Next we enable Tasks 4, 7, and 10 to wait for" );
puts( " a token from SEM1 in reverse-priority order." );
puts( " Then we send three tokens to SEM1 and wait to see" );
puts( " the order in which the tasks get tokens." );
puts( " This tests the semaphore post and queueing logic." );
puts( " The token should be acquired by Tasks 4, 7, and 10" );
puts( " in that order." );
puts( "Task 1 enabling Tasks 4, 7, and 10 to consume SEM1 tokens.");
errno = 0;
err = semGive( enable4 );
if ( err != OK )
{
printf( "semGive of enable4 returned error %x\r\n", errno );
}
errno = 0;
err = semGive( enable7 );
if ( err != OK )
{
printf( "semGive of enable7 returned error %x\r\n", errno );
}
errno = 0;
err = semGive( enable10 );
if ( err != OK )
{
printf( "semGive of enable10 returned error %x\r\n", errno );
}
puts( "Task 1 blocking for handshake from Tasks 4, 7, and 10..." );
err = semTake( complt4, WAIT_FOREVER );
err = semTake( complt7, WAIT_FOREVER );
err = semTake( complt10, WAIT_FOREVER );
for ( i = 0; i < 3; i++ )
{
puts( "Task 1 sending token to semaphore SEM1." );
errno = 0;
err = semGive( sema41_id );
if ( err != OK )
{
printf( "\nTask 1 send token to SEM1 returned error %x\r\n",
errno );
}
}
puts( "Task 1 blocking for handshake from Tasks 4, 7, and 10..." );
err = semTake( complt4, WAIT_FOREVER );
err = semTake( complt7, WAIT_FOREVER );
err = semTake( complt10, WAIT_FOREVER );
puts( "\n.......... Next Tasks 4, 7, and 10 look for tokens from SEM2" );
puts( " in reverse-priority order. However, SEM2 has only two" );
puts( " tokens available, so one task will fail to acquire one.");
puts( " Since the tasks did not wait on the semaphore, the");
puts( " loser of the race will return an error 0x3d0002");
puts( "Task 1 enabling Tasks 4, 7, and 10 to consume SEM2 tokens.");
errno = 0;
err = semGive( enable4 );
if ( err != OK )
{
printf( "semGive of enable4 returned error %x\r\n", errno );
}
errno = 0;
err = semGive( enable7 );
if ( err != OK )
{
printf( "semGive of enable7 returned error %x\r\n", errno );
}
errno = 0;
err = semGive( enable10 );
if ( err != OK )
{
printf( "semGive of enable10 returned error %x\r\n", errno );
}
puts( "Task 1 blocking for handshake from Tasks 4, 7, and 10..." );
err = semTake( complt4, WAIT_FOREVER );
err = semTake( complt7, WAIT_FOREVER );
err = semTake( complt10, WAIT_FOREVER );
puts( "\n.......... Next Tasks 4, 7, and 10 look for tokens from SEM3" );
puts( " in reverse-priority order. However, SEM3 will be sent" );
puts( " only two tokens, so one task will fail to acquire one.");
puts( " Since the tasks do wait on the semaphore, the lowest");
puts( " priority task will return an errno 0x3d0004");
puts( "Task 1 enabling Tasks 4, 7, and 10 to consume SEM3 tokens.");
errno = 0;
err = semGive( enable4 );
if ( err != OK )
{
printf( "semGive of enable4 returned error %x\r\n", errno );
}
taskDelay( 2 );
errno = 0;
err = semGive( enable7 );
if ( err != OK )
{
printf( "semGive of enable7 returned error %x\r\n", errno );
}
taskDelay( 2 );
errno = 0;
err = semGive( enable10 );
if ( err != OK )
{
printf( "semGive of enable10 returned error %x\r\n", errno );
}
taskDelay( 2 );
puts( "Task 1 blocking for handshake from Tasks 4, 7, and 10..." );
errno = 0;
err = semTake( complt4, WAIT_FOREVER );
err = semTake( complt7, WAIT_FOREVER );
err = semTake( complt10, WAIT_FOREVER );
for ( i = 0; i < 2; i++ )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -