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/. */
12 #define SHM_NAME "c:\\data\\counter"
13 #define SEM_NAME1 "c:\\data\\foo.sem"
14 #define SEM_NAME2 "c:\\data\\bar.sem"
15 #define EXE_NAME "nspr_tests_semapong.exe"
17 #define SHM_NAME "/tmp/counter"
18 #define SEM_NAME1 "/tmp/foo.sem"
19 #define SEM_NAME2 "/tmp/bar.sem"
20 #define EXE_NAME "semapong"
24 #define ITERATIONS 1000
26 static PRBool debug_mode = PR_FALSE;
27 static PRIntn iterations = ITERATIONS;
28 static PRSem *sem1, *sem2;
30 static void Help(void)
32 fprintf(stderr, "semaping test program usage:\n");
33 fprintf(stderr, "\t-d debug mode (FALSE)\n");
34 fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS);
35 fprintf(stderr, "\t-h this message\n");
38 int main(int argc, char **argv)
44 char iterations_buf[32];
49 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
51 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
52 if (PL_OPT_BAD == os) continue;
53 switch (opt->option) {
54 case 'd': /* debug mode */
57 case 'c': /* loop count */
58 iterations = atoi(opt->value);
66 PL_DestroyOptState(opt);
68 if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) {
69 fprintf(stderr, "warning: removed shared memory %s left over "
70 "from previous run\n", SHM_NAME);
72 if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
73 fprintf(stderr, "warning: removed semaphore %s left over "
74 "from previous run\n", SEM_NAME1);
76 if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
77 fprintf(stderr, "warning: removed semaphore %s left over "
78 "from previous run\n", SEM_NAME2);
81 shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE);
83 fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
84 PR_GetError(), PR_GetOSError());
87 counter_addr = PR_AttachSharedMemory(shm, 0);
88 if (NULL == counter_addr) {
89 fprintf(stderr, "PR_AttachSharedMemory failed\n");
93 sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
95 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
96 PR_GetError(), PR_GetOSError());
99 sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
101 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
102 PR_GetError(), PR_GetOSError());
106 child_arg = &child_argv[0];
107 *child_arg++ = EXE_NAME;
108 if (debug_mode != PR_FALSE) {
111 if (iterations != ITERATIONS) {
113 PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations);
114 *child_arg++ = iterations_buf;
117 proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
119 fprintf(stderr, "PR_CreateProcess failed\n");
124 * Process 1 waits on semaphore 1 and posts to semaphore 2.
126 for (i = 0; i < iterations; i++) {
127 if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
128 fprintf(stderr, "PR_WaitSemaphore failed\n");
131 if (*counter_addr == 2*i) {
132 if (debug_mode) printf("process 1: counter = %d\n", *counter_addr);
134 fprintf(stderr, "process 1: counter should be %d but is %d\n",
139 if (PR_PostSemaphore(sem2) == PR_FAILURE) {
140 fprintf(stderr, "PR_PostSemaphore failed\n");
144 if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
145 fprintf(stderr, "PR_DetachSharedMemory failed\n");
148 if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
149 fprintf(stderr, "PR_CloseSharedMemory failed\n");
152 if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
153 fprintf(stderr, "PR_CloseSemaphore failed\n");
155 if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
156 fprintf(stderr, "PR_CloseSemaphore failed\n");
159 if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
160 fprintf(stderr, "PR_WaitProcess failed\n");
163 if (exit_code != 0) {
164 fprintf(stderr, "process 2 failed with exit code %d\n", exit_code);
168 if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) {
169 fprintf(stderr, "PR_DeleteSharedMemory failed\n");
171 if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
172 fprintf(stderr, "PR_DeleteSemaphore failed\n");
174 if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
175 fprintf(stderr, "PR_DeleteSemaphore failed\n");