1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1999-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
40
#define SHM_NAME "/tmp/counter"
41
#define SEM_NAME1 "/tmp/foo.sem"
42
#define SEM_NAME2 "/tmp/bar.sem"
43
#define ITERATIONS 1000
45
static PRBool debug_mode = PR_FALSE;
46
static PRIntn iterations = ITERATIONS;
47
static PRSem *sem1, *sem2;
49
static void Help(void)
51
fprintf(stderr, "semapong test program usage:\n");
52
fprintf(stderr, "\t-d debug mode (FALSE)\n");
53
fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS);
54
fprintf(stderr, "\t-h this message\n");
57
int main(int argc, char **argv)
63
PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
65
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
66
if (PL_OPT_BAD == os) continue;
67
switch (opt->option) {
68
case 'd': /* debug mode */
71
case 'c': /* loop count */
72
iterations = atoi(opt->value);
80
PL_DestroyOptState(opt);
82
shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), 0, 0666);
84
fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
85
PR_GetError(), PR_GetOSError());
88
sem1 = PR_OpenSemaphore(SEM_NAME1, 0, 0, 0);
90
fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
91
PR_GetError(), PR_GetOSError());
94
sem2 = PR_OpenSemaphore(SEM_NAME2, 0, 0, 0);
96
fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
97
PR_GetError(), PR_GetOSError());
101
counter_addr = PR_AttachSharedMemory(shm, 0);
102
if (NULL == counter_addr) {
103
fprintf(stderr, "PR_AttachSharedMemory failed\n");
108
* Process 2 waits on semaphore 2 and posts to semaphore 1.
110
for (i = 0; i < iterations; i++) {
111
if (PR_WaitSemaphore(sem2) == PR_FAILURE) {
112
fprintf(stderr, "PR_WaitSemaphore failed\n");
115
if (*counter_addr == 2*i+1) {
116
if (debug_mode) printf("process 2: counter = %d\n", *counter_addr);
118
fprintf(stderr, "process 2: counter should be %d but is %d\n",
119
2*i+1, *counter_addr);
123
if (PR_PostSemaphore(sem1) == PR_FAILURE) {
124
fprintf(stderr, "PR_PostSemaphore failed\n");
128
if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
129
fprintf(stderr, "PR_DetachSharedMemory failed\n");
132
if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
133
fprintf(stderr, "PR_CloseSharedMemory failed\n");
136
if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
137
fprintf(stderr, "PR_CloseSemaphore failed\n");
139
if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
140
fprintf(stderr, "PR_CloseSemaphore failed\n");