1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/exit.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 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 +#include "prio.h" 1.10 +#include "prprf.h" 1.11 +#include "prinit.h" 1.12 +#include "prthread.h" 1.13 +#include "prproces.h" 1.14 +#include "prinrval.h" 1.15 + 1.16 +#include "plgetopt.h" 1.17 + 1.18 +#include <stdlib.h> 1.19 + 1.20 +static PRInt32 dally = 0; 1.21 +static PRFileDesc *err = NULL; 1.22 +static PRBool verbose = PR_FALSE, force = PR_FALSE; 1.23 + 1.24 +static void Help(void) 1.25 +{ 1.26 + PR_fprintf(err, "Usage: [-t s] [-h]\n"); 1.27 + PR_fprintf(err, "\t-d Verbose output (default: FALSE)\n"); 1.28 + PR_fprintf(err, "\t-x Forced termination (default: FALSE)\n"); 1.29 + PR_fprintf(err, "\t-t Time for thread to block (default: 10 seconds)\n"); 1.30 + PR_fprintf(err, "\t-h This message and nothing else\n"); 1.31 +} /* Help */ 1.32 + 1.33 +static void Dull(void *arg) 1.34 +{ 1.35 + PR_Sleep(PR_SecondsToInterval(dally)); 1.36 + if (verbose && force) 1.37 + PR_fprintf(err, "If you see this, the test failed\n"); 1.38 +} /* Dull */ 1.39 + 1.40 +static PRIntn PR_CALLBACK RealMain(PRIntn argc, char **argv) 1.41 +{ 1.42 + PLOptStatus os; 1.43 + PLOptState *opt = PL_CreateOptState(argc, argv, "ht:dx"); 1.44 + 1.45 + err = PR_GetSpecialFD(PR_StandardError); 1.46 + 1.47 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.48 + { 1.49 + if (PL_OPT_BAD == os) continue; 1.50 + switch (opt->option) 1.51 + { 1.52 + case 'd': /* verbosity */ 1.53 + verbose = PR_TRUE; 1.54 + break; 1.55 + case 'x': /* force exit */ 1.56 + force = PR_TRUE; 1.57 + break; 1.58 + case 't': /* seconds to dally in child */ 1.59 + dally = atoi(opt->value); 1.60 + break; 1.61 + case 'h': /* user wants some guidance */ 1.62 + default: 1.63 + Help(); /* so give him an earful */ 1.64 + return 2; /* but not a lot else */ 1.65 + } 1.66 + } 1.67 + PL_DestroyOptState(opt); 1.68 + 1.69 + if (0 == dally) dally = 10; 1.70 + 1.71 + /* 1.72 + * Create LOCAL and GLOBAL threads 1.73 + */ 1.74 + (void)PR_CreateThread( 1.75 + PR_USER_THREAD, Dull, NULL, PR_PRIORITY_NORMAL, 1.76 + PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); 1.77 + 1.78 + (void)PR_CreateThread( 1.79 + PR_USER_THREAD, Dull, NULL, PR_PRIORITY_NORMAL, 1.80 + PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0); 1.81 + 1.82 + if (verbose) 1.83 + PR_fprintf( 1.84 + err, "Main is exiting now. Program should exit %s.\n", 1.85 + (force) ? "immediately" : "after child dally time"); 1.86 + 1.87 + if (force) 1.88 + { 1.89 + PR_ProcessExit(0); 1.90 + if (verbose) 1.91 + { 1.92 + PR_fprintf(err, "You should not have gotten here.\n"); 1.93 + return 1; 1.94 + } 1.95 + } 1.96 + return 0; 1.97 + 1.98 +} 1.99 + 1.100 + 1.101 +int main(int argc, char **argv) 1.102 +{ 1.103 + PRIntn rv; 1.104 + 1.105 + PR_STDIO_INIT(); 1.106 + rv = PR_Initialize(RealMain, argc, argv, 0); 1.107 + return rv; 1.108 +} /* main */