From 7302bdff8e0ca23b11784f39ffeafaea46e02212 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 2 Jun 2023 13:02:59 -0400 Subject: [PATCH] Provide operations flexibility in what resources they lock. --- .../notes/operation-virtualize-locktypes.rst | 12 ++++++++++++ smtk/operation/Operation.cxx | 7 ++++++- smtk/operation/Operation.h | 18 ++++++++++++++++-- smtk/operation/SpecificationOps.h | 5 ----- 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 doc/release/notes/operation-virtualize-locktypes.rst diff --git a/doc/release/notes/operation-virtualize-locktypes.rst b/doc/release/notes/operation-virtualize-locktypes.rst new file mode 100644 index 0000000000..bb5f9a7d06 --- /dev/null +++ b/doc/release/notes/operation-virtualize-locktypes.rst @@ -0,0 +1,12 @@ +Operation System +---------------- + +Operations can customize what resources they lock +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The base :smtk:`Operation ` class now provides +a virtual method, ``identifyLocksRequired()`` to allow subclasses to customize +the default set of resources to be locked and their lock levels (read vs. write). +This allows operations that may need to lock components related (say by +:smtk:`Links `) to external resources to include those +resources in the operation's lock set. diff --git a/smtk/operation/Operation.cxx b/smtk/operation/Operation.cxx index c5df659f8c..adaa518c09 100644 --- a/smtk/operation/Operation.cxx +++ b/smtk/operation/Operation.cxx @@ -123,10 +123,15 @@ bool Operation::ableToOperate() return this->parameters()->isValid(); } +ResourceAccessMap Operation::identifyLocksRequired() +{ + return extractResourcesAndLockTypes(this->parameters()); +} + Operation::Result Operation::operate() { // Gather all requested resources and their lock types. - auto resourcesAndLockTypes = extractResourcesAndLockTypes(this->parameters()); + auto resourcesAndLockTypes = this->identifyLocksRequired(); // Mutex to prevent multiple Operations from locking resources at the same // time (which could result in deadlock). diff --git a/smtk/operation/Operation.h b/smtk/operation/Operation.h index 07055cc16d..9e8becc705 100644 --- a/smtk/operation/Operation.h +++ b/smtk/operation/Operation.h @@ -10,11 +10,13 @@ #ifndef smtk_operation_Operation_h #define smtk_operation_Operation_h -#include "smtk/CoreExports.h" +#include "smtk/resource/Lock.h" + #include "smtk/PublicPointerDefs.h" #include "smtk/SharedFromThis.h" #include +#include #include #include #include @@ -38,6 +40,12 @@ class Operation; using Handler = std::function&)>; +/// Hold a set of resources to be locked for an operation along with the type of lock to acquire. +using ResourceAccessMap = std::map< + std::weak_ptr, + smtk::resource::LockType, + std::owner_less>>; + /// Operation is a base class for all SMTK operations. SMTK operations are /// essentially functors that operate on SMTK resources and resource components. /// Their input parameters and output results are described by an SMTK attribute @@ -191,7 +199,13 @@ public: protected: Operation(); - // Perform the actual operation and construct the result. + /// Identify resources to lock, and whether to lock them for reading or writing. + /// + /// The default implementation simply calls extractResourceAndLockTypes() + /// on the operation's parameters. + virtual ResourceAccessMap identifyLocksRequired(); + + /// Perform the actual operation and construct the result. virtual Result operateInternal() = 0; // Apply post-processing to the result object. This method should not modify diff --git a/smtk/operation/SpecificationOps.h b/smtk/operation/SpecificationOps.h index ede605d0bc..3414c7c9bf 100644 --- a/smtk/operation/SpecificationOps.h +++ b/smtk/operation/SpecificationOps.h @@ -34,11 +34,6 @@ namespace operation /// an API around Specification, giving it functions that are unique to its role /// as an operation specification. -typedef std::map< - std::weak_ptr, - smtk::resource::LockType, - std::owner_less>> - ResourceAccessMap; typedef std::vector ComponentDefinitionVector; /// Return a new set of parameters for an operation. -- GitLab