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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
43 #define SHM_NAME "/tmp/counter"
44 #define SEM_NAME1 "/tmp/foo.sem"
45 #define SEM_NAME2 "/tmp/bar.sem"
48 #define ITERATIONS 1000
50 static PRBool debug_mode
= PR_FALSE
;
51 static PRIntn iterations
= ITERATIONS
;
52 static PRSem
*sem1
, *sem2
;
54 static void Help(void)
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");
62 int main(int argc
, char **argv
)
68 char iterations_buf
[32];
73 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dc:h");
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 */
81 case 'c': /* loop count */
82 iterations
= atoi(opt
->value
);
90 PL_DestroyOptState(opt
);
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
);
96 if (PR_DeleteSemaphore(SEM_NAME1
) == PR_SUCCESS
) {
97 fprintf(stderr
, "warning: removed semaphore %s left over "
98 "from previous run\n", SEM_NAME1
);
100 if (PR_DeleteSemaphore(SEM_NAME2
) == PR_SUCCESS
) {
101 fprintf(stderr
, "warning: removed semaphore %s left over "
102 "from previous run\n", SEM_NAME2
);
105 shm
= PR_OpenSharedMemory(SHM_NAME
, sizeof(*counter_addr
), PR_SHM_CREATE
, SHM_MODE
);
107 fprintf(stderr
, "PR_OpenSharedMemory failed (%d, %d)\n",
108 PR_GetError(), PR_GetOSError());
111 counter_addr
= PR_AttachSharedMemory(shm
, 0);
112 if (NULL
== counter_addr
) {
113 fprintf(stderr
, "PR_AttachSharedMemory failed\n");
117 sem1
= PR_OpenSemaphore(SEM_NAME1
, PR_SEM_CREATE
, SEM_MODE
, 1);
119 fprintf(stderr
, "PR_OpenSemaphore failed (%d, %d)\n",
120 PR_GetError(), PR_GetOSError());
123 sem2
= PR_OpenSemaphore(SEM_NAME2
, PR_SEM_CREATE
, SEM_MODE
, 0);
125 fprintf(stderr
, "PR_OpenSemaphore failed (%d, %d)\n",
126 PR_GetError(), PR_GetOSError());
130 child_arg
= &child_argv
[0];
131 *child_arg
++ = "semapong";
132 if (debug_mode
!= PR_FALSE
) {
135 if (iterations
!= ITERATIONS
) {
137 PR_snprintf(iterations_buf
, sizeof(iterations_buf
), "%d", iterations
);
138 *child_arg
++ = iterations_buf
;
141 proc
= PR_CreateProcess(child_argv
[0], child_argv
, NULL
, NULL
);
143 fprintf(stderr
, "PR_CreateProcess failed\n");
148 * Process 1 waits on semaphore 1 and posts to semaphore 2.
150 for (i
= 0; i
< iterations
; i
++) {
151 if (PR_WaitSemaphore(sem1
) == PR_FAILURE
) {
152 fprintf(stderr
, "PR_WaitSemaphore failed\n");
155 if (*counter_addr
== 2*i
) {
156 if (debug_mode
) printf("process 1: counter = %d\n", *counter_addr
);
158 fprintf(stderr
, "process 1: counter should be %d but is %d\n",
163 if (PR_PostSemaphore(sem2
) == PR_FAILURE
) {
164 fprintf(stderr
, "PR_PostSemaphore failed\n");
168 if (PR_DetachSharedMemory(shm
, counter_addr
) == PR_FAILURE
) {
169 fprintf(stderr
, "PR_DetachSharedMemory failed\n");
172 if (PR_CloseSharedMemory(shm
) == PR_FAILURE
) {
173 fprintf(stderr
, "PR_CloseSharedMemory failed\n");
176 if (PR_CloseSemaphore(sem1
) == PR_FAILURE
) {
177 fprintf(stderr
, "PR_CloseSemaphore failed\n");
179 if (PR_CloseSemaphore(sem2
) == PR_FAILURE
) {
180 fprintf(stderr
, "PR_CloseSemaphore failed\n");
183 if (PR_WaitProcess(proc
, &exit_code
) == PR_FAILURE
) {
184 fprintf(stderr
, "PR_WaitProcess failed\n");
187 if (exit_code
!= 0) {
188 fprintf(stderr
, "process 2 failed with exit code %d\n", exit_code
);
192 if (PR_DeleteSharedMemory(SHM_NAME
) == PR_FAILURE
) {
193 fprintf(stderr
, "PR_DeleteSharedMemory failed\n");
195 if (PR_DeleteSemaphore(SEM_NAME1
) == PR_FAILURE
) {
196 fprintf(stderr
, "PR_DeleteSemaphore failed\n");
198 if (PR_DeleteSemaphore(SEM_NAME2
) == PR_FAILURE
) {
199 fprintf(stderr
, "PR_DeleteSemaphore failed\n");