1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/join.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,220 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/*********************************************************************** 1.10 +** 1.11 +** Name: dbmalloc1.c 1.12 +** 1.13 +** Description: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions. 1.14 +** 1.15 +** Modification History: 1.16 +** 1.17 +** 19-May-97 AGarcia - separate the four join tests into different unit test modules. 1.18 +** AGarcia- Converted the test to accomodate the debug_mode flag. 1.19 +** The debug mode will print all of the printfs associated with this test. 1.20 +** The regress mode will be the default mode. Since the regress tool limits 1.21 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.22 +** have been handled with an if (debug_mode) statement. 1.23 +***********************************************************************/ 1.24 + 1.25 +/*********************************************************************** 1.26 +** Includes 1.27 +***********************************************************************/ 1.28 +/* Used to get the command line option */ 1.29 +#include "plgetopt.h" 1.30 +#include "prttools.h" 1.31 + 1.32 +#include "nspr.h" 1.33 + 1.34 +#include <stdio.h> 1.35 +#include <stdlib.h> 1.36 +#include <string.h> 1.37 + 1.38 +/*********************************************************************** 1.39 +** PRIVATE FUNCTION: Test_Result 1.40 +** DESCRIPTION: Used in conjunction with the regress tool, prints out the 1.41 +** status of the test case. 1.42 +** INPUTS: PASS/FAIL 1.43 +** OUTPUTS: None 1.44 +** RETURN: None 1.45 +** SIDE EFFECTS: 1.46 +** 1.47 +** RESTRICTIONS: 1.48 +** None 1.49 +** MEMORY: NA 1.50 +** ALGORITHM: Determine what the status is and print accordingly. 1.51 +** 1.52 +***********************************************************************/ 1.53 + 1.54 + 1.55 +static void Test_Result (int result) 1.56 +{ 1.57 + if (result == PASS) 1.58 + printf ("PASS\n"); 1.59 + else 1.60 + printf ("FAIL\n"); 1.61 + exit (1); 1.62 +} 1.63 + 1.64 + 1.65 +/* 1.66 + Program to test joining of threads. Two threads are created. One 1.67 + to be waited upon until it has started. The other to join after it has 1.68 + completed. 1.69 +*/ 1.70 + 1.71 + 1.72 +static void PR_CALLBACK lowPriority(void *arg) 1.73 +{ 1.74 +} 1.75 + 1.76 +static void PR_CALLBACK highPriority(void *arg) 1.77 +{ 1.78 +} 1.79 + 1.80 +static void PR_CALLBACK unjoinable(void *arg) 1.81 +{ 1.82 + PR_Sleep(PR_INTERVAL_NO_TIMEOUT); 1.83 +} 1.84 + 1.85 +void runTest(PRThreadScope scope1, PRThreadScope scope2) 1.86 +{ 1.87 + PRThread *low,*high; 1.88 + 1.89 + /* create the low and high priority threads */ 1.90 + 1.91 + low = PR_CreateThread(PR_USER_THREAD, 1.92 + lowPriority, 0, 1.93 + PR_PRIORITY_LOW, 1.94 + scope1, 1.95 + PR_JOINABLE_THREAD, 1.96 + 0); 1.97 + if (!low) { 1.98 + if (debug_mode) printf("\tcannot create low priority thread\n"); 1.99 + else Test_Result(FAIL); 1.100 + return; 1.101 + } 1.102 + 1.103 + high = PR_CreateThread(PR_USER_THREAD, 1.104 + highPriority, 0, 1.105 + PR_PRIORITY_HIGH, 1.106 + scope2, 1.107 + PR_JOINABLE_THREAD, 1.108 + 0); 1.109 + if (!high) { 1.110 + if (debug_mode) printf("\tcannot create high priority thread\n"); 1.111 + else Test_Result(FAIL); 1.112 + return; 1.113 + } 1.114 + 1.115 + /* Do the joining for both threads */ 1.116 + if (PR_JoinThread(low) == PR_FAILURE) { 1.117 + if (debug_mode) printf("\tcannot join low priority thread\n"); 1.118 + else Test_Result (FAIL); 1.119 + return; 1.120 + } else { 1.121 + if (debug_mode) printf("\tjoined low priority thread\n"); 1.122 + } 1.123 + if (PR_JoinThread(high) == PR_FAILURE) { 1.124 + if (debug_mode) printf("\tcannot join high priority thread\n"); 1.125 + else Test_Result(FAIL); 1.126 + return; 1.127 + } else { 1.128 + if (debug_mode) printf("\tjoined high priority thread\n"); 1.129 + } 1.130 +} 1.131 + 1.132 +void joinWithUnjoinable(void) 1.133 +{ 1.134 + PRThread *thread; 1.135 + 1.136 + /* create the unjoinable thread */ 1.137 + 1.138 + thread = PR_CreateThread(PR_USER_THREAD, 1.139 + unjoinable, 0, 1.140 + PR_PRIORITY_NORMAL, 1.141 + PR_GLOBAL_THREAD, 1.142 + PR_UNJOINABLE_THREAD, 1.143 + 0); 1.144 + if (!thread) { 1.145 + if (debug_mode) printf("\tcannot create unjoinable thread\n"); 1.146 + else Test_Result(FAIL); 1.147 + return; 1.148 + } 1.149 + 1.150 + if (PR_JoinThread(thread) == PR_SUCCESS) { 1.151 + if (debug_mode) printf("\tsuccessfully joined with unjoinable thread?!\n"); 1.152 + else Test_Result(FAIL); 1.153 + return; 1.154 + } else { 1.155 + if (debug_mode) printf("\tcannot join with unjoinable thread, as expected\n"); 1.156 + if (PR_GetError() != PR_INVALID_ARGUMENT_ERROR) { 1.157 + if (debug_mode) printf("\tWrong error code\n"); 1.158 + else Test_Result(FAIL); 1.159 + return; 1.160 + } 1.161 + } 1.162 + if (PR_Interrupt(thread) == PR_FAILURE) { 1.163 + if (debug_mode) printf("\tcannot interrupt unjoinable thread\n"); 1.164 + else Test_Result(FAIL); 1.165 + return; 1.166 + } else { 1.167 + if (debug_mode) printf("\tinterrupted unjoinable thread\n"); 1.168 + } 1.169 +} 1.170 + 1.171 +static PRIntn PR_CALLBACK RealMain(int argc, char **argv) 1.172 +{ 1.173 + /* The command line argument: -d is used to determine if the test is being run 1.174 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.175 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.176 + test. 1.177 + Usage: test_name -d 1.178 + */ 1.179 + 1.180 + PLOptStatus os; 1.181 + PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 1.182 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.183 + { 1.184 + if (PL_OPT_BAD == os) continue; 1.185 + switch (opt->option) 1.186 + { 1.187 + case 'd': /* debug mode */ 1.188 + debug_mode = 1; 1.189 + break; 1.190 + default: 1.191 + break; 1.192 + } 1.193 + } 1.194 + PL_DestroyOptState(opt); 1.195 + 1.196 + /* main test */ 1.197 + printf("User-User test\n"); 1.198 + runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD); 1.199 + printf("User-Kernel test\n"); 1.200 + runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD); 1.201 + printf("Kernel-User test\n"); 1.202 + runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD); 1.203 + printf("Kernel-Kernel test\n"); 1.204 + runTest(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD); 1.205 + printf("Join with unjoinable thread\n"); 1.206 + joinWithUnjoinable(); 1.207 + 1.208 + printf("PASSED\n"); 1.209 + 1.210 + return 0; 1.211 +} 1.212 + 1.213 + 1.214 + 1.215 + 1.216 +int main(int argc, char **argv) 1.217 +{ 1.218 + PRIntn rv; 1.219 + 1.220 + PR_STDIO_INIT(); 1.221 + rv = PR_Initialize(RealMain, argc, argv, 0); 1.222 + return rv; 1.223 +} /* main */