diff options
author | dkf <dkf@noemail.net> | 2008-11-29 12:18:33 (GMT) |
---|---|---|
committer | dkf <dkf@noemail.net> | 2008-11-29 12:18:33 (GMT) |
commit | 762a4bd90b70236504629d8b8529937a0ea93577 (patch) | |
tree | bd50c52cbee4c650bd30aeff6ef038bf15441f18 /generic/tclThreadStorage.c | |
parent | 2341a08a48da134e546e692faf81bf6fb40ec1f3 (diff) | |
download | tcl-762a4bd90b70236504629d8b8529937a0ea93577.zip tcl-762a4bd90b70236504629d8b8529937a0ea93577.tar.gz tcl-762a4bd90b70236504629d8b8529937a0ea93577.tar.bz2 |
Code now simple enough that we can improve its performance by applying the
double-checked locking pattern.
FossilOrigin-Name: 3f0a0fe4bccdd86f8d6847b687dd137cf7f45e52
Diffstat (limited to 'generic/tclThreadStorage.c')
-rw-r--r-- | generic/tclThreadStorage.c | 23 |
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 |