nsprpub/pr/tests/join.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /***********************************************************************
michael@0 7 **
michael@0 8 ** Name: dbmalloc1.c
michael@0 9 **
michael@0 10 ** Description: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions.
michael@0 11 **
michael@0 12 ** Modification History:
michael@0 13 **
michael@0 14 ** 19-May-97 AGarcia - separate the four join tests into different unit test modules.
michael@0 15 ** AGarcia- Converted the test to accomodate the debug_mode flag.
michael@0 16 ** The debug mode will print all of the printfs associated with this test.
michael@0 17 ** The regress mode will be the default mode. Since the regress tool limits
michael@0 18 ** the output to a one line status:PASS or FAIL,all of the printf statements
michael@0 19 ** have been handled with an if (debug_mode) statement.
michael@0 20 ***********************************************************************/
michael@0 21
michael@0 22 /***********************************************************************
michael@0 23 ** Includes
michael@0 24 ***********************************************************************/
michael@0 25 /* Used to get the command line option */
michael@0 26 #include "plgetopt.h"
michael@0 27 #include "prttools.h"
michael@0 28
michael@0 29 #include "nspr.h"
michael@0 30
michael@0 31 #include <stdio.h>
michael@0 32 #include <stdlib.h>
michael@0 33 #include <string.h>
michael@0 34
michael@0 35 /***********************************************************************
michael@0 36 ** PRIVATE FUNCTION: Test_Result
michael@0 37 ** DESCRIPTION: Used in conjunction with the regress tool, prints out the
michael@0 38 ** status of the test case.
michael@0 39 ** INPUTS: PASS/FAIL
michael@0 40 ** OUTPUTS: None
michael@0 41 ** RETURN: None
michael@0 42 ** SIDE EFFECTS:
michael@0 43 **
michael@0 44 ** RESTRICTIONS:
michael@0 45 ** None
michael@0 46 ** MEMORY: NA
michael@0 47 ** ALGORITHM: Determine what the status is and print accordingly.
michael@0 48 **
michael@0 49 ***********************************************************************/
michael@0 50
michael@0 51
michael@0 52 static void Test_Result (int result)
michael@0 53 {
michael@0 54 if (result == PASS)
michael@0 55 printf ("PASS\n");
michael@0 56 else
michael@0 57 printf ("FAIL\n");
michael@0 58 exit (1);
michael@0 59 }
michael@0 60
michael@0 61
michael@0 62 /*
michael@0 63 Program to test joining of threads. Two threads are created. One
michael@0 64 to be waited upon until it has started. The other to join after it has
michael@0 65 completed.
michael@0 66 */
michael@0 67
michael@0 68
michael@0 69 static void PR_CALLBACK lowPriority(void *arg)
michael@0 70 {
michael@0 71 }
michael@0 72
michael@0 73 static void PR_CALLBACK highPriority(void *arg)
michael@0 74 {
michael@0 75 }
michael@0 76
michael@0 77 static void PR_CALLBACK unjoinable(void *arg)
michael@0 78 {
michael@0 79 PR_Sleep(PR_INTERVAL_NO_TIMEOUT);
michael@0 80 }
michael@0 81
michael@0 82 void runTest(PRThreadScope scope1, PRThreadScope scope2)
michael@0 83 {
michael@0 84 PRThread *low,*high;
michael@0 85
michael@0 86 /* create the low and high priority threads */
michael@0 87
michael@0 88 low = PR_CreateThread(PR_USER_THREAD,
michael@0 89 lowPriority, 0,
michael@0 90 PR_PRIORITY_LOW,
michael@0 91 scope1,
michael@0 92 PR_JOINABLE_THREAD,
michael@0 93 0);
michael@0 94 if (!low) {
michael@0 95 if (debug_mode) printf("\tcannot create low priority thread\n");
michael@0 96 else Test_Result(FAIL);
michael@0 97 return;
michael@0 98 }
michael@0 99
michael@0 100 high = PR_CreateThread(PR_USER_THREAD,
michael@0 101 highPriority, 0,
michael@0 102 PR_PRIORITY_HIGH,
michael@0 103 scope2,
michael@0 104 PR_JOINABLE_THREAD,
michael@0 105 0);
michael@0 106 if (!high) {
michael@0 107 if (debug_mode) printf("\tcannot create high priority thread\n");
michael@0 108 else Test_Result(FAIL);
michael@0 109 return;
michael@0 110 }
michael@0 111
michael@0 112 /* Do the joining for both threads */
michael@0 113 if (PR_JoinThread(low) == PR_FAILURE) {
michael@0 114 if (debug_mode) printf("\tcannot join low priority thread\n");
michael@0 115 else Test_Result (FAIL);
michael@0 116 return;
michael@0 117 } else {
michael@0 118 if (debug_mode) printf("\tjoined low priority thread\n");
michael@0 119 }
michael@0 120 if (PR_JoinThread(high) == PR_FAILURE) {
michael@0 121 if (debug_mode) printf("\tcannot join high priority thread\n");
michael@0 122 else Test_Result(FAIL);
michael@0 123 return;
michael@0 124 } else {
michael@0 125 if (debug_mode) printf("\tjoined high priority thread\n");
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 void joinWithUnjoinable(void)
michael@0 130 {
michael@0 131 PRThread *thread;
michael@0 132
michael@0 133 /* create the unjoinable thread */
michael@0 134
michael@0 135 thread = PR_CreateThread(PR_USER_THREAD,
michael@0 136 unjoinable, 0,
michael@0 137 PR_PRIORITY_NORMAL,
michael@0 138 PR_GLOBAL_THREAD,
michael@0 139 PR_UNJOINABLE_THREAD,
michael@0 140 0);
michael@0 141 if (!thread) {
michael@0 142 if (debug_mode) printf("\tcannot create unjoinable thread\n");
michael@0 143 else Test_Result(FAIL);
michael@0 144 return;
michael@0 145 }
michael@0 146
michael@0 147 if (PR_JoinThread(thread) == PR_SUCCESS) {
michael@0 148 if (debug_mode) printf("\tsuccessfully joined with unjoinable thread?!\n");
michael@0 149 else Test_Result(FAIL);
michael@0 150 return;
michael@0 151 } else {
michael@0 152 if (debug_mode) printf("\tcannot join with unjoinable thread, as expected\n");
michael@0 153 if (PR_GetError() != PR_INVALID_ARGUMENT_ERROR) {
michael@0 154 if (debug_mode) printf("\tWrong error code\n");
michael@0 155 else Test_Result(FAIL);
michael@0 156 return;
michael@0 157 }
michael@0 158 }
michael@0 159 if (PR_Interrupt(thread) == PR_FAILURE) {
michael@0 160 if (debug_mode) printf("\tcannot interrupt unjoinable thread\n");
michael@0 161 else Test_Result(FAIL);
michael@0 162 return;
michael@0 163 } else {
michael@0 164 if (debug_mode) printf("\tinterrupted unjoinable thread\n");
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168 static PRIntn PR_CALLBACK RealMain(int argc, char **argv)
michael@0 169 {
michael@0 170 /* The command line argument: -d is used to determine if the test is being run
michael@0 171 in debug mode. The regress tool requires only one line output:PASS or FAIL.
michael@0 172 All of the printfs associated with this test has been handled with a if (debug_mode)
michael@0 173 test.
michael@0 174 Usage: test_name -d
michael@0 175 */
michael@0 176
michael@0 177 PLOptStatus os;
michael@0 178 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
michael@0 179 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 180 {
michael@0 181 if (PL_OPT_BAD == os) continue;
michael@0 182 switch (opt->option)
michael@0 183 {
michael@0 184 case 'd': /* debug mode */
michael@0 185 debug_mode = 1;
michael@0 186 break;
michael@0 187 default:
michael@0 188 break;
michael@0 189 }
michael@0 190 }
michael@0 191 PL_DestroyOptState(opt);
michael@0 192
michael@0 193 /* main test */
michael@0 194 printf("User-User test\n");
michael@0 195 runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
michael@0 196 printf("User-Kernel test\n");
michael@0 197 runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
michael@0 198 printf("Kernel-User test\n");
michael@0 199 runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
michael@0 200 printf("Kernel-Kernel test\n");
michael@0 201 runTest(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
michael@0 202 printf("Join with unjoinable thread\n");
michael@0 203 joinWithUnjoinable();
michael@0 204
michael@0 205 printf("PASSED\n");
michael@0 206
michael@0 207 return 0;
michael@0 208 }
michael@0 209
michael@0 210
michael@0 211
michael@0 212
michael@0 213 int main(int argc, char **argv)
michael@0 214 {
michael@0 215 PRIntn rv;
michael@0 216
michael@0 217 PR_STDIO_INIT();
michael@0 218 rv = PR_Initialize(RealMain, argc, argv, 0);
michael@0 219 return rv;
michael@0 220 } /* main */

mercurial