Attachment "tclMutexTest.diff4" to
ticket [893f8cc5db]
added by
chw
2025-10-12 16:49:54.
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 37 30 2c 31 30 20 2b 37 32 2c 31 32 @@ -70,10 +72,12
01a0: 20 40 40 0a 20 74 79 70 65 64 65 66 20 73 74 72 @@. typedef str
01b0: 75 63 74 20 7b 0a 20 20 20 20 20 69 6e 74 20 6e uct {. int n
01c0: 75 6d 54 68 72 65 61 64 73 3b 09 09 2f 2a 20 4e umThreads;../* N
01d0: 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 umber of threads
01e0: 20 69 6e 20 74 65 73 74 20 72 75 6e 20 2a 2f 0a in test run */.
01f0: 20 20 20 20 20 69 6e 74 20 6e 75 6d 52 65 63 75 int numRecu
0200: 72 73 69 6f 6e 73 3b 09 09 2f 2a 20 4e 75 6d 62 rsions;../* Numb
0210: 65 72 20 6f 66 20 6d 75 74 65 78 20 6c 6f 63 6b er of mutex lock
0220: 20 72 65 63 75 72 73 69 6f 6e 73 20 2a 2f 0a 20 recursions */.
0230: 20 20 20 20 69 6e 74 20 6e 75 6d 49 74 65 72 61 int numItera
0240: 74 69 6f 6e 73 3b 09 09 2f 2a 20 4e 75 6d 62 65 tions;../* Numbe
0250: 72 20 6f 66 20 74 69 6d 65 73 20 65 61 63 68 20 r of times each
0260: 74 68 72 65 61 64 20 73 68 6f 75 6c 64 20 6c 6f thread should lo
0270: 6f 70 20 2a 2f 0a 20 20 20 20 20 69 6e 74 20 79 op */. int y
0280: 69 65 6c 64 3b 09 09 09 2f 2a 20 57 68 65 74 68 ield;.../* Wheth
0290: 65 72 20 74 68 72 65 61 64 73 20 73 68 6f 75 6c er threads shoul
02a0: 64 20 79 69 65 6c 64 20 77 68 65 6e 20 6c 6f 6f d yield when loo
02b0: 70 69 6e 67 20 2a 2f 0a 2b 20 20 20 20 69 6e 74 ping */.+ int
02c0: 20 6e 75 6d 53 74 61 72 74 65 64 3b 09 09 2f 2a numStarted;../*
02d0: 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 Number of threa
02e0: 64 73 20 77 68 69 63 68 20 61 72 65 20 73 74 61 ds which are sta
02f0: 72 74 65 64 20 2a 2f 0a 2b 20 20 20 20 69 6e 74 rted */.+ int
0300: 20 72 75 6e 3b 09 09 09 2f 2a 20 57 68 65 74 68 run;.../* Wheth
0310: 65 72 20 74 68 72 65 61 64 73 20 73 68 6f 75 6c er threads shoul
0320: 64 20 73 74 61 72 74 20 2a 2f 0a 20 20 20 20 20 d start */.
0330: 75 6e 69 6f 6e 20 7b 0a 20 09 54 63 6c 5f 57 69 union {. .Tcl_Wi
0340: 64 65 55 49 6e 74 20 63 6f 75 6e 74 65 72 3b 09 deUInt counter;.
0350: 09 2f 2a 20 55 73 65 64 20 69 6e 20 6c 6f 63 6b ./* Used in lock
0360: 20 74 65 73 74 73 20 2a 2f 0a 20 09 50 72 6f 64 tests */. .Prod
0370: 75 63 65 72 43 6f 6e 73 75 6d 65 72 51 75 65 75 ucerConsumerQueu
0380: 65 20 71 75 65 75 65 3b 09 2f 2a 20 55 73 65 64 e queue;./* Used
0390: 20 69 6e 20 63 6f 6e 64 69 74 69 6f 6e 20 76 61 in condition va
03a0: 72 69 61 62 6c 65 20 74 65 73 74 73 20 2a 2f 0a riable tests */.
03b0: 20 20 20 20 20 7d 20 75 3b 0a 20 7d 20 4d 75 74 } u;. } Mut
03c0: 65 78 53 68 61 72 65 64 43 6f 6e 74 65 78 74 3b exSharedContext;
03d0: 0a 40 40 20 2d 38 37 2c 31 33 20 2b 39 31 2c 31 .@@ -87,13 +91,1
03e0: 30 20 40 40 0a 20 20 20 20 20 54 63 6c 5f 54 68 0 @@. Tcl_Th
03f0: 72 65 61 64 49 64 20 74 68 72 65 61 64 49 64 3b readId threadId;
0400: 09 09 20 20 2f 2a 20 4f 6e 6c 79 20 61 63 63 65 .. /* Only acce
0410: 73 73 20 69 6e 20 63 72 65 61 74 6f 72 20 2a 2f ss in creator */
0420: 0a 20 20 20 20 20 54 63 6c 5f 57 69 64 65 55 49 . Tcl_WideUI
0430: 6e 74 20 6e 75 6d 4f 70 65 72 61 74 69 6f 6e 73 nt numOperations
0440: 3b 09 09 20 20 2f 2a 20 55 73 65 20 69 73 20 64 ;.. /* Use is d
0450: 65 70 65 6e 64 65 6e 74 20 6f 6e 20 74 68 65 20 ependent on the
0460: 74 65 73 74 20 2a 2f 0a 20 20 20 20 20 54 63 6c test */. Tcl
0470: 5f 57 69 64 65 55 49 6e 74 20 74 69 6d 65 6f 75 _WideUInt timeou
0480: 74 73 3b 09 09 20 20 2f 2a 20 54 69 6d 65 6f 75 ts;.. /* Timeou
0490: 74 73 20 6f 6e 20 63 6f 6e 64 69 74 69 6f 6e 20 ts on condition
04a0: 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 7d 20 variables */. }
04b0: 4d 75 74 65 78 54 68 72 65 61 64 43 6f 6e 74 65 MutexThreadConte
04c0: 78 74 3b 0a 20 0a 2d 2f 2a 20 55 73 65 64 20 74 xt;. .-/* Used t
04d0: 6f 20 74 72 61 63 6b 20 68 6f 77 20 6d 61 6e 79 o track how many
04e0: 20 74 65 73 74 20 74 68 72 65 61 64 73 20 72 75 test threads ru
04f0: 6e 6e 69 6e 67 2e 20 41 6c 73 6f 20 75 73 65 64 nning. Also used
0500: 20 61 73 20 74 72 69 67 67 65 72 20 2a 2f 0a 2d as trigger */.-
0510: 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 static volatile
0520: 69 6e 74 20 6d 75 74 65 78 54 68 72 65 61 64 43 int mutexThreadC
0530: 6f 75 6e 74 3b 0a 2d 0a 20 73 74 61 74 69 63 20 ount;.-. static
0540: 54 63 6c 5f 54 68 72 65 61 64 43 72 65 61 74 65 Tcl_ThreadCreate
0550: 54 79 70 65 09 43 6f 75 6e 74 65 72 54 68 72 65 Type.CounterThre
0560: 61 64 50 72 6f 63 28 76 6f 69 64 20 2a 63 6c 69 adProc(void *cli
0570: 65 6e 74 44 61 74 61 29 3b 0a 20 73 74 61 74 69 entData);. stati
0580: 63 20 69 6e 74 09 09 09 54 65 73 74 4d 75 74 65 c int...TestMute
0590: 78 4c 6f 63 6b 28 54 63 6c 5f 49 6e 74 65 72 70 xLock(Tcl_Interp
05a0: 20 2a 69 6e 74 65 72 70 2c 0a 20 09 09 09 09 20 *interp,. ....
05b0: 20 20 20 4d 75 74 65 78 53 68 61 72 65 64 43 6f MutexSharedCo
05c0: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 50 74 ntext *contextPt
05d0: 72 29 3b 0a 20 73 74 61 74 69 63 20 69 6e 74 09 r);. static int.
05e0: 09 09 54 65 73 74 43 6f 6e 64 69 74 69 6f 6e 56 ..TestConditionV
05f0: 61 72 69 61 62 6c 65 28 54 63 6c 5f 49 6e 74 65 ariable(Tcl_Inte
0600: 72 70 20 2a 69 6e 74 65 72 70 2c 0a 20 09 09 09 rp *interp,. ...
0610: 09 20 20 20 20 4d 75 74 65 78 53 68 61 72 65 64 . MutexShared
0620: 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 Context *context
0630: 50 74 72 29 3b 0a 40 40 20 2d 32 33 31 2c 31 32 Ptr);.@@ -231,12
0640: 20 2b 32 33 32 2c 31 38 20 40 40 0a 20 20 20 20 +232,18 @@.
0650: 20 4d 75 74 65 78 53 68 61 72 65 64 43 6f 6e 74 MutexSharedCont
0660: 65 78 74 20 2a 63 6f 6e 74 65 78 74 50 74 72 29 ext *contextPtr)
0670: 0a 20 7b 0a 20 20 20 20 20 4d 75 74 65 78 54 68 . {. MutexTh
0680: 72 65 61 64 43 6f 6e 74 65 78 74 20 2a 74 68 72 readContext *thr
0690: 65 61 64 43 6f 6e 74 65 78 74 73 50 74 72 20 3d eadContextsPtr =
06a0: 20 28 4d 75 74 65 78 54 68 72 65 61 64 43 6f 6e (MutexThreadCon
06b0: 74 65 78 74 20 2a 29 0a 20 09 20 20 20 20 54 63 text *). . Tc
06c0: 6c 5f 41 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a l_Alloc(sizeof(*
06d0: 74 68 72 65 61 64 43 6f 6e 74 65 78 74 73 50 74 threadContextsPt
06e0: 72 29 20 2a 20 63 6f 6e 74 65 78 74 50 74 72 2d r) * contextPtr-
06f0: 3e 6e 75 6d 54 68 72 65 61 64 73 29 3b 0a 20 0a >numThreads);. .
0700: 2b 20 20 20 20 54 63 6c 5f 4d 75 74 65 78 4c 6f + Tcl_MutexLo
0710: 63 6b 28 26 74 68 72 65 61 64 53 74 61 72 74 4d ck(&threadStartM
0720: 75 74 65 78 29 3b 0a 2b 20 20 20 20 2f 2a 20 49 utex);.+ /* I
0730: 6e 66 6c 61 74 65 20 74 68 72 65 61 64 53 74 61 nflate threadSta
0740: 72 74 43 6f 6e 64 20 66 6f 72 20 6c 61 74 65 72 rtCond for later
0750: 20 2a 2f 0a 2b 20 20 20 20 54 63 6c 5f 54 69 6d */.+ Tcl_Tim
0760: 65 20 6e 75 6c 6c 54 69 6d 65 20 3d 20 7b 20 30 e nullTime = { 0
0770: 2c 20 30 20 7d 3b 0a 2b 20 20 20 20 54 63 6c 5f , 0 };.+ Tcl_
0780: 43 6f 6e 64 69 74 69 6f 6e 57 61 69 74 28 26 74 ConditionWait(&t
0790: 68 72 65 61 64 53 74 61 72 74 43 6f 6e 64 2c 20 hreadStartCond,
07a0: 26 74 68 72 65 61 64 53 74 61 72 74 4d 75 74 65 &threadStartMute
07b0: 78 2c 20 26 6e 75 6c 6c 54 69 6d 65 29 3b 0a 2b x, &nullTime);.+
07c0: 0a 2b 20 20 20 20 63 6f 6e 74 65 78 74 50 74 72 .+ contextPtr
07d0: 2d 3e 6e 75 6d 53 74 61 72 74 65 64 20 3d 20 30 ->numStarted = 0
07e0: 3b 0a 2b 20 20 20 20 63 6f 6e 74 65 78 74 50 74 ;.+ contextPt
07f0: 72 2d 3e 72 75 6e 20 3d 20 30 3b 0a 20 20 20 20 r->run = 0;.
0800: 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 75 2e 63 contextPtr->u.c
0810: 6f 75 6e 74 65 72 20 3d 20 30 3b 0a 2d 20 20 20 ounter = 0;.-
0820: 20 6d 75 74 65 78 54 68 72 65 61 64 43 6f 75 6e mutexThreadCoun
0830: 74 20 3d 20 30 3b 0a 20 20 20 20 20 66 6f 72 20 t = 0;. for
0840: 28 69 6e 74 20 69 20 3d 20 30 3b 20 69 20 3c 20 (int i = 0; i <
0850: 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54 contextPtr->numT
0860: 68 72 65 61 64 73 3b 20 69 2b 2b 29 20 7b 0a 20 hreads; i++) {.
0870: 09 74 68 72 65 61 64 43 6f 6e 74 65 78 74 73 50 .threadContextsP
0880: 74 72 5b 69 5d 2e 73 68 61 72 65 64 43 6f 6e 74 tr[i].sharedCont
0890: 65 78 74 50 74 72 20 3d 20 63 6f 6e 74 65 78 74 extPtr = context
08a0: 50 74 72 3b 0a 20 09 74 68 72 65 61 64 43 6f 6e Ptr;. .threadCon
08b0: 74 65 78 74 73 50 74 72 5b 69 5d 2e 6e 75 6d 4f textsPtr[i].numO
08c0: 70 65 72 61 74 69 6f 6e 73 20 3d 20 30 3b 20 2f perations = 0; /
08d0: 2a 20 49 6e 69 74 20 74 68 6f 75 67 68 20 6e 6f * Init though no
08e0: 74 20 75 73 65 64 20 2a 2f 0a 20 0a 20 09 69 66 t used */. . .if
08f0: 20 28 54 63 6c 5f 43 72 65 61 74 65 54 68 72 65 (Tcl_CreateThre
0900: 61 64 28 26 74 68 72 65 61 64 43 6f 6e 74 65 78 ad(&threadContex
0910: 74 73 50 74 72 5b 69 5d 2e 74 68 72 65 61 64 49 tsPtr[i].threadI
0920: 64 2c 0a 40 40 20 2d 32 34 33 2c 31 31 20 2b 32 d,.@@ -243,11 +2
0930: 35 30 2c 31 39 20 40 40 0a 20 09 09 43 6f 75 6e 50,19 @@. ..Coun
0940: 74 65 72 54 68 72 65 61 64 50 72 6f 63 2c 20 26 terThreadProc, &
0950: 74 68 72 65 61 64 43 6f 6e 74 65 78 74 73 50 74 threadContextsPt
0960: 72 5b 69 5d 2c 0a 20 09 09 54 43 4c 5f 54 48 52 r[i],. ..TCL_THR
0970: 45 41 44 5f 53 54 41 43 4b 5f 44 45 46 41 55 4c EAD_STACK_DEFAUL
0980: 54 2c 20 54 43 4c 5f 54 48 52 45 41 44 5f 4a 4f T, TCL_THREAD_JO
0990: 49 4e 41 42 4c 45 29 20 21 3d 20 54 43 4c 5f 4f INABLE) != TCL_O
09a0: 4b 29 20 7b 0a 20 09 20 20 20 20 54 63 6c 5f 50 K) {. . Tcl_P
09b0: 61 6e 69 63 28 22 46 61 69 6c 65 64 20 74 6f 20 anic("Failed to
09c0: 63 72 65 61 74 65 20 25 64 27 74 68 20 74 68 72 create %d'th thr
09d0: 65 61 64 5c 6e 22 2c 20 69 29 3b 0a 20 09 7d 0a ead\n", i);. .}.
09e0: 20 20 20 20 20 7d 0a 2d 20 20 20 20 6d 75 74 65 }.- mute
09f0: 78 54 68 72 65 61 64 43 6f 75 6e 74 20 3d 20 63 xThreadCount = c
0a00: 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54 68 ontextPtr->numTh
0a10: 72 65 61 64 73 3b 20 2f 2a 20 57 69 6c 6c 20 66 reads; /* Will f
0a20: 69 72 65 20 6f 66 66 20 61 6c 6c 20 74 65 73 74 ire off all test
0a30: 20 74 68 72 65 61 64 73 20 2a 2f 0a 2b 0a 2b 20 threads */.+.+
0a40: 20 20 20 2f 2a 20 57 61 69 74 20 66 6f 72 20 61 /* Wait for a
0a50: 6c 6c 20 74 68 72 65 61 64 73 20 62 65 69 6e 67 ll threads being
0a60: 20 72 65 61 64 79 20 2a 2f 0a 2b 20 20 20 20 77 ready */.+ w
0a70: 68 69 6c 65 20 28 63 6f 6e 74 65 78 74 50 74 72 hile (contextPtr
0a80: 2d 3e 6e 75 6d 53 74 61 72 74 65 64 20 3c 20 63 ->numStarted < c
0a90: 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54 68 ontextPtr->numTh
0aa0: 72 65 61 64 73 29 20 7b 0a 2b 09 54 63 6c 5f 43 reads) {.+.Tcl_C
0ab0: 6f 6e 64 69 74 69 6f 6e 57 61 69 74 28 26 74 68 onditionWait(&th
0ac0: 72 65 61 64 53 74 61 72 74 43 6f 6e 64 2c 20 26 readStartCond, &
0ad0: 74 68 72 65 61 64 53 74 61 72 74 4d 75 74 65 78 threadStartMutex
0ae0: 2c 20 4e 55 4c 4c 29 3b 0a 2b 20 20 20 20 7d 0a , NULL);.+ }.
0af0: 2b 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 + /* Trigger
0b00: 73 74 61 72 74 20 73 69 67 6e 61 6c 20 2a 2f 0a start signal */.
0b10: 2b 20 20 20 20 63 6f 6e 74 65 78 74 50 74 72 2d + contextPtr-
0b20: 3e 72 75 6e 20 3d 20 31 3b 0a 2b 20 20 20 20 54 >run = 1;.+ T
0b30: 63 6c 5f 43 6f 6e 64 69 74 69 6f 6e 4e 6f 74 69 cl_ConditionNoti
0b40: 66 79 28 26 74 68 72 65 61 64 53 74 61 72 74 43 fy(&threadStartC
0b50: 6f 6e 64 29 3b 0a 2b 20 20 20 20 54 63 6c 5f 4d ond);.+ Tcl_M
0b60: 75 74 65 78 55 6e 6c 6f 63 6b 28 26 74 68 72 65 utexUnlock(&thre
0b70: 61 64 53 74 61 72 74 4d 75 74 65 78 29 3b 0a 20 adStartMutex);.
0b80: 0a 20 20 20 20 20 2f 2a 20 57 61 69 74 20 66 6f . /* Wait fo
0b90: 72 20 61 6c 6c 20 74 68 72 65 61 64 73 20 2a 2f r all threads */
0ba0: 0a 20 20 20 20 20 66 6f 72 20 28 69 6e 74 20 69 . for (int i
0bb0: 20 3d 20 30 3b 20 69 20 3c 20 63 6f 6e 74 65 78 = 0; i < contex
0bc0: 74 50 74 72 2d 3e 6e 75 6d 54 68 72 65 61 64 73 tPtr->numThreads
0bd0: 3b 20 69 2b 2b 29 20 7b 0a 20 09 69 6e 74 20 74 ; i++) {. .int t
0be0: 68 72 65 61 64 52 65 73 75 6c 74 3b 0a 20 09 54 hreadResult;. .T
0bf0: 63 6c 5f 4a 6f 69 6e 54 68 72 65 61 64 28 74 68 cl_JoinThread(th
0c00: 72 65 61 64 43 6f 6e 74 65 78 74 73 50 74 72 5b readContextsPtr[
0c10: 69 5d 2e 74 68 72 65 61 64 49 64 2c 20 26 74 68 i].threadId, &th
0c20: 72 65 61 64 52 65 73 75 6c 74 29 3b 0a 40 40 20 readResult);.@@
0c30: 2d 32 37 39 2c 31 34 20 2b 32 39 34 2c 31 38 20 -279,14 +294,18
0c40: 40 40 0a 20 20 20 20 20 76 6f 69 64 20 2a 63 6c @@. void *cl
0c50: 69 65 6e 74 44 61 74 61 29 0a 20 7b 0a 20 20 20 ientData). {.
0c60: 20 20 4d 75 74 65 78 54 68 72 65 61 64 43 6f 6e MutexThreadCon
0c70: 74 65 78 74 20 2a 74 68 72 65 61 64 43 6f 6e 74 text *threadCont
0c80: 65 78 74 50 74 72 20 3d 20 28 4d 75 74 65 78 54 extPtr = (MutexT
0c90: 68 72 65 61 64 43 6f 6e 74 65 78 74 20 2a 29 63 hreadContext *)c
0ca0: 6c 69 65 6e 74 44 61 74 61 3b 0a 20 20 20 20 20 lientData;.
0cb0: 4d 75 74 65 78 53 68 61 72 65 64 43 6f 6e 74 65 MutexSharedConte
0cc0: 78 74 20 2a 63 6f 6e 74 65 78 74 50 74 72 20 3d xt *contextPtr =
0cd0: 20 74 68 72 65 61 64 43 6f 6e 74 65 78 74 50 74 threadContextPt
0ce0: 72 2d 3e 73 68 61 72 65 64 43 6f 6e 74 65 78 74 r->sharedContext
0cf0: 50 74 72 3b 0a 20 0a 2d 20 20 20 20 2f 2a 20 53 Ptr;. .- /* S
0d00: 70 69 6e 20 77 61 69 74 20 75 6e 74 69 6c 20 67 pin wait until g
0d10: 69 76 65 6e 20 74 68 65 20 72 75 6e 20 73 69 67 iven the run sig
0d20: 6e 61 6c 20 2a 2f 0a 2d 20 20 20 20 77 68 69 6c nal */.- whil
0d30: 65 20 28 6d 75 74 65 78 54 68 72 65 61 64 43 6f e (mutexThreadCo
0d40: 75 6e 74 20 3c 20 63 6f 6e 74 65 78 74 50 74 72 unt < contextPtr
0d50: 2d 3e 6e 75 6d 54 68 72 65 61 64 73 29 20 7b 0a ->numThreads) {.
0d60: 2d 09 59 69 65 6c 64 54 6f 4f 74 68 65 72 54 68 -.YieldToOtherTh
0d70: 72 65 61 64 73 28 29 3b 0a 2b 20 20 20 20 2f 2a reads();.+ /*
0d80: 20 57 61 69 74 20 66 6f 72 20 73 74 61 72 74 20 Wait for start
0d90: 73 69 67 6e 61 6c 20 2a 2f 0a 2b 20 20 20 20 54 signal */.+ T
0da0: 63 6c 5f 4d 75 74 65 78 4c 6f 63 6b 28 26 74 68 cl_MutexLock(&th
0db0: 72 65 61 64 53 74 61 72 74 4d 75 74 65 78 29 3b readStartMutex);
0dc0: 0a 2b 20 20 20 20 63 6f 6e 74 65 78 74 50 74 72 .+ contextPtr
0dd0: 2d 3e 6e 75 6d 53 74 61 72 74 65 64 2b 2b 3b 0a ->numStarted++;.
0de0: 2b 20 20 20 20 54 63 6c 5f 43 6f 6e 64 69 74 69 + Tcl_Conditi
0df0: 6f 6e 4e 6f 74 69 66 79 28 26 74 68 72 65 61 64 onNotify(&thread
0e00: 53 74 61 72 74 43 6f 6e 64 29 3b 0a 2b 20 20 20 StartCond);.+
0e10: 20 77 68 69 6c 65 20 28 21 63 6f 6e 74 65 78 74 while (!context
0e20: 50 74 72 2d 3e 72 75 6e 29 20 7b 0a 2b 09 54 63 Ptr->run) {.+.Tc
0e30: 6c 5f 43 6f 6e 64 69 74 69 6f 6e 57 61 69 74 28 l_ConditionWait(
0e40: 26 74 68 72 65 61 64 53 74 61 72 74 43 6f 6e 64 &threadStartCond
0e50: 2c 20 26 74 68 72 65 61 64 53 74 61 72 74 4d 75 , &threadStartMu
0e60: 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 tex, NULL);.
0e70: 20 7d 0a 2b 20 20 20 20 54 63 6c 5f 4d 75 74 65 }.+ Tcl_Mute
0e80: 78 55 6e 6c 6f 63 6b 28 26 74 68 72 65 61 64 53 xUnlock(&threadS
0e90: 74 61 72 74 4d 75 74 65 78 29 3b 0a 20 0a 20 20 tartMutex);. .
0ea0: 20 20 20 66 6f 72 20 28 69 6e 74 20 69 20 3d 20 for (int i =
0eb0: 30 3b 20 69 20 3c 20 63 6f 6e 74 65 78 74 50 74 0; i < contextPt
0ec0: 72 2d 3e 6e 75 6d 49 74 65 72 61 74 69 6f 6e 73 r->numIterations
0ed0: 3b 20 69 2b 2b 29 20 7b 0a 20 09 4c 6f 63 6b 54 ; i++) {. .LockT
0ee0: 65 73 74 43 6f 6e 74 65 78 74 28 63 6f 6e 74 65 estContext(conte
0ef0: 78 74 50 74 72 2d 3e 6e 75 6d 52 65 63 75 72 73 xtPtr->numRecurs
0f00: 69 6f 6e 73 29 3b 0a 20 09 54 63 6c 5f 57 69 64 ions);. .Tcl_Wid
0f10: 65 55 49 6e 74 20 74 65 6d 70 20 3d 20 63 6f 6e eUInt temp = con
0f20: 74 65 78 74 50 74 72 2d 3e 75 2e 63 6f 75 6e 74 textPtr->u.count
0f30: 65 72 3b 0a 20 09 69 66 20 28 63 6f 6e 74 65 78 er;. .if (contex
0f40: 74 50 74 72 2d 3e 79 69 65 6c 64 29 20 7b 0a 40 tPtr->yield) {.@
0f50: 40 20 2d 33 32 37 2c 31 30 20 2b 33 34 36 2c 31 @ -327,10 +346,1
0f60: 32 20 40 40 0a 20 09 72 65 74 75 72 6e 20 54 43 2 @@. .return TC
0f70: 4c 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 7d 0a L_ERROR;. }.
0f80: 20 20 20 20 20 69 6e 74 20 6e 75 6d 50 72 6f 64 int numProd
0f90: 75 63 65 72 73 20 3d 20 63 6f 6e 74 65 78 74 50 ucers = contextP
0fa0: 74 72 2d 3e 6e 75 6d 54 68 72 65 61 64 73 20 2f tr->numThreads /
0fb0: 20 32 3b 0a 20 20 20 20 20 69 6e 74 20 6e 75 6d 2;. int num
0fc0: 43 6f 6e 73 75 6d 65 72 73 20 3d 20 63 6f 6e 74 Consumers = cont
0fd0: 65 78 74 50 74 72 2d 3e 6e 75 6d 54 68 72 65 61 extPtr->numThrea
0fe0: 64 73 20 2d 20 6e 75 6d 50 72 6f 64 75 63 65 72 ds - numProducer
0ff0: 73 3b 0a 20 0a 2b 20 20 20 20 63 6f 6e 74 65 78 s;. .+ contex
1000: 74 50 74 72 2d 3e 6e 75 6d 53 74 61 72 74 65 64 tPtr->numStarted
1010: 20 3d 20 30 3b 0a 2b 20 20 20 20 63 6f 6e 74 65 = 0;.+ conte
1020: 78 74 50 74 72 2d 3e 72 75 6e 20 3d 20 30 3b 0a xtPtr->run = 0;.
1030: 20 20 20 20 20 63 6f 6e 74 65 78 74 50 74 72 2d contextPtr-
1040: 3e 75 2e 71 75 65 75 65 2e 63 61 6e 44 65 71 75 >u.queue.canDequ
1050: 65 75 65 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 eue = NULL;.
1060: 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 75 2e 71 contextPtr->u.q
1070: 75 65 75 65 2e 63 61 6e 45 6e 71 75 65 75 65 20 ueue.canEnqueue
1080: 3d 20 4e 55 4c 4c 3b 0a 20 0a 20 20 20 20 20 2f = NULL;. . /
1090: 2a 0a 20 20 20 20 20 20 2a 20 61 76 61 69 6c 61 *. * availa
10a0: 62 6c 65 20 74 72 61 63 6b 73 20 68 6f 77 20 6d ble tracks how m
10b0: 61 6e 79 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 any elements in
10c0: 74 68 65 20 76 69 72 74 75 61 6c 20 71 75 65 75 the virtual queu
10d0: 65 0a 40 40 20 2d 33 34 34 2c 31 31 20 2b 33 36 e.@@ -344,11 +36
10e0: 35 2c 31 34 20 40 40 0a 20 20 20 20 20 4d 75 74 5,14 @@. Mut
10f0: 65 78 54 68 72 65 61 64 43 6f 6e 74 65 78 74 20 exThreadContext
1100: 2a 63 6f 6e 73 75 6d 65 72 43 6f 6e 74 65 78 74 *consumerContext
1110: 73 50 74 72 20 3d 20 28 4d 75 74 65 78 54 68 72 sPtr = (MutexThr
1120: 65 61 64 43 6f 6e 74 65 78 74 20 2a 29 54 63 6c eadContext *)Tcl
1130: 5f 41 6c 6c 6f 63 28 0a 20 09 20 20 20 20 73 69 _Alloc(. . si
1140: 7a 65 6f 66 28 2a 63 6f 6e 73 75 6d 65 72 43 6f zeof(*consumerCo
1150: 6e 74 65 78 74 73 50 74 72 29 20 2a 20 6e 75 6d ntextsPtr) * num
1160: 43 6f 6e 73 75 6d 65 72 73 29 3b 0a 20 20 20 20 Consumers);.
1170: 20 4d 75 74 65 78 54 68 72 65 61 64 43 6f 6e 74 MutexThreadCont
1180: 65 78 74 20 2a 70 72 6f 64 75 63 65 72 43 6f 6e ext *producerCon
1190: 74 65 78 74 73 50 74 72 20 3d 20 28 4d 75 74 65 textsPtr = (Mute
11a0: 78 54 68 72 65 61 64 43 6f 6e 74 65 78 74 20 2a xThreadContext *
11b0: 29 54 63 6c 5f 41 6c 6c 6f 63 28 0a 20 09 20 20 )Tcl_Alloc(. .
11c0: 20 20 73 69 7a 65 6f 66 28 2a 70 72 6f 64 75 63 sizeof(*produc
11d0: 65 72 43 6f 6e 74 65 78 74 73 50 74 72 29 20 2a erContextsPtr) *
11e0: 20 6e 75 6d 50 72 6f 64 75 63 65 72 73 29 3b 0a numProducers);.
11f0: 20 0a 2d 20 20 20 20 6d 75 74 65 78 54 68 72 65 .- mutexThre
1200: 61 64 43 6f 75 6e 74 20 3d 20 30 3b 0a 2b 20 20 adCount = 0;.+
1210: 20 20 54 63 6c 5f 4d 75 74 65 78 4c 6f 63 6b 28 Tcl_MutexLock(
1220: 26 74 68 72 65 61 64 53 74 61 72 74 4d 75 74 65 &threadStartMute
1230: 78 29 3b 0a 2b 20 20 20 20 2f 2a 20 49 6e 66 6c x);.+ /* Infl
1240: 61 74 65 20 74 68 72 65 61 64 53 74 61 72 74 43 ate threadStartC
1250: 6f 6e 64 20 66 6f 72 20 6c 61 74 65 72 20 2a 2f ond for later */
1260: 0a 2b 20 20 20 20 54 63 6c 5f 54 69 6d 65 20 6e .+ Tcl_Time n
1270: 75 6c 6c 54 69 6d 65 20 3d 20 7b 20 30 2c 20 30 ullTime = { 0, 0
1280: 20 7d 3b 0a 2b 20 20 20 20 54 63 6c 5f 43 6f 6e };.+ Tcl_Con
1290: 64 69 74 69 6f 6e 57 61 69 74 28 26 74 68 72 65 ditionWait(&thre
12a0: 61 64 53 74 61 72 74 43 6f 6e 64 2c 20 26 74 68 adStartCond, &th
12b0: 72 65 61 64 53 74 61 72 74 4d 75 74 65 78 2c 20 readStartMutex,
12c0: 26 6e 75 6c 6c 54 69 6d 65 29 3b 0a 20 0a 20 20 &nullTime);. .
12d0: 20 20 20 66 6f 72 20 28 69 6e 74 20 69 20 3d 20 for (int i =
12e0: 30 3b 20 69 20 3c 20 6e 75 6d 43 6f 6e 73 75 6d 0; i < numConsum
12f0: 65 72 73 3b 20 69 2b 2b 29 20 7b 0a 20 09 63 6f ers; i++) {. .co
1300: 6e 73 75 6d 65 72 43 6f 6e 74 65 78 74 73 50 74 nsumerContextsPt
1310: 72 5b 69 5d 2e 73 68 61 72 65 64 43 6f 6e 74 65 r[i].sharedConte
1320: 78 74 50 74 72 20 3d 20 63 6f 6e 74 65 78 74 50 xtPtr = contextP
1330: 74 72 3b 0a 20 09 63 6f 6e 73 75 6d 65 72 43 6f tr;. .consumerCo
1340: 6e 74 65 78 74 73 50 74 72 5b 69 5d 2e 6e 75 6d ntextsPtr[i].num
1350: 4f 70 65 72 61 74 69 6f 6e 73 20 3d 20 30 3b 0a Operations = 0;.
1360: 20 09 63 6f 6e 73 75 6d 65 72 43 6f 6e 74 65 78 .consumerContex
1370: 74 73 50 74 72 5b 69 5d 2e 74 69 6d 65 6f 75 74 tsPtr[i].timeout
1380: 73 20 3d 20 30 3b 0a 40 40 20 2d 33 37 30 2c 31 s = 0;.@@ -370,1
1390: 31 20 2b 33 39 34 2c 31 38 20 40 40 0a 20 09 09 1 +394,18 @@. ..
13a0: 54 43 4c 5f 54 48 52 45 41 44 5f 53 54 41 43 4b TCL_THREAD_STACK
13b0: 5f 44 45 46 41 55 4c 54 2c 20 54 43 4c 5f 54 48 _DEFAULT, TCL_TH
13c0: 52 45 41 44 5f 4a 4f 49 4e 41 42 4c 45 29 20 21 READ_JOINABLE) !
13d0: 3d 20 54 43 4c 5f 4f 4b 29 20 7b 0a 20 09 20 20 = TCL_OK) {. .
13e0: 20 20 54 63 6c 5f 50 61 6e 69 63 28 22 46 61 69 Tcl_Panic("Fai
13f0: 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 25 64 led to create %d
1400: 27 74 68 20 74 68 72 65 61 64 5c 6e 22 2c 20 28 'th thread\n", (
1410: 69 6e 74 29 20 69 29 3b 0a 20 09 7d 0a 20 20 20 int) i);. .}.
1420: 20 20 7d 0a 20 0a 2d 20 20 20 20 6d 75 74 65 78 }. .- mutex
1430: 54 68 72 65 61 64 43 6f 75 6e 74 20 3d 20 63 6f ThreadCount = co
1440: 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54 68 72 ntextPtr->numThr
1450: 65 61 64 73 3b 20 2f 2a 20 57 69 6c 6c 20 74 72 eads; /* Will tr
1460: 69 67 67 65 72 20 61 6c 6c 20 74 68 72 65 61 64 igger all thread
1470: 73 20 2a 2f 0a 2b 20 20 20 20 2f 2a 20 57 61 69 s */.+ /* Wai
1480: 74 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 t for all thread
1490: 73 20 62 65 69 6e 67 20 72 65 61 64 79 20 2a 2f s being ready */
14a0: 0a 2b 20 20 20 20 77 68 69 6c 65 20 28 63 6f 6e .+ while (con
14b0: 74 65 78 74 50 74 72 2d 3e 6e 75 6d 53 74 61 72 textPtr->numStar
14c0: 74 65 64 20 3c 20 6e 75 6d 43 6f 6e 73 75 6d 65 ted < numConsume
14d0: 72 73 20 2b 20 6e 75 6d 50 72 6f 64 75 63 65 72 rs + numProducer
14e0: 73 29 20 7b 0a 2b 09 54 63 6c 5f 43 6f 6e 64 69 s) {.+.Tcl_Condi
14f0: 74 69 6f 6e 57 61 69 74 28 26 74 68 72 65 61 64 tionWait(&thread
1500: 53 74 61 72 74 43 6f 6e 64 2c 20 26 74 68 72 65 StartCond, &thre
1510: 61 64 53 74 61 72 74 4d 75 74 65 78 2c 20 4e 55 adStartMutex, NU
1520: 4c 4c 29 3b 0a 2b 20 20 20 20 7d 0a 2b 20 20 20 LL);.+ }.+
1530: 20 2f 2a 20 54 72 69 67 67 65 72 20 73 74 61 72 /* Trigger star
1540: 74 20 73 69 67 6e 61 6c 20 2a 2f 0a 2b 20 20 20 t signal */.+
1550: 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 72 75 6e contextPtr->run
1560: 20 3d 20 31 3b 0a 2b 20 20 20 20 54 63 6c 5f 43 = 1;.+ Tcl_C
1570: 6f 6e 64 69 74 69 6f 6e 4e 6f 74 69 66 79 28 26 onditionNotify(&
1580: 74 68 72 65 61 64 53 74 61 72 74 43 6f 6e 64 29 threadStartCond)
1590: 3b 0a 2b 20 20 20 20 54 63 6c 5f 4d 75 74 65 78 ;.+ Tcl_Mutex
15a0: 55 6e 6c 6f 63 6b 28 26 74 68 72 65 61 64 53 74 Unlock(&threadSt
15b0: 61 72 74 4d 75 74 65 78 29 3b 0a 20 0a 20 20 20 artMutex);. .
15c0: 20 20 2f 2a 20 50 72 6f 64 75 63 65 72 20 74 6f /* Producer to
15d0: 74 61 6c 2c 20 74 68 72 65 61 64 2c 20 74 69 6d tal, thread, tim
15e0: 65 6f 75 74 73 2c 20 43 6f 6e 73 75 6d 65 72 20 eouts, Consumer
15f0: 74 6f 74 61 6c 2c 20 74 68 72 65 61 64 2c 20 74 total, thread, t
1600: 69 6d 65 6f 75 74 73 20 2a 2f 0a 20 20 20 20 20 imeouts */.
1610: 54 63 6c 5f 4f 62 6a 20 2a 72 65 73 75 6c 74 73 Tcl_Obj *results
1620: 5b 36 5d 3b 0a 20 20 20 20 20 72 65 73 75 6c 74 [6];. result
1630: 73 5b 31 5d 20 3d 20 54 63 6c 5f 4e 65 77 4c 69 s[1] = Tcl_NewLi
1640: 73 74 4f 62 6a 28 6e 75 6d 50 72 6f 64 75 63 65 stObj(numProduce
1650: 72 73 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 rs, NULL);.
1660: 72 65 73 75 6c 74 73 5b 34 5d 20 3d 20 54 63 6c results[4] = Tcl
1670: 5f 4e 65 77 4c 69 73 74 4f 62 6a 28 6e 75 6d 43 _NewListObj(numC
1680: 6f 6e 73 75 6d 65 72 73 2c 20 4e 55 4c 4c 29 3b onsumers, NULL);
1690: 0a 40 40 20 2d 34 33 39 2c 31 34 20 2b 34 37 30 .@@ -439,14 +470
16a0: 2c 31 38 20 40 40 0a 20 0a 20 20 20 20 20 2f 2a ,18 @@. . /*
16b0: 20 4c 69 6d 69 74 20 6f 6e 20 74 6f 74 61 6c 20 Limit on total
16c0: 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 72 61 74 number of operat
16d0: 69 6f 6e 73 20 61 63 72 6f 73 73 20 61 6c 6c 20 ions across all
16e0: 74 68 72 65 61 64 73 20 2a 2f 0a 20 20 20 20 20 threads */.
16f0: 54 63 6c 5f 57 69 64 65 55 49 6e 74 20 6c 69 6d Tcl_WideUInt lim
1700: 69 74 3b 0a 20 20 20 20 20 6c 69 6d 69 74 20 3d it;. limit =
1710: 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d contextPtr->num
1720: 54 68 72 65 61 64 73 20 2a 20 28 54 63 6c 5f 57 Threads * (Tcl_W
1730: 69 64 65 55 49 6e 74 29 20 63 6f 6e 74 65 78 74 ideUInt) context
1740: 50 74 72 2d 3e 6e 75 6d 49 74 65 72 61 74 69 6f Ptr->numIteratio
1750: 6e 73 3b 0a 20 0a 2d 20 20 20 20 2f 2a 20 53 70 ns;. .- /* Sp
1760: 69 6e 20 77 61 69 74 20 75 6e 74 69 6c 20 67 69 in wait until gi
1770: 76 65 6e 20 74 68 65 20 72 75 6e 20 73 69 67 6e ven the run sign
1780: 61 6c 20 2a 2f 0a 2d 20 20 20 20 77 68 69 6c 65 al */.- while
1790: 20 28 6d 75 74 65 78 54 68 72 65 61 64 43 6f 75 (mutexThreadCou
17a0: 6e 74 20 3c 20 63 6f 6e 74 65 78 74 50 74 72 2d nt < contextPtr-
17b0: 3e 6e 75 6d 54 68 72 65 61 64 73 29 20 7b 0a 2d >numThreads) {.-
17c0: 09 59 69 65 6c 64 54 6f 4f 74 68 65 72 54 68 72 .YieldToOtherThr
17d0: 65 61 64 73 28 29 3b 0a 2b 20 20 20 20 2f 2a 20 eads();.+ /*
17e0: 57 61 69 74 20 66 6f 72 20 73 74 61 72 74 20 73 Wait for start s
17f0: 69 67 6e 61 6c 20 2a 2f 0a 2b 20 20 20 20 54 63 ignal */.+ Tc
1800: 6c 5f 4d 75 74 65 78 4c 6f 63 6b 28 26 74 68 72 l_MutexLock(&thr
1810: 65 61 64 53 74 61 72 74 4d 75 74 65 78 29 3b 0a eadStartMutex);.
1820: 2b 20 20 20 20 63 6f 6e 74 65 78 74 50 74 72 2d + contextPtr-
1830: 3e 6e 75 6d 53 74 61 72 74 65 64 2b 2b 3b 0a 2b >numStarted++;.+
1840: 20 20 20 20 54 63 6c 5f 43 6f 6e 64 69 74 69 6f Tcl_Conditio
1850: 6e 4e 6f 74 69 66 79 28 26 74 68 72 65 61 64 53 nNotify(&threadS
1860: 74 61 72 74 43 6f 6e 64 29 3b 0a 2b 20 20 20 20 tartCond);.+
1870: 77 68 69 6c 65 20 28 21 63 6f 6e 74 65 78 74 50 while (!contextP
1880: 74 72 2d 3e 72 75 6e 29 20 7b 0a 2b 09 54 63 6c tr->run) {.+.Tcl
1890: 5f 43 6f 6e 64 69 74 69 6f 6e 57 61 69 74 28 26 _ConditionWait(&
18a0: 74 68 72 65 61 64 53 74 61 72 74 43 6f 6e 64 2c threadStartCond,
18b0: 20 26 74 68 72 65 61 64 53 74 61 72 74 4d 75 74 &threadStartMut
18c0: 65 78 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 ex, NULL);.
18d0: 7d 0a 2b 20 20 20 20 54 63 6c 5f 4d 75 74 65 78 }.+ Tcl_Mutex
18e0: 55 6e 6c 6f 63 6b 28 26 74 68 72 65 61 64 53 74 Unlock(&threadSt
18f0: 61 72 74 4d 75 74 65 78 29 3b 0a 20 0a 20 20 20 artMutex);. .
1900: 20 20 4c 6f 63 6b 54 65 73 74 43 6f 6e 74 65 78 LockTestContex
1910: 74 28 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 t(contextPtr->nu
1920: 6d 52 65 63 75 72 73 69 6f 6e 73 29 3b 0a 20 20 mRecursions);.
1930: 20 20 20 77 68 69 6c 65 20 28 63 6f 6e 74 65 78 while (contex
1940: 74 50 74 72 2d 3e 75 2e 71 75 65 75 65 2e 74 6f tPtr->u.queue.to
1950: 74 61 6c 45 6e 71 75 65 75 65 64 20 3c 20 6c 69 talEnqueued < li
1960: 6d 69 74 29 20 7b 0a 20 09 69 66 20 28 63 6f 6e mit) {. .if (con
1970: 74 65 78 74 50 74 72 2d 3e 75 2e 71 75 65 75 65 textPtr->u.queue
1980: 2e 61 76 61 69 6c 61 62 6c 65 20 3d 3d 20 63 6f .available == co
1990: 6e 74 65 78 74 50 74 72 2d 3e 75 2e 71 75 65 75 ntextPtr->u.queu
19a0: 65 2e 63 61 70 61 63 69 74 79 29 20 7b 0a 20 09 e.capacity) {. .
19b0: 20 20 20 20 54 63 6c 5f 54 69 6d 65 20 62 65 66 Tcl_Time bef
19c0: 6f 72 65 2c 20 61 66 74 65 72 3b 0a 40 40 20 2d ore, after;.@@ -
19d0: 35 30 31 2c 31 34 20 2b 35 33 36 2c 31 38 20 40 501,14 +536,18 @
19e0: 40 0a 20 0a 20 20 20 20 20 2f 2a 20 4c 69 6d 69 @. . /* Limi
19f0: 74 20 6f 6e 20 74 6f 74 61 6c 20 6e 75 6d 62 65 t on total numbe
1a00: 72 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 20 r of operations
1a10: 61 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 across all threa
1a20: 64 73 20 2a 2f 0a 20 20 20 20 20 54 63 6c 5f 57 ds */. Tcl_W
1a30: 69 64 65 55 49 6e 74 20 6c 69 6d 69 74 3b 0a 20 ideUInt limit;.
1a40: 20 20 20 20 6c 69 6d 69 74 20 3d 20 63 6f 6e 74 limit = cont
1a50: 65 78 74 50 74 72 2d 3e 6e 75 6d 54 68 72 65 61 extPtr->numThrea
1a60: 64 73 20 2a 20 28 54 63 6c 5f 57 69 64 65 55 49 ds * (Tcl_WideUI
1a70: 6e 74 29 20 63 6f 6e 74 65 78 74 50 74 72 2d 3e nt) contextPtr->
1a80: 6e 75 6d 49 74 65 72 61 74 69 6f 6e 73 3b 0a 20 numIterations;.
1a90: 0a 2d 20 20 20 20 2f 2a 20 53 70 69 6e 20 77 61 .- /* Spin wa
1aa0: 69 74 20 75 6e 74 69 6c 20 67 69 76 65 6e 20 74 it until given t
1ab0: 68 65 20 72 75 6e 20 73 69 67 6e 61 6c 20 2a 2f he run signal */
1ac0: 0a 2d 20 20 20 20 77 68 69 6c 65 20 28 6d 75 74 .- while (mut
1ad0: 65 78 54 68 72 65 61 64 43 6f 75 6e 74 20 3c 20 exThreadCount <
1ae0: 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 54 contextPtr->numT
1af0: 68 72 65 61 64 73 29 20 7b 0a 2d 09 59 69 65 6c hreads) {.-.Yiel
1b00: 64 54 6f 4f 74 68 65 72 54 68 72 65 61 64 73 28 dToOtherThreads(
1b10: 29 3b 0a 2b 20 20 20 20 2f 2a 20 57 61 69 74 20 );.+ /* Wait
1b20: 66 6f 72 20 73 74 61 72 74 20 73 69 67 6e 61 6c for start signal
1b30: 20 2a 2f 0a 2b 20 20 20 20 54 63 6c 5f 4d 75 74 */.+ Tcl_Mut
1b40: 65 78 4c 6f 63 6b 28 26 74 68 72 65 61 64 53 74 exLock(&threadSt
1b50: 61 72 74 4d 75 74 65 78 29 3b 0a 2b 20 20 20 20 artMutex);.+
1b60: 63 6f 6e 74 65 78 74 50 74 72 2d 3e 6e 75 6d 53 contextPtr->numS
1b70: 74 61 72 74 65 64 2b 2b 3b 0a 2b 20 20 20 20 54 tarted++;.+ T
1b80: 63 6c 5f 43 6f 6e 64 69 74 69 6f 6e 4e 6f 74 69 cl_ConditionNoti
1b90: 66 79 28 26 74 68 72 65 61 64 53 74 61 72 74 43 fy(&threadStartC
1ba0: 6f 6e 64 29 3b 0a 2b 20 20 20 20 77 68 69 6c 65 ond);.+ while
1bb0: 20 28 21 63 6f 6e 74 65 78 74 50 74 72 2d 3e 72 (!contextPtr->r
1bc0: 75 6e 29 20 7b 0a 2b 09 54 63 6c 5f 43 6f 6e 64 un) {.+.Tcl_Cond
1bd0: 69 74 69 6f 6e 57 61 69 74 28 26 74 68 72 65 61 itionWait(&threa
1be0: 64 53 74 61 72 74 43 6f 6e 64 2c 20 26 74 68 72 dStartCond, &thr
1bf0: 65 61 64 53 74 61 72 74 4d 75 74 65 78 2c 20 4e eadStartMutex, N
1c00: 55 4c 4c 29 3b 0a 20 20 20 20 20 7d 0a 2b 20 20 ULL);. }.+
1c10: 20 20 54 63 6c 5f 4d 75 74 65 78 55 6e 6c 6f 63 Tcl_MutexUnloc
1c20: 6b 28 26 74 68 72 65 61 64 53 74 61 72 74 4d 75 k(&threadStartMu
1c30: 74 65 78 29 3b 0a 20 0a 20 20 20 20 20 4c 6f 63 tex);. . Loc
1c40: 6b 54 65 73 74 43 6f 6e 74 65 78 74 28 63 6f 6e kTestContext(con
1c50: 74 65 78 74 50 74 72 2d 3e 6e 75 6d 52 65 63 75 textPtr->numRecu
1c60: 72 73 69 6f 6e 73 29 3b 0a 20 20 20 20 20 77 68 rsions);. wh
1c70: 69 6c 65 20 28 63 6f 6e 74 65 78 74 50 74 72 2d ile (contextPtr-
1c80: 3e 75 2e 71 75 65 75 65 2e 74 6f 74 61 6c 44 65 >u.queue.totalDe
1c90: 71 75 65 75 65 64 20 3c 20 6c 69 6d 69 74 29 20 queued < limit)
1ca0: 7b 0a 20 09 69 66 20 28 63 6f 6e 74 65 78 74 50 {. .if (contextP
1cb0: 74 72 2d 3e 75 2e 71 75 65 75 65 2e 61 76 61 69 tr->u.queue.avai
1cc0: 6c 61 62 6c 65 20 3d 3d 20 30 29 20 7b 0a 20 09 lable == 0) {. .
1cd0: 20 20 20 20 54 63 6c 5f 54 69 6d 65 20 62 65 66 Tcl_Time bef
1ce0: 6f 72 65 2c 20 61 66 74 65 72 3b 0a 0a ore, after;..