/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/* Control level code for the program 'mxtest' */

#include "../h/rci.h"

#include "mxtest.h"

static int setBuf[MEMSIZE];
static int getBuf[MEMSIZE];
static int numTries = 0;
static int numCollisions = 0;

#define NUMTIMES	1000

static int initDone = 0;
#define INIT_CHECK()	if (!initDone) doInit();
static doInit()
{
	int k;

	initDone = 1;
	for (k=0; k<MEMSIZE; k++)
	 { setBuf[k] = 2;
	   getBuf[k] = 0;
	 }
	numTries = 0;
	numCollisions = 0;
	requestFlagInit (&initDone);
}

mutexTest(td, doMutex)
RCI_DESC *td;
int doMutex;
{
	int *obj = (int*)getMemByName("obj");
	int i, k;

	INIT_CHECK();

	for (i=0; i<NUMTIMES; i++)
	 { int val0;
	   if (!doMutex)
	    { bcopy (obj, getBuf, MEMSIZE*sizeof(int));
	      bcopy (setBuf, obj, MEMSIZE*sizeof(int));
	    }
	   else
	    { readMem (obj, getBuf);
	      writeMem (obj, setBuf);
	    }
	   val0 = getBuf[0];
	   for (k=1; k<MEMSIZE; k++)
	    { if (getBuf[k] != val0)
	       { numCollisions++;
		 break;
	       }
	    }
	   numTries++;
	   if ((numTries % 10000) == 0)
	    { rciPrintf ("%8d tries, %d collisions\n",
			numTries, numCollisions);
	    }
	 }
}
