summaryrefslogtreecommitdiffstats
path: root/generic/tclThreadStorage.c
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2008-11-29 12:18:34 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2008-11-29 12:18:34 (GMT)
commita083198d1f3f7108b6455c4dad72ac0d85a2015a (patch)
treebd50c52cbee4c650bd30aeff6ef038bf15441f18 /generic/tclThreadStorage.c
parentc8e7eb6d47e9cd382589a64d04200b61344351eb (diff)
downloadtcl-a083198d1f3f7108b6455c4dad72ac0d85a2015a.zip
tcl-a083198d1f3f7108b6455c4dad72ac0d85a2015a.tar.gz
tcl-a083198d1f3f7108b6455c4dad72ac0d85a2015a.tar.bz2
Code now simple enough that we can improve its performance by applying the
double-checked locking pattern.
Diffstat (limited to 'generic/tclThreadStorage.c')
-rw-r--r--generic/tclThreadStorage.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/generic/tclThreadStorage.c b/generic/tclThreadStorage.c
index 71fa1e9..1568998 100644
--- a/generic/tclThreadStorage.c
+++ b/generic/tclThreadStorage.c
@@ -10,7 +10,7 @@
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclThreadStorage.c,v 1.17 2008/11/29 12:15:30 dkf Exp $
+ * RCS: @(#) $Id: tclThreadStorage.c,v 1.18 2008/11/29 12:18:35 dkf Exp $
*/
#include "tclInt.h"
@@ -222,18 +222,23 @@ TclThreadStorageKeySet(
/*
* Get the lock while we check if this TSD is new or not. Note that this
- * is the only place where Tcl_ThreadDataKey values are set.
+ * is the only place where Tcl_ThreadDataKey values are set. We use a
+ * double-checked lock to try to avoid having to grab this lock a lot,
+ * since it is on quite a few critical paths and will only get set once in
+ * each location.
*/
- Tcl_MutexLock(&tsdMaster.mutex);
if (keyPtr->offset == 0) {
- /*
- * The Tcl_ThreadDataKey hasn't been used yet. Make a new one.
- */
-
- keyPtr->offset = ++tsdMaster.counter;
+ Tcl_MutexLock(&tsdMaster.mutex);
+ if (keyPtr->offset == 0) {
+ /*
+ * The Tcl_ThreadDataKey hasn't been used yet. Make a new one.
+ */
+
+ keyPtr->offset = ++tsdMaster.counter;
+ }
+ Tcl_MutexUnlock(&tsdMaster.mutex);
}
- Tcl_MutexUnlock(&tsdMaster.mutex);
/*
* Check if this is the first time this Tcl_ThreadDataKey has been used