| 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 |
#include "prio.h" |
| 7 |
#include "prprf.h" |
| 8 |
#include "prinit.h" |
| 9 |
#include "prthread.h" |
| 10 |
#include "prproces.h" |
| 11 |
#include "prinrval.h" |
| 12 |
|
| 13 |
#include "plgetopt.h" |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
|
| 17 |
static PRInt32 dally = 0; |
| 18 |
static PRFileDesc* err = NULL; |
| 19 |
static PRBool verbose = PR_FALSE, force = PR_FALSE; |
| 20 |
|
| 21 |
static void Help(void) { |
| 22 |
PR_fprintf(err, "Usage: [-t s] [-h]\n"); |
| 23 |
PR_fprintf(err, "\t-d Verbose output (default: FALSE)\n"); |
| 24 |
PR_fprintf(err, "\t-x Forced termination (default: FALSE)\n"); |
| 25 |
PR_fprintf(err, "\t-t Time for thread to block (default: 10 seconds)\n"); |
| 26 |
PR_fprintf(err, "\t-h This message and nothing else\n"); |
| 27 |
} /* Help */ |
| 28 |
|
| 29 |
static void Dull(void* arg) { |
| 30 |
PR_Sleep(PR_SecondsToInterval(dally)); |
| 31 |
if (verbose && force) { |
| 32 |
PR_fprintf(err, "If you see this, the test failed\n"); |
| 33 |
} |
| 34 |
} /* Dull */ |
| 35 |
|
| 36 |
static PRIntn PR_CALLBACK RealMain(PRIntn argc, char** argv) { |
| 37 |
PLOptStatus os; |
| 38 |
PLOptState* opt = PL_CreateOptState(argc, argv, "ht:dx"); |
| 39 |
|
| 40 |
err = PR_GetSpecialFD(PR_StandardError); |
| 41 |
|
| 42 |
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
| 43 |
if (PL_OPT_BAD == os) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
switch (opt->option) { |
| 47 |
case 'd': /* verbosity */ |
| 48 |
verbose = PR_TRUE; |
| 49 |
break; |
| 50 |
case 'x': /* force exit */ |
| 51 |
force = PR_TRUE; |
| 52 |
break; |
| 53 |
case 't': /* seconds to dally in child */ |
| 54 |
dally = atoi(opt->value); |
| 55 |
break; |
| 56 |
case 'h': /* user wants some guidance */ |
| 57 |
default: |
| 58 |
Help(); /* so give him an earful */ |
| 59 |
return 2; /* but not a lot else */ |
| 60 |
} |
| 61 |
} |
| 62 |
PL_DestroyOptState(opt); |
| 63 |
|
| 64 |
if (0 == dally) { |
| 65 |
dally = 10; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Create LOCAL and GLOBAL threads |
| 70 |
*/ |
| 71 |
(void)PR_CreateThread(PR_USER_THREAD, Dull, NULL, PR_PRIORITY_NORMAL, |
| 72 |
PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); |
| 73 |
|
| 74 |
(void)PR_CreateThread(PR_USER_THREAD, Dull, NULL, PR_PRIORITY_NORMAL, |
| 75 |
PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0); |
| 76 |
|
| 77 |
if (verbose) |
| 78 |
PR_fprintf(err, "Main is exiting now. Program should exit %s.\n", |
| 79 |
(force) ? "immediately" : "after child dally time"); |
| 80 |
|
| 81 |
if (force) { |
| 82 |
PR_ProcessExit(0); |
| 83 |
if (verbose) { |
| 84 |
PR_fprintf(err, "You should not have gotten here.\n"); |
| 85 |
return 1; |
| 86 |
} |
| 87 |
} |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
int main(int argc, char** argv) { |
| 92 |
PRIntn rv; |
| 93 |
|
| 94 |
PR_STDIO_INIT(); |
| 95 |
rv = PR_Initialize(RealMain, argc, argv, 0); |
| 96 |
return rv; |
| 97 |
} /* main */ |