www.pudn.com > Tornado_Train_Workshop_demo_program.rar > fifoQ0.c


/* fifoQ0.c - demonstrate the Haas effect with WindView */ 
 
#include "vxWorks.h" 
#include "msgQLib.h" 
#include "taskLib.h" 
#include "semLib.h" 
#include "wvLib.h" 
#include "stdio.h" 
 
#define MSGS_MAX (5)		/* Queue dimensions */ 
#define MSG_SIZE_MAX (64) 
 
#define PRI_RECV_LOW (150)	/* Task priorities */ 
#define PRI_RECV_MED (125) 
#define PRI_SEND     (100) 
 
#define OPTS	     (0) 
#define STACK_SIZE   (20000) 
 
MSG_Q_ID theQ = NULL; 
SEM_ID   syncSem = NULL; 
 
int tSend;			/* Task IDs */ 
int tRecvLow; 
int tRecvMed; 
 
char message [] = "My bonnie lies over the ocean."; 
 
int countLow; 
int countMed; 
 
LOCAL BOOL started = FALSE; 
 
LOCAL void send (void);		/* Task entry point functions */ 
LOCAL void recvLow (void); 
LOCAL void recvMed (void); 
 
STATUS fifoQStart (void) 
    { 
    if (started) 
	{ 
	printf ("Already started.\n"); 
	return ERROR; 
	} 
 
    countLow = 0; 
    countMed = 0; 
 
    if (theQ == NULL) 
	theQ = msgQCreate (MSGS_MAX, MSG_SIZE_MAX, MSG_Q_FIFO); 
 
    if (syncSem == NULL) 
	syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY); 
 
    if (theQ == NULL || syncSem == NULL) 
	{ 
	printf ("Couldn't start demonstration.\n"); 
	return ERROR; 
	} 
 
    if (   (tRecvLow = taskSpawn ("tRecvLow", PRI_RECV_LOW, OPTS, STACK_SIZE, 
				  (FUNCPTR) recvLow, 0,0,0,0,0,0,0,0,0,0))  
	    == ERROR 
	|| (tRecvMed = taskSpawn ("tRecvMed", PRI_RECV_MED, OPTS, STACK_SIZE, 
				  (FUNCPTR) recvMed, 0,0,0,0,0,0,0,0,0,0)) 
	    == ERROR 
	|| (tSend =    taskSpawn ("tSend", PRI_SEND, OPTS, STACK_SIZE, 
				  (FUNCPTR) send,    0,0,0,0,0,0,0,0,0,0)) 
	    == ERROR) 
	{ 
	printf ("Couldn't spawn tasks.\n"); 
	return ERROR; 
	} 
 
    started = TRUE; 
    return OK; 
    } 
 
STATUS fifoQStop (void) 
    { 
    char dummy [1]; 
 
    if (!started) 
	{ 
	printf ("Not started.\n"); 
	return ERROR; 
	} 
 
    taskDelete (tSend); 
    taskDelete (tRecvMed); 
    taskDelete (tRecvLow); 
 
    while (semTake (syncSem, NO_WAIT) == OK) 
	; 
 
    while (msgQReceive (theQ, dummy, 1, NO_WAIT) != ERROR) 
	; 
 
    started = FALSE; 
    return OK; 
    } 
 
LOCAL void send (void) 
    { 
    FOREVER 
	{ 
	taskDelay (1); 
 
	msgQSend (theQ, message, sizeof (message), MSG_PRI_NORMAL, WAIT_FOREVER); 
 
	semGive (syncSem); 
	} 
    } 
 
LOCAL void recvLow (void) 
    { 
    char msg [MSG_SIZE_MAX]; 
 
    FOREVER 
	{ 
	msgQReceive (theQ, msg, sizeof (msg), WAIT_FOREVER); 
 
	++countLow; 
 
	wvEvent (1, (char *) &countLow, sizeof (countLow)); 
 
	printf ("Message received: %s\n", msg); 
	} 
    } 
 
LOCAL void recvMed (void) 
    { 
    char msg [MSG_SIZE_MAX]; 
 
    FOREVER 
	{ 
	semTake (syncSem, WAIT_FOREVER); 
 
	msgQReceive (theQ, msg, sizeof (msg), WAIT_FOREVER); 
 
	++countMed; 
 
	wvEvent (2, (char *) &countMed, sizeof (countMed)); 
	} 
    }