VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/semapong.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include "nspr.h"
39#include "plgetopt.h"
40
41#include <stdio.h>
42
43#define SHM_NAME "/tmp/counter"
44#define SEM_NAME1 "/tmp/foo.sem"
45#define SEM_NAME2 "/tmp/bar.sem"
46#define ITERATIONS 1000
47
48static PRBool debug_mode = PR_FALSE;
49static PRIntn iterations = ITERATIONS;
50static PRSem *sem1, *sem2;
51
52static void Help(void)
53{
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");
58} /* Help */
59
60int main(int argc, char **argv)
61{
62 PRIntn i;
63 PRSharedMemory *shm;
64 PRIntn *counter_addr;
65 PLOptStatus os;
66 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
67
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 */
72 debug_mode = PR_TRUE;
73 break;
74 case 'c': /* loop count */
75 iterations = atoi(opt->value);
76 break;
77 case 'h':
78 default:
79 Help();
80 return 2;
81 }
82 }
83 PL_DestroyOptState(opt);
84
85 shm = PR_OpenSharedMemory(SHM_NAME, sizeof(*counter_addr), 0, 0666);
86 if (NULL == shm) {
87 fprintf(stderr, "PR_OpenSharedMemory failed (%d, %d)\n",
88 PR_GetError(), PR_GetOSError());
89 exit(1);
90 }
91 sem1 = PR_OpenSemaphore(SEM_NAME1, 0, 0, 0);
92 if (NULL == sem1) {
93 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
94 PR_GetError(), PR_GetOSError());
95 exit(1);
96 }
97 sem2 = PR_OpenSemaphore(SEM_NAME2, 0, 0, 0);
98 if (NULL == sem2) {
99 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
100 PR_GetError(), PR_GetOSError());
101 exit(1);
102 }
103
104 counter_addr = PR_AttachSharedMemory(shm, 0);
105 if (NULL == counter_addr) {
106 fprintf(stderr, "PR_AttachSharedMemory failed\n");
107 exit(1);
108 }
109
110 /*
111 * Process 2 waits on semaphore 2 and posts to semaphore 1.
112 */
113 for (i = 0; i < iterations; i++) {
114 if (PR_WaitSemaphore(sem2) == PR_FAILURE) {
115 fprintf(stderr, "PR_WaitSemaphore failed\n");
116 exit(1);
117 }
118 if (*counter_addr == 2*i+1) {
119 if (debug_mode) printf("process 2: counter = %d\n", *counter_addr);
120 } else {
121 fprintf(stderr, "process 2: counter should be %d but is %d\n",
122 2*i+1, *counter_addr);
123 exit(1);
124 }
125 (*counter_addr)++;
126 if (PR_PostSemaphore(sem1) == PR_FAILURE) {
127 fprintf(stderr, "PR_PostSemaphore failed\n");
128 exit(1);
129 }
130 }
131 if (PR_DetachSharedMemory(shm, counter_addr) == PR_FAILURE) {
132 fprintf(stderr, "PR_DetachSharedMemory failed\n");
133 exit(1);
134 }
135 if (PR_CloseSharedMemory(shm) == PR_FAILURE) {
136 fprintf(stderr, "PR_CloseSharedMemory failed\n");
137 exit(1);
138 }
139 if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
140 fprintf(stderr, "PR_CloseSemaphore failed\n");
141 }
142 if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
143 fprintf(stderr, "PR_CloseSemaphore failed\n");
144 }
145 printf("PASS\n");
146 return 0;
147}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette