sem.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/. */
  5. /***********************************************************************
  6. **
  7. ** Name: sem.c
  8. **
  9. ** Description: Tests Semaphonre functions.
  10. **
  11. ** Modification History:
  12. ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  13. ** The debug mode will print all of the printfs associated with this test.
  14. ** The regress mode will be the default mode. Since the regress tool limits
  15. ** the output to a one line status:PASS or FAIL,all of the printf statements
  16. ** have been handled with an if (debug_mode) statement.
  17. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  18. ** recognize the return code from tha main program.
  19. ***********************************************************************/
  20. /***********************************************************************
  21. ** Includes
  22. ***********************************************************************/
  23. /* Used to get the command line option */
  24. #include "plgetopt.h"
  25. #include "nspr.h"
  26. #include "prpriv.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. PRIntn failed_already=0;
  31. PRIntn debug_mode;
  32. /*
  33. Since we don't have stdin, stdout everywhere, we will fake
  34. it with our in-memory buffers called stdin and stdout.
  35. */
  36. #define SBSIZE 1024
  37. #include "obsolete/prsem.h"
  38. static char stdinBuf[SBSIZE];
  39. static char stdoutBuf[SBSIZE];
  40. static PRUintn stdinBufIdx = 0;
  41. static PRUintn stdoutBufIdx = 0;
  42. static PRStatus finalResult = PR_SUCCESS;
  43. static size_t dread (PRUintn device, char *buf, size_t bufSize)
  44. {
  45. PRUintn i;
  46. /* during first read call, initialize the stdinBuf buffer*/
  47. if (stdinBufIdx == 0) {
  48. for (i=0; i<SBSIZE; i++) {
  49. stdinBuf[i] = i;
  50. }
  51. }
  52. /* now copy data from stdinBuf to the given buffer upto bufSize */
  53. for (i=0; i<bufSize; i++) {
  54. if (stdinBufIdx == SBSIZE) {
  55. break;
  56. }
  57. buf[i] = stdinBuf[stdinBufIdx++];
  58. }
  59. return i;
  60. }
  61. static size_t dwrite (PRUintn device, char *buf, size_t bufSize)
  62. {
  63. PRUintn i, j;
  64. /* copy data from the given buffer upto bufSize to stdoutBuf */
  65. for (i=0; i<bufSize; i++) {
  66. if (stdoutBufIdx == SBSIZE) {
  67. break;
  68. }
  69. stdoutBuf[stdoutBufIdx++] = buf[i];
  70. }
  71. /* during last write call, compare the two buffers */
  72. if (stdoutBufIdx == SBSIZE)
  73. for (j=0; j<SBSIZE; j++)
  74. if (stdinBuf[j] != stdoutBuf[j]) {
  75. if (debug_mode) {
  76. printf("data mismatch for index= %d \n", j);
  77. }
  78. finalResult = PR_FAILURE;
  79. }
  80. return i;
  81. }
  82. /*------------------ Following is the real test program ---------*/
  83. /*
  84. Program to copy standard input to standard output. The program
  85. uses two threads. One reads the input and puts the data in a
  86. double buffer. The other reads the buffer contents and writes
  87. it to standard output.
  88. */
  89. PRSemaphore *emptyBufs; /* number of empty buffers */
  90. PRSemaphore *fullBufs; /* number of buffers that are full */
  91. #define BSIZE 100
  92. struct {
  93. char data[BSIZE];
  94. PRUintn nbytes; /* number of bytes in this buffer */
  95. } buf[2];
  96. static void PR_CALLBACK reader(void *arg)
  97. {
  98. PRUintn i = 0;
  99. size_t nbytes;
  100. do {
  101. (void) PR_WaitSem(emptyBufs);
  102. nbytes = dread(0, buf[i].data, BSIZE);
  103. buf[i].nbytes = nbytes;
  104. PR_PostSem(fullBufs);
  105. i = (i + 1) % 2;
  106. } while (nbytes > 0);
  107. }
  108. static void writer(void)
  109. {
  110. PRUintn i = 0;
  111. size_t nbytes;
  112. do {
  113. (void) PR_WaitSem(fullBufs);
  114. nbytes = buf[i].nbytes;
  115. if (nbytes > 0) {
  116. nbytes = dwrite(1, buf[i].data, nbytes);
  117. PR_PostSem(emptyBufs);
  118. i = (i + 1) % 2;
  119. }
  120. } while (nbytes > 0);
  121. }
  122. int main(int argc, char **argv)
  123. {
  124. PRThread *r;
  125. PR_STDIO_INIT();
  126. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  127. {
  128. /* The command line argument: -d is used to determine if the test is being run
  129. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  130. All of the printfs associated with this test has been handled with a if (debug_mode)
  131. test.
  132. Usage: test_name -d
  133. */
  134. PLOptStatus os;
  135. PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  136. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  137. {
  138. if (PL_OPT_BAD == os) {
  139. continue;
  140. }
  141. switch (opt->option)
  142. {
  143. case 'd': /* debug mode */
  144. debug_mode = 1;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. PL_DestroyOptState(opt);
  151. }
  152. /* main test */
  153. emptyBufs = PR_NewSem(2); /* two empty buffers */
  154. fullBufs = PR_NewSem(0); /* zero full buffers */
  155. /* create the reader thread */
  156. r = PR_CreateThread(PR_USER_THREAD,
  157. reader, 0,
  158. PR_PRIORITY_NORMAL,
  159. PR_LOCAL_THREAD,
  160. PR_UNJOINABLE_THREAD,
  161. 0);
  162. /* Do the writer operation in this thread */
  163. writer();
  164. PR_DestroySem(emptyBufs);
  165. PR_DestroySem(fullBufs);
  166. if (finalResult == PR_SUCCESS) {
  167. if (debug_mode) {
  168. printf("sem Test Passed.\n");
  169. }
  170. }
  171. else {
  172. if (debug_mode) {
  173. printf("sem Test Failed.\n");
  174. }
  175. failed_already=1;
  176. }
  177. PR_Cleanup();
  178. if(failed_already) {
  179. return 1;
  180. }
  181. else {
  182. return 0;
  183. }
  184. }