tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cleanup.c (3292B)


      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 "prprf.h"
      7 #include "prio.h"
      8 #include "prinit.h"
      9 #include "prthread.h"
     10 #include "prinrval.h"
     11 
     12 #include "plgetopt.h"
     13 
     14 #include <stdlib.h>
     15 
     16 static void PR_CALLBACK Thread(void* sleep) {
     17  PR_Sleep(PR_SecondsToInterval((PRUint32)sleep));
     18  printf("Thread exiting\n");
     19 }
     20 
     21 static void Help(void) {
     22  PRFileDesc* err = PR_GetSpecialFD(PR_StandardError);
     23  PR_fprintf(err, "Cleanup usage: [-g] [-s n] [-t n] [-c n] [-h]\n");
     24  PR_fprintf(err, "\t-c   Call cleanup before exiting     (default: false)\n");
     25  PR_fprintf(err, "\t-G   Use global threads only         (default: local)\n");
     26  PR_fprintf(err, "\t-t n Number of threads involved      (default: 1)\n");
     27  PR_fprintf(err, "\t-s n Seconds thread(s) should dally  (defaut: 10)\n");
     28  PR_fprintf(err, "\t-S n Seconds main() should dally     (defaut: 5)\n");
     29  PR_fprintf(err, "\t-C n Value to set concurrency        (default 1)\n");
     30  PR_fprintf(err, "\t-h   This message and nothing else\n");
     31 } /* Help */
     32 
     33 int main(int argc, char** argv) {
     34  PLOptStatus os;
     35  PRBool cleanup = PR_FALSE;
     36  PRThreadScope type = PR_LOCAL_THREAD;
     37  PRFileDesc* err = PR_GetSpecialFD(PR_StandardError);
     38  PLOptState* opt = PL_CreateOptState(argc, argv, "Ghs:S:t:cC:");
     39  PRIntn concurrency = 1, child_sleep = 10, main_sleep = 5, threads = 1;
     40 
     41  while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     42    if (PL_OPT_BAD == os) {
     43      continue;
     44    }
     45    switch (opt->option) {
     46      case 'c': /* call PR_Cleanup() before exiting */
     47        cleanup = PR_TRUE;
     48        break;
     49      case 'G': /* local vs global threads */
     50        type = PR_GLOBAL_THREAD;
     51        break;
     52      case 's': /* time to sleep */
     53        child_sleep = atoi(opt->value);
     54        break;
     55      case 'S': /* time to sleep */
     56        main_sleep = atoi(opt->value);
     57        break;
     58      case 'C': /* number of cpus to create */
     59        concurrency = atoi(opt->value);
     60        break;
     61      case 't': /* number of threads to create */
     62        threads = atoi(opt->value);
     63        break;
     64      case 'h':   /* user wants some guidance */
     65        Help();   /* so give him an earful */
     66        return 2; /* but not a lot else */
     67        break;
     68      default:
     69        break;
     70    }
     71  }
     72  PL_DestroyOptState(opt);
     73 
     74  PR_fprintf(err, "Cleanup settings\n");
     75  PR_fprintf(err, "\tThread type: %s\n",
     76             (PR_LOCAL_THREAD == type) ? "LOCAL" : "GLOBAL");
     77  PR_fprintf(err, "\tConcurrency: %d\n", concurrency);
     78  PR_fprintf(err, "\tNumber of threads: %d\n", threads);
     79  PR_fprintf(err, "\tThread sleep: %d\n", child_sleep);
     80  PR_fprintf(err, "\tMain sleep: %d\n", main_sleep);
     81  PR_fprintf(err, "\tCleanup will %sbe called\n\n", (cleanup) ? "" : "NOT ");
     82 
     83  PR_SetConcurrency(concurrency);
     84 
     85  while (threads-- > 0)
     86    (void)PR_CreateThread(PR_USER_THREAD, Thread, (void*)child_sleep,
     87                          PR_PRIORITY_NORMAL, type, PR_UNJOINABLE_THREAD, 0);
     88  PR_Sleep(PR_SecondsToInterval(main_sleep));
     89 
     90  if (cleanup) {
     91    PR_Cleanup();
     92  }
     93 
     94  PR_fprintf(err, "main() exiting\n");
     95  return 0;
     96 } /* main */