VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/concur.c@ 55761

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

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.3 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) 1998-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/*
39** File: concur.c
40** Description: test of adding and removing concurrency options
41*/
42
43#include "prcvar.h"
44#include "prinit.h"
45#include "prinrval.h"
46#include "prlock.h"
47#include "prprf.h"
48#include "prmem.h"
49#include "prlog.h"
50
51#include "plgetopt.h"
52
53#if defined(XP_MAC)
54#include "pprio.h"
55#else
56#include "private/pprio.h"
57#endif
58
59#include <stdlib.h>
60
61#define DEFAULT_RANGE 10
62#define DEFAULT_LOOPS 100
63
64static PRThreadScope thread_scope = PR_LOCAL_THREAD;
65
66typedef struct Context
67{
68 PRLock *ml;
69 PRCondVar *cv;
70 PRIntn want, have;
71} Context;
72
73
74/*
75** Make the instance of 'context' static (not on the stack)
76** for Win16 threads
77*/
78static Context context = {NULL, NULL, 0, 0};
79
80static void PR_CALLBACK Dull(void *arg)
81{
82 Context *context = (Context*)arg;
83 PR_Lock(context->ml);
84 context->have += 1;
85 while (context->want >= context->have)
86 PR_WaitCondVar(context->cv, PR_INTERVAL_NO_TIMEOUT);
87 context->have -= 1;
88 PR_Unlock(context->ml);
89} /* Dull */
90
91PRIntn PR_CALLBACK Concur(PRIntn argc, char **argv)
92{
93 PRUintn cpus;
94 PLOptStatus os;
95 PRThread **threads;
96 PRBool debug = PR_FALSE;
97 PRUintn range = DEFAULT_RANGE;
98 PRStatus rc;
99 PRUintn cnt;
100 PRUintn loops = DEFAULT_LOOPS;
101 PRIntervalTime hundredMills = PR_MillisecondsToInterval(100);
102 PLOptState *opt = PL_CreateOptState(argc, argv, "Gdl:r:");
103 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
104 {
105 if (PL_OPT_BAD == os) continue;
106 switch (opt->option)
107 {
108 case 'G': /* GLOBAL threads */
109 thread_scope = PR_GLOBAL_THREAD;
110 break;
111 case 'd': /* debug mode */
112 debug = PR_TRUE;
113 break;
114 case 'r': /* range limit */
115 range = atoi(opt->value);
116 break;
117 case 'l': /* loop counter */
118 loops = atoi(opt->value);
119 break;
120 default:
121 break;
122 }
123 }
124 PL_DestroyOptState(opt);
125
126 if (0 == range) range = DEFAULT_RANGE;
127 if (0 == loops) loops = DEFAULT_LOOPS;
128
129 context.ml = PR_NewLock();
130 context.cv = PR_NewCondVar(context.ml);
131
132 if (debug)
133 PR_fprintf(
134 PR_STDERR, "Testing with %d CPUs and %d interations\n", range, loops);
135
136 threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * range);
137 while (--loops > 0)
138 {
139 for (cpus = 1; cpus <= range; ++cpus)
140 {
141 PR_SetConcurrency(cpus);
142 context.want = cpus;
143
144 threads[cpus - 1] = PR_CreateThread(
145 PR_USER_THREAD, Dull, &context, PR_PRIORITY_NORMAL,
146 thread_scope, PR_JOINABLE_THREAD, 0);
147 }
148
149 PR_Sleep(hundredMills);
150
151 for (cpus = range; cpus > 0; cpus--)
152 {
153 PR_SetConcurrency(cpus);
154 context.want = cpus - 1;
155
156 PR_Lock(context.ml);
157 PR_NotifyCondVar(context.cv);
158 PR_Unlock(context.ml);
159 }
160 for(cnt = 0; cnt < range; cnt++) {
161 rc = PR_JoinThread(threads[cnt]);
162 PR_ASSERT(rc == PR_SUCCESS);
163 }
164 }
165
166
167 if (debug)
168 PR_fprintf(
169 PR_STDERR, "Waiting for %d thread(s) to exit\n", context.have);
170
171 while (context.have > 0) PR_Sleep(hundredMills);
172
173 if (debug)
174 PR_fprintf(
175 PR_STDERR, "Finished [want: %d, have: %d]\n",
176 context.want, context.have);
177
178 PR_DestroyLock(context.ml);
179 PR_DestroyCondVar(context.cv);
180 PR_DELETE(threads);
181
182 PR_fprintf(PR_STDERR, "PASSED\n");
183
184 return 0;
185} /* Concur */
186
187PRIntn main(PRIntn argc, char **argv)
188{
189 PR_STDIO_INIT();
190 return PR_Initialize(Concur, argc, argv, 0);
191} /* main */
192
193/* concur.c */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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