michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /*********************************************************************** michael@0: ** 1996 - Netscape Communications Corporation michael@0: ** michael@0: ** Name: cvar.c michael@0: ** michael@0: ** Description: Tests Condition Variable Operations michael@0: ** michael@0: ** Modification History: michael@0: ** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. michael@0: ** The debug mode will print all of the printfs associated with this test. michael@0: ** The regress mode will be the default mode. Since the regress tool limits michael@0: ** the output to a one line status:PASS or FAIL,all of the printf statements michael@0: ** have been handled with an if (debug_mode) statement. michael@0: ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to michael@0: ** recognize the return code from tha main program. michael@0: ** 12-June-97 Revert to return code 0 and 1. michael@0: ***********************************************************************/ michael@0: michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: michael@0: #include "nspr.h" michael@0: michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: PRMonitor *mon; michael@0: #define DEFAULT_COUNT 1000 michael@0: PRInt32 count = 0; michael@0: PRIntn debug_mode; michael@0: michael@0: #define kQSIZE 1 michael@0: michael@0: typedef struct { michael@0: PRLock *bufLock; michael@0: int startIdx; michael@0: int numFull; michael@0: PRCondVar *notFull; michael@0: PRCondVar *notEmpty; michael@0: void *data[kQSIZE]; michael@0: } CircBuf; michael@0: michael@0: static PRBool failed = PR_FALSE; michael@0: michael@0: /* michael@0: ** NewCB creates and initializes a new circular buffer. michael@0: */ michael@0: static CircBuf* NewCB(void) michael@0: { michael@0: CircBuf *cbp; michael@0: michael@0: cbp = PR_NEW(CircBuf); michael@0: if (cbp == NULL) michael@0: return (NULL); michael@0: michael@0: cbp->bufLock = PR_NewLock(); michael@0: cbp->startIdx = 0; michael@0: cbp->numFull = 0; michael@0: cbp->notFull = PR_NewCondVar(cbp->bufLock); michael@0: cbp->notEmpty = PR_NewCondVar(cbp->bufLock); michael@0: michael@0: return (cbp); michael@0: } michael@0: michael@0: /* michael@0: ** DeleteCB frees a circular buffer. michael@0: */ michael@0: static void DeleteCB(CircBuf *cbp) michael@0: { michael@0: PR_DestroyLock(cbp->bufLock); michael@0: PR_DestroyCondVar(cbp->notFull); michael@0: PR_DestroyCondVar(cbp->notEmpty); michael@0: PR_DELETE(cbp); michael@0: } michael@0: michael@0: michael@0: /* michael@0: ** PutCBData puts new data on the queue. If the queue is full, it waits michael@0: ** until there is room. michael@0: */ michael@0: static void PutCBData(CircBuf *cbp, void *data) michael@0: { michael@0: PR_Lock(cbp->bufLock); michael@0: /* wait while the buffer is full */ michael@0: while (cbp->numFull == kQSIZE) michael@0: PR_WaitCondVar(cbp->notFull,PR_INTERVAL_NO_TIMEOUT); michael@0: cbp->data[(cbp->startIdx + cbp->numFull) % kQSIZE] = data; michael@0: cbp->numFull += 1; michael@0: michael@0: /* let a waiting reader know that there is data */ michael@0: PR_NotifyCondVar(cbp->notEmpty); michael@0: PR_Unlock(cbp->bufLock); michael@0: michael@0: } michael@0: michael@0: michael@0: /* michael@0: ** GetCBData gets the oldest data on the queue. If the queue is empty, it waits michael@0: ** until new data appears. michael@0: */ michael@0: static void* GetCBData(CircBuf *cbp) michael@0: { michael@0: void *data; michael@0: michael@0: PR_Lock(cbp->bufLock); michael@0: /* wait while the buffer is empty */ michael@0: while (cbp->numFull == 0) michael@0: PR_WaitCondVar(cbp->notEmpty,PR_INTERVAL_NO_TIMEOUT); michael@0: data = cbp->data[cbp->startIdx]; michael@0: cbp->startIdx =(cbp->startIdx + 1) % kQSIZE; michael@0: cbp->numFull -= 1; michael@0: michael@0: /* let a waiting writer know that there is room */ michael@0: PR_NotifyCondVar(cbp->notFull); michael@0: PR_Unlock(cbp->bufLock); michael@0: michael@0: return (data); michael@0: } michael@0: michael@0: michael@0: /************************************************************************/ michael@0: michael@0: static int alive; michael@0: michael@0: static void PR_CALLBACK CXReader(void *arg) michael@0: { michael@0: CircBuf *cbp = (CircBuf *)arg; michael@0: PRInt32 i, n; michael@0: void *data; michael@0: michael@0: n = count / 2; michael@0: for (i = 0; i < n; i++) { michael@0: data = GetCBData(cbp); michael@0: if ((int)data != i) michael@0: if (debug_mode) printf("data mismatch at for i = %d usec\n", i); michael@0: } michael@0: michael@0: PR_EnterMonitor(mon); michael@0: --alive; michael@0: PR_Notify(mon); michael@0: PR_ExitMonitor(mon); michael@0: } michael@0: michael@0: static void PR_CALLBACK CXWriter(void *arg) michael@0: { michael@0: CircBuf *cbp = (CircBuf *)arg; michael@0: PRInt32 i, n; michael@0: michael@0: n = count / 2; michael@0: for (i = 0; i < n; i++) michael@0: PutCBData(cbp, (void *)i); michael@0: michael@0: PR_EnterMonitor(mon); michael@0: --alive; michael@0: PR_Notify(mon); michael@0: PR_ExitMonitor(mon); michael@0: } michael@0: michael@0: static void CondWaitContextSwitch(PRThreadScope scope1, PRThreadScope scope2) michael@0: { michael@0: PRThread *t1, *t2; michael@0: CircBuf *cbp; michael@0: michael@0: PR_EnterMonitor(mon); michael@0: michael@0: alive = 2; michael@0: michael@0: cbp = NewCB(); michael@0: michael@0: t1 = PR_CreateThread(PR_USER_THREAD, michael@0: CXReader, cbp, michael@0: PR_PRIORITY_NORMAL, michael@0: scope1, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0); michael@0: PR_ASSERT(t1); michael@0: t2 = PR_CreateThread(PR_USER_THREAD, michael@0: CXWriter, cbp, michael@0: PR_PRIORITY_NORMAL, michael@0: scope2, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0); michael@0: PR_ASSERT(t2); michael@0: michael@0: /* Wait for both of the threads to exit */ michael@0: while (alive) { michael@0: PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: michael@0: DeleteCB(cbp); michael@0: michael@0: PR_ExitMonitor(mon); michael@0: } michael@0: michael@0: static void CondWaitContextSwitchUU(void) michael@0: { michael@0: CondWaitContextSwitch(PR_LOCAL_THREAD, PR_LOCAL_THREAD); michael@0: } michael@0: michael@0: static void CondWaitContextSwitchUK(void) michael@0: { michael@0: CondWaitContextSwitch(PR_LOCAL_THREAD, PR_GLOBAL_THREAD); michael@0: } michael@0: michael@0: static void CondWaitContextSwitchKK(void) michael@0: { michael@0: CondWaitContextSwitch(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD); michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: static void Measure(void (*func)(void), const char *msg) michael@0: { michael@0: PRIntervalTime start, stop; michael@0: double d; michael@0: michael@0: start = PR_IntervalNow(); michael@0: (*func)(); michael@0: stop = PR_IntervalNow(); michael@0: michael@0: d = (double)PR_IntervalToMicroseconds(stop - start); michael@0: michael@0: if (debug_mode) printf("%40s: %6.2f usec\n", msg, d / count); michael@0: michael@0: if (0 == d) failed = PR_TRUE; michael@0: } michael@0: michael@0: static PRIntn PR_CALLBACK RealMain(int argc, char **argv) michael@0: { michael@0: /* The command line argument: -d is used to determine if the test is being run michael@0: in debug mode. The regress tool requires only one line output:PASS or FAIL. michael@0: All of the printfs associated with this test has been handled with a if (debug_mode) michael@0: test. michael@0: Usage: test_name [-d] [-c n] michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dc:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = 1; michael@0: break; michael@0: case 'c': /* loop count */ michael@0: count = atoi(opt->value); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: if (0 == count) count = DEFAULT_COUNT; michael@0: michael@0: mon = PR_NewMonitor(); michael@0: michael@0: Measure(CondWaitContextSwitchUU, "cond var wait context switch- user/user"); michael@0: Measure(CondWaitContextSwitchUK, "cond var wait context switch- user/kernel"); michael@0: Measure(CondWaitContextSwitchKK, "cond var wait context switch- kernel/kernel"); michael@0: michael@0: PR_DestroyMonitor(mon); michael@0: michael@0: if (debug_mode) printf("%s\n", (failed) ? "FAILED" : "PASSED"); michael@0: michael@0: if(failed) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: PRIntn rv; michael@0: michael@0: PR_STDIO_INIT(); michael@0: rv = PR_Initialize(RealMain, argc, argv, 0); michael@0: return rv; michael@0: } /* main */