fileio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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: fileio.c
  8. **
  9. ** Description: Program to copy one file to another.
  10. **
  11. ** Modification History:
  12. ** 14-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. ** 12-June-97 Revert to return code 0 and 1, remove debug option (obsolete).
  20. ***********************************************************************/
  21. /***********************************************************************
  22. ** Includes
  23. ***********************************************************************/
  24. #include "prinit.h"
  25. #include "prthread.h"
  26. #include "prlock.h"
  27. #include "prcvar.h"
  28. #include "prmon.h"
  29. #include "prmem.h"
  30. #include "prio.h"
  31. #include "prlog.h"
  32. #include <stdio.h>
  33. #include "obsolete/prsem.h"
  34. #define TBSIZE 1024
  35. static PRUint8 tbuf[TBSIZE];
  36. static PRFileDesc *t1, *t2;
  37. PRIntn failed_already=0;
  38. PRIntn debug_mode;
  39. static void InitialSetup(void)
  40. {
  41. PRUintn i;
  42. PRInt32 nWritten, rv;
  43. t1 = PR_Open("t1.tmp", PR_CREATE_FILE | PR_RDWR, 0);
  44. PR_ASSERT(t1 != NULL);
  45. for (i=0; i<TBSIZE; i++) {
  46. tbuf[i] = i;
  47. }
  48. nWritten = PR_Write((PRFileDesc*)t1, tbuf, TBSIZE);
  49. PR_ASSERT(nWritten == TBSIZE);
  50. rv = PR_Seek(t1,0,PR_SEEK_SET);
  51. PR_ASSERT(rv == 0);
  52. t2 = PR_Open("t2.tmp", PR_CREATE_FILE | PR_RDWR, 0);
  53. PR_ASSERT(t2 != NULL);
  54. }
  55. static void VerifyAndCleanup(void)
  56. {
  57. PRUintn i;
  58. PRInt32 nRead, rv;
  59. for (i=0; i<TBSIZE; i++) {
  60. tbuf[i] = 0;
  61. }
  62. rv = PR_Seek(t2,0,PR_SEEK_SET);
  63. PR_ASSERT(rv == 0);
  64. nRead = PR_Read((PRFileDesc*)t2, tbuf, TBSIZE);
  65. PR_ASSERT(nRead == TBSIZE);
  66. for (i=0; i<TBSIZE; i++)
  67. if (tbuf[i] != (PRUint8)i) {
  68. if (debug_mode) {
  69. printf("data mismatch for index= %d \n", i);
  70. }
  71. else {
  72. failed_already=1;
  73. }
  74. }
  75. PR_Close(t1);
  76. PR_Close(t2);
  77. PR_Delete("t1.tmp");
  78. PR_Delete("t2.tmp");
  79. if (debug_mode) {
  80. printf("fileio test passed\n");
  81. }
  82. }
  83. /*------------------ Following is the real test program ---------*/
  84. /*
  85. Program to copy one file to another. Two temporary files get
  86. created. First one gets written in one write call. Then,
  87. a reader thread reads from this file into a double buffer.
  88. The writer thread writes from double buffer into the other
  89. temporary file. The second temporary file gets verified
  90. for accurate data.
  91. */
  92. PRSemaphore *emptyBufs; /* number of empty buffers */
  93. PRSemaphore *fullBufs; /* number of buffers that are full */
  94. #define BSIZE 100
  95. struct {
  96. char data[BSIZE];
  97. PRUintn nbytes; /* number of bytes in this buffer */
  98. } buf[2];
  99. static void PR_CALLBACK reader(void *arg)
  100. {
  101. PRUintn i = 0;
  102. PRInt32 nbytes;
  103. do {
  104. (void) PR_WaitSem(emptyBufs);
  105. nbytes = PR_Read((PRFileDesc*)arg, buf[i].data, BSIZE);
  106. if (nbytes >= 0) {
  107. buf[i].nbytes = nbytes;
  108. PR_PostSem(fullBufs);
  109. i = (i + 1) % 2;
  110. }
  111. } while (nbytes > 0);
  112. }
  113. static void PR_CALLBACK writer(void *arg)
  114. {
  115. PRUintn i = 0;
  116. PRInt32 nbytes;
  117. do {
  118. (void) PR_WaitSem(fullBufs);
  119. nbytes = buf[i].nbytes;
  120. if (nbytes > 0) {
  121. nbytes = PR_Write((PRFileDesc*)arg, buf[i].data, nbytes);
  122. PR_PostSem(emptyBufs);
  123. i = (i + 1) % 2;
  124. }
  125. } while (nbytes > 0);
  126. }
  127. int main(int argc, char **argv)
  128. {
  129. PRThread *r, *w;
  130. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  131. PR_STDIO_INIT();
  132. emptyBufs = PR_NewSem(2); /* two empty buffers */
  133. fullBufs = PR_NewSem(0); /* zero full buffers */
  134. /* Create initial temp file setup */
  135. InitialSetup();
  136. /* create the reader thread */
  137. r = PR_CreateThread(PR_USER_THREAD,
  138. reader, t1,
  139. PR_PRIORITY_NORMAL,
  140. PR_LOCAL_THREAD,
  141. PR_JOINABLE_THREAD,
  142. 0);
  143. w = PR_CreateThread(PR_USER_THREAD,
  144. writer, t2,
  145. PR_PRIORITY_NORMAL,
  146. PR_LOCAL_THREAD,
  147. PR_JOINABLE_THREAD,
  148. 0);
  149. /* Do the joining for both threads */
  150. (void) PR_JoinThread(r);
  151. (void) PR_JoinThread(w);
  152. /* Do the verification and clean up */
  153. VerifyAndCleanup();
  154. PR_DestroySem(emptyBufs);
  155. PR_DestroySem(fullBufs);
  156. PR_Cleanup();
  157. if(failed_already)
  158. {
  159. printf("Fail\n");
  160. return 1;
  161. }
  162. else
  163. {
  164. printf("PASS\n");
  165. return 0;
  166. }
  167. }