|
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 #include "nspr.h" |
|
7 #include "plgetopt.h" |
|
8 |
|
9 #include <stdio.h> |
|
10 |
|
11 #ifdef SYMBIAN |
|
12 #define SHM_NAME "c:\\data\\counter" |
|
13 #define SEM_NAME1 "c:\\data\\foo.sem" |
|
14 #define SEM_NAME2 "c:\\data\\bar.sem" |
|
15 #else |
|
16 #define SHM_NAME "/tmp/counter" |
|
17 #define SEM_NAME1 "/tmp/foo.sem" |
|
18 #define SEM_NAME2 "/tmp/bar.sem" |
|
19 #endif |
|
20 #define ITERATIONS 1000 |
|
21 |
|
22 static PRBool debug_mode = PR_FALSE; |
|
23 static PRIntn iterations = ITERATIONS; |
|
24 static PRSem *sem1, *sem2; |
|
25 |
|
26 static void Help(void) |
|
27 { |
|
28 fprintf(stderr, "semapong test program usage:\n"); |
|
29 fprintf(stderr, "\t-d debug mode (FALSE)\n"); |
|
30 fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS); |
|
31 fprintf(stderr, "\t-h this message\n"); |
|
32 } /* Help */ |
|
33 |
|
34 int main(int argc, char **argv) |
|
35 { |
|
36 PRIntn i; |
|
37 PRSharedMemory *shm; |
|
38 PRIntn *counter_addr; |
|
39 PLOptStatus os; |
|
40 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h"); |
|
41 |
|
42 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
|
43 if (PL_OPT_BAD == os) continue; |
|
44 switch (opt->option) { |
|
45 case 'd': /* debug mode */ |
|
46 debug_mode = PR_TRUE; |
|
47 break; |
|
48 case 'c': /* loop count */ |
|
49 iterations = atoi(opt->value); |
|
50 break; |
|
51 case 'h': |
|
52 default: |
|
53 Help(); |
|
54 return 2; |
|
55 } |
|
56 } |
|
57 PL_DestroyOptState(opt); |
|
58 |
|
59 shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), 0, 0666); |
|
60 if (NULL == shm) { |
|
61 fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n", |
|
62 PR_GetError(), PR_GetOSError()); |
|
63 exit(1); |
|
64 } |
|
65 sem1 = PR_OpenSemaphore(SEM_NAME1, 0, 0, 0); |
|
66 if (NULL == sem1) { |
|
67 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
|
68 PR_GetError(), PR_GetOSError()); |
|
69 exit(1); |
|
70 } |
|
71 sem2 = PR_OpenSemaphore(SEM_NAME2, 0, 0, 0); |
|
72 if (NULL == sem2) { |
|
73 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", |
|
74 PR_GetError(), PR_GetOSError()); |
|
75 exit(1); |
|
76 } |
|
77 |
|
78 counter_addr = PR_AttachSharedMemory(shm, 0); |
|
79 if (NULL == counter_addr) { |
|
80 fprintf(stderr, "PR_AttachSharedMemory failed\n"); |
|
81 exit(1); |
|
82 } |
|
83 |
|
84 /* |
|
85 * Process 2 waits on semaphore 2 and posts to semaphore 1. |
|
86 */ |
|
87 for (i = 0; i < iterations; i++) { |
|
88 if (PR_WaitSemaphore(sem2) == PR_FAILURE) { |
|
89 fprintf(stderr, "PR_WaitSemaphore failed\n"); |
|
90 exit(1); |
|
91 } |
|
92 if (*counter_addr == 2*i+1) { |
|
93 if (debug_mode) printf("process 2: counter = %d\n", *counter_addr); |
|
94 } else { |
|
95 fprintf(stderr, "process 2: counter should be %d but is %d\n", |
|
96 2*i+1, *counter_addr); |
|
97 exit(1); |
|
98 } |
|
99 (*counter_addr)++; |
|
100 if (PR_PostSemaphore(sem1) == PR_FAILURE) { |
|
101 fprintf(stderr, "PR_PostSemaphore failed\n"); |
|
102 exit(1); |
|
103 } |
|
104 } |
|
105 if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) { |
|
106 fprintf(stderr, "PR_DetachSharedMemory failed\n"); |
|
107 exit(1); |
|
108 } |
|
109 if (PR_CloseSharedMemory(shm) == PR_FAILURE) { |
|
110 fprintf(stderr, "PR_CloseSharedMemory failed\n"); |
|
111 exit(1); |
|
112 } |
|
113 if (PR_CloseSemaphore(sem1) == PR_FAILURE) { |
|
114 fprintf(stderr, "PR_CloseSemaphore failed\n"); |
|
115 } |
|
116 if (PR_CloseSemaphore(sem2) == PR_FAILURE) { |
|
117 fprintf(stderr, "PR_CloseSemaphore failed\n"); |
|
118 } |
|
119 printf("PASS\n"); |
|
120 return 0; |
|
121 } |