# HG changeset patch # User Sebastien Jodogne # Date 1719992365 -7200 # Node ID 2a2fae513366600b812e58f74b78037cc1aa38cf # Parent 3757a7b9e6f134b0475dea28dc56c64e017feda5 clarified locking in RestCallbacks diff -r 3757a7b9e6f1 -r 2a2fae513366 Sources/RestCallbacks.cpp --- a/Sources/RestCallbacks.cpp Tue Jul 02 19:16:45 2024 +0200 +++ b/Sources/RestCallbacks.cpp Wed Jul 03 09:39:25 2024 +0200 @@ -29,6 +29,7 @@ #include "PythonString.h" #include +#include class RestCallback : public boost::noncopyable @@ -63,8 +64,7 @@ }; -// Concurrent accesses to the callbacks are protected by the -// "PythonLock" (GIL mutex) +static boost::shared_mutex restCallbacksMutex_; static std::list restCallbacks_; @@ -72,14 +72,16 @@ const char* uri, const OrthancPluginHttpRequest* request) { - PythonLock lock; - + boost::shared_lock restCallbacksLock(restCallbacksMutex_); + for (std::list::const_iterator it = restCallbacks_.begin(); it != restCallbacks_.end(); ++it) { assert(*it != NULL); if ((*it)->IsMatch(uri)) { + PythonLock lock; + /** * Construct an instance object of the "orthanc.RestOutput" * class. This is done by calling the constructor function @@ -217,11 +219,15 @@ PyErr_SetString(PyExc_ValueError, "Expected a string (URI) and a callback function"); return NULL; } + + { + boost::unique_lock restCallbacksLock(restCallbacksMutex_); + + ORTHANC_PLUGINS_LOG_INFO("Registering a Python REST callback on URI: " + std::string(uri)); + OrthancPlugins::RegisterRestCallback(uri, true /* thread safe */); - ORTHANC_PLUGINS_LOG_INFO("Registering a Python REST callback on URI: " + std::string(uri)); - OrthancPlugins::RegisterRestCallback(uri, true /* thread safe */); - - restCallbacks_.push_back(new RestCallback(uri, callback)); + restCallbacks_.push_back(new RestCallback(uri, callback)); + } Py_INCREF(Py_None); return Py_None; @@ -231,8 +237,8 @@ void FinalizeRestCallbacks() { - PythonLock lock; - + boost::unique_lock restCallbacksLock(restCallbacksMutex_); + for (std::list::iterator it = restCallbacks_.begin(); it != restCallbacks_.end(); ++it) {