Skip to content

Issue 315: making cereal thread safe #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ if(NOT CMAKE_VERSION VERSION_LESS 3.0) # installing cereal requires INTERFACE li
option(JUST_INSTALL_CEREAL "Don't do anything besides installing the library" OFF)
endif()

option(THREAD_SAFE "Use mutexes to ensure thread safety" OFF)
if(THREAD_SAFE)
add_definitions(-DCEREAL_THREAD_SAFE)
endif()

if(NOT MSVC)
set(CMAKE_CXX_FLAGS "-Wall -Werror -g -Wextra -Wshadow -pedantic ${CMAKE_CXX_FLAGS}")
if(CMAKE_VERSION VERSION_LESS 3.1)
Expand Down
10 changes: 10 additions & 0 deletions include/cereal/details/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#include <memory>
#include <unordered_map>
#include <stdexcept>
#ifdef CEREAL_THREAD_SAFE
#include <mutex>
#endif

#include <cereal/macros.hpp>
#include <cereal/details/static_object.hpp>
Expand Down Expand Up @@ -393,8 +396,15 @@ namespace cereal
{
std::unordered_map<std::size_t, std::uint32_t> mapping;

#ifdef CEREAL_THREAD_SAFE
std::mutex mutex;
#endif

std::uint32_t find( std::size_t hash, std::uint32_t version )
{
#ifdef CEREAL_THREAD_SAFE
std::unique_lock<std::mutex> lock(mutex);
#endif
const auto result = mapping.emplace( hash, version );
return result.first->second;
}
Expand Down
12 changes: 11 additions & 1 deletion include/cereal/details/polymorphic_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
#include <functional>
#include <typeindex>
#include <map>
#ifdef CEREAL_THREAD_SAFE
#include <mutex>
#endif

//! Binds a polymorhic type to all registered archives
/*! This binds a polymorphic type to all compatible registered archives that
Expand Down Expand Up @@ -114,6 +117,9 @@ namespace cereal
{
//! Maps from base type index to a map from derived type index to caster
std::map<std::type_index, std::map<std::type_index, std::vector<PolymorphicCaster const*>>> map;
#ifdef CEREAL_THREAD_SAFE
std::mutex mapMutex;
#endif

//! Error message used for unregistered polymorphic casts
#define UNREGISTERED_POLYMORPHIC_CAST_EXCEPTION(LoadSave) \
Expand Down Expand Up @@ -218,7 +224,11 @@ namespace cereal
assuming dynamic type information is available */
PolymorphicVirtualCaster()
{
auto & baseMap = StaticObject<PolymorphicCasters>::getInstance().map;
auto & polymorphicCasters = StaticObject<PolymorphicCasters>::getInstance();
#ifdef CEREAL_THREAD_SAFE
std::unique_lock<std::mutex> lock(polymorphicCasters.mapMutex);
#endif
auto & baseMap = polymorphicCasters.map;
auto baseKey = std::type_index(typeid(Base));
auto lb = baseMap.lower_bound(baseKey);

Expand Down