thread1.c 3.66 KB
#include <os.h>
#include <rmon.h>
#include "corefunc.h"

/*
 * thread1
 *
 * Tests for:
 *	osStopThread
 *	osStartThread
 *
 * Assumed working:
 *	osCreateThread
 *	osYieldThread
 *	osCreateMesgQueue
 *	osSendMesg
 *	osRecvMesg
 *	osGetIntMask
 *	osSetIntMask
 *
 * Description:
 *	Check that osStopThread takes thread off run queue
 *	Check that redundant calls to osStopThread have no effect
 *	Check that osStopThread does not affect interrupt mask
 *	Check that osStartThread puts thread back on run queue
 *	Check that osStopThread with a NULL argument stops running thread
 */

#define	NUM_LOG	9

static void		slave(void *);
static int		logExecution(int);

static OSMesgQueue	slaveMesgQueue;
static OSMesg		slaveMesgBuf[1];

static OSThread		slaveThread;
static char		slaveStack[STACKSIZE];

static int		logTab[NUM_LOG];
static int		*logPtr = logTab;
static int		expLogTab[NUM_LOG] = { 0, 1, 0, 0, 0, 1, 0, 0, 1 };

int
thread1(void)
{
    OSIntMask savedMask, currentMask;
    int i;
    int addFailures, numFailures = 0;

    savedMask = osSetIntMask(OS_IM_ALL);

    osCreateMesgQueue(&slaveMesgQueue, slaveMesgBuf, 1);

    osCreateThread(&slaveThread, 100, slave, (void *)0,
		slaveStack+STACKSIZE, 10);
    osStartThread(&slaveThread);

    if (logExecution(0))	/* 0 */
	numFailures++;

    osYieldThread();		/* let slave run */

    if (logExecution(0))	/* 2 */
	numFailures++;

    osStopThread(&slaveThread);	/* stop it */

    osStopThread(&slaveThread);	/* stop it again */

    currentMask = osGetIntMask();
    if (currentMask != OS_IM_ALL) {
	osSyncPrintf("thread1: expected interrupt mask 0x%x, actual 0x%x\n",
		OS_IM_ALL, currentMask);
	numFailures++;
    }

    osYieldThread();		/* allow slave to run, but it can't! */

    if (logExecution(0))	/* 3 */
	numFailures++;

    osStartThread(&slaveThread);

    if (logExecution(0))	/* 4 */
	numFailures++;

    osYieldThread();		/* allow slave to run again */

    if (logExecution(0))	/* 6 */
	numFailures++;

    osStartThread(&slaveThread);

    if (logExecution(0))	/* 7 */
	numFailures++;

    osYieldThread();		/* allow slave to run again */

    (void)osRecvMesg(&slaveMesgQueue, (OSMesg *)&addFailures, OS_MESG_BLOCK);
    numFailures += addFailures;

    /*
     * Verify everybody ran when they were supposed to.
     */
    if (logPtr != &logTab[NUM_LOG]) {
	osSyncPrintf("thread1: log table underflow\n");
	numFailures++;
    }
    for (i = 0; i < logPtr - logTab; i++) {
	if (expLogTab[i] != logTab[i]) {
	    osSyncPrintf("thread1: expected log id %d, actual %d at entry %d\n",
		expLogTab[i], logTab[i], i);
	    numFailures++;
	}
    }

    /*
     * Restore original interrupt mask and check it once more.
     */
    currentMask = osSetIntMask(savedMask);
    if (currentMask != OS_IM_ALL) {
	osSyncPrintf("thread1: expected interrupt mask 0x%x, actual 0x%x\n",
		OS_IM_ALL, currentMask);
	numFailures++;
    }
    return(numFailures);
}

static void
slave(void *arg)
{
    OSIntMask currentMask;
    int numFailures = 0;

    if (logExecution(1))	/* 1 */
	numFailures++;

    osYieldThread();		/* let master run */

    if (logExecution(1))	/* 5 */
	numFailures++;

    osStopThread(NULL);		/* stop slave */

    currentMask = osGetIntMask();
    if (currentMask != OS_IM_ALL) {
	osSyncPrintf("thread1: expected interrupt mask 0x%x, actual 0x%x\n",
		OS_IM_ALL, currentMask);
	numFailures++;
    }

    if (logExecution(1))	/* 7 */
	numFailures++;

    osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
}

static int
logExecution(int id)
{
    if (logPtr >= &logTab[NUM_LOG]) {
	osSyncPrintf("thread1: log table overflow\n");
	return(1);
    }
/*
    osSyncPrintf("%d. log %d\n", logPtr-logTab, id);
*/
    *logPtr++ = id;
    return(0);
}