/* source.cpp */ /* includes */ #include "vxWorks.h" #include "stdio.h" #include "stdlib.h" #include "semLib.h" #include "taskLib.h" #include "assert.h" #include "sched.h" #include "kernelLib.h" #define STACK_SIZE 20000 int tidTask1; /* Task IDs */ int tidTask2; int tidProgStop; #define START 1 #define STOP 2 volatile UINT8 runState1; volatile UINT8 runState2; SEM_ID stopSemId; SEM_ID mutexSemId; /* forward declarations */ void task1 (void); void task2 (void); void increment (int num); void progStop(void); STATUS progStart (void); static int amount = 0; /************************************************************************* * * progStart - start the sample program. * * Create various semaphores and spawn various tasks, while doing * incredibly little error checking. * * RETURNS: OK */ STATUS progStart (void) { /* setup semaphores */ stopSemId = semCCreate(SEM_Q_FIFO, 0); mutexSemId = semBCreate(SEM_Q_FIFO, SEM_FULL); /* get started */ runState1 = START; runState2 = START; kernelTimeSlice(100); amount = 0; tidTask1 = taskSpawn ("task1", 1, 0, STACK_SIZE, (FUNCPTR) task1,0,0,0,0,0,0,0,0,0,0); tidTask2 = taskSpawn ("task2", 1, 0, STACK_SIZE, (FUNCPTR) task2,0,0,0,0,0,0,0,0,0,0); tidProgStop = taskSpawn ("progStop", 200, 0, STACK_SIZE, (FUNCPTR) progStop,0,0,0,0,0,0,0,0,0,0); return (OK); } void task1() { for (long int i=0; i < 10000; i++) increment(1); semGive(stopSemId); } void task2() { for (long int i=0; i < 10000; i++) increment(1); semGive(stopSemId); } void increment (int num) { amount = amount + num; } /************************************************************************* * * progStop - stops the program * * Call this routine to end it all. */ void progStop (void) { // wait for both tasks to finish semTake(stopSemId, WAIT_FOREVER); semTake(stopSemId, WAIT_FOREVER); printf("Amount = %d\n", amount); /* clean up */ semDelete (stopSemId); semDelete (mutexSemId); printf ("BYE!TSCHUESS!ADIEU!\n"); }