home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / cleanup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.0 KB  |  113 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "prprf.h"
  20. #include "prio.h"
  21. #include "prinit.h"
  22. #include "prthread.h"
  23. #include "prinrval.h"
  24.  
  25. #include "plgetopt.h"
  26.  
  27. #include <stdlib.h>
  28.  
  29. static void PR_CALLBACK Thread(void *sleep)
  30. {
  31.     PR_Sleep(PR_SecondsToInterval((PRUint32)sleep));
  32.     printf("Thread exiting\n");
  33. }
  34.  
  35. static void Help(void)
  36. {
  37.     PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
  38.     PR_fprintf(err, "Cleanup usage: [-g] [-s n] [-t n] [-c n] [-h]\n");
  39.     PR_fprintf(err, "\t-c   Call cleanup before exiting     (default: false)\n");
  40.     PR_fprintf(err, "\t-G   Use global threads only         (default: local)\n");
  41.     PR_fprintf(err, "\t-t n Number of threads involved      (default: 1)\n");
  42.     PR_fprintf(err, "\t-s n Seconds thread(s) should dally  (defaut: 10)\n");
  43.     PR_fprintf(err, "\t-S n Seconds main() should dally     (defaut: 5)\n");
  44.     PR_fprintf(err, "\t-C n Value to set concurrency        (default 1)\n");
  45.     PR_fprintf(err, "\t-h   This message and nothing else\n");
  46. }  /* Help */
  47.  
  48. PRIntn main(PRIntn argc, char **argv)
  49. {
  50.     PLOptStatus os;
  51.     PRBool cleanup = PR_FALSE;
  52.     PRThreadScope type = PR_LOCAL_THREAD;
  53.     PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
  54.     PLOptState *opt = PL_CreateOptState(argc, argv, "Ghs:S:t:cC:");
  55.     PRIntn concurrency = 1, child_sleep = 10, main_sleep = 5, threads = 1;
  56.  
  57.     PR_STDIO_INIT();
  58.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  59.     {
  60.         if (PL_OPT_BAD == os) continue;
  61.         switch (opt->option)
  62.         {
  63.         case 'c':  /* call PR_Cleanup() before exiting */
  64.             cleanup = PR_TRUE;
  65.             break;
  66.         case 'G':  /* local vs global threads */
  67.             type = PR_GLOBAL_THREAD;
  68.             break;
  69.         case 's':  /* time to sleep */
  70.             child_sleep = atoi(opt->value);
  71.             break;
  72.         case 'S':  /* time to sleep */
  73.             main_sleep = atoi(opt->value);
  74.             break;
  75.         case 'C':  /* number of cpus to create */
  76.             concurrency = atoi(opt->value);
  77.             break;
  78.         case 't':  /* number of threads to create */
  79.             threads = atoi(opt->value);
  80.             break;
  81.         case 'h':  /* user wants some guidance */
  82.             Help();  /* so give him an earful */
  83.             return 2;  /* but not a lot else */
  84.             break;
  85.          default:
  86.             break;
  87.         }
  88.     }
  89.     PL_DestroyOptState(opt);
  90.  
  91.     PR_fprintf(err, "Cleanup settings\n");
  92.     PR_fprintf(err, "\tThread type: %s\n",
  93.         (PR_LOCAL_THREAD == type) ? "LOCAL" : "GLOBAL");
  94.     PR_fprintf(err, "\tConcurrency: %d\n", concurrency);
  95.     PR_fprintf(err, "\tNumber of threads: %d\n", threads);
  96.     PR_fprintf(err, "\tThread sleep: %d\n", child_sleep);
  97.     PR_fprintf(err, "\tMain sleep: %d\n", main_sleep); 
  98.     PR_fprintf(err, "\tCleanup will %sbe called\n\n", (cleanup) ? "" : "NOT "); 
  99.  
  100.     PR_SetConcurrency(concurrency);
  101.  
  102.     while (threads-- > 0)
  103.         (void)PR_CreateThread(
  104.             PR_USER_THREAD, Thread, (void*)child_sleep, PR_PRIORITY_NORMAL,
  105.                type, PR_UNJOINABLE_THREAD, 0);
  106.     PR_Sleep(PR_SecondsToInterval(main_sleep));
  107.  
  108.     if (cleanup) PR_Cleanup();
  109.  
  110.     PR_fprintf(err, "main() exiting\n");
  111.     return 0;
  112. }  /* main */
  113.