VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/sema.c@ 89890

最後變更 在這個檔案從89890是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.9 KB
 
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 SEM_NAME1 "/tmp/foo.sem"
44#define SEM_NAME2 "/tmp/bar.sem"
45#define SEM_MODE 0666
46#define ITERATIONS 1000
47
48static PRBool debug_mode = PR_FALSE;
49static PRIntn iterations = ITERATIONS;
50static PRIntn counter;
51static PRSem *sem1, *sem2;
52
53/*
54 * Thread 2 waits on semaphore 2 and posts to semaphore 1.
55 */
56void ThreadFunc(void *arg)
57{
58 PRIntn i;
59
60 for (i = 0; i < iterations; i++) {
61 if (PR_WaitSemaphore(sem2) == PR_FAILURE) {
62 fprintf(stderr, "PR_WaitSemaphore failed\n");
63 exit(1);
64 }
65 if (counter == 2*i+1) {
66 if (debug_mode) printf("thread 2: counter = %d\n", counter);
67 } else {
68 fprintf(stderr, "thread 2: counter should be %d but is %d\n",
69 2*i+1, counter);
70 exit(1);
71 }
72 counter++;
73 if (PR_PostSemaphore(sem1) == PR_FAILURE) {
74 fprintf(stderr, "PR_PostSemaphore failed\n");
75 exit(1);
76 }
77 }
78}
79
80static void Help(void)
81{
82 fprintf(stderr, "sema test program usage:\n");
83 fprintf(stderr, "\t-d debug mode (FALSE)\n");
84 fprintf(stderr, "\t-c <count> loop count (%d)\n", ITERATIONS);
85 fprintf(stderr, "\t-h this message\n");
86} /* Help */
87
88int main(int argc, char **argv)
89{
90 PRThread *thred;
91 PRIntn i;
92 PLOptStatus os;
93 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:h");
94
95 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
96 if (PL_OPT_BAD == os) continue;
97 switch (opt->option) {
98 case 'd': /* debug mode */
99 debug_mode = PR_TRUE;
100 break;
101 case 'c': /* loop count */
102 iterations = atoi(opt->value);
103 break;
104 case 'h':
105 default:
106 Help();
107 return 2;
108 }
109 }
110 PL_DestroyOptState(opt);
111
112 if (PR_DeleteSemaphore(SEM_NAME1) == PR_SUCCESS) {
113 fprintf(stderr, "warning: removed semaphore %s left over "
114 "from previous run\n", SEM_NAME1);
115 }
116 if (PR_DeleteSemaphore(SEM_NAME2) == PR_SUCCESS) {
117 fprintf(stderr, "warning: removed semaphore %s left over "
118 "from previous run\n", SEM_NAME2);
119 }
120
121 sem1 = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 1);
122 if (NULL == sem1) {
123 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
124 PR_GetError(), PR_GetOSError());
125 exit(1);
126 }
127 sem2 = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE, SEM_MODE, 0);
128 if (NULL == sem2) {
129 fprintf(stderr, "PR_OpenSemaphore failed\n");
130 exit(1);
131 }
132 thred = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
133 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
134 if (NULL == thred) {
135 fprintf(stderr, "PR_CreateThread failed\n");
136 exit(1);
137 }
138
139 /*
140 * Thread 1 waits on semaphore 1 and posts to semaphore 2.
141 */
142 for (i = 0; i < iterations; i++) {
143 if (PR_WaitSemaphore(sem1) == PR_FAILURE) {
144 fprintf(stderr, "PR_WaitSemaphore failed\n");
145 exit(1);
146 }
147 if (counter == 2*i) {
148 if (debug_mode) printf("thread 1: counter = %d\n", counter);
149 } else {
150 fprintf(stderr, "thread 1: counter should be %d but is %d\n",
151 2*i, counter);
152 exit(1);
153 }
154 counter++;
155 if (PR_PostSemaphore(sem2) == PR_FAILURE) {
156 fprintf(stderr, "PR_PostSemaphore failed\n");
157 exit(1);
158 }
159 }
160
161 if (PR_JoinThread(thred) == PR_FAILURE) {
162 fprintf(stderr, "PR_JoinThread failed\n");
163 exit(1);
164 }
165
166 if (PR_CloseSemaphore(sem1) == PR_FAILURE) {
167 fprintf(stderr, "PR_CloseSemaphore failed\n");
168 }
169 if (PR_CloseSemaphore(sem2) == PR_FAILURE) {
170 fprintf(stderr, "PR_CloseSemaphore failed\n");
171 }
172 if (PR_DeleteSemaphore(SEM_NAME1) == PR_FAILURE) {
173 fprintf(stderr, "PR_DeleteSemaphore failed\n");
174 }
175 if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
176 fprintf(stderr, "PR_DeleteSemaphore failed\n");
177 }
178 printf("PASS\n");
179 return 0;
180}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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