nsprpub/pr/tests/exit.c

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

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

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial