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: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions.
11 **
12 ** Modification History:
13 **
14 ** 19-May-97 AGarcia - separate the four join tests into different unit test modules.
15 **           AGarcia- Converted the test to accomodate the debug_mode flag.
16 **           The debug mode will print all of the printfs associated with this test.
17 **           The regress mode will be the default mode. Since the regress tool limits
18 **           the output to a one line status:PASS or FAIL,all of the printf statements
19 **           have been handled with an if (debug_mode) statement.
20 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
21 **          recognize the return code from tha main program.
22 ***********************************************************************/
23 
24 /***********************************************************************
25 ** Includes
26 ***********************************************************************/
27 /* Used to get the command line option */
28 #include "plgetopt.h"
29 
30 #include "nspr.h"
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 PRIntn failed_already=0;
37 PRIntn debug_mode;
38 
39 
40 /*
41     Program to test joining of threads.  Two threads are created.  One
42     to be waited upon until it has started.  The other to join after it has
43     completed.
44 */
45 
46 
lowPriority(void * arg)47 static void lowPriority(void *arg)
48 {
49 }
50 
highPriority(void * arg)51 static void highPriority(void *arg)
52 {
53 }
54 
runTest(PRThreadScope scope1,PRThreadScope scope2)55 void runTest(PRThreadScope scope1, PRThreadScope scope2)
56 {
57     PRThread *low,*high;
58 
59     /* create the low and high priority threads */
60 
61     low = PR_CreateThread(PR_USER_THREAD,
62                           lowPriority, 0,
63                           PR_PRIORITY_LOW,
64                           scope1,
65                           PR_JOINABLE_THREAD,
66                           0);
67     if (!low) {
68         if (debug_mode) {
69             printf("\tcannot create low priority thread\n");
70         }
71         else {
72             failed_already=1;
73         }
74         return;
75     }
76 
77     high = PR_CreateThread(PR_USER_THREAD,
78                            highPriority, 0,
79                            PR_PRIORITY_HIGH,
80                            scope2,
81                            PR_JOINABLE_THREAD,
82                            0);
83     if (!high) {
84         if (debug_mode) {
85             printf("\tcannot create high priority thread\n");
86         }
87         else {
88             failed_already=1;
89         }
90         return;
91     }
92 
93     /* Do the joining for both threads */
94     if (PR_JoinThread(low) == PR_FAILURE) {
95         if (debug_mode) {
96             printf("\tcannot join low priority thread\n");
97         }
98         else {
99             failed_already=1;
100         }
101         return;
102     } else {
103         if (debug_mode) {
104             printf("\tjoined low priority thread\n");
105         }
106     }
107     if (PR_JoinThread(high) == PR_FAILURE) {
108         if (debug_mode) {
109             printf("\tcannot join high priority thread\n");
110         }
111         else {
112             failed_already=1;
113         }
114         return;
115     } else {
116         if (debug_mode) {
117             printf("\tjoined high priority thread\n");
118         }
119     }
120 }
121 
RealMain(PRIntn argc,char ** argv)122 static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
123 {
124     /* The command line argument: -d is used to determine if the test is being run
125     in debug mode. The regress tool requires only one line output:PASS or FAIL.
126     All of the printfs associated with this test has been handled with a if (debug_mode)
127     test.
128     Usage: test_name -d
129     */
130 
131     PLOptStatus os;
132     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
133     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
134     {
135         if (PL_OPT_BAD == os) {
136             continue;
137         }
138         switch (opt->option)
139         {
140             case 'd':  /* debug mode */
141                 debug_mode = 1;
142                 break;
143             default:
144                 break;
145         }
146     }
147     PL_DestroyOptState(opt);
148 
149     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
150     PR_STDIO_INIT();
151 
152     /* main test */
153 
154     if (debug_mode) {
155         printf("Kernel-User test\n");
156     }
157     runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
158 
159 
160     if(failed_already)
161     {
162         printf("FAIL\n");
163         return 1;
164     }
165     else
166     {
167         printf("PASS\n");
168         return 0;
169     }
170 
171 }
172 
173 
main(int argc,char ** argv)174 int main(int argc, char **argv)
175 {
176     PRIntn rv;
177 
178     PR_STDIO_INIT();
179     rv = PR_Initialize(RealMain, argc, argv, 0);
180     return rv;
181 }  /* main */
182