1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /***********************************************************************
10 ** Description: Tests Semaphonre functions.
12 ** Modification History:
13 ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
14 ** The debug mode will print all of the printfs associated with this test.
15 ** The regress mode will be the default mode. Since the regress tool limits
16 ** the output to a one line status:PASS or FAIL,all of the printf statements
17 ** have been handled with an if (debug_mode) statement.
18 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
19 ** recognize the return code from tha main program.
20 ***********************************************************************/
22 /***********************************************************************
24 ***********************************************************************/
25 /* Used to get the command line option */
35 PRIntn failed_already=0;
39 Since we don't have stdin, stdout everywhere, we will fake
40 it with our in-memory buffers called stdin and stdout.
45 #include "obsolete/prsem.h"
47 static char stdinBuf[SBSIZE];
48 static char stdoutBuf[SBSIZE];
50 static PRUintn stdinBufIdx = 0;
51 static PRUintn stdoutBufIdx = 0;
52 static PRStatus finalResult = PR_SUCCESS;
55 static size_t dread (PRUintn device, char *buf, size_t bufSize)
59 /* during first read call, initialize the stdinBuf buffer*/
60 if (stdinBufIdx == 0) {
61 for (i=0; i<SBSIZE; i++)
65 /* now copy data from stdinBuf to the given buffer upto bufSize */
66 for (i=0; i<bufSize; i++) {
67 if (stdinBufIdx == SBSIZE)
69 buf[i] = stdinBuf[stdinBufIdx++];
75 static size_t dwrite (PRUintn device, char *buf, size_t bufSize)
79 /* copy data from the given buffer upto bufSize to stdoutBuf */
80 for (i=0; i<bufSize; i++) {
81 if (stdoutBufIdx == SBSIZE)
83 stdoutBuf[stdoutBufIdx++] = buf[i];
86 /* during last write call, compare the two buffers */
87 if (stdoutBufIdx == SBSIZE)
88 for (j=0; j<SBSIZE; j++)
89 if (stdinBuf[j] != stdoutBuf[j]) {
90 if (debug_mode) printf("data mismatch for index= %d \n", j);
91 finalResult = PR_FAILURE;
97 /*------------------ Following is the real test program ---------*/
99 Program to copy standard input to standard output. The program
100 uses two threads. One reads the input and puts the data in a
101 double buffer. The other reads the buffer contents and writes
102 it to standard output.
105 PRSemaphore *emptyBufs; /* number of empty buffers */
106 PRSemaphore *fullBufs; /* number of buffers that are full */
112 PRUintn nbytes; /* number of bytes in this buffer */
115 static void PR_CALLBACK reader(void *arg)
121 (void) PR_WaitSem(emptyBufs);
122 nbytes = dread(0, buf[i].data, BSIZE);
123 buf[i].nbytes = nbytes;
124 PR_PostSem(fullBufs);
126 } while (nbytes > 0);
129 static void writer(void)
135 (void) PR_WaitSem(fullBufs);
136 nbytes = buf[i].nbytes;
138 nbytes = dwrite(1, buf[i].data, nbytes);
139 PR_PostSem(emptyBufs);
142 } while (nbytes > 0);
145 int main(int argc, char **argv)
150 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
153 /* The command line argument: -d is used to determine if the test is being run
154 in debug mode. The regress tool requires only one line output:PASS or FAIL.
155 All of the printfs associated with this test has been handled with a if (debug_mode)
160 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
161 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
163 if (PL_OPT_BAD == os) continue;
166 case 'd': /* debug mode */
173 PL_DestroyOptState(opt);
178 emptyBufs = PR_NewSem(2); /* two empty buffers */
180 fullBufs = PR_NewSem(0); /* zero full buffers */
182 /* create the reader thread */
184 r = PR_CreateThread(PR_USER_THREAD,
188 PR_UNJOINABLE_THREAD,
191 /* Do the writer operation in this thread */
194 PR_DestroySem(emptyBufs);
195 PR_DestroySem(fullBufs);
197 if (finalResult == PR_SUCCESS) {
198 if (debug_mode) printf("sem Test Passed.\n");
201 if (debug_mode) printf("sem Test Failed.\n");