1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Netscape Portable Runtime (NSPR).
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-2000
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "prio.h"
#include "prprf.h"
#include "prlog.h"
#include "prmem.h"
#include "pratom.h"
#include "prlock.h"
#include "prmwait.h"
#include "prclist.h"
#include "prerror.h"
#include "prinrval.h"
#include "prnetdb.h"
#include "prthread.h"
#include "plstr.h"
#include "plerror.h"
#include "plgetopt.h"
#include <string.h>
typedef struct Shared
{
const char *title;
PRLock *list_lock;
PRWaitGroup *group;
PRIntervalTime timeout;
} Shared;
typedef enum Verbosity {silent, quiet, chatty, noisy} Verbosity;
static PRFileDesc *debug = NULL;
static PRInt32 desc_allocated = 0;
static PRUint16 default_port = 12273;
static enum Verbosity verbosity = quiet;
static PRInt32 ops_required = 1000, ops_done = 0;
static PRThreadScope thread_scope = PR_LOCAL_THREAD;
static PRIntn client_threads = 20, worker_threads = 2, wait_objects = 50;
#if defined(DEBUG)
#define MW_ASSERT(_expr) \
((_expr)?((void)0):_MW_Assert(# _expr,__FILE__,__LINE__))
static void _MW_Assert(const char *s, const char *file, PRIntn ln)
{
if (NULL != debug) PL_FPrintError(debug, NULL);
PR_Assert(s, file, ln);
} /* _MW_Assert */
#else
#define MW_ASSERT(_expr)
#endif
static void PrintRecvDesc(PRRecvWait *desc, const char *msg)
{
const char *tag[] = {
"PR_MW_INTERRUPT", "PR_MW_TIMEOUT",
"PR_MW_FAILURE", "PR_MW_SUCCESS", "PR_MW_PENDING"};
PR_fprintf(
debug, "%s: PRRecvWait(@0x%x): {fd: 0x%x, outcome: %s, tmo: %u}\n",
msg, desc, desc->fd, tag[desc->outcome + 3], desc->timeout);
} /* PrintRecvDesc */
static Shared *MakeShared(const char *title)
{
Shared *shared = PR_NEWZAP(Shared);
shared->group = PR_CreateWaitGroup(1);
shared->timeout = PR_SecondsToInterval(1);
shared->list_lock = PR_NewLock();
shared->title = title;
return shared;
} /* MakeShared */
static void DestroyShared(Shared *shared)
{
PRStatus rv;
if (verbosity > quiet)
PR_fprintf(debug, "%s: destroying group\n", shared->title);
rv = PR_DestroyWaitGroup(shared->group);
MW_ASSERT(PR_SUCCESS == rv);
PR_DestroyLock(shared->list_lock);
PR_DELETE(shared);
} /* DestroyShared */
static PRRecvWait *CreateRecvWait(PRFileDesc *fd, PRIntervalTime timeout)
{
PRRecvWait *desc_out = PR_NEWZAP(PRRecvWait);
MW_ASSERT(NULL != desc_out);
MW_ASSERT(NULL != fd);
desc_out->fd = fd;
desc_out->timeout = timeout;
desc_out->buffer.length = 120;
desc_out->buffer.start = PR_CALLOC(120);
PR_AtomicIncrement(&desc_allocated);
if (verbosity > chatty)
PrintRecvDesc(desc_out, "Allocated");
return desc_out;
} /* CreateRecvWait */
static void DestroyRecvWait(PRRecvWait *desc_out)
{
if (verbosity > chatty)
PrintRecvDesc(desc_out, "Destroying");
PR_Close(desc_out->fd);
if (NULL != desc_out->buffer.start)
PR_DELETE(desc_out->buffer.start);
PR_Free(desc_out);
(void)PR_AtomicDecrement(&desc_allocated);
} /* DestroyRecvWait */
static void CancelGroup(Shared *shared)
{
PRRecvWait *desc_out;
if (verbosity > quiet)
PR_fprintf(debug, "%s Reclaiming wait descriptors\n", shared->title);
do
{
desc_out = PR_CancelWaitGroup(shared->group);
if (NULL != desc_out) DestroyRecvWait(desc_out);
} while (NULL != desc_out);
MW_ASSERT(0 == desc_allocated);
MW_ASSERT(PR_GROUP_EMPTY_ERROR == PR_GetError());
} /* CancelGroup */
static void PR_CALLBACK ClientThread(void* arg)
{
PRStatus rv;
PRInt32 bytes;
PRIntn empty_flags = 0;
PRNetAddr server_address;
unsigned char buffer[100];
Shared *shared = (Shared*)arg;
PRFileDesc *server = PR_NewTCPSocket();
if ((NULL == server)
&& (PR_PENDING_INTERRUPT_ERROR == PR_GetError())) return;
MW_ASSERT(NULL != server);
if (verbosity > chatty)
PR_fprintf(debug, "%s: Server socket @0x%x\n", shared->title, server);
/* Initialize the buffer so that Purify won't complain */
memset(buffer, 0, sizeof(buffer));
rv = PR_InitializeNetAddr(PR_IpAddrLoopback, default_port, &server_address);
MW_ASSERT(PR_SUCCESS == rv);
if (verbosity > quiet)
PR_fprintf(debug, "%s: Client opening connection\n", shared->title);
rv = PR_Connect(server, &server_address, PR_INTERVAL_NO_TIMEOUT);
if (PR_FAILURE == rv)
{
if (verbosity > silent) PL_FPrintError(debug, "Client connect failed");
return;
}
while (ops_done < ops_required)
{
bytes = PR_Send(
server, buffer, sizeof(buffer), empty_flags, PR_INTERVAL_NO_TIMEOUT);
if ((-1 == bytes) && (PR_PENDING_INTERRUPT_ERROR == PR_GetError())) break;
MW_ASSERT(sizeof(buffer) == bytes);
if (verbosity > chatty)
PR_fprintf(
debug, "%s: Client sent %d bytes\n",
shared->title, sizeof(buffer));
bytes = PR_Recv(
server, buffer, sizeof(buffer), empty_flags, PR_INTERVAL_NO_TIMEOUT);
if (verbosity > chatty)
PR_fprintf(
debug, "%s: Client received %d bytes\n",
shared->title, sizeof(buffer));
if ((-1 == bytes) && (PR_PENDING_INTERRUPT_ERROR == PR_GetError())) break;
MW_ASSERT(sizeof(buffer) == bytes);
PR_Sleep(shared->timeout);
}
rv = PR_Close(server);
MW_ASSERT(PR_SUCCESS == rv);
} /* ClientThread */
static void OneInThenCancelled(Shared *shared)
{
PRStatus rv;
PRRecvWait *desc_out, *desc_in = PR_NEWZAP(PRRecvWait);
shared->timeout = PR_INTERVAL_NO_TIMEOUT;
desc_in->fd = PR_NewTCPSocket();
desc_in->timeout = shared->timeout;
if (verbosity > chatty) PrintRecvDesc(desc_in, "Adding desc");
rv = PR_AddWaitFileDesc(shared->group, desc_in);
MW_ASSERT(PR_SUCCESS == rv);
if (verbosity > chatty) PrintRecvDesc(desc_in, "Cancelling");
rv = PR_CancelWaitFileDesc(shared->group, desc_in);
MW_ASSERT(PR_SUCCESS == rv);
desc_out = PR_WaitRecvReady(shared->group);
MW_ASSERT(desc_out == desc_in);
MW_ASSERT(PR_MW_INTERRUPT == desc_out->outcome);
MW_ASSERT(PR_PENDING_INTERRUPT_ERROR == PR_GetError());
if (verbosity > chatty) PrintRecvDesc(desc_out, "Ready");
rv = PR_Close(desc_in->fd);
MW_ASSERT(PR_SUCCESS == rv);
if (verbosity > quiet)
PR_fprintf(debug, "%s: destroying group\n", shared->title);
PR_DELETE(desc_in);
} /* OneInThenCancelled */
static void OneOpOneThread(Shared *shared)
{
PRStatus rv;
PRRecvWait *desc_out, *desc_in = PR_NEWZAP(PRRecvWait);
desc_in->fd = PR_NewTCPSocket();
desc_in->timeout = shared->timeout;
if (verbosity > chatty) PrintRecvDesc(desc_in, "Adding desc");
rv = PR_AddWaitFileDesc(shared->group, desc_in);
MW_ASSERT(PR_SUCCESS == rv);
desc_out = PR_WaitRecvReady(shared->group);
MW_ASSERT(desc_out == desc_in);
MW_ASSERT(PR_MW_TIMEOUT == desc_out->outcome);
MW_ASSERT(PR_IO_TIMEOUT_ERROR == PR_GetError());
if (verbosity > chatty) PrintRecvDesc(desc_out, "Ready");
rv = PR_Close(desc_in->fd);
MW_ASSERT(PR_SUCCESS == rv);
PR_DELETE(desc_in);
} /* OneOpOneThread */
static void ManyOpOneThread(Shared *shared)
{
PRStatus rv;
PRIntn index;
PRRecvWait *desc_in;
PRRecvWait *desc_out;
if (verbosity > quiet)
PR_fprintf(debug, "%s: adding %d descs\n", shared->title, wait_objects);
for (index = 0; index < wait_objects; ++index)
{
desc_in = CreateRecvWait(PR_NewTCPSocket(), shared->timeout);
rv = PR_AddWaitFileDesc(shared->group, desc_in);
MW_ASSERT(PR_SUCCESS == rv);
}
while (ops_done < ops_required)
{
desc_out = PR_WaitRecvReady(shared->group);
MW_ASSERT(PR_MW_TIMEOUT == desc_out->outcome);
MW_ASSERT(PR_IO_TIMEOUT_ERROR == PR_GetError());
if (verbosity > chatty) PrintRecvDesc(desc_out, "Ready/readding");
rv = PR_AddWaitFileDesc(shared->group, desc_out);
MW_ASSERT(PR_SUCCESS == rv);
(void)PR_AtomicIncrement(&ops_done);
}
CancelGroup(shared);
} /* ManyOpOneThread */
static void PR_CALLBACK SomeOpsThread(void *arg)
{
PRRecvWait *desc_out;
PRStatus rv = PR_SUCCESS;
Shared *shared = (Shared*)arg;
do /* until interrupted */
{
desc_out = PR_WaitRecvReady(shared->group);
if (NULL == desc_out)
{
MW_ASSERT(PR_PENDING_INTERRUPT_ERROR == PR_GetError());
if (verbosity > quiet) PR_fprintf(debug, "Aborted\n");
break;
}
MW_ASSERT(PR_MW_TIMEOUT == desc_out->outcome);
MW_ASSERT(PR_IO_TIMEOUT_ERROR == PR_GetError());
if (verbosity > chatty) PrintRecvDesc(desc_out, "Ready");
if (verbosity > chatty) PrintRecvDesc(desc_out, "Re-Adding");
desc_out->timeout = shared->timeout;
rv = PR_AddWaitFileDesc(shared->group, desc_out);
PR_AtomicIncrement(&ops_done);
if (ops_done > ops_required) break;
} while (PR_SUCCESS == rv);
MW_ASSERT(PR_SUCCESS == rv);
} /* SomeOpsThread */
static void SomeOpsSomeThreads(Shared *shared)
{
PRStatus rv;
PRThread **thread;
PRIntn index;
PRRecvWait *desc_in;
thread = (PRThread**)PR_CALLOC(sizeof(PRThread*) * worker_threads);
/* Create some threads */
if (verbosity > quiet)
PR_fprintf(debug, "%s: creating threads\n", shared->title);
for (index = 0; index < worker_threads; ++index)
{
thread[index] = PR_CreateThread(
PR_USER_THREAD, SomeOpsThread, shared,
PR_PRIORITY_HIGH, thread_scope,
PR_JOINABLE_THREAD, 16 * 1024);
}
/* then create some operations */
if (verbosity > quiet)
PR_fprintf(debug, "%s: creating desc\n", shared->title);
for (index = 0; index < wait_objects; ++index)
{
desc_in = CreateRecvWait(PR_NewTCPSocket(), shared->timeout);
rv = PR_AddWaitFileDesc(shared->group, desc_in);
MW_ASSERT(PR_SUCCESS == rv);
}
if (verbosity > quiet)
PR_fprintf(debug, "%s: sleeping\n", shared->title);
while (ops_done < ops_required) PR_Sleep(shared->timeout);
if (verbosity > quiet)
PR_fprintf(debug, "%s: interrupting/joining threads\n", shared->title);
for (index = 0; index < worker_threads; ++index)
{
rv = PR_Interrupt(thread[index]);
MW_ASSERT(PR_SUCCESS == rv);
rv = PR_JoinThread(thread[index]);
MW_ASSERT(PR_SUCCESS == rv);
}
PR_DELETE(thread);
CancelGroup(shared);
} /* SomeOpsSomeThreads */
static PRStatus ServiceRequest(Shared *shared, PRRecvWait *desc)
{
PRInt32 bytes_out;
if (verbosity > chatty)
PR_fprintf(
debug, "%s: Service received %d bytes\n",
shared->title, desc->bytesRecv);
if (0 == desc->bytesRecv) goto quitting;
if ((-1 == desc->bytesRecv)
&& (PR_PENDING_INTERRUPT_ERROR == PR_GetError())) goto aborted;
bytes_out = PR_Send(
desc->fd, desc->buffer.start, desc->bytesRecv, 0, shared->timeout);
if (verbosity > chatty)
PR_fprintf(
debug, "%s: Service sent %d bytes\n",
shared->title, bytes_out);
if ((-1 == bytes_out)
&& (PR_PENDING_INTERRUPT_ERROR == PR_GetError())) goto aborted;
MW_ASSERT(bytes_out == desc->bytesRecv);
return PR_SUCCESS;
aborted:
quitting:
return PR_FAILURE;
} /* ServiceRequest */
static void PR_CALLBACK ServiceThread(void *arg)
{
PRStatus rv = PR_SUCCESS;
PRRecvWait *desc_out = NULL;
Shared *shared = (Shared*)arg;
do /* until interrupted */
{
if (NULL != desc_out)
{
desc_out->timeout = PR_INTERVAL_NO_TIMEOUT;
if (verbosity > chatty)
PrintRecvDesc(desc_out, "Service re-adding");
rv = PR_AddWaitFileDesc(shared->group, desc_out);
MW_ASSERT(PR_SUCCESS == rv);
}
desc_out = PR_WaitRecvReady(shared->group);
if (NULL == desc_out)
{
MW_ASSERT(PR_PENDING_INTERRUPT_ERROR == PR_GetError());
break;
}
switch (desc_out->outcome)
{
case PR_MW_SUCCESS:
{
PR_AtomicIncrement(&ops_done);
if (verbosity > chatty)
PrintRecvDesc(desc_out, "Service ready");
rv = ServiceRequest(shared, desc_out);
break;
}
case PR_MW_INTERRUPT:
MW_ASSERT(PR_PENDING_INTERRUPT_ERROR == PR_GetError());
rv = PR_FAILURE; /* if interrupted, then exit */
break;
case PR_MW_TIMEOUT:
MW_ASSERT(PR_IO_TIMEOUT_ERROR == PR_GetError());
case PR_MW_FAILURE:
if (verbosity > silent)
PL_FPrintError(debug, "RecvReady failure");
break;
default:
break;
}
} while (PR_SUCCESS == rv);
if (NULL != desc_out) DestroyRecvWait(desc_out);
} /* ServiceThread */
static void PR_CALLBACK EnumerationThread(void *arg)
{
PRStatus rv;
PRIntn count;
PRRecvWait *desc;
Shared *shared = (Shared*)arg;
PRIntervalTime five_seconds = PR_SecondsToInterval(5);
PRMWaitEnumerator *enumerator = PR_CreateMWaitEnumerator(shared->group);
MW_ASSERT(NULL != enumerator);
while (PR_SUCCESS == PR_Sleep(five_seconds))
{
count = 0;
desc = NULL;
while (NULL != (desc = PR_EnumerateWaitGroup(enumerator, desc)))
{
if (verbosity > chatty) PrintRecvDesc(desc, shared->title);
count += 1;
}
if (verbosity > silent)
PR_fprintf(debug,
"%s Enumerated %d objects\n", shared->title, count);
}
MW_ASSERT(PR_PENDING_INTERRUPT_ERROR == PR_GetError());
rv = PR_DestroyMWaitEnumerator(enumerator);
MW_ASSERT(PR_SUCCESS == rv);
} /* EnumerationThread */
static void PR_CALLBACK ServerThread(void *arg)
{
PRStatus rv;
PRIntn index;
PRRecvWait *desc_in;
PRThread **worker_thread;
Shared *shared = (Shared*)arg;
PRFileDesc *listener, *service;
PRNetAddr server_address, client_address;
worker_thread = (PRThread**)PR_CALLOC(sizeof(PRThread*) * worker_threads);
if (verbosity > quiet)
PR_fprintf(debug, "%s: Server creating worker_threads\n", shared->title);
for (index = 0; index < worker_threads; ++index)
{
worker_thread[index] = PR_CreateThread(
PR_USER_THREAD, ServiceThread, shared,
PR_PRIORITY_HIGH, thread_scope,
PR_JOINABLE_THREAD, 16 * 1024);
}
rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &server_address);
MW_ASSERT(PR_SUCCESS == rv);
listener = PR_NewTCPSocket(); MW_ASSERT(NULL != listener);
if (verbosity > chatty)
PR_fprintf(
debug, "%s: Server listener socket @0x%x\n",
shared->title, listener);
rv = PR_Bind(listener, &server_address); MW_ASSERT(PR_SUCCESS == rv);
rv = PR_Listen(listener, 10); MW_ASSERT(PR_SUCCESS == rv);
while (ops_done < ops_required)
{
if (verbosity > quiet)
PR_fprintf(debug, "%s: Server accepting connection\n", shared->title);
service = PR_Accept(listener, &client_address, PR_INTERVAL_NO_TIMEOUT);
if (NULL == service)
{
if (PR_PENDING_INTERRUPT_ERROR == PR_GetError()) break;
PL_PrintError("Accept failed");
MW_ASSERT(!"Accept failed");
}
else
{
desc_in = CreateRecvWait(service, shared->timeout);
desc_in->timeout = PR_INTERVAL_NO_TIMEOUT;
if (verbosity > chatty)
PrintRecvDesc(desc_in, "Service adding");
rv = PR_AddWaitFileDesc(shared->group, desc_in);
MW_ASSERT(PR_SUCCESS == rv);
}
}
if (verbosity > quiet)
PR_fprintf(debug, "%s: Server interrupting worker_threads\n", shared->title);
for (index = 0; index < worker_threads; ++index)
{
rv = PR_Interrupt(worker_thread[index]);
MW_ASSERT(PR_SUCCESS == rv);
rv = PR_JoinThread(worker_thread[index]);
MW_ASSERT(PR_SUCCESS == rv);
}
PR_DELETE(worker_thread);
PR_Close(listener);
CancelGroup(shared);
} /* ServerThread */
static void RealOneGroupIO(Shared *shared)
{
/*
** Create a server that listens for connections and then services
** requests that come in over those connections. The server never
** deletes a connection and assumes a basic RPC model of operation.
**
** Use worker_threads threads to service how every many open ports
** there might be.
**
** Oh, ya. Almost forget. Create (some) clients as well.
*/
PRStatus rv;
PRIntn index;
PRThread *server_thread, *enumeration_thread, **client_thread;
if (verbosity > quiet)
PR_fprintf(debug, "%s: creating server_thread\n", shared->title);
server_thread = PR_CreateThread(
PR_USER_THREAD, ServerThread, shared,
PR_PRIORITY_HIGH, thread_scope,
PR_JOINABLE_THREAD, 16 * 1024);
if (verbosity > quiet)
PR_fprintf(debug, "%s: creating enumeration_thread\n", shared->title);
enumeration_thread = PR_CreateThread(
PR_USER_THREAD, EnumerationThread, shared,
PR_PRIORITY_HIGH, thread_scope,
PR_JOINABLE_THREAD, 16 * 1024);
if (verbosity > quiet)
PR_fprintf(debug, "%s: snoozing before creating clients\n", shared->title);
PR_Sleep(5 * shared->timeout);
if (verbosity > quiet)
PR_fprintf(debug, "%s: creating client_threads\n", shared->title);
client_thread = (PRThread**)PR_CALLOC(sizeof(PRThread*) * client_threads);
for (index = 0; index < client_threads; ++index)
{
client_thread[index] = PR_CreateThread(
PR_USER_THREAD, ClientThread, shared,
PR_PRIORITY_NORMAL, thread_scope,
PR_JOINABLE_THREAD, 16 * 1024);
}
while (ops_done < ops_required) PR_Sleep(shared->timeout);
if (verbosity > quiet)
PR_fprintf(debug, "%s: interrupting/joining client_threads\n", shared->title);
for (index = 0; index < client_threads; ++index)
{
rv = PR_Interrupt(client_thread[index]);
MW_ASSERT(PR_SUCCESS == rv);
rv = PR_JoinThread(client_thread[index]);
MW_ASSERT(PR_SUCCESS == rv);
}
PR_DELETE(client_thread);
if (verbosity > quiet)
PR_fprintf(debug, "%s: interrupting/joining enumeration_thread\n", shared->title);
rv = PR_Interrupt(enumeration_thread);
MW_ASSERT(PR_SUCCESS == rv);
rv = PR_JoinThread(enumeration_thread);
MW_ASSERT(PR_SUCCESS == rv);
if (verbosity > quiet)
PR_fprintf(debug, "%s: interrupting/joining server_thread\n", shared->title);
rv = PR_Interrupt(server_thread);
MW_ASSERT(PR_SUCCESS == rv);
rv = PR_JoinThread(server_thread);
MW_ASSERT(PR_SUCCESS == rv);
} /* RealOneGroupIO */
static void RunThisOne(
void (*func)(Shared*), const char *name, const char *test_name)
{
Shared *shared;
if ((NULL == test_name) || (0 == PL_strcmp(name, test_name)))
{
if (verbosity > silent)
PR_fprintf(debug, "%s()\n", name);
shared = MakeShared(name);
ops_done = 0;
func(shared); /* run the test */
MW_ASSERT(0 == desc_allocated);
DestroyShared(shared);
}
} /* RunThisOne */
static Verbosity ChangeVerbosity(Verbosity verbosity, PRIntn delta)
{
PRIntn verbage = (PRIntn)verbosity;
return (Verbosity)(verbage += delta);
} /* ChangeVerbosity */
PRIntn main(PRIntn argc, char **argv)
{
PLOptStatus os;
const char *test_name = NULL;
PLOptState *opt = PL_CreateOptState(argc, argv, "dqGc:o:p:t:w:");
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
{
if (PL_OPT_BAD == os) continue;
switch (opt->option)
{
case 0:
test_name = opt->value;
break;
case 'd': /* debug mode */
if (verbosity < noisy)
verbosity = ChangeVerbosity(verbosity, 1);
break;
case 'q': /* debug mode */
if (verbosity > silent)
verbosity = ChangeVerbosity(verbosity, -1);
break;
case 'G': /* use global threads */
thread_scope = PR_GLOBAL_THREAD;
break;
case 'c': /* number of client threads */
client_threads = atoi(opt->value);
break;
case 'o': /* operations to compelete */
ops_required = atoi(opt->value);
break;
case 'p': /* default port */
default_port = atoi(opt->value);
break;
case 't': /* number of threads waiting */
worker_threads = atoi(opt->value);
break;
case 'w': /* number of wait objects */
wait_objects = atoi(opt->value);
break;
default:
break;
}
}
PL_DestroyOptState(opt);
if (verbosity > 0)
debug = PR_GetSpecialFD(PR_StandardError);
RunThisOne(OneInThenCancelled, "OneInThenCancelled", test_name);
RunThisOne(OneOpOneThread, "OneOpOneThread", test_name);
RunThisOne(ManyOpOneThread, "ManyOpOneThread", test_name);
RunThisOne(SomeOpsSomeThreads, "SomeOpsSomeThreads", test_name);
RunThisOne(RealOneGroupIO, "RealOneGroupIO", test_name);
return 0;
} /* main */
/* multwait.c */
|