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

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/semapong.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 ITERATIONS 1000
 
44
 
 
45
static PRBool debug_mode = PR_FALSE;
 
46
static PRIntn iterations = ITERATIONS;
 
47
static PRSem *sem1, *sem2;
 
48
 
 
49
static void Help(void)
 
50
{
 
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");
 
55
}  /* Help */
 
56
 
 
57
int main(int argc, char **argv)
 
58
{
 
59
    PRIntn i;
 
60
    PRSharedMemory *shm;
 
61
    PRIntn *counter_addr;
 
62
    PLOptStatus os;
 
63
    PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
 
64
 
 
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 */
 
69
                debug_mode = PR_TRUE;
 
70
                break;
 
71
            case 'c':  /* loop count */
 
72
                iterations = atoi(opt->value);
 
73
                break;
 
74
            case 'h':
 
75
            default:
 
76
                Help();
 
77
                return 2;
 
78
        }
 
79
    }
 
80
    PL_DestroyOptState(opt);
 
81
 
 
82
    shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), 0, 0666);
 
83
    if (NULL == shm) {
 
84
        fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
 
85
                PR_GetError(), PR_GetOSError());
 
86
        exit(1);
 
87
    }
 
88
    sem1 = PR_OpenSemaphore(SEM_NAME1, 0, 0, 0);
 
89
    if (NULL == sem1) {
 
90
        fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
 
91
                PR_GetError(), PR_GetOSError());
 
92
        exit(1);
 
93
    }
 
94
    sem2 = PR_OpenSemaphore(SEM_NAME2, 0, 0, 0);
 
95
    if (NULL == sem2) {
 
96
        fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
 
97
                PR_GetError(), PR_GetOSError());
 
98
        exit(1);
 
99
    }
 
100
 
 
101
    counter_addr = PR_AttachSharedMemory(shm, 0);
 
102
    if (NULL == counter_addr) {
 
103
        fprintf(stderr, "PR_AttachSharedMemory failed\n");
 
104
        exit(1);
 
105
    }
 
106
 
 
107
    /*
 
108
     * Process 2 waits on semaphore 2 and posts to semaphore 1.
 
109
     */
 
110
    for (i = 0; i < iterations; i++) {
 
111
        if (PR_WaitSemaphore(sem2) == PR_FAILURE) {
 
112
            fprintf(stderr, "PR_WaitSemaphore failed\n");
 
113
            exit(1);
 
114
        }
 
115
        if (*counter_addr == 2*i+1) {
 
116
            if (debug_mode) printf("process 2: counter = %d\n", *counter_addr);
 
117
        } else {
 
118
            fprintf(stderr, "process 2: counter should be %d but is %d\n",
 
119
                    2*i+1, *counter_addr);
 
120
            exit(1);
 
121
        }
 
122
        (*counter_addr)++;
 
123
        if (PR_PostSemaphore(sem1) == PR_FAILURE) {
 
124
            fprintf(stderr, "PR_PostSemaphore failed\n");
 
125
            exit(1);
 
126
        }
 
127
    }
 
128
    if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
 
129
        fprintf(stderr, "PR_DetachSharedMemory failed\n");
 
130
        exit(1);
 
131
    }
 
132
    if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
 
133
        fprintf(stderr, "PR_CloseSharedMemory failed\n");
 
134
        exit(1);
 
135
    }
 
136
    if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
 
137
        fprintf(stderr, "PR_CloseSemaphore failed\n");
 
138
    }
 
139
    if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
 
140
        fprintf(stderr, "PR_CloseSemaphore failed\n");
 
141
    }
 
142
    printf("PASS\n");
 
143
    return 0;
 
144
}