Commit 20e74fc9 authored by Jussi Lindgren's avatar Jussi Lindgren

Kernel: Made some more managers thread safe

parent 05bf8ac0
......@@ -29,6 +29,8 @@ CAlgorithmManager::~CAlgorithmManager(void)
CIdentifier CAlgorithmManager::createAlgorithm(
const CIdentifier& rAlgorithmClassIdentifier)
{
boost::mutex::scoped_lock lock(m_oLock);
const IAlgorithmDesc* l_pAlgorithmDesc=NULL;
IAlgorithm* l_pAlgorithm=getKernelContext().getPluginManager().createAlgorithm(rAlgorithmClassIdentifier, &l_pAlgorithmDesc);
if(!l_pAlgorithm || !l_pAlgorithmDesc)
......@@ -49,6 +51,8 @@ CIdentifier CAlgorithmManager::createAlgorithm(
boolean CAlgorithmManager::releaseAlgorithm(
const CIdentifier& rAlgorithmIdentifier)
{
boost::mutex::scoped_lock lock(m_oLock);
map < CIdentifier, pair < CAlgorithm*, CAlgorithmProxy* > >::iterator itAlgorithm;
itAlgorithm=m_vAlgorithm.find(rAlgorithmIdentifier);
if(itAlgorithm==m_vAlgorithm.end())
......@@ -76,6 +80,8 @@ boolean CAlgorithmManager::releaseAlgorithm(
boolean CAlgorithmManager::releaseAlgorithm(
IAlgorithmProxy& rAlgorithm)
{
boost::mutex::scoped_lock lock(m_oLock);
map < CIdentifier, pair < CAlgorithm*, CAlgorithmProxy* > >::iterator itAlgorithm;
for(itAlgorithm=m_vAlgorithm.begin(); itAlgorithm!=m_vAlgorithm.end(); itAlgorithm++)
{
......@@ -105,6 +111,8 @@ boolean CAlgorithmManager::releaseAlgorithm(
IAlgorithmProxy& CAlgorithmManager::getAlgorithm(
const CIdentifier& rAlgorithmIdentifier)
{
boost::mutex::scoped_lock lock(m_oLock);
map < CIdentifier, pair < CAlgorithm*, CAlgorithmProxy* > >::const_iterator itAlgorithm;
itAlgorithm=m_vAlgorithm.find(rAlgorithmIdentifier);
if(itAlgorithm==m_vAlgorithm.end())
......@@ -117,6 +125,8 @@ IAlgorithmProxy& CAlgorithmManager::getAlgorithm(
CIdentifier CAlgorithmManager::getNextAlgorithmIdentifier(
const CIdentifier& rPreviousIdentifier) const
{
boost::mutex::scoped_lock lock(m_oLock);
map < CIdentifier, pair < CAlgorithm*, CAlgorithmProxy* > >::const_iterator itAlgorithm=m_vAlgorithm.begin();
if(rPreviousIdentifier==OV_UndefinedIdentifier)
......
......@@ -5,6 +5,9 @@
#include <map>
#include <boost/thread.hpp> // for mutex
#include <boost/thread/condition.hpp>
namespace OpenViBE
{
namespace Kernel
......@@ -39,6 +42,8 @@ namespace OpenViBE
protected:
std::map < OpenViBE::CIdentifier, std::pair < OpenViBE::Kernel::CAlgorithm*, OpenViBE::Kernel::CAlgorithmProxy* > > m_vAlgorithm;
mutable boost::mutex m_oLock; // Protects the array for threads
};
};
};
......
......@@ -178,12 +178,16 @@ CConfigurationManager::CConfigurationManager(const IKernelContext& rKernelContex
void CConfigurationManager::clear(void)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
m_vConfigurationToken.clear();
}
boolean CConfigurationManager::addConfigurationFromFile(
const CString& rFileNameWildCard)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
this->getLogManager() << LogLevel_Info << "Adding configuration file(s) [" << rFileNameWildCard << "]\n";
boolean l_bResult;
......@@ -201,6 +205,8 @@ CIdentifier CConfigurationManager::createConfigurationToken(
const CString& rConfigurationTokenName,
const CString& rConfigurationTokenValue)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(this->lookUpConfigurationTokenIdentifier(rConfigurationTokenName, false)!=OV_UndefinedIdentifier)
{
this->getLogManager() << LogLevel_Warning << "Configuration token name " << rConfigurationTokenName << " already exists\n";
......@@ -216,6 +222,8 @@ CIdentifier CConfigurationManager::createConfigurationToken(
boolean CConfigurationManager::releaseConfigurationToken(
const CIdentifier& rConfigurationTokenIdentifier)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, SConfigurationToken >::iterator itConfigurationToken=m_vConfigurationToken.find(rConfigurationTokenIdentifier);
if(itConfigurationToken==m_vConfigurationToken.end())
{
......@@ -229,6 +237,8 @@ boolean CConfigurationManager::releaseConfigurationToken(
CIdentifier CConfigurationManager::getNextConfigurationTokenIdentifier(
const CIdentifier& rPreviousConfigurationTokenIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, SConfigurationToken >::const_iterator itConfigurationToken;
if(rPreviousConfigurationTokenIdentifier==OV_UndefinedIdentifier)
......@@ -253,6 +263,8 @@ CIdentifier CConfigurationManager::getNextConfigurationTokenIdentifier(
CString CConfigurationManager::getConfigurationTokenName(
const CIdentifier& rConfigurationTokenIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, SConfigurationToken >::const_iterator itConfigurationToken=m_vConfigurationToken.find(rConfigurationTokenIdentifier);
if(itConfigurationToken!=m_vConfigurationToken.end())
{
......@@ -264,6 +276,8 @@ CString CConfigurationManager::getConfigurationTokenName(
CString CConfigurationManager::getConfigurationTokenValue(
const CIdentifier& rConfigurationTokenIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, SConfigurationToken >::const_iterator itConfigurationToken=m_vConfigurationToken.find(rConfigurationTokenIdentifier);
if(itConfigurationToken!=m_vConfigurationToken.end())
{
......@@ -278,6 +292,8 @@ boolean CConfigurationManager::setConfigurationTokenName(
const CIdentifier& rConfigurationTokenIdentifier,
const CString& rConfigurationTokenName)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(this->lookUpConfigurationTokenIdentifier(rConfigurationTokenName, false)!=OV_UndefinedIdentifier)
{
getLogManager() << LogLevel_Warning << "Configuration token " << rConfigurationTokenName << " already exists\n";
......@@ -299,6 +315,8 @@ boolean CConfigurationManager::setConfigurationTokenValue(
const CIdentifier& rConfigurationTokenIdentifier,
const CString& rConfigurationTokenValue)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, SConfigurationToken >::iterator itConfigurationToken=m_vConfigurationToken.find(rConfigurationTokenIdentifier);
if(itConfigurationToken==m_vConfigurationToken.end())
{
......@@ -314,6 +332,8 @@ boolean CConfigurationManager::addOrReplaceConfigurationToken(
const CString& rConfigurationTokenName,
const CString& rConfigurationTokenValue)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
const CIdentifier l_oOldIdentifier = this->lookUpConfigurationTokenIdentifier(rConfigurationTokenName, false);
if(l_oOldIdentifier == OV_UndefinedIdentifier)
{
......@@ -331,6 +351,8 @@ CIdentifier CConfigurationManager::lookUpConfigurationTokenIdentifier(
const CString& rConfigurationTokenName,
const boolean bRecursive) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, SConfigurationToken >::const_iterator itConfigurationToken=m_vConfigurationToken.begin();
while(itConfigurationToken!=m_vConfigurationToken.end())
{
......@@ -350,6 +372,8 @@ CIdentifier CConfigurationManager::lookUpConfigurationTokenIdentifier(
CString CConfigurationManager::lookUpConfigurationTokenValue(
const CString& rConfigurationTokenName) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, SConfigurationToken >::const_iterator itConfigurationToken=m_vConfigurationToken.begin();
while(itConfigurationToken!=m_vConfigurationToken.end())
{
......@@ -384,6 +408,8 @@ CString CConfigurationManager::expand(
CIdentifier CConfigurationManager::getUnusedIdentifier(void) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
uint64 l_ui64Identifier=(((uint64)rand())<<32)+((uint64)rand());
CIdentifier l_oResult;
std::map < CIdentifier, SConfigurationToken >::const_iterator i;
......
......@@ -6,6 +6,9 @@
#include <map>
#include <string>
#include <boost/thread.hpp> // for mutex
#include <boost/thread/condition.hpp>
namespace OpenViBE
{
namespace Kernel
......@@ -105,6 +108,9 @@ namespace OpenViBE
protected:
std::map < OpenViBE::CIdentifier, OpenViBE::Kernel::SConfigurationToken > m_vConfigurationToken;
mutable boost::recursive_mutex m_oLock; // Protects the array for threads.
};
};
};
......
#include "ovkCTypeManager.h"
#include "../ovk_tools.h"
#include <string>
......@@ -33,6 +35,8 @@ CTypeManager::CTypeManager(const IKernelContext& rKernelContext)
CIdentifier CTypeManager::getNextTypeIdentifier(
const CIdentifier& rPreviousIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
return getNextIdentifier< CString >(m_vName, rPreviousIdentifier);
}
......@@ -40,10 +44,13 @@ boolean CTypeManager::registerType(
const CIdentifier& rTypeIdentifier,
const CString& sTypeName)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(isRegistered(rTypeIdentifier))
{
return false;
}
m_vName[rTypeIdentifier]=sTypeName;
this->getLogManager() << LogLevel_Trace << "Registered type id " << rTypeIdentifier << " - " << sTypeName << "\n";
return true;
......@@ -54,6 +61,8 @@ boolean CTypeManager::registerStreamType(
const CString& sTypeName,
const CIdentifier& rParentTypeIdentifier)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(isRegistered(rTypeIdentifier))
{
return false;
......@@ -72,14 +81,19 @@ boolean CTypeManager::registerEnumerationType(
const CIdentifier& rTypeIdentifier,
const CString& sTypeName)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(isRegistered(rTypeIdentifier))
{
return false;
}
m_vName[rTypeIdentifier]=sTypeName;
m_vEnumeration[rTypeIdentifier];
this->getLogManager() << LogLevel_Trace << "Registered enumeration type id " << rTypeIdentifier << " - " << sTypeName << "\n";
return true;
}
boolean CTypeManager::registerEnumerationEntry(
......@@ -87,6 +101,8 @@ boolean CTypeManager::registerEnumerationEntry(
const CString& sEntryName,
const uint64 ui64EntryValue)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::iterator itEnumeration=m_vEnumeration.find(rTypeIdentifier);
if(itEnumeration==m_vEnumeration.end())
{
......@@ -105,6 +121,8 @@ boolean CTypeManager::registerBitMaskType(
const CIdentifier& rTypeIdentifier,
const CString& sTypeName)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(isRegistered(rTypeIdentifier))
{
return false;
......@@ -120,6 +138,8 @@ boolean CTypeManager::registerBitMaskEntry(
const CString& sEntryName,
const uint64 ui64EntryValue)
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::iterator itBitMask=m_vBitMask.find(rTypeIdentifier);
if(itBitMask==m_vBitMask.end())
{
......@@ -149,12 +169,16 @@ boolean CTypeManager::registerBitMaskEntry(
boolean CTypeManager::isRegistered(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
return m_vName.find(rTypeIdentifier)!=m_vName.end()?true:false;
}
boolean CTypeManager::isStream(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
return m_vStream.find(rTypeIdentifier)!=m_vStream.end()?true:false;
}
......@@ -162,6 +186,8 @@ boolean CTypeManager::isDerivedFromStream(
const CIdentifier& rTypeIdentifier,
const CIdentifier& rParentTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map < CIdentifier, CIdentifier >::const_iterator it=m_vStream.find(rTypeIdentifier);
std::map < CIdentifier, CIdentifier >::const_iterator itParent=m_vStream.find(rParentTypeIdentifier);
if(it==m_vStream.end()) return false;
......@@ -180,38 +206,50 @@ boolean CTypeManager::isDerivedFromStream(
boolean CTypeManager::isEnumeration(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
return m_vEnumeration.find(rTypeIdentifier)!=m_vEnumeration.end()?true:false;
}
boolean CTypeManager::isBitMask(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
return m_vBitMask.find(rTypeIdentifier)!=m_vBitMask.end()?true:false;
}
CString CTypeManager::getTypeName(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(!isRegistered(rTypeIdentifier))
{
return CString("");
}
return m_vName.find(rTypeIdentifier)->second;
}
CIdentifier CTypeManager::getStreamParentType(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
if(!isStream(rTypeIdentifier))
{
return OV_UndefinedIdentifier;
}
return m_vStream.find(rTypeIdentifier)->second;
}
uint64 CTypeManager::getEnumerationEntryCount(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itEnumeration=m_vEnumeration.find(rTypeIdentifier);
if(itEnumeration==m_vEnumeration.end())
{
......@@ -226,6 +264,8 @@ boolean CTypeManager::getEnumerationEntry(
CString& sEntryName,
uint64& rEntryValue) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itEnumeration=m_vEnumeration.find(rTypeIdentifier);
if(itEnumeration==m_vEnumeration.end())
{
......@@ -252,6 +292,8 @@ CString CTypeManager::getEnumerationEntryNameFromValue(
const CIdentifier& rTypeIdentifier,
const uint64 ui64EntryValue) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itEnumeration=m_vEnumeration.find(rTypeIdentifier);
if(itEnumeration==m_vEnumeration.end())
{
......@@ -269,6 +311,8 @@ uint64 CTypeManager::getEnumerationEntryValueFromName(
const CIdentifier& rTypeIdentifier,
const CString& rEntryName) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itEnumeration=m_vEnumeration.find(rTypeIdentifier);
std::map<uint64, CString>::const_iterator itEnumerationEntry;
if(itEnumeration==m_vEnumeration.end())
......@@ -314,6 +358,8 @@ uint64 CTypeManager::getEnumerationEntryValueFromName(
uint64 CTypeManager::getBitMaskEntryCount(
const CIdentifier& rTypeIdentifier) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itBitMask=m_vBitMask.find(rTypeIdentifier);
if(itBitMask==m_vBitMask.end())
{
......@@ -328,6 +374,8 @@ boolean CTypeManager::getBitMaskEntry(
CString& sEntryName,
uint64& rEntryValue) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itBitMask=m_vBitMask.find(rTypeIdentifier);
if(itBitMask==m_vBitMask.end())
{
......@@ -354,6 +402,8 @@ CString CTypeManager::getBitMaskEntryNameFromValue(
const CIdentifier& rTypeIdentifier,
const uint64 ui64EntryValue) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itBitMask=m_vBitMask.find(rTypeIdentifier);
if(itBitMask==m_vBitMask.end())
{
......@@ -371,6 +421,8 @@ uint64 CTypeManager::getBitMaskEntryValueFromName(
const CIdentifier& rTypeIdentifier,
const CString& rEntryName) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itBitMask=m_vBitMask.find(rTypeIdentifier);
std::map<uint64, CString>::const_iterator itBitMaskEntry;
if(itBitMask==m_vBitMask.end())
......@@ -417,6 +469,8 @@ CString CTypeManager::getBitMaskEntryCompositionNameFromValue(
const CIdentifier& rTypeIdentifier,
const uint64 ui64EntryCompositionValue) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itBitMask=m_vBitMask.find(rTypeIdentifier);
if(itBitMask==m_vBitMask.end())
{
......@@ -452,6 +506,8 @@ uint64 CTypeManager::getBitMaskEntryCompositionValueFromName(
const CIdentifier& rTypeIdentifier,
const CString& rEntryCompositionName) const
{
boost::recursive_mutex::scoped_lock lock(m_oLock);
std::map<CIdentifier, std::map<uint64, CString> >::const_iterator itBitMask=m_vBitMask.find(rTypeIdentifier);
if(itBitMask==m_vBitMask.end())
{
......
......@@ -5,6 +5,10 @@
#include <map>
#include <boost/thread.hpp> // for mutex
#include <boost/thread/condition.hpp>
#include <boost/thread/recursive_mutex.hpp>
namespace OpenViBE
{
namespace Kernel
......@@ -103,6 +107,8 @@ namespace OpenViBE
std::map<OpenViBE::CIdentifier, std::map<OpenViBE::uint64, OpenViBE::CString> > m_vEnumeration;
std::map<OpenViBE::CIdentifier, std::map<OpenViBE::uint64, OpenViBE::CString> > m_vBitMask;
std::map<OpenViBE::CIdentifier, OpenViBE::CIdentifier> m_vStream;
mutable boost::recursive_mutex m_oLock; // Protects the arrays for threads. Recursive as the functions touching the arrays call each other.
};
};
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment