Replace ReadWriteLockable with std::lock_guard in HFMFormatRegistry/ModelFormatRegistry

This commit is contained in:
sabrina-shanman 2018-12-03 11:24:31 -08:00
parent 6ec0e42ded
commit 0da4669370
4 changed files with 30 additions and 21 deletions

View file

@ -14,38 +14,44 @@
namespace hfm { namespace hfm {
FormatRegistry::MIMETypeID FormatRegistry::registerMIMEType(const MIMEType& mimeType, std::unique_ptr<Serializer::Factory>& supportedFactory) { FormatRegistry::MIMETypeID FormatRegistry::registerMIMEType(const MIMEType& mimeType, std::unique_ptr<Serializer::Factory>& supportedFactory) {
std::lock_guard<std::mutex> lock(_libraryLock);
MIMETypeID id = _mimeTypeLibrary.registerMIMEType(mimeType); MIMETypeID id = _mimeTypeLibrary.registerMIMEType(mimeType);
withWriteLock([&](){ _supportedFormats.emplace_back(id, supportedFactory);
_supportedFormats.emplace_back(id, supportedFactory);
});
return id; return id;
} }
void FormatRegistry::unregisterMIMEType(const MIMETypeID& mimeTypeID) { void FormatRegistry::unregisterMIMEType(const MIMETypeID& mimeTypeID) {
withWriteLock([&](){ std::lock_guard<std::mutex> lock(_libraryLock);
for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) {
if ((*it).mimeTypeID == mimeTypeID) { for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) {
_supportedFormats.erase(it); if ((*it).mimeTypeID == mimeTypeID) {
break; _supportedFormats.erase(it);
} break;
} }
}); }
_mimeTypeLibrary.unregisterMIMEType(mimeTypeID); _mimeTypeLibrary.unregisterMIMEType(mimeTypeID);
} }
std::shared_ptr<Serializer> FormatRegistry::getSerializerForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const { std::shared_ptr<Serializer> FormatRegistry::getSerializerForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const {
return resultWithReadLock<std::shared_ptr<Serializer>>([&](){ // TODO: shared_lock in C++14
for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { std::lock_guard<std::mutex> lock(*const_cast<std::mutex*>(&_libraryLock));
if ((*it).mimeTypeID == mimeTypeID) {
return (*it).factory->get(); for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) {
} if ((*it).mimeTypeID == mimeTypeID) {
return (*it).factory->get();
} }
return std::shared_ptr<Serializer>(); }
}); return std::shared_ptr<Serializer>();
} }
std::shared_ptr<Serializer> FormatRegistry::getSerializerForMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const { std::shared_ptr<Serializer> 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<std::mutex> lock(*const_cast<std::mutex*>(&_libraryLock));
id = _mimeTypeLibrary.findMatchingMIMEType(data, mapping, url, webMediaType);
}
return getSerializerForMIMETypeID(id); return getSerializerForMIMETypeID(id);
} }

View file

@ -18,7 +18,7 @@
namespace hfm { namespace hfm {
class FormatRegistry : public ReadWriteLockable { class FormatRegistry {
public: public:
using MIMETypeID = MIMETypeLibrary::ID; using MIMETypeID = MIMETypeLibrary::ID;
static const MIMETypeID INVALID_MIME_TYPE_ID { MIMETypeLibrary::INVALID_ID }; static const MIMETypeID INVALID_MIME_TYPE_ID { MIMETypeLibrary::INVALID_ID };
@ -31,6 +31,7 @@ public:
protected: protected:
MIMETypeLibrary _mimeTypeLibrary; MIMETypeLibrary _mimeTypeLibrary;
std::mutex _libraryLock;
class SupportedFormat { class SupportedFormat {
public: public:
SupportedFormat(const MIMETypeID& mimeTypeID, std::unique_ptr<Serializer::Factory>& factory) : SupportedFormat(const MIMETypeID& mimeTypeID, std::unique_ptr<Serializer::Factory>& factory) :

View file

@ -27,9 +27,10 @@ void ModelFormatRegistry::addDefaultFormats() {
void ModelFormatRegistry::addFormat(const std::shared_ptr<hfm::Format>& format) { void ModelFormatRegistry::addFormat(const std::shared_ptr<hfm::Format>& format) {
format->registerFormat(*this); format->registerFormat(*this);
withWriteLock([&](){ {
std::lock_guard<std::mutex> lock(_formatsLock);
formats.push_back(format); formats.push_back(format);
}); }
} }
ModelFormatRegistry::~ModelFormatRegistry() { ModelFormatRegistry::~ModelFormatRegistry() {

View file

@ -24,6 +24,7 @@ public:
protected: protected:
void addDefaultFormats(); void addDefaultFormats();
std::vector<std::shared_ptr<hfm::Format>> formats; std::vector<std::shared_ptr<hfm::Format>> formats;
std::mutex _formatsLock;
}; };
#endif // hifi_ModelFormatRegistry_h #endif // hifi_ModelFormatRegistry_h