~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/semaping.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
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/
 
7
 * 
 
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.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
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
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
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
 
32
 * GPL.
 
33
 */
 
34
 
 
35
#include "nspr.h"
 
36
#include "plgetopt.h"
 
37
 
 
38
#include <stdio.h>
 
39
 
 
40
#define SHM_NAME "/tmp/counter"
 
41
#define SEM_NAME1 "/tmp/foo.sem"
 
42
#define SEM_NAME2 "/tmp/bar.sem"
 
43
#define SEM_MODE  0666
 
44
#define SHM_MODE  0666
 
45
#define ITERATIONS 1000
 
46
 
 
47
static PRBool debug_mode = PR_FALSE;
 
48
static PRIntn iterations = ITERATIONS;
 
49
static PRSem *sem1, *sem2;
 
50
 
 
51
static void Help(void)
 
52
{
 
53
    fprintf(stderr, "semaping test program usage:\n");
 
54
    fprintf(stderr, "\t-d           debug mode         (FALSE)\n");
 
55
    fprintf(stderr, "\t-c <count>   loop count         (%d)\n", ITERATIONS);
 
56
    fprintf(stderr, "\t-h           this message\n");
 
57
}  /* Help */
 
58
 
 
59
int main(int argc, char **argv)
 
60
{
 
61
    PRProcess *proc;
 
62
    PRIntn i;
 
63
    char *child_argv[32];
 
64
    char **child_arg;
 
65
    char iterations_buf[32];
 
66
    PRSharedMemory *shm;
 
67
    PRIntn *counter_addr;
 
68
    PRInt32 exit_code;
 
69
    PLOptStatus os;
 
70
    PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
 
71
 
 
72
    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
 
73
        if (PL_OPT_BAD == os) continue;
 
74
        switch (opt->option) {
 
75
            case 'd':  /* debug mode */
 
76
                debug_mode = PR_TRUE;
 
77
                break;
 
78
            case 'c':  /* loop count */
 
79
                iterations = atoi(opt->value);
 
80
                break;
 
81
            case 'h':
 
82
            default:
 
83
                Help();
 
84
                return 2;
 
85
        }
 
86
    }
 
87
    PL_DestroyOptState(opt);
 
88
 
 
89
    if (PR_DeleteSharedMemory(SHM_NAME) == PR_SUCCESS) {
 
90
        fprintf(stderr, "warning: removed shared memory %s left over "
 
91
                "from previous run\n", SHM_NAME);
 
92
    }
 
93
    if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
 
94
        fprintf(stderr, "warning: removed semaphore %s left over "
 
95
                "from previous run\n", SEM_NAME1);
 
96
    }
 
97
    if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
 
98
        fprintf(stderr, "warning: removed semaphore %s left over "
 
99
                "from previous run\n", SEM_NAME2);
 
100
    }
 
101
 
 
102
    shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), PR_SHM_CREATE, SHM_MODE);
 
103
    if (NULL == shm) {
 
104
        fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
 
105
                PR_GetError(), PR_GetOSError());
 
106
        exit(1);
 
107
    }
 
108
    counter_addr = PR_AttachSharedMemory(shm, 0);
 
109
    if (NULL == counter_addr) {
 
110
        fprintf(stderr, "PR_AttachSharedMemory failed\n");
 
111
        exit(1);
 
112
    }
 
113
    *counter_addr = 0;
 
114
    sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
 
115
    if (NULL == sem1) {
 
116
        fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
 
117
                PR_GetError(), PR_GetOSError());
 
118
        exit(1);
 
119
    }
 
120
    sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
 
121
    if (NULL == sem2) {
 
122
        fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
 
123
                PR_GetError(), PR_GetOSError());
 
124
        exit(1);
 
125
    }
 
126
 
 
127
    child_arg = &child_argv[0];
 
128
    *child_arg++ = "semapong";
 
129
    if (debug_mode != PR_FALSE) {
 
130
        *child_arg++ = "-d";
 
131
    }
 
132
    if (iterations != ITERATIONS) {
 
133
        *child_arg++ = "-c";
 
134
        PR_snprintf(iterations_buf, sizeof(iterations_buf), "%d", iterations);
 
135
        *child_arg++ = iterations_buf;
 
136
    }
 
137
    *child_arg = NULL;
 
138
    proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
 
139
    if (NULL == proc) {
 
140
        fprintf(stderr, "PR_CreateProcess failed\n");
 
141
        exit(1);
 
142
    }
 
143
 
 
144
    /*
 
145
     * Process 1 waits on semaphore 1 and posts to semaphore 2.
 
146
     */
 
147
    for (i = 0; i < iterations; i++) {
 
148
        if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
 
149
            fprintf(stderr, "PR_WaitSemaphore failed\n");
 
150
            exit(1);
 
151
        }
 
152
        if (*counter_addr == 2*i) {
 
153
            if (debug_mode) printf("process 1: counter = %d\n", *counter_addr);
 
154
        } else {
 
155
            fprintf(stderr, "process 1: counter should be %d but is %d\n",
 
156
                    2*i, *counter_addr);
 
157
            exit(1);
 
158
        }
 
159
        (*counter_addr)++;
 
160
        if (PR_PostSemaphore(sem2) == PR_FAILURE) {
 
161
            fprintf(stderr, "PR_PostSemaphore failed\n");
 
162
            exit(1);
 
163
        }
 
164
    }
 
165
    if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
 
166
        fprintf(stderr, "PR_DetachSharedMemory failed\n");
 
167
        exit(1);
 
168
    }
 
169
    if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
 
170
        fprintf(stderr, "PR_CloseSharedMemory failed\n");
 
171
        exit(1);
 
172
    }
 
173
    if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
 
174
        fprintf(stderr, "PR_CloseSemaphore failed\n");
 
175
    }
 
176
    if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
 
177
        fprintf(stderr, "PR_CloseSemaphore failed\n");
 
178
    }
 
179
 
 
180
    if (PR_WaitProcess(proc, &exit_code) == PR_FAILURE) {
 
181
        fprintf(stderr, "PR_WaitProcess failed\n");
 
182
        exit(1);
 
183
    }
 
184
    if (exit_code != 0) {
 
185
        fprintf(stderr, "process 2 failed with exit code %d\n", exit_code);
 
186
        exit(1);
 
187
    }
 
188
 
 
189
    if (PR_DeleteSharedMemory(SHM_NAME) == PR_FAILURE) {
 
190
        fprintf(stderr, "PR_DeleteSharedMemory failed\n");
 
191
    }
 
192
    if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
 
193
        fprintf(stderr, "PR_DeleteSemaphore failed\n");
 
194
    }
 
195
    if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
 
196
        fprintf(stderr, "PR_DeleteSemaphore failed\n");
 
197
    }
 
198
    printf("PASS\n");
 
199
    return 0;
 
200
}