From a083198d1f3f7108b6455c4dad72ac0d85a2015a Mon Sep 17 00:00:00 2001 From: dkf Date: Sat, 29 Nov 2008 12:18:34 +0000 Subject: Code now simple enough that we can improve its performance by applying the double-checked locking pattern. --- ChangeLog | 3 ++- generic/tclThreadStorage.c | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9e5f936..e9cafe5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,7 +2,8 @@ * generic/tclThreadStorage.c: General revisions to make code clearer and more like the style used in the rest of the core. Includes adding - more comments and explanation of what is going on. + more comments and explanation of what is going on. Reduce the amount + of locking required. 2008-11-27 Alexandre Ferrieux 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 -- cgit v0.12