VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/sem.c@ 26515

Last change on this file since 26515 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/***********************************************************************
39**
40** Name: sem.c
41**
42** Description: Tests Semaphonre functions.
43**
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***********************************************************************/
53
54/***********************************************************************
55** Includes
56***********************************************************************/
57/* Used to get the command line option */
58#include "plgetopt.h"
59
60#include "nspr.h"
61#include "prpriv.h"
62
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66
67PRIntn failed_already=0;
68PRIntn debug_mode;
69
70/*
71 Since we don't have stdin, stdout everywhere, we will fake
72 it with our in-memory buffers called stdin and stdout.
73*/
74
75#define SBSIZE 1024
76
77#ifdef XP_MAC
78#include "prlog.h"
79#include "prsem.h"
80#define printf PR_LogPrint
81extern void SetupMacPrintfLog(char *logFile);
82#else
83#include "obsolete/prsem.h"
84#endif
85
86static char stdinBuf[SBSIZE];
87static char stdoutBuf[SBSIZE];
88
89static PRUintn stdinBufIdx = 0;
90static PRUintn stdoutBufIdx = 0;
91static PRStatus finalResult = PR_SUCCESS;
92
93
94static size_t dread (PRUintn device, char *buf, size_t bufSize)
95{
96 PRUintn i;
97
98 /* during first read call, initialize the stdinBuf buffer*/
99 if (stdinBufIdx == 0) {
100 for (i=0; i<SBSIZE; i++)
101 stdinBuf[i] = i;
102 }
103
104 /* now copy data from stdinBuf to the given buffer upto bufSize */
105 for (i=0; i<bufSize; i++) {
106 if (stdinBufIdx == SBSIZE)
107 break;
108 buf[i] = stdinBuf[stdinBufIdx++];
109 }
110
111 return i;
112}
113
114static size_t dwrite (PRUintn device, char *buf, size_t bufSize)
115{
116 PRUintn i, j;
117
118 /* copy data from the given buffer upto bufSize to stdoutBuf */
119 for (i=0; i<bufSize; i++) {
120 if (stdoutBufIdx == SBSIZE)
121 break;
122 stdoutBuf[stdoutBufIdx++] = buf[i];
123 }
124
125 /* during last write call, compare the two buffers */
126 if (stdoutBufIdx == SBSIZE)
127 for (j=0; j<SBSIZE; j++)
128 if (stdinBuf[j] != stdoutBuf[j]) {
129 if (debug_mode) printf("data mismatch for index= %d \n", j);
130 finalResult = PR_FAILURE;
131 }
132
133 return i;
134}
135
136/*------------------ Following is the real test program ---------*/
137/*
138 Program to copy standard input to standard output. The program
139 uses two threads. One reads the input and puts the data in a
140 double buffer. The other reads the buffer contents and writes
141 it to standard output.
142*/
143
144PRSemaphore *emptyBufs; /* number of empty buffers */
145PRSemaphore *fullBufs; /* number of buffers that are full */
146
147#define BSIZE 100
148
149struct {
150 char data[BSIZE];
151 PRUintn nbytes; /* number of bytes in this buffer */
152} buf[2];
153
154static void PR_CALLBACK reader(void *arg)
155{
156 PRUintn i = 0;
157 size_t nbytes;
158
159 do {
160 (void) PR_WaitSem(emptyBufs);
161 nbytes = dread(0, buf[i].data, BSIZE);
162 buf[i].nbytes = nbytes;
163 PR_PostSem(fullBufs);
164 i = (i + 1) % 2;
165 } while (nbytes > 0);
166}
167
168static void writer(void)
169{
170 PRUintn i = 0;
171 size_t nbytes;
172
173 do {
174 (void) PR_WaitSem(fullBufs);
175 nbytes = buf[i].nbytes;
176 if (nbytes > 0) {
177 nbytes = dwrite(1, buf[i].data, nbytes);
178 PR_PostSem(emptyBufs);
179 i = (i + 1) % 2;
180 }
181 } while (nbytes > 0);
182}
183
184int main(int argc, char **argv)
185{
186 PRThread *r;
187
188 PR_STDIO_INIT();
189 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
190
191 {
192 /* The command line argument: -d is used to determine if the test is being run
193 in debug mode. The regress tool requires only one line output:PASS or FAIL.
194 All of the printfs associated with this test has been handled with a if (debug_mode)
195 test.
196 Usage: test_name -d
197 */
198 PLOptStatus os;
199 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
200 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
201 {
202 if (PL_OPT_BAD == os) continue;
203 switch (opt->option)
204 {
205 case 'd': /* debug mode */
206 debug_mode = 1;
207 break;
208 default:
209 break;
210 }
211 }
212 PL_DestroyOptState(opt);
213 }
214
215 /* main test */
216
217#ifdef XP_MAC
218 SetupMacPrintfLog("sem.log");
219 debug_mode = 1;
220#endif
221
222 emptyBufs = PR_NewSem(2); /* two empty buffers */
223
224 fullBufs = PR_NewSem(0); /* zero full buffers */
225
226 /* create the reader thread */
227
228 r = PR_CreateThread(PR_USER_THREAD,
229 reader, 0,
230 PR_PRIORITY_NORMAL,
231 PR_LOCAL_THREAD,
232 PR_UNJOINABLE_THREAD,
233 0);
234
235 /* Do the writer operation in this thread */
236 writer();
237
238 PR_DestroySem(emptyBufs);
239 PR_DestroySem(fullBufs);
240
241 if (finalResult == PR_SUCCESS) {
242 if (debug_mode) printf("sem Test Passed.\n");
243 }
244 else{
245 if (debug_mode) printf("sem Test Failed.\n");
246 failed_already=1;
247 }
248 PR_Cleanup();
249 if(failed_already)
250 return 1;
251 else
252 return 0;
253}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette