thread9.c 3.33 KB
#include <os.h>
#include <rmon.h>
#include "corefunc.h"

/*
 * thread9
 *
 * Tests for:
 *	osSetThreadPri (on stopped threads)
 *
 * Assumed working:
 *	osSetIntMask
 *	osGetIntMask
 *	osCreateThread
 *	osStartThread
 *	osStopThread
 *
 * Description:
 *	Check that setting the priority of a stopped thread correctly affects
 *		its relative position in the run queue when restarted.
 */

#define	NUM_LOG		10
#define	NUM_SLAVES	2

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

static OSMesgQueue	slaveMesgQueue;
static OSMesg		slaveMesgBuf[NUM_SLAVES];

static OSThread		slave1Thread;
static char		slave1Stack[STACKSIZE];

static OSThread		slave2Thread;
static char		slave2Stack[STACKSIZE];

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

int
thread9(void)
{
    OSIntMask savedMask, currentMask;
    OSMesg actualMesg, expectedMesg;
    int i, j;
    int addFailures, numFailures = 0;

    savedMask = osSetIntMask(OS_IM_ALL);

    osCreateMesgQueue(&slaveMesgQueue, slaveMesgBuf, NUM_SLAVES);

    osSetThreadPri(NULL, 40);

    osCreateThread(&slave1Thread, 101, slave, (void *)1, slave1Stack+STACKSIZE,
		  60);

    osCreateThread(&slave2Thread, 102, slave, (void *)2, slave2Stack+STACKSIZE,
		  50);

    osSetThreadPri(&slave1Thread, 20);
    osSetThreadPri(&slave2Thread, 30);

    logExecution(0);		/* 0 */

    osStartThread(&slave1Thread);
    osStartThread(&slave2Thread);

    logExecution(0);		/* 1 */

    /*
     * Lower priority to let slaves run.
     */
    osSetThreadPri(NULL, 10);

    logExecution(0);		/* 4 */

    /*
     * Raise my priority back up.
     */
    osSetThreadPri(NULL, 40);

    osSetThreadPri(&slave1Thread, 30);
    osSetThreadPri(&slave2Thread, 20);

    logExecution(0);		/* 5 */

    osStartThread(&slave1Thread);
    osStartThread(&slave2Thread);

    logExecution(0);		/* 6 */

    /*
     * Lower priority to let slaves run.
     */
    osSetThreadPri(NULL, 10);

    logExecution(0);		/* 9 */

    /*
     * Reap results.
     */
    for (i = 0; i < NUM_SLAVES; i++) {
	(void)osRecvMesg(&slaveMesgQueue, (OSMesg *)&addFailures,
		OS_MESG_BLOCK);
	numFailures += addFailures;
    }

    /*
     * Verify everybody ran when they were supposed to.
     */
    if (logPtr != &logTab[NUM_LOG]) {
	osSyncPrintf("thread9: log table underflow\n");
	numFailures++;
    }
    for (i = 0; i < logPtr - logTab; i++) {
	if (expLogTab[i] != logTab[i]) {
	    osSyncPrintf("thread9: 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("thread9: expected interrupt mask 0x%x, actual 0x%x\n",
		OS_IM_ALL, currentMask);
	numFailures++;
    }
    return(numFailures);
}

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

    id = (int)arg;

    logExecution(id);		/* 2, 3 */

    osStopThread(NULL);

    logExecution(id);		/* 7, 8 */

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

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