Debian Codegraph @

nsprpub/pr/tests/joinuu.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /***********************************************************************
7 **
8 ** Name: dbmalloc1.c
9 **
10 ** Description: Join tests user - user
11 **
12 ** Modification History:
13 **
14 ** 19-May-97 AGarcia - separate the four join tests into different unit test
15 *modules.
16 ** AGarcia- Converted the test to accomodate the debug_mode flag.
17 ** The debug mode will print all of the printfs associated with this
18 *test.
19 ** The regress mode will be the default mode. Since the regress tool
20 *limits
21 ** the output to a one line status:PASS or FAIL,all of the printf
22 *statements
23 ** have been handled with an if (debug_mode) statement.
24 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
25 *updated to
26 ** recognize the return code from tha main program.
27 ***********************************************************************/
28
29 /***********************************************************************
30 ** Includes
31 ***********************************************************************/
32 /* Used to get the command line option */
33 #include "plgetopt.h"
34
35 #include "nspr.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 PRIntn failed_already = 0;
42 PRIntn debug_mode;
43
44 /*
45 Program to test joining of threads. Two threads are created. One
46 to be waited upon until it has started. The other to join after it has
47 completed.
48 */
49
50 static void lowPriority(void* arg) {}
51
52 static void highPriority(void* arg) {}
53
54 void runTest(PRThreadScope scope1, PRThreadScope scope2) {
55 PRThread *low, *high;
56
57 /* create the low and high priority threads */
58
59 low = PR_CreateThread(PR_USER_THREAD, lowPriority, 0, PR_PRIORITY_LOW, scope1,
60 PR_JOINABLE_THREAD, 0);
61 if (!low) {
62 if (debug_mode) {
63 printf("\tcannot create low priority thread\n");
64 } else {
65 failed_already = 1;
66 }
67 return;
68 }
69
70 high = PR_CreateThread(PR_USER_THREAD, highPriority, 0, PR_PRIORITY_HIGH,
71 scope2, PR_JOINABLE_THREAD, 0);
72 if (!high) {
73 if (debug_mode) {
74 printf("\tcannot create high priority thread\n");
75 } else {
76 failed_already = 1;
77 }
78 return;
79 }
80
81 /* Do the joining for both threads */
82 if (PR_JoinThread(low) == PR_FAILURE) {
83 if (debug_mode) {
84 printf("\tcannot join low priority thread\n");
85 } else {
86 failed_already = 1;
87 }
88 return;
89 } else {
90 if (debug_mode) {
91 printf("\tjoined low priority thread\n");
92 }
93 }
94 if (PR_JoinThread(high) == PR_FAILURE) {
95 if (debug_mode) {
96 printf("\tcannot join high priority thread\n");
97 } else {
98 failed_already = 1;
99 }
100 return;
101 } else {
102 if (debug_mode) {
103 printf("\tjoined high priority thread\n");
104 }
105 }
106 }
107
108 static PRIntn PR_CALLBACK RealMain(PRIntn argc, char** argv) {
109 /* The command line argument: -d is used to determine if the test is being run
110 in debug mode. The regress tool requires only one line output:PASS or FAIL.
111 All of the printfs associated with this test has been handled with a if
112 (debug_mode) test. Usage: test_name -d
113 */
114
115 PLOptStatus os;
116 PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
117 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
118 if (PL_OPT_BAD == os) {
119 continue;
120 }
121 switch (opt->option) {
122 case 'd': /* debug mode */
123 debug_mode = 1;
124 break;
125 default:
126 break;
127 }
128 }
129 PL_DestroyOptState(opt);
130
131 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
132 PR_STDIO_INIT();
133
134 /* main test */
135 if (debug_mode) {
136 printf("User-User test\n");
137 }
138 runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
139
140 if (failed_already) {
141 printf("FAIL\n");
142 return 1;
143 } else {
144 printf("PASS\n");
145 return 0;
146 }
147 }
148
149 int main(int argc, char** argv) {
150 PRIntn rv;
151
152 PR_STDIO_INIT();
153 rv = PR_Initialize(RealMain, argc, argv, 0);
154 return rv;
155 } /* main */