home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / concur.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.1 KB  |  164 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. /*
  20. ** File:            concur.c 
  21. ** Description:     test of adding and removing concurrency options
  22. */
  23.  
  24. #include "prcvar.h"
  25. #include "prinit.h"
  26. #include "prinrval.h"
  27. #include "prlock.h"
  28. #include "prprf.h"
  29.  
  30. #include "plgetopt.h"
  31.  
  32. #if defined(XP_MAC)
  33. #include "pprio.h"
  34. #else
  35. #include "private/pprio.h"
  36. #endif
  37.  
  38. #include <stdlib.h>
  39.  
  40. #define DEFAULT_RANGE 20
  41. #define DEFAULT_LOOPS 100
  42.  
  43. static PRThreadScope thread_scope = PR_LOCAL_THREAD;
  44.  
  45. typedef struct Context
  46. {
  47.     PRLock *ml;
  48.     PRCondVar *cv;
  49.     PRIntn want, have;
  50. } Context;
  51.  
  52.  
  53. /*
  54. ** Make the instance of 'context' static (not on the stack)
  55. ** for Win16 threads
  56. */
  57. static Context context = {NULL, NULL, 0, 0};
  58.  
  59. static void PR_CALLBACK Dull(void *arg)
  60. {
  61.     Context *context = (Context*)arg;
  62.     PR_Lock(context->ml);
  63.     context->have += 1;
  64.     while (context->want >= context->have)
  65.         PR_WaitCondVar(context->cv, PR_INTERVAL_NO_TIMEOUT);
  66.     context->have -= 1;
  67.     PR_Unlock(context->ml);
  68. }  /* Dull */
  69.  
  70. PRIntn PR_CALLBACK Concur(PRIntn argc, char **argv)
  71. {
  72.     PRUintn cpus;
  73.     PLOptStatus os;
  74.     PRBool debug = PR_FALSE;
  75.     PRUintn range = DEFAULT_RANGE;
  76.     PRUintn loops = DEFAULT_LOOPS;
  77.     PRIntervalTime hundredMills = PR_MillisecondsToInterval(100);
  78.     PLOptState *opt = PL_CreateOptState(argc, argv, "Gdl:r:");
  79.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  80.     {
  81.         if (PL_OPT_BAD == os) continue;
  82.         switch (opt->option)
  83.         {
  84.         case 'G':  /* GLOBAL threads */
  85.             thread_scope = PR_GLOBAL_THREAD;
  86.             break;
  87.         case 'd':  /* debug mode */
  88.             debug = PR_TRUE;
  89.             break;
  90.         case 'r':  /* range limit */
  91.             range = atoi(opt->value);
  92.             break;
  93.         case 'l':  /* loop counter */
  94.             loops = atoi(opt->value);
  95.             break;
  96.          default:
  97.             break;
  98.         }
  99.     }
  100.     PL_DestroyOptState(opt);
  101.  
  102.     if (0 == range) range = DEFAULT_RANGE;
  103.     if (0 == loops) loops = DEFAULT_LOOPS;
  104.  
  105.     context.ml = PR_NewLock();
  106.     context.cv = PR_NewCondVar(context.ml);
  107.  
  108.     if (debug)
  109.         PR_fprintf(
  110.             PR_STDERR, "Testing with %d CPUs and %d interations\n", range, loops);
  111.  
  112.     while (--loops > 0)
  113.     {
  114.         for (cpus = 1; cpus < range; ++cpus)
  115.         {
  116.             PR_SetConcurrency(cpus);
  117.             context.want = cpus;
  118.  
  119.             (void)PR_CreateThread(
  120.                 PR_USER_THREAD, Dull, &context, PR_PRIORITY_NORMAL,
  121.                       thread_scope, PR_UNJOINABLE_THREAD, 0);
  122.         }
  123.  
  124.         PR_Sleep(hundredMills);
  125.  
  126.         for (cpus = range; cpus > 0; cpus--)
  127.         {
  128.             PR_SetConcurrency(cpus);
  129.             context.want = cpus - 1;
  130.  
  131.             PR_Lock(context.ml);
  132.             PR_NotifyCondVar(context.cv);
  133.             PR_Unlock(context.ml);
  134.         }
  135.     }
  136.  
  137.     
  138.     if (debug)
  139.         PR_fprintf(
  140.             PR_STDERR, "Waiting for %d thread(s) to exit\n", context.have);
  141.  
  142.     while (context.have > 0) PR_Sleep(hundredMills);
  143.  
  144.     if (debug)
  145.         PR_fprintf(
  146.             PR_STDERR, "Finished [want: %d, have: %d]\n",
  147.             context.want, context.have);
  148.  
  149.     PR_DestroyLock(context.ml);
  150.     PR_DestroyCondVar(context.cv);
  151.  
  152.     PR_fprintf(PR_STDERR, "PASSED\n");
  153.  
  154.     return 0;
  155. } /* Concur */
  156.  
  157. PRIntn main(PRIntn argc, char **argv)
  158. {
  159.     PR_STDIO_INIT();
  160.     return PR_Initialize(Concur, argc, argv, 0);
  161. }  /* main */
  162.  
  163. /* concur.c */
  164.