semaping.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "nspr.h"
  6. #include "plgetopt.h"
  7. #include <stdio.h>
  8. #ifdef DEBUG
  9. #define SEM_D "D"
  10. #else
  11. #define SEM_D
  12. #endif
  13. #ifdef IS_64
  14. #define SEM_64 "64"
  15. #else
  16. #define SEM_64
  17. #endif
  18. #define SHM_NAME "/tmp/counter" SEM_D SEM_64
  19. #define SEM_NAME1 "/tmp/foo.sem" SEM_D SEM_64
  20. #define SEM_NAME2 "/tmp/bar.sem" SEM_D SEM_64
  21. #define EXE_NAME "semapong"
  22. #define SEM_MODE 0666
  23. #define SHM_MODE 0666
  24. #define ITERATIONS 1000
  25. static PRBool debug_mode = PR_FALSE;
  26. static PRIntn iterations = ITERATIONS;
  27. static PRSem *sem1, *sem2;
  28. static void Help(void)
  29. {
  30. fprintf(stderr, "semaping test program usage:\n");
  31. fprintf(stderr, "\t-d debug mode (FALSE)\n");
  32. fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS);
  33. fprintf(stderr, "\t-h this message\n");
  34. } /* Help */
  35. int main(int argc, char **argv)
  36. {
  37. PRProcess *proc;
  38. PRIntn i;
  39. char *child_argv[32];
  40. char **child_arg;
  41. char iterations_buf[32];
  42. PRSharedMemory *shm;
  43. PRIntn *counter_addr;
  44. PRInt32 exit_code;
  45. PLOptStatus os;
  46. PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
  47. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
  48. if (PL_OPT_BAD == os) {
  49. continue;
  50. }
  51. switch (opt->option) {
  52. case 'd': /* debug mode */
  53. debug_mode = PR_TRUE;
  54. break;
  55. case 'c': /* loop count */
  56. iterations = atoi(opt->value);
  57. break;
  58. case 'h':
  59. default:
  60. Help();
  61. return 2;
  62. }
  63. }
  64. PL_DestroyOptState(opt);
  65. if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) {
  66. fprintf(stderr, "warning: removed shared memory %s left over "
  67. "from previous run\n", SHM_NAME);
  68. }
  69. if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
  70. fprintf(stderr, "warning: removed semaphore %s left over "
  71. "from previous run\n", SEM_NAME1);
  72. }
  73. if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
  74. fprintf(stderr, "warning: removed semaphore %s left over "
  75. "from previous run\n", SEM_NAME2);
  76. }
  77. shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE);
  78. if (NULL == shm) {
  79. fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
  80. PR_GetError(), PR_GetOSError());
  81. exit(1);
  82. }
  83. counter_addr = PR_AttachSharedMemory(shm, 0);
  84. if (NULL == counter_addr) {
  85. fprintf(stderr, "PR_AttachSharedMemory failed\n");
  86. exit(1);
  87. }
  88. *counter_addr = 0;
  89. sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
  90. if (NULL == sem1) {
  91. fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
  92. PR_GetError(), PR_GetOSError());
  93. exit(1);
  94. }
  95. sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
  96. if (NULL == sem2) {
  97. fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
  98. PR_GetError(), PR_GetOSError());
  99. exit(1);
  100. }
  101. child_arg = &child_argv[0];
  102. *child_arg++ = EXE_NAME;
  103. if (debug_mode != PR_FALSE) {
  104. *child_arg++ = "-d";
  105. }
  106. if (iterations != ITERATIONS) {
  107. *child_arg++ = "-c";
  108. PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations);
  109. *child_arg++ = iterations_buf;
  110. }
  111. *child_arg = NULL;
  112. proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
  113. if (NULL == proc) {
  114. fprintf(stderr, "PR_CreateProcess failed\n");
  115. exit(1);
  116. }
  117. /*
  118. * Process 1 waits on semaphore 1 and posts to semaphore 2.
  119. */
  120. for (i = 0; i < iterations; i++) {
  121. if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
  122. fprintf(stderr, "PR_WaitSemaphore failed\n");
  123. exit(1);
  124. }
  125. if (*counter_addr == 2*i) {
  126. if (debug_mode) {
  127. printf("process 1: counter = %d\n", *counter_addr);
  128. }
  129. } else {
  130. fprintf(stderr, "process 1: counter should be %d but is %d\n",
  131. 2*i, *counter_addr);
  132. exit(1);
  133. }
  134. (*counter_addr)++;
  135. if (PR_PostSemaphore(sem2) == PR_FAILURE) {
  136. fprintf(stderr, "PR_PostSemaphore failed\n");
  137. exit(1);
  138. }
  139. }
  140. if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
  141. fprintf(stderr, "PR_DetachSharedMemory failed\n");
  142. exit(1);
  143. }
  144. if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
  145. fprintf(stderr, "PR_CloseSharedMemory failed\n");
  146. exit(1);
  147. }
  148. if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
  149. fprintf(stderr, "PR_CloseSemaphore failed\n");
  150. }
  151. if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
  152. fprintf(stderr, "PR_CloseSemaphore failed\n");
  153. }
  154. if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
  155. fprintf(stderr, "PR_WaitProcess failed\n");
  156. exit(1);
  157. }
  158. if (exit_code != 0) {
  159. fprintf(stderr, "process 2 failed with exit code %d\n", exit_code);
  160. exit(1);
  161. }
  162. if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) {
  163. fprintf(stderr, "PR_DeleteSharedMemory failed\n");
  164. }
  165. if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
  166. fprintf(stderr, "PR_DeleteSemaphore failed\n");
  167. }
  168. if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
  169. fprintf(stderr, "PR_DeleteSemaphore failed\n");
  170. }
  171. printf("PASS\n");
  172. return 0;
  173. }