michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "prprf.h" michael@0: #include "prio.h" michael@0: #include "prinit.h" michael@0: #include "prthread.h" michael@0: #include "prinrval.h" michael@0: michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: michael@0: static void PR_CALLBACK Thread(void *sleep) michael@0: { michael@0: PR_Sleep(PR_SecondsToInterval((PRUint32)sleep)); michael@0: printf("Thread exiting\n"); michael@0: } michael@0: michael@0: static void Help(void) michael@0: { michael@0: PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); michael@0: PR_fprintf(err, "Cleanup usage: [-g] [-s n] [-t n] [-c n] [-h]\n"); michael@0: PR_fprintf(err, "\t-c Call cleanup before exiting (default: false)\n"); michael@0: PR_fprintf(err, "\t-G Use global threads only (default: local)\n"); michael@0: PR_fprintf(err, "\t-t n Number of threads involved (default: 1)\n"); michael@0: PR_fprintf(err, "\t-s n Seconds thread(s) should dally (defaut: 10)\n"); michael@0: PR_fprintf(err, "\t-S n Seconds main() should dally (defaut: 5)\n"); michael@0: PR_fprintf(err, "\t-C n Value to set concurrency (default 1)\n"); michael@0: PR_fprintf(err, "\t-h This message and nothing else\n"); michael@0: } /* Help */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PLOptStatus os; michael@0: PRBool cleanup = PR_FALSE; michael@0: PRThreadScope type = PR_LOCAL_THREAD; michael@0: PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "Ghs:S:t:cC:"); michael@0: PRIntn concurrency = 1, child_sleep = 10, main_sleep = 5, threads = 1; michael@0: michael@0: PR_STDIO_INIT(); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'c': /* call PR_Cleanup() before exiting */ michael@0: cleanup = PR_TRUE; michael@0: break; michael@0: case 'G': /* local vs global threads */ michael@0: type = PR_GLOBAL_THREAD; michael@0: break; michael@0: case 's': /* time to sleep */ michael@0: child_sleep = atoi(opt->value); michael@0: break; michael@0: case 'S': /* time to sleep */ michael@0: main_sleep = atoi(opt->value); michael@0: break; michael@0: case 'C': /* number of cpus to create */ michael@0: concurrency = atoi(opt->value); michael@0: break; michael@0: case 't': /* number of threads to create */ michael@0: threads = atoi(opt->value); michael@0: break; michael@0: case 'h': /* user wants some guidance */ michael@0: Help(); /* so give him an earful */ michael@0: return 2; /* but not a lot else */ michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: PR_fprintf(err, "Cleanup settings\n"); michael@0: PR_fprintf(err, "\tThread type: %s\n", michael@0: (PR_LOCAL_THREAD == type) ? "LOCAL" : "GLOBAL"); michael@0: PR_fprintf(err, "\tConcurrency: %d\n", concurrency); michael@0: PR_fprintf(err, "\tNumber of threads: %d\n", threads); michael@0: PR_fprintf(err, "\tThread sleep: %d\n", child_sleep); michael@0: PR_fprintf(err, "\tMain sleep: %d\n", main_sleep); michael@0: PR_fprintf(err, "\tCleanup will %sbe called\n\n", (cleanup) ? "" : "NOT "); michael@0: michael@0: PR_SetConcurrency(concurrency); michael@0: michael@0: while (threads-- > 0) michael@0: (void)PR_CreateThread( michael@0: PR_USER_THREAD, Thread, (void*)child_sleep, PR_PRIORITY_NORMAL, michael@0: type, PR_UNJOINABLE_THREAD, 0); michael@0: PR_Sleep(PR_SecondsToInterval(main_sleep)); michael@0: michael@0: if (cleanup) PR_Cleanup(); michael@0: michael@0: PR_fprintf(err, "main() exiting\n"); michael@0: return 0; michael@0: } /* main */