| 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: joinuk.c |
| 9 |
** |
| 10 |
** Description: Join kernel - 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 |
Program to test joining of threads. Two threads are created. One |
| 45 |
to be waited upon until it has started. The other to join after it has |
| 46 |
completed. |
| 47 |
*/ |
| 48 |
|
| 49 |
static void lowPriority(void* arg) {} |
| 50 |
|
| 51 |
static void highPriority(void* arg) {} |
| 52 |
|
| 53 |
void runTest(PRThreadScope scope1, PRThreadScope scope2) { |
| 54 |
PRThread *low, *high; |
| 55 |
|
| 56 |
/* create the low and high priority threads */ |
| 57 |
|
| 58 |
low = PR_CreateThread(PR_USER_THREAD, lowPriority, 0, PR_PRIORITY_LOW, scope1, |
| 59 |
PR_JOINABLE_THREAD, 0); |
| 60 |
if (!low) { |
| 61 |
if (debug_mode) { |
| 62 |
printf("\tcannot create low priority thread\n"); |
| 63 |
} else { |
| 64 |
failed_already = 1; |
| 65 |
} |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
high = PR_CreateThread(PR_USER_THREAD, highPriority, 0, PR_PRIORITY_HIGH, |
| 70 |
scope2, PR_JOINABLE_THREAD, 0); |
| 71 |
if (!high) { |
| 72 |
if (debug_mode) { |
| 73 |
printf("\tcannot create high priority thread\n"); |
| 74 |
} else { |
| 75 |
failed_already = 1; |
| 76 |
} |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
/* Do the joining for both threads */ |
| 81 |
if (PR_JoinThread(low) == PR_FAILURE) { |
| 82 |
if (debug_mode) { |
| 83 |
printf("\tcannot join low priority thread\n"); |
| 84 |
} else { |
| 85 |
failed_already = 1; |
| 86 |
} |
| 87 |
return; |
| 88 |
} else { |
| 89 |
if (debug_mode) { |
| 90 |
printf("\tjoined low priority thread\n"); |
| 91 |
} |
| 92 |
} |
| 93 |
if (PR_JoinThread(high) == PR_FAILURE) { |
| 94 |
if (debug_mode) { |
| 95 |
printf("\tcannot join high priority thread\n"); |
| 96 |
} else { |
| 97 |
failed_already = 1; |
| 98 |
} |
| 99 |
return; |
| 100 |
} else { |
| 101 |
if (debug_mode) { |
| 102 |
printf("\tjoined high priority thread\n"); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
static PRIntn PR_CALLBACK RealMain(PRIntn argc, char** argv) { |
| 108 |
/* The command line argument: -d is used to determine if the test is being run |
| 109 |
in debug mode. The regress tool requires only one line output:PASS or FAIL. |
| 110 |
All of the printfs associated with this test has been handled with a if |
| 111 |
(debug_mode) test. Usage: test_name -d |
| 112 |
*/ |
| 113 |
|
| 114 |
PLOptStatus os; |
| 115 |
PLOptState* opt = PL_CreateOptState(argc, argv, "d:"); |
| 116 |
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
| 117 |
if (PL_OPT_BAD == os) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
switch (opt->option) { |
| 121 |
case 'd': /* debug mode */ |
| 122 |
debug_mode = 1; |
| 123 |
break; |
| 124 |
default: |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
PL_DestroyOptState(opt); |
| 129 |
|
| 130 |
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 131 |
PR_STDIO_INIT(); |
| 132 |
|
| 133 |
/* main test */ |
| 134 |
|
| 135 |
if (debug_mode) { |
| 136 |
printf("User-Kernel test\n"); |
| 137 |
} |
| 138 |
runTest(PR_LOCAL_THREAD, PR_GLOBAL_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 */ |