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 {
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);
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<std::mutex> 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<Serializer> FormatRegistry::getSerializerForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const {
return resultWithReadLock<std::shared_ptr<Serializer>>([&](){
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<std::mutex> lock(*const_cast<std::mutex*>(&_libraryLock));
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 {
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);
}

View file

@ -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<Serializer::Factory>& factory) :

View file

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

View file

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