1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /***********************************************************************
42 ** Description: Tests Semaphonre functions.
44 ** Modification History:
45 ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
46 ** The debug mode will print all of the printfs associated with this test.
47 ** The regress mode will be the default mode. Since the regress tool limits
48 ** the output to a one line status:PASS or FAIL,all of the printf statements
49 ** have been handled with an if (debug_mode) statement.
50 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
51 ** recognize the return code from tha main program.
52 ***********************************************************************/
54 /***********************************************************************
56 ***********************************************************************/
57 /* Used to get the command line option */
67 PRIntn failed_already=0;
71 Since we don't have stdin, stdout everywhere, we will fake
72 it with our in-memory buffers called stdin and stdout.
77 #include "obsolete/prsem.h"
79 static char stdinBuf[SBSIZE];
80 static char stdoutBuf[SBSIZE];
82 static PRUintn stdinBufIdx = 0;
83 static PRUintn stdoutBufIdx = 0;
84 static PRStatus finalResult = PR_SUCCESS;
87 static size_t dread (PRUintn device, char *buf, size_t bufSize)
91 /* during first read call, initialize the stdinBuf buffer*/
92 if (stdinBufIdx == 0) {
93 for (i=0; i<SBSIZE; i++)
97 /* now copy data from stdinBuf to the given buffer upto bufSize */
98 for (i=0; i<bufSize; i++) {
99 if (stdinBufIdx == SBSIZE)
101 buf[i] = stdinBuf[stdinBufIdx++];
107 static size_t dwrite (PRUintn device, char *buf, size_t bufSize)
111 /* copy data from the given buffer upto bufSize to stdoutBuf */
112 for (i=0; i<bufSize; i++) {
113 if (stdoutBufIdx == SBSIZE)
115 stdoutBuf[stdoutBufIdx++] = buf[i];
118 /* during last write call, compare the two buffers */
119 if (stdoutBufIdx == SBSIZE)
120 for (j=0; j<SBSIZE; j++)
121 if (stdinBuf[j] != stdoutBuf[j]) {
122 if (debug_mode) printf("data mismatch for index= %d \n", j);
123 finalResult = PR_FAILURE;
129 /*------------------ Following is the real test program ---------*/
131 Program to copy standard input to standard output. The program
132 uses two threads. One reads the input and puts the data in a
133 double buffer. The other reads the buffer contents and writes
134 it to standard output.
137 PRSemaphore *emptyBufs; /* number of empty buffers */
138 PRSemaphore *fullBufs; /* number of buffers that are full */
144 PRUintn nbytes; /* number of bytes in this buffer */
147 static void PR_CALLBACK reader(void *arg)
153 (void) PR_WaitSem(emptyBufs);
154 nbytes = dread(0, buf[i].data, BSIZE);
155 buf[i].nbytes = nbytes;
156 PR_PostSem(fullBufs);
158 } while (nbytes > 0);
161 static void writer(void)
167 (void) PR_WaitSem(fullBufs);
168 nbytes = buf[i].nbytes;
170 nbytes = dwrite(1, buf[i].data, nbytes);
171 PR_PostSem(emptyBufs);
174 } while (nbytes > 0);
177 int main(int argc, char **argv)
182 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
185 /* The command line argument: -d is used to determine if the test is being run
186 in debug mode. The regress tool requires only one line output:PASS or FAIL.
187 All of the printfs associated with this test has been handled with a if (debug_mode)
192 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
193 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
195 if (PL_OPT_BAD == os) continue;
198 case 'd': /* debug mode */
205 PL_DestroyOptState(opt);
210 emptyBufs = PR_NewSem(2); /* two empty buffers */
212 fullBufs = PR_NewSem(0); /* zero full buffers */
214 /* create the reader thread */
216 r = PR_CreateThread(PR_USER_THREAD,
220 PR_UNJOINABLE_THREAD,
223 /* Do the writer operation in this thread */
226 PR_DestroySem(emptyBufs);
227 PR_DestroySem(fullBufs);
229 if (finalResult == PR_SUCCESS) {
230 if (debug_mode) printf("sem Test Passed.\n");
233 if (debug_mode) printf("sem Test Failed.\n");