nsprpub/pr/tests/cleanup.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/cleanup.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     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 "prprf.h"
    1.10 +#include "prio.h"
    1.11 +#include "prinit.h"
    1.12 +#include "prthread.h"
    1.13 +#include "prinrval.h"
    1.14 +
    1.15 +#include "plgetopt.h"
    1.16 +
    1.17 +#include <stdlib.h>
    1.18 +
    1.19 +static void PR_CALLBACK Thread(void *sleep)
    1.20 +{
    1.21 +    PR_Sleep(PR_SecondsToInterval((PRUint32)sleep));
    1.22 +    printf("Thread exiting\n");
    1.23 +}
    1.24 +
    1.25 +static void Help(void)
    1.26 +{
    1.27 +    PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
    1.28 +    PR_fprintf(err, "Cleanup usage: [-g] [-s n] [-t n] [-c n] [-h]\n");
    1.29 +    PR_fprintf(err, "\t-c   Call cleanup before exiting     (default: false)\n");
    1.30 +    PR_fprintf(err, "\t-G   Use global threads only         (default: local)\n");
    1.31 +    PR_fprintf(err, "\t-t n Number of threads involved      (default: 1)\n");
    1.32 +    PR_fprintf(err, "\t-s n Seconds thread(s) should dally  (defaut: 10)\n");
    1.33 +    PR_fprintf(err, "\t-S n Seconds main() should dally     (defaut: 5)\n");
    1.34 +    PR_fprintf(err, "\t-C n Value to set concurrency        (default 1)\n");
    1.35 +    PR_fprintf(err, "\t-h   This message and nothing else\n");
    1.36 +}  /* Help */
    1.37 +
    1.38 +int main(int argc, char **argv)
    1.39 +{
    1.40 +    PLOptStatus os;
    1.41 +    PRBool cleanup = PR_FALSE;
    1.42 +	PRThreadScope type = PR_LOCAL_THREAD;
    1.43 +    PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
    1.44 +    PLOptState *opt = PL_CreateOptState(argc, argv, "Ghs:S:t:cC:");
    1.45 +    PRIntn concurrency = 1, child_sleep = 10, main_sleep = 5, threads = 1;
    1.46 +
    1.47 +    PR_STDIO_INIT();
    1.48 +    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    1.49 +    {
    1.50 +        if (PL_OPT_BAD == os) continue;
    1.51 +        switch (opt->option)
    1.52 +        {
    1.53 +        case 'c':  /* call PR_Cleanup() before exiting */
    1.54 +            cleanup = PR_TRUE;
    1.55 +            break;
    1.56 +        case 'G':  /* local vs global threads */
    1.57 +            type = PR_GLOBAL_THREAD;
    1.58 +            break;
    1.59 +        case 's':  /* time to sleep */
    1.60 +            child_sleep = atoi(opt->value);
    1.61 +            break;
    1.62 +        case 'S':  /* time to sleep */
    1.63 +            main_sleep = atoi(opt->value);
    1.64 +            break;
    1.65 +        case 'C':  /* number of cpus to create */
    1.66 +            concurrency = atoi(opt->value);
    1.67 +            break;
    1.68 +        case 't':  /* number of threads to create */
    1.69 +            threads = atoi(opt->value);
    1.70 +            break;
    1.71 +        case 'h':  /* user wants some guidance */
    1.72 +            Help();  /* so give him an earful */
    1.73 +            return 2;  /* but not a lot else */
    1.74 +            break;
    1.75 +         default:
    1.76 +            break;
    1.77 +        }
    1.78 +    }
    1.79 +    PL_DestroyOptState(opt);
    1.80 +
    1.81 +    PR_fprintf(err, "Cleanup settings\n");
    1.82 +    PR_fprintf(err, "\tThread type: %s\n",
    1.83 +        (PR_LOCAL_THREAD == type) ? "LOCAL" : "GLOBAL");
    1.84 +    PR_fprintf(err, "\tConcurrency: %d\n", concurrency);
    1.85 +    PR_fprintf(err, "\tNumber of threads: %d\n", threads);
    1.86 +    PR_fprintf(err, "\tThread sleep: %d\n", child_sleep);
    1.87 +    PR_fprintf(err, "\tMain sleep: %d\n", main_sleep); 
    1.88 +    PR_fprintf(err, "\tCleanup will %sbe called\n\n", (cleanup) ? "" : "NOT "); 
    1.89 +
    1.90 +    PR_SetConcurrency(concurrency);
    1.91 +
    1.92 +	while (threads-- > 0)
    1.93 +		(void)PR_CreateThread(
    1.94 +        	PR_USER_THREAD, Thread, (void*)child_sleep, PR_PRIORITY_NORMAL,
    1.95 +       		type, PR_UNJOINABLE_THREAD, 0);
    1.96 +    PR_Sleep(PR_SecondsToInterval(main_sleep));
    1.97 +
    1.98 +    if (cleanup) PR_Cleanup();
    1.99 +
   1.100 +    PR_fprintf(err, "main() exiting\n");
   1.101 +    return 0;
   1.102 +}  /* main */

mercurial