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"
46 #define ITERATIONS 1000
48 static PRBool debug_mode
= PR_FALSE
;
49 static PRIntn iterations
= ITERATIONS
;
50 static PRSem
*sem1
, *sem2
;
52 static void Help(void)
54 fprintf(stderr
, "semapong test program usage:\n");
55 fprintf(stderr
, "\t-d debug mode (FALSE)\n");
56 fprintf(stderr
, "\t-c <count> loop count (%d)\n", ITERATIONS
);
57 fprintf(stderr
, "\t-h this message\n");
60 int main(int argc
, char **argv
)
66 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dc:h");
68 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
))) {
69 if (PL_OPT_BAD
== os
) continue;
70 switch (opt
->option
) {
71 case 'd': /* debug mode */
74 case 'c': /* loop count */
75 iterations
= atoi(opt
->value
);
83 PL_DestroyOptState(opt
);
85 shm
= PR_OpenSharedMemory(SHM_NAME
, sizeof(*counter_addr
), 0, 0666);
87 fprintf(stderr
, "PR_OpenSharedMemory failed (%d, %d)\n",
88 PR_GetError(), PR_GetOSError());
91 sem1
= PR_OpenSemaphore(SEM_NAME1
, 0, 0, 0);
93 fprintf(stderr
, "PR_OpenSemaphore failed (%d, %d)\n",
94 PR_GetError(), PR_GetOSError());
97 sem2
= PR_OpenSemaphore(SEM_NAME2
, 0, 0, 0);
99 fprintf(stderr
, "PR_OpenSemaphore failed (%d, %d)\n",
100 PR_GetError(), PR_GetOSError());
104 counter_addr
= PR_AttachSharedMemory(shm
, 0);
105 if (NULL
== counter_addr
) {
106 fprintf(stderr
, "PR_AttachSharedMemory failed\n");
111 * Process 2 waits on semaphore 2 and posts to semaphore 1.
113 for (i
= 0; i
< iterations
; i
++) {
114 if (PR_WaitSemaphore(sem2
) == PR_FAILURE
) {
115 fprintf(stderr
, "PR_WaitSemaphore failed\n");
118 if (*counter_addr
== 2*i
+1) {
119 if (debug_mode
) printf("process 2: counter = %d\n", *counter_addr
);
121 fprintf(stderr
, "process 2: counter should be %d but is %d\n",
122 2*i
+1, *counter_addr
);
126 if (PR_PostSemaphore(sem1
) == PR_FAILURE
) {
127 fprintf(stderr
, "PR_PostSemaphore failed\n");
131 if (PR_DetachSharedMemory(shm
, counter_addr
) == PR_FAILURE
) {
132 fprintf(stderr
, "PR_DetachSharedMemory failed\n");
135 if (PR_CloseSharedMemory(shm
) == PR_FAILURE
) {
136 fprintf(stderr
, "PR_CloseSharedMemory failed\n");
139 if (PR_CloseSemaphore(sem1
) == PR_FAILURE
) {
140 fprintf(stderr
, "PR_CloseSemaphore failed\n");
142 if (PR_CloseSemaphore(sem2
) == PR_FAILURE
) {
143 fprintf(stderr
, "PR_CloseSemaphore failed\n");