2
0
Эх сурвалжийг харах

Made CTypeList threadsafe

AlexVinS 10 жил өмнө
parent
commit
0f9de1c560
2 өөрчлөгдсөн 10 нэмэгдсэн , 2 устгасан
  1. 1 0
      lib/Connection.cpp
  2. 9 2
      lib/Connection.h

+ 1 - 0
lib/Connection.cpp

@@ -450,6 +450,7 @@ CTypeList::TypeInfoPtr CTypeList::registerType( const std::type_info *type )
 
 ui16 CTypeList::getTypeID(const std::type_info * type) const
 {
+	TSharedLock lock(mx);
 	auto i = typeInfos.find(type);
 	if(i != typeInfos.end())
 		return i->second->typeID;

+ 9 - 2
lib/Connection.h

@@ -147,7 +147,11 @@ public:
 		const char *name;
 		std::vector<TypeInfoPtr> children, parents;
 	};
+	typedef boost::shared_mutex TMutex;
+	typedef boost::unique_lock<TMutex> TUniqueLock;
+	typedef boost::shared_lock<TMutex> TSharedLock;
 private:
+	mutable TMutex mx;
 
 	std::map<const std::type_info *, TypeInfoPtr, TypeComparer> typeInfos;
 	std::map<std::pair<TypeInfoPtr, TypeInfoPtr>, std::unique_ptr<const IPointerCaster>> casters; //for each pair <Base, Der> we provide a caster (each registered relations creates a single entry here)
@@ -161,6 +165,7 @@ private:
 	template<boost::any(IPointerCaster::*CastingFunction)(const boost::any &) const>
 	boost::any castHelper(boost::any inputPtr, const std::type_info *fromArg, const std::type_info *toArg) const
 	{
+		TSharedLock lock(mx);
 		auto typesSequence = castSequence(fromArg, toArg);
 
 		boost::any ptr = inputPtr;
@@ -179,13 +184,16 @@ private:
 		return ptr;
 	}
 
-	TypeInfoPtr registerType(const std::type_info *type);	
+	TypeInfoPtr getTypeDescriptor(const std::type_info *type, bool throws = true) const; //if not throws, failure returns nullptr
+
+	TypeInfoPtr registerType(const std::type_info *type);
 public:
 	CTypeList();
 
 	template <typename Base, typename Derived>
 	void registerType(const Base * b = nullptr, const Derived * d = nullptr)
 	{
+		TUniqueLock lock(mx);
 		static_assert(std::is_base_of<Base, Derived>::value, "First registerType template parameter needs to ba a base class of the second one.");
 		static_assert(std::has_virtual_destructor<Base>::value, "Base class needs to have a virtual destructor.");
 		static_assert(!std::is_same<Base, Derived>::value, "Parameters of registerTypes should be two diffrenet types.");
@@ -200,7 +208,6 @@ public:
 	}
 
 	ui16 getTypeID(const std::type_info *type) const;
-	TypeInfoPtr getTypeDescriptor(const std::type_info *type, bool throws = true) const; //if not throws, failure returns nullptr
 
 	template <typename T>
 	ui16 getTypeID(const T * t = nullptr) const