~ubuntu-branches/ubuntu/gutsy/virtualbox-ose/gutsy

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/nsprpub/pr/tests/join.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-09-08 16:44:58 UTC
  • Revision ID: james.westby@ubuntu.com-20070908164458-wao29470vqtr8ksy
Tags: upstream-1.5.0-dfsg2
ImportĀ upstreamĀ versionĀ 1.5.0-dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
**
 
40
** Name: dbmalloc1.c
 
41
**
 
42
** Description: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions.
 
43
**
 
44
** Modification History:
 
45
** 
 
46
** 19-May-97 AGarcia - separate the four join tests into different unit test modules.
 
47
**                  AGarcia- Converted the test to accomodate the debug_mode flag.
 
48
**          The debug mode will print all of the printfs associated with this test.
 
49
**                  The regress mode will be the default mode. Since the regress tool limits
 
50
**          the output to a one line status:PASS or FAIL,all of the printf statements
 
51
**                  have been handled with an if (debug_mode) statement. 
 
52
***********************************************************************/
 
53
 
 
54
/***********************************************************************
 
55
** Includes
 
56
***********************************************************************/
 
57
/* Used to get the command line option */
 
58
#include "plgetopt.h"
 
59
#include "prttools.h"
 
60
 
 
61
#include "nspr.h"
 
62
 
 
63
#include <stdio.h>
 
64
#include <stdlib.h>
 
65
#include <string.h>
 
66
 
 
67
#ifdef XP_MAC
 
68
#include "prlog.h"
 
69
#define printf PR_LogPrint
 
70
extern void SetupMacPrintfLog(char *logFile);
 
71
#endif
 
72
/***********************************************************************
 
73
** PRIVATE FUNCTION:    Test_Result
 
74
** DESCRIPTION: Used in conjunction with the regress tool, prints out the
 
75
**                      status of the test case.
 
76
** INPUTS:      PASS/FAIL
 
77
** OUTPUTS:     None
 
78
** RETURN:      None
 
79
** SIDE EFFECTS:
 
80
**      
 
81
** RESTRICTIONS:
 
82
**      None
 
83
** MEMORY:      NA
 
84
** ALGORITHM:   Determine what the status is and print accordingly.
 
85
**      
 
86
***********************************************************************/
 
87
 
 
88
 
 
89
static void Test_Result (int result)
 
90
{
 
91
    if (result == PASS)
 
92
        printf ("PASS\n");
 
93
    else
 
94
        printf ("FAIL\n");
 
95
    exit (1);
 
96
}
 
97
 
 
98
 
 
99
/*
 
100
    Program to test joining of threads.  Two threads are created.  One
 
101
    to be waited upon until it has started.  The other to join after it has
 
102
    completed.
 
103
*/
 
104
 
 
105
 
 
106
static void PR_CALLBACK lowPriority(void *arg)
 
107
{
 
108
}
 
109
 
 
110
static void PR_CALLBACK highPriority(void *arg)
 
111
{
 
112
}
 
113
 
 
114
static void PR_CALLBACK unjoinable(void *arg)
 
115
{
 
116
    PR_Sleep(PR_INTERVAL_NO_TIMEOUT);
 
117
}
 
118
 
 
119
void runTest(PRThreadScope scope1, PRThreadScope scope2)
 
120
{
 
121
    PRThread *low,*high;
 
122
 
 
123
    /* create the low and high priority threads */
 
124
    
 
125
    low = PR_CreateThread(PR_USER_THREAD,
 
126
                     lowPriority, 0, 
 
127
                     PR_PRIORITY_LOW,
 
128
                     scope1,
 
129
                     PR_JOINABLE_THREAD,
 
130
                     0);
 
131
    if (!low) {
 
132
        if (debug_mode) printf("\tcannot create low priority thread\n");
 
133
        else Test_Result(FAIL);
 
134
        return;
 
135
    }
 
136
 
 
137
    high = PR_CreateThread(PR_USER_THREAD,
 
138
                     highPriority, 0, 
 
139
                     PR_PRIORITY_HIGH,
 
140
                     scope2,
 
141
                     PR_JOINABLE_THREAD,
 
142
                     0);
 
143
    if (!high) {
 
144
        if (debug_mode) printf("\tcannot create high priority thread\n");
 
145
        else Test_Result(FAIL);
 
146
        return;
 
147
    }
 
148
 
 
149
    /* Do the joining for both threads */
 
150
    if (PR_JoinThread(low) == PR_FAILURE) {
 
151
        if (debug_mode) printf("\tcannot join low priority thread\n");
 
152
        else Test_Result (FAIL);
 
153
        return;
 
154
    } else {
 
155
        if (debug_mode) printf("\tjoined low priority thread\n");
 
156
    }
 
157
    if (PR_JoinThread(high) == PR_FAILURE) {
 
158
        if (debug_mode) printf("\tcannot join high priority thread\n");
 
159
        else Test_Result(FAIL);
 
160
        return;
 
161
    } else {
 
162
        if (debug_mode) printf("\tjoined high priority thread\n");
 
163
    }
 
164
}
 
165
 
 
166
void joinWithUnjoinable(void)
 
167
{
 
168
    PRThread *thread;
 
169
 
 
170
    /* create the unjoinable thread */
 
171
    
 
172
    thread = PR_CreateThread(PR_USER_THREAD,
 
173
                     unjoinable, 0, 
 
174
                     PR_PRIORITY_NORMAL,
 
175
                     PR_GLOBAL_THREAD,
 
176
                     PR_UNJOINABLE_THREAD,
 
177
                     0);
 
178
    if (!thread) {
 
179
        if (debug_mode) printf("\tcannot create unjoinable thread\n");
 
180
        else Test_Result(FAIL);
 
181
        return;
 
182
    }
 
183
 
 
184
    if (PR_JoinThread(thread) == PR_SUCCESS) {
 
185
        if (debug_mode) printf("\tsuccessfully joined with unjoinable thread?!\n");
 
186
        else Test_Result(FAIL);
 
187
        return;
 
188
    } else {
 
189
        if (debug_mode) printf("\tcannot join with unjoinable thread, as expected\n");
 
190
        if (PR_GetError() != PR_INVALID_ARGUMENT_ERROR) {
 
191
            if (debug_mode) printf("\tWrong error code\n");
 
192
            else Test_Result(FAIL);
 
193
            return;
 
194
        }
 
195
    }
 
196
    if (PR_Interrupt(thread) == PR_FAILURE) {
 
197
        if (debug_mode) printf("\tcannot interrupt unjoinable thread\n");
 
198
        else Test_Result(FAIL);
 
199
        return;
 
200
    } else {
 
201
        if (debug_mode) printf("\tinterrupted unjoinable thread\n");
 
202
    }
 
203
}
 
204
 
 
205
static PRIntn PR_CALLBACK RealMain(int argc, char **argv)
 
206
{
 
207
    /* The command line argument: -d is used to determine if the test is being run
 
208
    in debug mode. The regress tool requires only one line output:PASS or FAIL.
 
209
    All of the printfs associated with this test has been handled with a if (debug_mode)
 
210
    test.
 
211
    Usage: test_name -d
 
212
    */
 
213
    
 
214
    PLOptStatus os;
 
215
    PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
 
216
    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
 
217
    {
 
218
        if (PL_OPT_BAD == os) continue;
 
219
        switch (opt->option)
 
220
        {
 
221
        case 'd':  /* debug mode */
 
222
            debug_mode = 1;
 
223
            break;
 
224
         default:
 
225
            break;
 
226
        }
 
227
    }
 
228
    PL_DestroyOptState(opt);
 
229
 
 
230
#ifdef XP_MAC
 
231
    SetupMacPrintfLog("join.log");
 
232
    debug_mode = 1;
 
233
#endif
 
234
 
 
235
    
 
236
    
 
237
 /* main test */
 
238
    printf("User-User test\n");
 
239
    runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
 
240
    printf("User-Kernel test\n");
 
241
    runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
 
242
    printf("Kernel-User test\n");
 
243
    runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
 
244
    printf("Kernel-Kernel test\n");
 
245
    runTest(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
 
246
    printf("Join with unjoinable thread\n");
 
247
    joinWithUnjoinable();
 
248
 
 
249
    printf("PASSED\n");
 
250
 
 
251
    return 0;
 
252
}
 
253
 
 
254
 
 
255
 
 
256
 
 
257
PRIntn main(PRIntn argc, char *argv[])
 
258
{
 
259
    PRIntn rv;
 
260
    
 
261
    PR_STDIO_INIT();
 
262
    rv = PR_Initialize(RealMain, argc, argv, 0);
 
263
    return rv;
 
264
}  /* main */