1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1999-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nspr.h"
|
---|
39 | #include "plgetopt.h"
|
---|
40 |
|
---|
41 | #include <stdio.h>
|
---|
42 |
|
---|
43 | #define SHM_NAME "/tmp/counter"
|
---|
44 | #define SEM_NAME1 "/tmp/foo.sem"
|
---|
45 | #define SEM_NAME2 "/tmp/bar.sem"
|
---|
46 | #define SEM_MODE 0666
|
---|
47 | #define SHM_MODE 0666
|
---|
48 | #define ITERATIONS 1000
|
---|
49 |
|
---|
50 | static PRBool debug_mode = PR_FALSE;
|
---|
51 | static PRIntn iterations = ITERATIONS;
|
---|
52 | static PRSem *sem1, *sem2;
|
---|
53 |
|
---|
54 | static void Help(void)
|
---|
55 | {
|
---|
56 | fprintf(stderr, "semaping test program usage:\n");
|
---|
57 | fprintf(stderr, "\t-d debug mode (FALSE)\n");
|
---|
58 | fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS);
|
---|
59 | fprintf(stderr, "\t-h this message\n");
|
---|
60 | } /* Help */
|
---|
61 |
|
---|
62 | int main(int argc, char **argv)
|
---|
63 | {
|
---|
64 | PRProcess *proc;
|
---|
65 | PRIntn i;
|
---|
66 | char *child_argv[32];
|
---|
67 | char **child_arg;
|
---|
68 | char iterations_buf[32];
|
---|
69 | PRSharedMemory *shm;
|
---|
70 | PRIntn *counter_addr;
|
---|
71 | PRInt32 exit_code;
|
---|
72 | PLOptStatus os;
|
---|
73 | PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
|
---|
74 |
|
---|
75 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
|
---|
76 | if (PL_OPT_BAD == os) continue;
|
---|
77 | switch (opt->option) {
|
---|
78 | case 'd': /* debug mode */
|
---|
79 | debug_mode = PR_TRUE;
|
---|
80 | break;
|
---|
81 | case 'c': /* loop count */
|
---|
82 | iterations = atoi(opt->value);
|
---|
83 | break;
|
---|
84 | case 'h':
|
---|
85 | default:
|
---|
86 | Help();
|
---|
87 | return 2;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | PL_DestroyOptState(opt);
|
---|
91 |
|
---|
92 | if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) {
|
---|
93 | fprintf(stderr, "warning: removed shared memory %s left over "
|
---|
94 | "from previous run\n", SHM_NAME);
|
---|
95 | }
|
---|
96 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
|
---|
97 | fprintf(stderr, "warning: removed semaphore %s left over "
|
---|
98 | "from previous run\n", SEM_NAME1);
|
---|
99 | }
|
---|
100 | if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
|
---|
101 | fprintf(stderr, "warning: removed semaphore %s left over "
|
---|
102 | "from previous run\n", SEM_NAME2);
|
---|
103 | }
|
---|
104 |
|
---|
105 | shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE);
|
---|
106 | if (NULL == shm) {
|
---|
107 | fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
|
---|
108 | PR_GetError(), PR_GetOSError());
|
---|
109 | exit(1);
|
---|
110 | }
|
---|
111 | counter_addr = PR_AttachSharedMemory(shm, 0);
|
---|
112 | if (NULL == counter_addr) {
|
---|
113 | fprintf(stderr, "PR_AttachSharedMemory failed\n");
|
---|
114 | exit(1);
|
---|
115 | }
|
---|
116 | *counter_addr = 0;
|
---|
117 | sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
|
---|
118 | if (NULL == sem1) {
|
---|
119 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
|
---|
120 | PR_GetError(), PR_GetOSError());
|
---|
121 | exit(1);
|
---|
122 | }
|
---|
123 | sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
|
---|
124 | if (NULL == sem2) {
|
---|
125 | fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
|
---|
126 | PR_GetError(), PR_GetOSError());
|
---|
127 | exit(1);
|
---|
128 | }
|
---|
129 |
|
---|
130 | child_arg = &child_argv[0];
|
---|
131 | *child_arg++ = "semapong";
|
---|
132 | if (debug_mode != PR_FALSE) {
|
---|
133 | *child_arg++ = "-d";
|
---|
134 | }
|
---|
135 | if (iterations != ITERATIONS) {
|
---|
136 | *child_arg++ = "-c";
|
---|
137 | PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations);
|
---|
138 | *child_arg++ = iterations_buf;
|
---|
139 | }
|
---|
140 | *child_arg = NULL;
|
---|
141 | proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
|
---|
142 | if (NULL == proc) {
|
---|
143 | fprintf(stderr, "PR_CreateProcess failed\n");
|
---|
144 | exit(1);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Process 1 waits on semaphore 1 and posts to semaphore 2.
|
---|
149 | */
|
---|
150 | for (i = 0; i < iterations; i++) {
|
---|
151 | if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
|
---|
152 | fprintf(stderr, "PR_WaitSemaphore failed\n");
|
---|
153 | exit(1);
|
---|
154 | }
|
---|
155 | if (*counter_addr == 2*i) {
|
---|
156 | if (debug_mode) printf("process 1: counter = %d\n", *counter_addr);
|
---|
157 | } else {
|
---|
158 | fprintf(stderr, "process 1: counter should be %d but is %d\n",
|
---|
159 | 2*i, *counter_addr);
|
---|
160 | exit(1);
|
---|
161 | }
|
---|
162 | (*counter_addr)++;
|
---|
163 | if (PR_PostSemaphore(sem2) == PR_FAILURE) {
|
---|
164 | fprintf(stderr, "PR_PostSemaphore failed\n");
|
---|
165 | exit(1);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
|
---|
169 | fprintf(stderr, "PR_DetachSharedMemory failed\n");
|
---|
170 | exit(1);
|
---|
171 | }
|
---|
172 | if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
|
---|
173 | fprintf(stderr, "PR_CloseSharedMemory failed\n");
|
---|
174 | exit(1);
|
---|
175 | }
|
---|
176 | if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
|
---|
177 | fprintf(stderr, "PR_CloseSemaphore failed\n");
|
---|
178 | }
|
---|
179 | if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
|
---|
180 | fprintf(stderr, "PR_CloseSemaphore failed\n");
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
|
---|
184 | fprintf(stderr, "PR_WaitProcess failed\n");
|
---|
185 | exit(1);
|
---|
186 | }
|
---|
187 | if (exit_code != 0) {
|
---|
188 | fprintf(stderr, "process 2 failed with exit code %d\n", exit_code);
|
---|
189 | exit(1);
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) {
|
---|
193 | fprintf(stderr, "PR_DeleteSharedMemory failed\n");
|
---|
194 | }
|
---|
195 | if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
|
---|
196 | fprintf(stderr, "PR_DeleteSemaphore failed\n");
|
---|
197 | }
|
---|
198 | if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
|
---|
199 | fprintf(stderr, "PR_DeleteSemaphore failed\n");
|
---|
200 | }
|
---|
201 | printf("PASS\n");
|
---|
202 | return 0;
|
---|
203 | }
|
---|