diff --git a/libraries/hfm/src/hfm/HFMFormatRegistry.cpp b/libraries/hfm/src/hfm/HFMFormatRegistry.cpp index 1be05e99d9..2a537af151 100644 --- a/libraries/hfm/src/hfm/HFMFormatRegistry.cpp +++ b/libraries/hfm/src/hfm/HFMFormatRegistry.cpp @@ -14,38 +14,44 @@ namespace hfm { FormatRegistry::MIMETypeID FormatRegistry::registerMIMEType(const MIMEType& mimeType, std::unique_ptr& supportedFactory) { + std::lock_guard lock(_libraryLock); + MIMETypeID id = _mimeTypeLibrary.registerMIMEType(mimeType); - withWriteLock([&](){ - _supportedFormats.emplace_back(id, supportedFactory); - }); + _supportedFormats.emplace_back(id, supportedFactory); return id; } void FormatRegistry::unregisterMIMEType(const MIMETypeID& mimeTypeID) { - withWriteLock([&](){ - for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { - if ((*it).mimeTypeID == mimeTypeID) { - _supportedFormats.erase(it); - break; - } + std::lock_guard lock(_libraryLock); + + for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { + if ((*it).mimeTypeID == mimeTypeID) { + _supportedFormats.erase(it); + break; } - }); + } _mimeTypeLibrary.unregisterMIMEType(mimeTypeID); } std::shared_ptr FormatRegistry::getSerializerForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const { - return resultWithReadLock>([&](){ - for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { - if ((*it).mimeTypeID == mimeTypeID) { - return (*it).factory->get(); - } + // TODO: shared_lock in C++14 + std::lock_guard lock(*const_cast(&_libraryLock)); + + for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { + if ((*it).mimeTypeID == mimeTypeID) { + return (*it).factory->get(); } - return std::shared_ptr(); - }); + } + return std::shared_ptr(); } std::shared_ptr FormatRegistry::getSerializerForMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const { - MIMETypeID id = _mimeTypeLibrary.findMatchingMIMEType(data, mapping, url, webMediaType); + MIMETypeID id; + { + // TODO: shared_lock in C++14 + std::lock_guard lock(*const_cast(&_libraryLock)); + id = _mimeTypeLibrary.findMatchingMIMEType(data, mapping, url, webMediaType); + } return getSerializerForMIMETypeID(id); } diff --git a/libraries/hfm/src/hfm/HFMFormatRegistry.h b/libraries/hfm/src/hfm/HFMFormatRegistry.h index 6e0bd5c66c..6a16dce0b9 100644 --- a/libraries/hfm/src/hfm/HFMFormatRegistry.h +++ b/libraries/hfm/src/hfm/HFMFormatRegistry.h @@ -18,7 +18,7 @@ namespace hfm { -class FormatRegistry : public ReadWriteLockable { +class FormatRegistry { public: using MIMETypeID = MIMETypeLibrary::ID; static const MIMETypeID INVALID_MIME_TYPE_ID { MIMETypeLibrary::INVALID_ID }; @@ -31,6 +31,7 @@ public: protected: MIMETypeLibrary _mimeTypeLibrary; + std::mutex _libraryLock; class SupportedFormat { public: SupportedFormat(const MIMETypeID& mimeTypeID, std::unique_ptr& factory) : diff --git a/libraries/model-networking/src/model-networking/ModelFormatRegistry.cpp b/libraries/model-networking/src/model-networking/ModelFormatRegistry.cpp index ccc89eb402..8adc4ea5d9 100644 --- a/libraries/model-networking/src/model-networking/ModelFormatRegistry.cpp +++ b/libraries/model-networking/src/model-networking/ModelFormatRegistry.cpp @@ -27,9 +27,10 @@ void ModelFormatRegistry::addDefaultFormats() { void ModelFormatRegistry::addFormat(const std::shared_ptr& format) { format->registerFormat(*this); - withWriteLock([&](){ + { + std::lock_guard lock(_formatsLock); formats.push_back(format); - }); + } } ModelFormatRegistry::~ModelFormatRegistry() { diff --git a/libraries/model-networking/src/model-networking/ModelFormatRegistry.h b/libraries/model-networking/src/model-networking/ModelFormatRegistry.h index becc53fc1b..c88873eaca 100644 --- a/libraries/model-networking/src/model-networking/ModelFormatRegistry.h +++ b/libraries/model-networking/src/model-networking/ModelFormatRegistry.h @@ -24,6 +24,7 @@ public: protected: void addDefaultFormats(); std::vector> formats; + std::mutex _formatsLock; }; #endif // hifi_ModelFormatRegistry_h