Tcl Source Code

Hex Artifact Content
Login

Artifact 874c388a9ba18bbe5f5a1ec9f231ce142b2b8eac1fc2ade697a39cb679a9beee:

Attachment "tclMutexTest.diff" to ticket [893f8cc5db] added by chw 2025-10-06 14:43:27.
0000: 49 6e 64 65 78 3a 20 67 65 6e 65 72 69 63 2f 74  Index: generic/t
0010: 63 6c 4d 75 74 65 78 54 65 73 74 2e 63 0a 3d 3d  clMutexTest.c.==
0020: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
0030: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
0040: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
0050: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
0060: 0a 2d 2d 2d 20 67 65 6e 65 72 69 63 2f 74 63 6c  .--- generic/tcl
0070: 4d 75 74 65 78 54 65 73 74 2e 63 0a 2b 2b 2b 20  MutexTest.c.+++ 
0080: 67 65 6e 65 72 69 63 2f 74 63 6c 4d 75 74 65 78  generic/tclMutex
0090: 54 65 73 74 2e 63 0a 40 40 20 2d 32 37 2c 31 30  Test.c.@@ -27,10
00a0: 20 2b 32 37 2c 31 32 20 40 40 0a 20 2f 2a 0a 20   +27,12 @@. /*. 
00b0: 20 2a 20 54 79 70 65 73 20 72 65 6c 61 74 65 64   * Types related
00c0: 20 74 6f 20 54 63 6c 5f 4d 75 74 65 78 20 74 65   to Tcl_Mutex te
00d0: 73 74 73 2e 0a 20 20 2a 2f 0a 20 0a 20 54 43 4c  sts..  */. . TCL
00e0: 5f 44 45 43 4c 41 52 45 5f 4d 55 54 45 58 28 74  _DECLARE_MUTEX(t
00f0: 65 73 74 43 6f 6e 74 65 78 74 4d 75 74 65 78 29  estContextMutex)
0100: 0a 2b 54 43 4c 5f 44 45 43 4c 41 52 45 5f 4d 55  .+TCL_DECLARE_MU
0110: 54 45 58 28 74 68 72 65 61 64 53 74 61 72 74 4d  TEX(threadStartM
0120: 75 74 65 78 29 0a 2b 73 74 61 74 69 63 20 54 63  utex).+static Tc
0130: 6c 5f 43 6f 6e 64 69 74 69 6f 6e 20 74 68 72 65  l_Condition thre
0140: 61 64 53 74 61 72 74 43 6f 6e 64 3b 0a 20 0a 20  adStartCond;. . 
0150: 73 74 61 74 69 63 20 69 6e 6c 69 6e 65 20 76 6f  static inline vo
0160: 69 64 0a 20 4c 6f 63 6b 54 65 73 74 43 6f 6e 74  id. LockTestCont
0170: 65 78 74 28 0a 20 20 20 20 20 69 6e 74 20 6e 75  ext(.     int nu
0180: 6d 52 65 63 75 72 73 69 6f 6e 73 29 0a 20 7b 0a  mRecursions). {.
0190: 40 40 20 2d 38 37 2c 31 33 20 2b 38 39 2c 31 30  @@ -87,13 +89,10
01a0: 20 40 40 0a 20 20 20 20 20 54 63 6c 5f 54 68 72   @@.     Tcl_Thr
01b0: 65 61 64 49 64 20 74 68 72 65 61 64 49 64 3b 09  eadId threadId;.
01c0: 09 20 20 2f 2a 20 4f 6e 6c 79 20 61 63 63 65 73  .  /* Only acces
01d0: 73 20 69 6e 20 63 72 65 61 74 6f 72 20 2a 2f 0a  s in creator */.
01e0: 20 20 20 20 20 54 63 6c 5f 57 69 64 65 55 49 6e       Tcl_WideUIn
01f0: 74 20 6e 75 6d 4f 70 65 72 61 74 69 6f 6e 73 3b  t numOperations;
0200: 09 09 20 20 2f 2a 20 55 73 65 20 69 73 20 64 65  ..  /* Use is de
0210: 70 65 6e 64 65 6e 74 20 6f 6e 20 74 68 65 20 74  pendent on the t
0220: 65 73 74 20 2a 2f 0a 20 20 20 20 20 54 63 6c 5f  est */.     Tcl_
0230: 57 69 64 65 55 49 6e 74 20 74 69 6d 65 6f 75 74  WideUInt timeout
0240: 73 3b 09 09 20 20 2f 2a 20 54 69 6d 65 6f 75 74  s;..  /* Timeout
0250: 73 20 6f 6e 20 63 6f 6e 64 69 74 69 6f 6e 20 76  s on condition v
0260: 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 7d 20 4d  ariables */. } M
0270: 75 74 65 78 54 68 72 65 61 64 43 6f 6e 74 65 78  utexThreadContex
0280: 74 3b 0a 20 0a 2d 2f 2a 20 55 73 65 64 20 74 6f  t;. .-/* Used to
0290: 20 74 72 61 63 6b 20 68 6f 77 20 6d 61 6e 79 20   track how many 
02a0: 74 65 73 74 20 74 68 72 65 61 64 73 20 72 75 6e  test threads run
02b0: 6e 69 6e 67 2e 20 41 6c 73 6f 20 75 73 65 64 20  ning. Also used 
02c0: 61 73 20 74 72 69 67 67 65 72 20 2a 2f 0a 2d 73  as trigger */.-s
02d0: 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 69  tatic volatile i
02e0: 6e 74 20 6d 75 74 65 78 54 68 72 65 61 64 43 6f  nt mutexThreadCo
02f0: 75 6e 74 3b 0a 2d 0a 20 73 74 61 74 69 63 20 54  unt;.-. static T
0300: 63 6c 5f 54 68 72 65 61 64 43 72 65 61 74 65 54  cl_ThreadCreateT
0310: 79 70 65 09 43 6f 75 6e 74 65 72 54 68 72 65 61  ype.CounterThrea
0320: 64 50 72 6f 63 28 76 6f 69 64 20 2a 63 6c 69 65  dProc(void *clie
0330: 6e 74 44 61 74 61 29 3b 0a 20 73 74 61 74 69 63  ntData);. static
0340: 20 69 6e 74 09 09 09 54 65 73 74 4d 75 74 65 78   int...TestMutex
0350: 4c 6f 63 6b 28 54 63 6c 5f 49 6e 74 65 72 70 20  Lock(Tcl_Interp 
0360: 2a 69 6e 74 65 72 70 2c 0a 20 09 09 09 09 20 20  *interp,. ....  
0370: 20 20 4d 75 74 65 78 53 68 61 72 65 64 43 6f 6e    MutexSharedCon
0380: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 50 74 72  text *contextPtr
0390: 29 3b 0a 20 73 74 61 74 69 63 20 69 6e 74 09 09  );. static int..
03a0: 09 54 65 73 74 43 6f 6e 64 69 74 69 6f 6e 56 61  .TestConditionVa
03b0: 72 69 61 62 6c 65 28 54 63 6c 5f 49 6e 74 65 72  riable(Tcl_Inter
03c0: 70 20 2a 69 6e 74 65 72 70 2c 0a 20 09 09 09 09  p *interp,. ....
03d0: 20 20 20 20 4d 75 74 65 78 53 68 61 72 65 64 43      MutexSharedC
03e0: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 50  ontext *contextP
03f0: 74 72 29 3b 0a 40 40 20 2d 32 33 31 2c 31 32 20  tr);.@@ -231,12 
0400: 2b 32 33 30 2c 31 36 20 40 40 0a 20 20 20 20 20  +230,16 @@.     
0410: 4d 75 74 65 78 53 68 61 72 65 64 43 6f 6e 74 65  MutexSharedConte
0420: 78 74 20 2a 63 6f 6e 74 65 78 74 50 74 72 29 0a  xt *contextPtr).
0430: 20 7b 0a 20 20 20 20 20 4d 75 74 65 78 54 68 72   {.     MutexThr
0440: 65 61 64 43 6f 6e 74 65 78 74 20 2a 74 68 72 65  eadContext *thre
0450: 61 64 43 6f 6e 74 65 78 74 73 50 74 72 20 3d 20  adContextsPtr = 
0460: 28 4d 75 74 65 78 54 68 72 65 61 64 43 6f 6e 74  (MutexThreadCont
0470: 65 78 74 20 2a 29 0a 20 09 20 20 20 20 54 63 6c  ext *). .    Tcl
0480: 5f 41 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74  _Alloc(sizeof(*t
0490: 68 72 65 61 64 43 6f 6e 74 65 78 74 73 50 74 72  hreadContextsPtr
04a0: 29 20 2a 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e  ) * contextPtr->
04b0: 6e 75 6d 54 68 72 65 61 64 73 29 3b 0a 20 0a 2b  numThreads);. .+
04c0: 20 20 20 20 54 63 6c 5f 4d 75 74 65 78 4c 6f 63      Tcl_MutexLoc
04d0: 6b 28 26 74 68 72 65 61 64 53 74 61 72 74 4d 75  k(&threadStartMu
04e0: 74 65 78 29 3b 0a 2b 20 20 20 20 2f 2a 20 49 6e  tex);.+    /* In
04f0: 66 6c 61 74 65 20 74 68 72 65 61 64 53 74 61 72  flate threadStar
0500: 74 43 6f 6e 64 20 66 6f 72 20 6c 61 74 65 72 20  tCond for later 
0510: 2a 2f 0a 2b 20 20 20 20 54 63 6c 5f 54 69 6d 65  */.+    Tcl_Time
0520: 20 6e 75 6c 6c 54 69 6d 65 20 3d 20 7b 20 30 2c   nullTime = { 0,
0530: 20 30 20 7d 3b 0a 2b 20 20 20 20 54 63 6c 5f 43   0 };.+    Tcl_C
0540: 6f 6e 64 69 74 69 6f 6e 57 61 69 74 28 26 74 68  onditionWait(&th
0550: 72 65 61 64 53 74 61 72 74 43 6f 6e 64 2c 20 26  readStartCond, &
0560: 74 68 72 65 61 64 53 74 61 72 74 4d 75 74 65 78  threadStartMutex
0570: 2c 20 26 6e 75 6c 6c 54 69 6d 65 29 3b 0a 2b 0a  , &nullTime);.+.
0580: 20 20 20 20 20 63 6f 6e 74 65 78 74 50 74 72 2d       contextPtr-
0590: 3e 75 2e 63 6f 75 6e 74 65 72 20 3d 20 30 3b 0a  >u.counter = 0;.
05a0: 2d 20 20 20 20 6d 75 74 65 78 54 68 72 65 61 64  -    mutexThread
05b0: 43 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  Count = 0;.     
05c0: 66 6f 72 20 28 69 6e 74 20 69 20 3d 20 30 3b 20  for (int i = 0; 
05d0: 69 20 3c 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e  i < contextPtr->
05e0: 6e 75 6d 54 68 72 65 61 64 73 3b 20 69 2b 2b 29  numThreads; i++)
05f0: 20 7b 0a 20 09 74 68 72 65 61 64 43 6f 6e 74 65   {. .threadConte
0600: 78 74 73 50 74 72 5b 69 5d 2e 73 68 61 72 65 64  xtsPtr[i].shared
0610: 43 6f 6e 74 65 78 74 50 74 72 20 3d 20 63 6f 6e  ContextPtr = con
0620: 74 65 78 74 50 74 72 3b 0a 20 09 74 68 72 65 61  textPtr;. .threa
0630: 64 43 6f 6e 74 65 78 74 73 50 74 72 5b 69 5d 2e  dContextsPtr[i].
0640: 6e 75 6d 4f 70 65 72 61 74 69 6f 6e 73 20 3d 20  numOperations = 
0650: 30 3b 20 2f 2a 20 49 6e 69 74 20 74 68 6f 75 67  0; /* Init thoug
0660: 68 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 0a  h not used */. .
0670: 20 09 69 66 20 28 54 63 6c 5f 43 72 65 61 74 65   .if (Tcl_Create
0680: 54 68 72 65 61 64 28 26 74 68 72 65 61 64 43 6f  Thread(&threadCo
0690: 6e 74 65 78 74 73 50 74 72 5b 69 5d 2e 74 68 72  ntextsPtr[i].thr
06a0: 65 61 64 49 64 2c 0a 40 40 20 2d 32 34 33 2c 31  eadId,.@@ -243,1
06b0: 31 20 2b 32 34 36 2c 31 34 20 40 40 0a 20 09 09  1 +246,14 @@. ..
06c0: 43 6f 75 6e 74 65 72 54 68 72 65 61 64 50 72 6f  CounterThreadPro
06d0: 63 2c 20 26 74 68 72 65 61 64 43 6f 6e 74 65 78  c, &threadContex
06e0: 74 73 50 74 72 5b 69 5d 2c 0a 20 09 09 54 43 4c  tsPtr[i],. ..TCL
06f0: 5f 54 48 52 45 41 44 5f 53 54 41 43 4b 5f 44 45  _THREAD_STACK_DE
0700: 46 41 55 4c 54 2c 20 54 43 4c 5f 54 48 52 45 41  FAULT, TCL_THREA
0710: 44 5f 4a 4f 49 4e 41 42 4c 45 29 20 21 3d 20 54  D_JOINABLE) != T
0720: 43 4c 5f 4f 4b 29 20 7b 0a 20 09 20 20 20 20 54  CL_OK) {. .    T
0730: 63 6c 5f 50 61 6e 69 63 28 22 46 61 69 6c 65 64  cl_Panic("Failed
0740: 20 74 6f 20 63 72 65 61 74 65 20 25 64 27 74 68   to create %d'th
0750: 20 74 68 72 65 61 64 5c 6e 22 2c 20 69 29 3b 0a   thread\n", i);.
0760: 20 09 7d 0a 20 20 20 20 20 7d 0a 2d 20 20 20 20   .}.     }.-    
0770: 6d 75 74 65 78 54 68 72 65 61 64 43 6f 75 6e 74  mutexThreadCount
0780: 20 3d 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e   = contextPtr->n
0790: 75 6d 54 68 72 65 61 64 73 3b 20 2f 2a 20 57 69  umThreads; /* Wi
07a0: 6c 6c 20 66 69 72 65 20 6f 66 66 20 61 6c 6c 20  ll fire off all 
07b0: 74 65 73 74 20 74 68 72 65 61 64 73 20 2a 2f 0a  test threads */.
07c0: 2b 0a 2b 20 20 20 20 2f 2a 20 54 72 69 67 67 65  +.+    /* Trigge
07d0: 72 20 73 74 61 72 74 20 73 69 67 6e 61 6c 20 2a  r start signal *
07e0: 2f 0a 2b 20 20 20 20 54 63 6c 5f 43 6f 6e 64 69  /.+    Tcl_Condi
07f0: 74 69 6f 6e 4e 6f 74 69 66 79 28 26 74 68 72 65  tionNotify(&thre
0800: 61 64 53 74 61 72 74 43 6f 6e 64 29 3b 0a 2b 20  adStartCond);.+ 
0810: 20 20 20 54 63 6c 5f 4d 75 74 65 78 55 6e 6c 6f     Tcl_MutexUnlo
0820: 63 6b 28 26 74 68 72 65 61 64 53 74 61 72 74 4d  ck(&threadStartM
0830: 75 74 65 78 29 3b 0a 20 0a 20 20 20 20 20 2f 2a  utex);. .     /*
0840: 20 57 61 69 74 20 66 6f 72 20 61 6c 6c 20 74 68   Wait for all th
0850: 72 65 61 64 73 20 2a 2f 0a 20 20 20 20 20 66 6f  reads */.     fo
0860: 72 20 28 69 6e 74 20 69 20 3d 20 30 3b 20 69 20  r (int i = 0; i 
0870: 3c 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75  < contextPtr->nu
0880: 6d 54 68 72 65 61 64 73 3b 20 69 2b 2b 29 20 7b  mThreads; i++) {
0890: 0a 20 09 69 6e 74 20 74 68 72 65 61 64 52 65 73  . .int threadRes
08a0: 75 6c 74 3b 0a 20 09 54 63 6c 5f 4a 6f 69 6e 54  ult;. .Tcl_JoinT
08b0: 68 72 65 61 64 28 74 68 72 65 61 64 43 6f 6e 74  hread(threadCont
08c0: 65 78 74 73 50 74 72 5b 69 5d 2e 74 68 72 65 61  extsPtr[i].threa
08d0: 64 49 64 2c 20 26 74 68 72 65 61 64 52 65 73 75  dId, &threadResu
08e0: 6c 74 29 3b 0a 40 40 20 2d 32 37 39 2c 31 34 20  lt);.@@ -279,14 
08f0: 2b 32 38 35 2c 31 34 20 40 40 0a 20 20 20 20 20  +285,14 @@.     
0900: 76 6f 69 64 20 2a 63 6c 69 65 6e 74 44 61 74 61  void *clientData
0910: 29 0a 20 7b 0a 20 20 20 20 20 4d 75 74 65 78 54  ). {.     MutexT
0920: 68 72 65 61 64 43 6f 6e 74 65 78 74 20 2a 74 68  hreadContext *th
0930: 72 65 61 64 43 6f 6e 74 65 78 74 50 74 72 20 3d  readContextPtr =
0940: 20 28 4d 75 74 65 78 54 68 72 65 61 64 43 6f 6e   (MutexThreadCon
0950: 74 65 78 74 20 2a 29 63 6c 69 65 6e 74 44 61 74  text *)clientDat
0960: 61 3b 0a 20 20 20 20 20 4d 75 74 65 78 53 68 61  a;.     MutexSha
0970: 72 65 64 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  redContext *cont
0980: 65 78 74 50 74 72 20 3d 20 74 68 72 65 61 64 43  extPtr = threadC
0990: 6f 6e 74 65 78 74 50 74 72 2d 3e 73 68 61 72 65  ontextPtr->share
09a0: 64 43 6f 6e 74 65 78 74 50 74 72 3b 0a 20 0a 2d  dContextPtr;. .-
09b0: 20 20 20 20 2f 2a 20 53 70 69 6e 20 77 61 69 74      /* Spin wait
09c0: 20 75 6e 74 69 6c 20 67 69 76 65 6e 20 74 68 65   until given the
09d0: 20 72 75 6e 20 73 69 67 6e 61 6c 20 2a 2f 0a 2d   run signal */.-
09e0: 20 20 20 20 77 68 69 6c 65 20 28 6d 75 74 65 78      while (mutex
09f0: 54 68 72 65 61 64 43 6f 75 6e 74 20 3c 20 63 6f  ThreadCount < co
0a00: 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54 68 72  ntextPtr->numThr
0a10: 65 61 64 73 29 20 7b 0a 2d 09 59 69 65 6c 64 54  eads) {.-.YieldT
0a20: 6f 4f 74 68 65 72 54 68 72 65 61 64 73 28 29 3b  oOtherThreads();
0a30: 0a 2d 20 20 20 20 7d 0a 2b 20 20 20 20 2f 2a 20  .-    }.+    /* 
0a40: 57 61 69 74 20 66 6f 72 20 73 74 61 72 74 20 73  Wait for start s
0a50: 69 67 6e 61 6c 20 2a 2f 0a 2b 20 20 20 20 54 63  ignal */.+    Tc
0a60: 6c 5f 4d 75 74 65 78 4c 6f 63 6b 28 26 74 68 72  l_MutexLock(&thr
0a70: 65 61 64 53 74 61 72 74 4d 75 74 65 78 29 3b 0a  eadStartMutex);.
0a80: 2b 20 20 20 20 54 63 6c 5f 43 6f 6e 64 69 74 69  +    Tcl_Conditi
0a90: 6f 6e 57 61 69 74 28 26 74 68 72 65 61 64 53 74  onWait(&threadSt
0aa0: 61 72 74 43 6f 6e 64 2c 20 26 74 68 72 65 61 64  artCond, &thread
0ab0: 53 74 61 72 74 4d 75 74 65 78 2c 20 4e 55 4c 4c  StartMutex, NULL
0ac0: 29 3b 0a 2b 20 20 20 20 54 63 6c 5f 4d 75 74 65  );.+    Tcl_Mute
0ad0: 78 55 6e 6c 6f 63 6b 28 26 74 68 72 65 61 64 53  xUnlock(&threadS
0ae0: 74 61 72 74 4d 75 74 65 78 29 3b 0a 20 0a 20 20  tartMutex);. .  
0af0: 20 20 20 66 6f 72 20 28 69 6e 74 20 69 20 3d 20     for (int i = 
0b00: 30 3b 20 69 20 3c 20 63 6f 6e 74 65 78 74 50 74  0; i < contextPt
0b10: 72 2d 3e 6e 75 6d 49 74 65 72 61 74 69 6f 6e 73  r->numIterations
0b20: 3b 20 69 2b 2b 29 20 7b 0a 20 09 4c 6f 63 6b 54  ; i++) {. .LockT
0b30: 65 73 74 43 6f 6e 74 65 78 74 28 63 6f 6e 74 65  estContext(conte
0b40: 78 74 50 74 72 2d 3e 6e 75 6d 52 65 63 75 72 73  xtPtr->numRecurs
0b50: 69 6f 6e 73 29 3b 0a 20 09 54 63 6c 5f 57 69 64  ions);. .Tcl_Wid
0b60: 65 55 49 6e 74 20 74 65 6d 70 20 3d 20 63 6f 6e  eUInt temp = con
0b70: 74 65 78 74 50 74 72 2d 3e 75 2e 63 6f 75 6e 74  textPtr->u.count
0b80: 65 72 3b 0a 20 09 69 66 20 28 63 6f 6e 74 65 78  er;. .if (contex
0b90: 74 50 74 72 2d 3e 79 69 65 6c 64 29 20 7b 0a 40  tPtr->yield) {.@
0ba0: 40 20 2d 33 34 34 2c 31 31 20 2b 33 35 30 2c 31  @ -344,11 +350,1
0bb0: 34 20 40 40 0a 20 20 20 20 20 4d 75 74 65 78 54  4 @@.     MutexT
0bc0: 68 72 65 61 64 43 6f 6e 74 65 78 74 20 2a 63 6f  hreadContext *co
0bd0: 6e 73 75 6d 65 72 43 6f 6e 74 65 78 74 73 50 74  nsumerContextsPt
0be0: 72 20 3d 20 28 4d 75 74 65 78 54 68 72 65 61 64  r = (MutexThread
0bf0: 43 6f 6e 74 65 78 74 20 2a 29 54 63 6c 5f 41 6c  Context *)Tcl_Al
0c00: 6c 6f 63 28 0a 20 09 20 20 20 20 73 69 7a 65 6f  loc(. .    sizeo
0c10: 66 28 2a 63 6f 6e 73 75 6d 65 72 43 6f 6e 74 65  f(*consumerConte
0c20: 78 74 73 50 74 72 29 20 2a 20 6e 75 6d 43 6f 6e  xtsPtr) * numCon
0c30: 73 75 6d 65 72 73 29 3b 0a 20 20 20 20 20 4d 75  sumers);.     Mu
0c40: 74 65 78 54 68 72 65 61 64 43 6f 6e 74 65 78 74  texThreadContext
0c50: 20 2a 70 72 6f 64 75 63 65 72 43 6f 6e 74 65 78   *producerContex
0c60: 74 73 50 74 72 20 3d 20 28 4d 75 74 65 78 54 68  tsPtr = (MutexTh
0c70: 72 65 61 64 43 6f 6e 74 65 78 74 20 2a 29 54 63  readContext *)Tc
0c80: 6c 5f 41 6c 6c 6f 63 28 0a 20 09 20 20 20 20 73  l_Alloc(. .    s
0c90: 69 7a 65 6f 66 28 2a 70 72 6f 64 75 63 65 72 43  izeof(*producerC
0ca0: 6f 6e 74 65 78 74 73 50 74 72 29 20 2a 20 6e 75  ontextsPtr) * nu
0cb0: 6d 50 72 6f 64 75 63 65 72 73 29 3b 0a 20 0a 2d  mProducers);. .-
0cc0: 20 20 20 20 6d 75 74 65 78 54 68 72 65 61 64 43      mutexThreadC
0cd0: 6f 75 6e 74 20 3d 20 30 3b 0a 2b 20 20 20 20 54  ount = 0;.+    T
0ce0: 63 6c 5f 4d 75 74 65 78 4c 6f 63 6b 28 26 74 68  cl_MutexLock(&th
0cf0: 72 65 61 64 53 74 61 72 74 4d 75 74 65 78 29 3b  readStartMutex);
0d00: 0a 2b 20 20 20 20 2f 2a 20 49 6e 66 6c 61 74 65  .+    /* Inflate
0d10: 20 74 68 72 65 61 64 53 74 61 72 74 43 6f 6e 64   threadStartCond
0d20: 20 66 6f 72 20 6c 61 74 65 72 20 2a 2f 0a 2b 20   for later */.+ 
0d30: 20 20 20 54 63 6c 5f 54 69 6d 65 20 6e 75 6c 6c     Tcl_Time null
0d40: 54 69 6d 65 20 3d 20 7b 20 30 2c 20 30 20 7d 3b  Time = { 0, 0 };
0d50: 0a 2b 20 20 20 20 54 63 6c 5f 43 6f 6e 64 69 74  .+    Tcl_Condit
0d60: 69 6f 6e 57 61 69 74 28 26 74 68 72 65 61 64 53  ionWait(&threadS
0d70: 74 61 72 74 43 6f 6e 64 2c 20 26 74 68 72 65 61  tartCond, &threa
0d80: 64 53 74 61 72 74 4d 75 74 65 78 2c 20 26 6e 75  dStartMutex, &nu
0d90: 6c 6c 54 69 6d 65 29 3b 0a 20 0a 20 20 20 20 20  llTime);. .     
0da0: 66 6f 72 20 28 69 6e 74 20 69 20 3d 20 30 3b 20  for (int i = 0; 
0db0: 69 20 3c 20 6e 75 6d 43 6f 6e 73 75 6d 65 72 73  i < numConsumers
0dc0: 3b 20 69 2b 2b 29 20 7b 0a 20 09 63 6f 6e 73 75  ; i++) {. .consu
0dd0: 6d 65 72 43 6f 6e 74 65 78 74 73 50 74 72 5b 69  merContextsPtr[i
0de0: 5d 2e 73 68 61 72 65 64 43 6f 6e 74 65 78 74 50  ].sharedContextP
0df0: 74 72 20 3d 20 63 6f 6e 74 65 78 74 50 74 72 3b  tr = contextPtr;
0e00: 0a 20 09 63 6f 6e 73 75 6d 65 72 43 6f 6e 74 65  . .consumerConte
0e10: 78 74 73 50 74 72 5b 69 5d 2e 6e 75 6d 4f 70 65  xtsPtr[i].numOpe
0e20: 72 61 74 69 6f 6e 73 20 3d 20 30 3b 0a 20 09 63  rations = 0;. .c
0e30: 6f 6e 73 75 6d 65 72 43 6f 6e 74 65 78 74 73 50  onsumerContextsP
0e40: 74 72 5b 69 5d 2e 74 69 6d 65 6f 75 74 73 20 3d  tr[i].timeouts =
0e50: 20 30 3b 0a 40 40 20 2d 33 37 30 2c 31 31 20 2b   0;.@@ -370,11 +
0e60: 33 37 39 2c 31 33 20 40 40 0a 20 09 09 54 43 4c  379,13 @@. ..TCL
0e70: 5f 54 48 52 45 41 44 5f 53 54 41 43 4b 5f 44 45  _THREAD_STACK_DE
0e80: 46 41 55 4c 54 2c 20 54 43 4c 5f 54 48 52 45 41  FAULT, TCL_THREA
0e90: 44 5f 4a 4f 49 4e 41 42 4c 45 29 20 21 3d 20 54  D_JOINABLE) != T
0ea0: 43 4c 5f 4f 4b 29 20 7b 0a 20 09 20 20 20 20 54  CL_OK) {. .    T
0eb0: 63 6c 5f 50 61 6e 69 63 28 22 46 61 69 6c 65 64  cl_Panic("Failed
0ec0: 20 74 6f 20 63 72 65 61 74 65 20 25 64 27 74 68   to create %d'th
0ed0: 20 74 68 72 65 61 64 5c 6e 22 2c 20 28 69 6e 74   thread\n", (int
0ee0: 29 20 69 29 3b 0a 20 09 7d 0a 20 20 20 20 20 7d  ) i);. .}.     }
0ef0: 0a 20 0a 2d 20 20 20 20 6d 75 74 65 78 54 68 72  . .-    mutexThr
0f00: 65 61 64 43 6f 75 6e 74 20 3d 20 63 6f 6e 74 65  eadCount = conte
0f10: 78 74 50 74 72 2d 3e 6e 75 6d 54 68 72 65 61 64  xtPtr->numThread
0f20: 73 3b 20 2f 2a 20 57 69 6c 6c 20 74 72 69 67 67  s; /* Will trigg
0f30: 65 72 20 61 6c 6c 20 74 68 72 65 61 64 73 20 2a  er all threads *
0f40: 2f 0a 2b 20 20 20 20 2f 2a 20 54 72 69 67 67 65  /.+    /* Trigge
0f50: 72 20 73 74 61 72 74 20 73 69 67 6e 61 6c 20 2a  r start signal *
0f60: 2f 0a 2b 20 20 20 20 54 63 6c 5f 43 6f 6e 64 69  /.+    Tcl_Condi
0f70: 74 69 6f 6e 4e 6f 74 69 66 79 28 26 74 68 72 65  tionNotify(&thre
0f80: 61 64 53 74 61 72 74 43 6f 6e 64 29 3b 0a 2b 20  adStartCond);.+ 
0f90: 20 20 20 54 63 6c 5f 4d 75 74 65 78 55 6e 6c 6f     Tcl_MutexUnlo
0fa0: 63 6b 28 26 74 68 72 65 61 64 53 74 61 72 74 4d  ck(&threadStartM
0fb0: 75 74 65 78 29 3b 0a 20 0a 20 20 20 20 20 2f 2a  utex);. .     /*
0fc0: 20 50 72 6f 64 75 63 65 72 20 74 6f 74 61 6c 2c   Producer total,
0fd0: 20 74 68 72 65 61 64 2c 20 74 69 6d 65 6f 75 74   thread, timeout
0fe0: 73 2c 20 43 6f 6e 73 75 6d 65 72 20 74 6f 74 61  s, Consumer tota
0ff0: 6c 2c 20 74 68 72 65 61 64 2c 20 74 69 6d 65 6f  l, thread, timeo
1000: 75 74 73 20 2a 2f 0a 20 20 20 20 20 54 63 6c 5f  uts */.     Tcl_
1010: 4f 62 6a 20 2a 72 65 73 75 6c 74 73 5b 36 5d 3b  Obj *results[6];
1020: 0a 20 20 20 20 20 72 65 73 75 6c 74 73 5b 31 5d  .     results[1]
1030: 20 3d 20 54 63 6c 5f 4e 65 77 4c 69 73 74 4f 62   = Tcl_NewListOb
1040: 6a 28 6e 75 6d 50 72 6f 64 75 63 65 72 73 2c 20  j(numProducers, 
1050: 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 72 65 73 75  NULL);.     resu
1060: 6c 74 73 5b 34 5d 20 3d 20 54 63 6c 5f 4e 65 77  lts[4] = Tcl_New
1070: 4c 69 73 74 4f 62 6a 28 6e 75 6d 43 6f 6e 73 75  ListObj(numConsu
1080: 6d 65 72 73 2c 20 4e 55 4c 4c 29 3b 0a 40 40 20  mers, NULL);.@@ 
1090: 2d 34 33 39 2c 31 34 20 2b 34 35 30 2c 31 34 20  -439,14 +450,14 
10a0: 40 40 0a 20 0a 20 20 20 20 20 2f 2a 20 4c 69 6d  @@. .     /* Lim
10b0: 69 74 20 6f 6e 20 74 6f 74 61 6c 20 6e 75 6d 62  it on total numb
10c0: 65 72 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73  er of operations
10d0: 20 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65   across all thre
10e0: 61 64 73 20 2a 2f 0a 20 20 20 20 20 54 63 6c 5f  ads */.     Tcl_
10f0: 57 69 64 65 55 49 6e 74 20 6c 69 6d 69 74 3b 0a  WideUInt limit;.
1100: 20 20 20 20 20 6c 69 6d 69 74 20 3d 20 63 6f 6e       limit = con
1110: 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54 68 72 65  textPtr->numThre
1120: 61 64 73 20 2a 20 28 54 63 6c 5f 57 69 64 65 55  ads * (Tcl_WideU
1130: 49 6e 74 29 20 63 6f 6e 74 65 78 74 50 74 72 2d  Int) contextPtr-
1140: 3e 6e 75 6d 49 74 65 72 61 74 69 6f 6e 73 3b 0a  >numIterations;.
1150: 20 0a 2d 20 20 20 20 2f 2a 20 53 70 69 6e 20 77   .-    /* Spin w
1160: 61 69 74 20 75 6e 74 69 6c 20 67 69 76 65 6e 20  ait until given 
1170: 74 68 65 20 72 75 6e 20 73 69 67 6e 61 6c 20 2a  the run signal *
1180: 2f 0a 2d 20 20 20 20 77 68 69 6c 65 20 28 6d 75  /.-    while (mu
1190: 74 65 78 54 68 72 65 61 64 43 6f 75 6e 74 20 3c  texThreadCount <
11a0: 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d   contextPtr->num
11b0: 54 68 72 65 61 64 73 29 20 7b 0a 2d 09 59 69 65  Threads) {.-.Yie
11c0: 6c 64 54 6f 4f 74 68 65 72 54 68 72 65 61 64 73  ldToOtherThreads
11d0: 28 29 3b 0a 2d 20 20 20 20 7d 0a 2b 20 20 20 20  ();.-    }.+    
11e0: 2f 2a 20 57 61 69 74 20 66 6f 72 20 73 74 61 72  /* Wait for star
11f0: 74 20 73 69 67 6e 61 6c 20 2a 2f 0a 2b 20 20 20  t signal */.+   
1200: 20 54 63 6c 5f 4d 75 74 65 78 4c 6f 63 6b 28 26   Tcl_MutexLock(&
1210: 74 68 72 65 61 64 53 74 61 72 74 4d 75 74 65 78  threadStartMutex
1220: 29 3b 0a 2b 20 20 20 20 54 63 6c 5f 43 6f 6e 64  );.+    Tcl_Cond
1230: 69 74 69 6f 6e 57 61 69 74 28 26 74 68 72 65 61  itionWait(&threa
1240: 64 53 74 61 72 74 43 6f 6e 64 2c 20 26 74 68 72  dStartCond, &thr
1250: 65 61 64 53 74 61 72 74 4d 75 74 65 78 2c 20 4e  eadStartMutex, N
1260: 55 4c 4c 29 3b 0a 2b 20 20 20 20 54 63 6c 5f 4d  ULL);.+    Tcl_M
1270: 75 74 65 78 55 6e 6c 6f 63 6b 28 26 74 68 72 65  utexUnlock(&thre
1280: 61 64 53 74 61 72 74 4d 75 74 65 78 29 3b 0a 20  adStartMutex);. 
1290: 0a 20 20 20 20 20 4c 6f 63 6b 54 65 73 74 43 6f  .     LockTestCo
12a0: 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 50 74 72  ntext(contextPtr
12b0: 2d 3e 6e 75 6d 52 65 63 75 72 73 69 6f 6e 73 29  ->numRecursions)
12c0: 3b 0a 20 20 20 20 20 77 68 69 6c 65 20 28 63 6f  ;.     while (co
12d0: 6e 74 65 78 74 50 74 72 2d 3e 75 2e 71 75 65 75  ntextPtr->u.queu
12e0: 65 2e 74 6f 74 61 6c 45 6e 71 75 65 75 65 64 20  e.totalEnqueued 
12f0: 3c 20 6c 69 6d 69 74 29 20 7b 0a 20 09 69 66 20  < limit) {. .if 
1300: 28 63 6f 6e 74 65 78 74 50 74 72 2d 3e 75 2e 71  (contextPtr->u.q
1310: 75 65 75 65 2e 61 76 61 69 6c 61 62 6c 65 20 3d  ueue.available =
1320: 3d 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 75 2e  = contextPtr->u.
1330: 71 75 65 75 65 2e 63 61 70 61 63 69 74 79 29 20  queue.capacity) 
1340: 7b 0a 20 09 20 20 20 20 54 63 6c 5f 54 69 6d 65  {. .    Tcl_Time
1350: 20 62 65 66 6f 72 65 2c 20 61 66 74 65 72 3b 0a   before, after;.
1360: 40 40 20 2d 35 30 31 2c 31 34 20 2b 35 31 32 2c  @@ -501,14 +512,
1370: 31 34 20 40 40 0a 20 0a 20 20 20 20 20 2f 2a 20  14 @@. .     /* 
1380: 4c 69 6d 69 74 20 6f 6e 20 74 6f 74 61 6c 20 6e  Limit on total n
1390: 75 6d 62 65 72 20 6f 66 20 6f 70 65 72 61 74 69  umber of operati
13a0: 6f 6e 73 20 61 63 72 6f 73 73 20 61 6c 6c 20 74  ons across all t
13b0: 68 72 65 61 64 73 20 2a 2f 0a 20 20 20 20 20 54  hreads */.     T
13c0: 63 6c 5f 57 69 64 65 55 49 6e 74 20 6c 69 6d 69  cl_WideUInt limi
13d0: 74 3b 0a 20 20 20 20 20 6c 69 6d 69 74 20 3d 20  t;.     limit = 
13e0: 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54  contextPtr->numT
13f0: 68 72 65 61 64 73 20 2a 20 28 54 63 6c 5f 57 69  hreads * (Tcl_Wi
1400: 64 65 55 49 6e 74 29 20 63 6f 6e 74 65 78 74 50  deUInt) contextP
1410: 74 72 2d 3e 6e 75 6d 49 74 65 72 61 74 69 6f 6e  tr->numIteration
1420: 73 3b 0a 20 0a 2d 20 20 20 20 2f 2a 20 53 70 69  s;. .-    /* Spi
1430: 6e 20 77 61 69 74 20 75 6e 74 69 6c 20 67 69 76  n wait until giv
1440: 65 6e 20 74 68 65 20 72 75 6e 20 73 69 67 6e 61  en the run signa
1450: 6c 20 2a 2f 0a 2d 20 20 20 20 77 68 69 6c 65 20  l */.-    while 
1460: 28 6d 75 74 65 78 54 68 72 65 61 64 43 6f 75 6e  (mutexThreadCoun
1470: 74 20 3c 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e  t < contextPtr->
1480: 6e 75 6d 54 68 72 65 61 64 73 29 20 7b 0a 2d 09  numThreads) {.-.
1490: 59 69 65 6c 64 54 6f 4f 74 68 65 72 54 68 72 65  YieldToOtherThre
14a0: 61 64 73 28 29 3b 0a 2d 20 20 20 20 7d 0a 2b 20  ads();.-    }.+ 
14b0: 20 20 20 2f 2a 20 57 61 69 74 20 66 6f 72 20 73     /* Wait for s
14c0: 74 61 72 74 20 73 69 67 6e 61 6c 20 2a 2f 0a 2b  tart signal */.+
14d0: 20 20 20 20 54 63 6c 5f 4d 75 74 65 78 4c 6f 63      Tcl_MutexLoc
14e0: 6b 28 26 74 68 72 65 61 64 53 74 61 72 74 4d 75  k(&threadStartMu
14f0: 74 65 78 29 3b 0a 2b 20 20 20 20 54 63 6c 5f 43  tex);.+    Tcl_C
1500: 6f 6e 64 69 74 69 6f 6e 57 61 69 74 28 26 74 68  onditionWait(&th
1510: 72 65 61 64 53 74 61 72 74 43 6f 6e 64 2c 20 26  readStartCond, &
1520: 74 68 72 65 61 64 53 74 61 72 74 4d 75 74 65 78  threadStartMutex
1530: 2c 20 4e 55 4c 4c 29 3b 0a 2b 20 20 20 20 54 63  , NULL);.+    Tc
1540: 6c 5f 4d 75 74 65 78 55 6e 6c 6f 63 6b 28 26 74  l_MutexUnlock(&t
1550: 68 72 65 61 64 53 74 61 72 74 4d 75 74 65 78 29  hreadStartMutex)
1560: 3b 0a 20 0a 20 20 20 20 20 4c 6f 63 6b 54 65 73  ;. .     LockTes
1570: 74 43 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  tContext(context
1580: 50 74 72 2d 3e 6e 75 6d 52 65 63 75 72 73 69 6f  Ptr->numRecursio
1590: 6e 73 29 3b 0a 20 20 20 20 20 77 68 69 6c 65 20  ns);.     while 
15a0: 28 63 6f 6e 74 65 78 74 50 74 72 2d 3e 75 2e 71  (contextPtr->u.q
15b0: 75 65 75 65 2e 74 6f 74 61 6c 44 65 71 75 65 75  ueue.totalDequeu
15c0: 65 64 20 3c 20 6c 69 6d 69 74 29 20 7b 0a 20 09  ed < limit) {. .
15d0: 69 66 20 28 63 6f 6e 74 65 78 74 50 74 72 2d 3e  if (contextPtr->
15e0: 75 2e 71 75 65 75 65 2e 61 76 61 69 6c 61 62 6c  u.queue.availabl
15f0: 65 20 3d 3d 20 30 29 20 7b 0a 20 09 20 20 20 20  e == 0) {. .    
1600: 54 63 6c 5f 54 69 6d 65 20 62 65 66 6f 72 65 2c  Tcl_Time before,
1610: 20 61 66 74 65 72 3b 0a 0a                        after;..