Skip to content

Commit

Permalink
ValuePlug : Add now argument to clearHashCache() method
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaddon committed Jul 10, 2023
1 parent df1b76e commit fff8815
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ API
- OpenColorIOAlgo : Added a new namespace that allows the OpenColorIO config and working space to be defined via the Gaffer context.
- OpenColorIOConfigPlug : Added a new plug type to aid in configuring the OpenColorIO context for a ScriptNode.
- ImageReader/ImageWriter : Added a `config` argument to the `DefaultColorSpaceFunction` definition. This is passed the OpenColorIO config currently being used by the node.
- ValuePlugs : Added Python bindings for `ValueType` type alias.
- ValuePlug :
- Added Python bindings for derived classes' `ValueType` type alias.
- Added `now` argument to `clearHashCache()` method.
- Color4fVectorDataPlug : Added a plug type for storing arrays of `Color4f`.
- TypedObjectPlug : Added default value for `direction` and `defaultValue` constructor arguments.
- VectorDataWidget : Added `setErrored()` and `getErrored()` methods to control an error state. Errors are reflected by a red background colour.
Expand Down
8 changes: 5 additions & 3 deletions include/Gaffer/ValuePlug.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ class GAFFER_API ValuePlug : public Plug
/// Returns the total number of entries in both global and per-thread hash caches
static size_t hashCacheTotalUsage();
/// Clears the hash cache.
/// > Note : Clearing occurs on a per-thread basis as and when
/// > each thread next accesses the cache.
static void clearHashCache();
/// > Note : By default, clearing occurs on a per-thread basis as
/// > and when each thread next accesses its cache. Pass `now = true`
/// > to force all thread-local caches to be cleared immediately
/// > (this is not thread-safe with respect to concurrent computations).
static void clearHashCache( bool now = false );

/// The standard hash cache mode relies on correctly implemented
/// affects() methods to selectively clear the cache for dirtied
Expand Down
25 changes: 17 additions & 8 deletions src/Gaffer/ValuePlug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class ValuePlug::HashProcess : public Process
g_globalCache.setMaxCost( g_cacheSizeLimit );
}

static void clearCache()
static void clearCache( bool now = false )
{
g_globalCache.clear();
// It's not documented explicitly, but it is safe to iterate over an
Expand All @@ -341,11 +341,20 @@ class ValuePlug::HashProcess : public Process
tbb::enumerable_thread_specific<ThreadData>::iterator it, eIt;
for( it = g_threadData.begin(), eIt = g_threadData.end(); it != eIt; ++it )
{
// We can't clear the cache now, because it is most likely
// in use by the owning thread. Instead we set this flag to
// politely request that the thread clears the cache itself
// at its earliest convenience - in the HashProcess constructor.
it->clearCache.store( 1, std::memory_order_release );
if( now )
{
// Not thread-safe - caller is responsible for ensuring there
// are no concurrent computes.
it->cache.clear();
}
else
{
// We can't clear the cache now, because it is most likely
// in use by the owning thread. Instead we set this flag to
// politely request that the thread clears the cache itself
// at its earliest convenience - in the HashProcess constructor.
it->clearCache.store( 1, std::memory_order_release );
}
}
}

Expand Down Expand Up @@ -1239,9 +1248,9 @@ void ValuePlug::setHashCacheSizeLimit( size_t maxEntriesPerThread )
HashProcess::setCacheSizeLimit( maxEntriesPerThread );
}

void ValuePlug::clearHashCache()
void ValuePlug::clearHashCache( bool now )
{
HashProcess::clearCache();
HashProcess::clearCache( now );
}

size_t ValuePlug::hashCacheTotalUsage()
Expand Down
2 changes: 1 addition & 1 deletion src/GafferModule/ValuePlugBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void GafferModule::bindValuePlug()
.staticmethod( "setHashCacheSizeLimit" )
.def( "hashCacheTotalUsage", &ValuePlug::hashCacheTotalUsage )
.staticmethod( "hashCacheTotalUsage" )
.def( "clearHashCache", &ValuePlug::clearHashCache )
.def( "clearHashCache", &ValuePlug::clearHashCache, arg( "now" ) = false )
.staticmethod( "clearHashCache" )
.def( "getHashCacheMode", &ValuePlug::getHashCacheMode )
.staticmethod( "getHashCacheMode" )
Expand Down

0 comments on commit fff8815

Please sign in to comment.