nsprpub/pr/tests/sem.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     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 /***********************************************************************
     7 **
     8 ** Name: sem.c
     9 **
    10 ** Description: Tests Semaphonre functions.
    11 **
    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 /***********************************************************************
    23 ** Includes
    24 ***********************************************************************/
    25 /* Used to get the command line option */
    26 #include "plgetopt.h"
    28 #include "nspr.h"
    29 #include "prpriv.h"
    31 #include <stdio.h>
    32 #include <stdlib.h>
    33 #include <string.h>
    35 PRIntn failed_already=0;
    36 PRIntn debug_mode;
    38 /* 
    39 	Since we don't have stdin, stdout everywhere, we will fake 
    40 	it with our in-memory buffers called stdin and stdout.
    41 */
    43 #define SBSIZE 1024
    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)
    56 {
    57 	PRUintn	i;
    59 	/* during first read call, initialize the stdinBuf buffer*/
    60 	if (stdinBufIdx == 0) {
    61 		for (i=0; i<SBSIZE; i++)
    62 			stdinBuf[i] = i;
    63 	}
    65 	/* now copy data from stdinBuf to the given buffer upto bufSize */
    66 	for (i=0; i<bufSize; i++) {
    67 		if (stdinBufIdx == SBSIZE)
    68 			break;
    69 		buf[i] = stdinBuf[stdinBufIdx++];
    70 	}
    72 	return i;
    73 }
    75 static size_t dwrite (PRUintn device, char *buf, size_t bufSize)
    76 {
    77 	PRUintn	i, j;
    79 	/* copy data from the given buffer upto bufSize to stdoutBuf */
    80 	for (i=0; i<bufSize; i++) {
    81 		if (stdoutBufIdx == SBSIZE)
    82 			break;
    83 		stdoutBuf[stdoutBufIdx++] = buf[i];
    84 	}
    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;
    92 			}
    94 	return i;
    95 }
    97 /*------------------ Following is the real test program ---------*/
    98 /*
    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.
   103 */
   105 PRSemaphore	*emptyBufs;	/* number of empty buffers */
   106 PRSemaphore *fullBufs;	/* number of buffers that are full */
   108 #define BSIZE	100
   110 struct {
   111 	char data[BSIZE];
   112 	PRUintn nbytes;		/* number of bytes in this buffer */
   113 } buf[2];
   115 static void PR_CALLBACK reader(void *arg)
   116 {
   117 	PRUintn	i = 0;
   118 	size_t	nbytes;
   120 	do {
   121 		(void) PR_WaitSem(emptyBufs);
   122 		nbytes = dread(0, buf[i].data, BSIZE);
   123 		buf[i].nbytes = nbytes;
   124 		PR_PostSem(fullBufs);
   125 		i = (i + 1) % 2;
   126 	} while (nbytes > 0);
   127 }
   129 static void writer(void)
   130 {
   131 	PRUintn	i = 0;
   132 	size_t	nbytes;
   134 	do {
   135 		(void) PR_WaitSem(fullBufs);
   136 		nbytes = buf[i].nbytes;
   137 		if (nbytes > 0) {
   138 			nbytes = dwrite(1, buf[i].data, nbytes);
   139 			PR_PostSem(emptyBufs);
   140 			i = (i + 1) % 2;
   141 		}
   142 	} while (nbytes > 0);
   143 }
   145 int main(int argc, char **argv)
   146 {
   147 	PRThread *r;
   149     PR_STDIO_INIT();
   150     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   152     {
   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)
   156     	test.
   157     	Usage: test_name -d
   158     	*/
   159     	PLOptStatus os;
   160     	PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
   161     	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   162         {
   163     		if (PL_OPT_BAD == os) continue;
   164             switch (opt->option)
   165             {
   166             case 'd':  /* debug mode */
   167     			debug_mode = 1;
   168                 break;
   169              default:
   170                 break;
   171             }
   172         }
   173     	PL_DestroyOptState(opt);
   174     }        
   176  /* main test */
   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,
   185 				      reader, 0, 
   186 				      PR_PRIORITY_NORMAL,
   187 				      PR_LOCAL_THREAD,
   188     				  PR_UNJOINABLE_THREAD,
   189 				      0);
   191 	/* Do the writer operation in this thread */
   192 	writer();
   194 	PR_DestroySem(emptyBufs);
   195 	PR_DestroySem(fullBufs);
   197 	if (finalResult == PR_SUCCESS) {
   198 		if (debug_mode) printf("sem Test Passed.\n");
   199 	}
   200 	else{
   201 		if (debug_mode) printf("sem Test Failed.\n");
   202 		failed_already=1;
   203 	}
   204     PR_Cleanup();
   205 	if(failed_already)	
   206 		return 1;
   207 	else
   208 		return 0;
   209 }

mercurial