Added a ScopedLock typedef that can be used without requiring extra any #ifdefs

This commit is contained in:
Ray Speth 2012-05-31 14:57:54 +00:00
parent cc042e088f
commit 2970e0c693
20 changed files with 115 additions and 225 deletions

View file

@ -7,14 +7,9 @@
#include <vector>
#if defined(THREAD_SAFE_CANTERA)
#include <boost/thread/mutex.hpp>
#endif
namespace Cantera
{
//! Base class for factories.
/*! This class maintains a registry of
* all factories that derive from it, and deletes them all when

View file

@ -0,0 +1,51 @@
/*!
* @file ct_thread.h
* Header file containing utilities used to ensure thread safety.
*/
#ifndef CT_THREAD_H
#define CT_THREAD_H
#include "config.h"
#ifdef THREAD_SAFE_CANTERA
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#endif
namespace Cantera {
#ifdef THREAD_SAFE_CANTERA
#if defined(BOOST_HAS_WINTHREADS)
typedef unsigned int cthreadId_t;
#elif defined(BOOST_HAS_PTHREADS)
typedef pthread_t cthreadId_t;
#endif
class thread_equal
{
public:
bool operator()(cthreadId_t L, cthreadId_t R) {
#if defined(BOOST_HAS_WINTHREADS)
return L == R;
#elif defined(BOOST_HAS_PTHREADS)
return pthread_equal(L, R);
#endif
}
};
typedef boost::mutex mutex_t;
typedef boost::mutex::scoped_lock ScopedLock;
#else
typedef int mutex_t;
typedef int ScopedLock;
#endif // THREAD_SAFE_CANTERA
}
#endif

View file

@ -12,10 +12,7 @@
#include "cantera/base/ct_defs.h"
#include "reaction_defs.h"
#include "cantera/base/FactoryBase.h"
#if defined(THREAD_SAFE_CANTERA)
#include <boost/thread/mutex.hpp>
#endif
#include "cantera/base/ct_thread.h"
namespace Cantera
{
@ -118,9 +115,7 @@ public:
* to the existing factory is returned.
*/
static FalloffFactory* factory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(falloff_mutex) ;
#endif
ScopedLock lock(falloff_mutex) ;
if (!s_factory) {
s_factory = new FalloffFactory;
}
@ -128,9 +123,7 @@ public:
}
virtual void deleteFactory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(falloff_mutex) ;
#endif
ScopedLock lock(falloff_mutex);
if (s_factory) {
delete s_factory;
s_factory = 0;
@ -169,10 +162,8 @@ private:
//! default constructor, which is defined as private
FalloffFactory() {}
#if defined(THREAD_SAFE_CANTERA)
//! Mutex for use when calling the factory
static boost::mutex falloff_mutex ;
#endif
static mutex_t falloff_mutex ;
};
}

View file

@ -10,10 +10,7 @@
#include "Kinetics.h"
#include "cantera/base/xml.h"
#include "cantera/base/FactoryBase.h"
#if defined(THREAD_SAFE_CANTERA)
#include <boost/thread/mutex.hpp>
#endif
#include "cantera/base/ct_thread.h"
namespace Cantera
{
@ -39,9 +36,7 @@ class KineticsFactory : public FactoryBase
public:
static KineticsFactory* factory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(kinetics_mutex) ;
#endif
ScopedLock lock(kinetics_mutex);
if (!s_factory) {
s_factory = new KineticsFactory;
}
@ -54,9 +49,7 @@ public:
}
virtual void deleteFactory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(kinetics_mutex) ;
#endif
ScopedLock lock(kinetics_mutex);
if (s_factory) {
delete s_factory ;
s_factory = 0 ;
@ -75,9 +68,7 @@ private:
static KineticsFactory* s_factory;
KineticsFactory() {}
#if defined(THREAD_SAFE_CANTERA)
static boost::mutex kinetics_mutex ;
#endif
static mutex_t kinetics_mutex;
};

View file

@ -13,7 +13,7 @@
#include "SpeciesThermo.h"
#include "cantera/base/ctexceptions.h"
#include "cantera/base/FactoryBase.h"
#include "cantera/base/ct_thread.h"
namespace Cantera
{
@ -210,10 +210,8 @@ private:
//! Pointer to the sole instance of this class, which is static
static SpeciesThermoFactory* s_factory;
#if defined(THREAD_SAFE_CANTERA)
//! Decl of the static mutex variable that locks the %SpeciesThermo factory singleton
static boost::mutex species_thermo_mutex;
#endif
static mutex_t species_thermo_mutex;
//! Constructor. This is made private, so that only the static
//! method factory() can instantiate the class.

View file

@ -12,11 +12,7 @@
#include "ThermoPhase.h"
#include "cantera/base/xml.h"
#if defined(THREAD_SAFE_CANTERA)
#include <boost/thread/mutex.hpp>
#endif
#include "cantera/base/ct_thread.h"
#include "cantera/base/FactoryBase.h"
namespace Cantera
@ -67,9 +63,7 @@ public:
//! Static function that creates a static instance of the factory.
static ThermoFactory* factory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(thermo_mutex);
#endif
ScopedLock lock(thermo_mutex);
if (!s_factory) {
s_factory = new ThermoFactory;
}
@ -78,9 +72,7 @@ public:
//! delete the static instance of this factory
virtual void deleteFactory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(thermo_mutex);
#endif
ScopedLock lock(thermo_mutex);
if (s_factory) {
delete s_factory;
s_factory = 0;
@ -114,12 +106,8 @@ private:
//! Private constructors prevents usage
ThermoFactory() {};
#if defined(THREAD_SAFE_CANTERA)
//! Decl for locking mutex for thermo factory singleton
static boost::mutex thermo_mutex;
#endif
static mutex_t thermo_mutex;
};
//! Create a new thermo manager instance.

View file

@ -16,14 +16,12 @@
// Cantera includes
#include "cantera/base/ct_defs.h"
#include "cantera/base/ct_thread.h"
#include "TransportBase.h"
#include "cantera/base/FactoryBase.h"
//#include "LiquidTransportData.h"
#include "LiquidTransportParams.h"
#if defined(THREAD_SAFE_CANTERA)
#include <boost/thread/mutex.hpp>
#endif
//======================================================================================================================
namespace Cantera
{
@ -119,9 +117,7 @@ public:
* @endcode
*/
static TransportFactory* factory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(transport_mutex) ;
#endif
ScopedLock transportLock(transport_mutex);
if (!s_factory) {
s_factory = new TransportFactory();
}
@ -232,10 +228,8 @@ private:
//! object allowed
static TransportFactory* s_factory;
#if defined(THREAD_SAFE_CANTERA)
//! Static instance of the mutex used to ensure the proper reading of the transport database
static boost::mutex transport_mutex;
#endif
static mutex_t transport_mutex;
//! The constructor is private; use static method factory() to
//! get a pointer to a factory instance

View file

@ -9,10 +9,7 @@
#include "ReactorBase.h"
#include "cantera/base/FactoryBase.h"
#if defined(THREAD_SAFE_CANTERA)
#include <boost/thread/mutex.hpp>
#endif
#include "cantera/base/ct_thread.h"
namespace Cantera
{
@ -23,9 +20,7 @@ class ReactorFactory : Cantera::FactoryBase
public:
static ReactorFactory* factory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(reactor_mutex) ;
#endif
ScopedLock lock(reactor_mutex);
if (!s_factory) {
s_factory = new ReactorFactory;
}
@ -33,9 +28,7 @@ public:
}
virtual void deleteFactory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(reactor_mutex) ;
#endif
ScopedLock lock(reactor_mutex);
if (s_factory) {
delete s_factory;
s_factory = 0;
@ -55,11 +48,8 @@ public:
virtual ReactorBase* newReactor(std::string reactorType);
private:
static ReactorFactory* s_factory;
#if defined(THREAD_SAFE_CANTERA)
static boost::mutex reactor_mutex ;
#endif
static mutex_t reactor_mutex;
ReactorFactory() {}
};

View file

@ -25,83 +25,30 @@ namespace Cantera {
// If running multiple threads in a cpp application, the Application class
// is the only internal object that is single instance with static data.
// Synchronize access to those data structures Using macros to avoid
// polluting code with a lot of ifdef's
#ifdef THREAD_SAFE_CANTERA
cthreadId_t getThisThreadId()
{
#if defined(BOOST_HAS_WINTHREADS)
return ::GetCurrentThreadId();
#elif defined(BOOST_HAS_PTHREADS)
return pthread_self();
#endif
}
#endif
//! Mutex for input directory access
static boost::mutex dir_mutex;
static mutex_t dir_mutex;
//! Mutex for access to string messages
static boost::mutex msg_mutex;
static mutex_t msg_mutex;
//! Mutex for creating singletons within the application object
static boost::mutex app_mutex;
// Mutex for controlling access to the log file
//static boost::mutex log_mutex;
static mutex_t app_mutex;
//! Mutex for controlling access to XML file storage
static boost::mutex xml_mutex;
static mutex_t xml_mutex;
//! Macro for locking input directory access
#define DIR_LOCK() boost::mutex::scoped_lock d_lock(dir_mutex)
//! Macro for locking access to string messages
#define MSG_LOCK() boost::mutex::scoped_lock m_lock(msg_mutex)
//! Macro for locking creating singletons in the application state
#define APP_LOCK() boost::mutex::scoped_lock a_lock(app_mutex)
//! Macro for locking XML file writing
#define XML_LOCK() boost::mutex::scoped_lock x_lock(xml_mutex)
#ifdef WITH_HTML_LOGS
//static boost::mutex html_mutex; // html logs
//#define HTML_LOCK() boost::mutex::scoped_lock h_lock(html_mutex)
#endif
#if defined(BOOST_HAS_WINTHREADS)
class thread_equal
{
public:
bool operator()(cthreadId_t L, cthreadId_t R) {
return L == R ;
}
} ;
cthreadId_t getThisThreadId()
{
return ::GetCurrentThreadId() ;
}
#elif defined(BOOST_HAS_PTHREADS)
typedef pthread_t cthreadId_t ;
class thread_equal
{
public:
bool operator()(cthreadId_t L, cthreadId_t R) {
return pthread_equal(L, R) ;
}
} ;
cthreadId_t getThisThreadId()
{
return pthread_self() ;
}
#endif
#else
#define DIR_LOCK()
#define MSG_LOCK()
#define APP_LOCK()
#define XML_LOCK()
#ifdef WITH_HTML_LOGS
//#define HTML_LOCK()
#endif
#endif
Application::Messages::Messages() :
errorMessage(0),
@ -359,7 +306,7 @@ void Application::Messages::write_logfile(std::string file)
#ifdef THREAD_SAFE_CANTERA
Application::Messages* Application::ThreadMessages::operator ->()
{
MSG_LOCK() ;
ScopedLock msgLock(msg_mutex);
cthreadId_t curId = getThisThreadId() ;
threadMsgMap_t::iterator iter = m_threadMsgMap.find(curId) ;
if (iter != m_threadMsgMap.end()) {
@ -372,7 +319,7 @@ Application::Messages* Application::ThreadMessages::operator ->()
void Application::ThreadMessages::removeThreadMessages()
{
MSG_LOCK() ;
ScopedLock msgLock(msg_mutex);
cthreadId_t curId = getThisThreadId() ;
threadMsgMap_t::iterator iter = m_threadMsgMap.find(curId) ;
if (iter != m_threadMsgMap.end()) {
@ -407,7 +354,7 @@ Application::Application() :
}
Application* Application::Instance() {
APP_LOCK();
ScopedLock appLock(app_mutex);
if (Application::s_app == 0) {
Application::s_app = new Application();
}
@ -425,7 +372,7 @@ Application::~Application()
}
void Application::ApplicationDestroy() {
APP_LOCK();
ScopedLock appLock(app_mutex);
if (Application::s_app != 0) {
delete Application::s_app;
Application::s_app = 0;
@ -442,7 +389,7 @@ void Application::thread_complete()
XML_Node* Application::get_XML_File(std::string file, int debug)
{
XML_LOCK();
ScopedLock xmlLock(xml_mutex);
std::string path = "";
path = findInputFile(file);
#ifdef _WIN32
@ -537,7 +484,7 @@ XML_Node* Application::get_XML_File(std::string file, int debug)
void Application::close_XML_File(std::string file)
{
XML_LOCK();
ScopedLock xmlLock(xml_mutex);
if (file == "all") {
std::map<string, XML_Node*>::iterator
b = xmlfiles.begin(),
@ -704,7 +651,7 @@ void Application::setDefaultDirectories()
void Application::addDataDirectory(std::string dir)
{
DIR_LOCK() ;
ScopedLock dirLock(dir_mutex);
if (inputDirs.size() == 0) {
setDefaultDirectories();
}
@ -723,7 +670,7 @@ void Application::addDataDirectory(std::string dir)
std::string Application::findInputFile(std::string name)
{
DIR_LOCK() ;
ScopedLock dirLock(dir_mutex);
string::size_type islash = name.find('/');
string::size_type ibslash = name.find('\\');
string inname;

View file

@ -2,13 +2,9 @@
#define CT_BASE_APPLICATION_H
#include "cantera/base/config.h"
#include "cantera/base/ct_thread.h"
#include "cantera/base/logger.h"
#ifdef THREAD_SAFE_CANTERA
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#endif
#include <map>
#include <memory>
#include <string>
@ -28,13 +24,6 @@ class XML_Node;
* data is stored in the class Application.
*/
#ifdef THREAD_SAFE_CANTERA
#if defined(BOOST_HAS_WINTHREADS)
typedef unsigned int cthreadId_t;
#elif defined(BOOST_HAS_PTHREADS)
typedef pthread_t cthreadId_t;
#endif
#endif
//! Class to hold global data.
/*!

View file

@ -117,10 +117,7 @@ void write_logfile(std::string file)
// **************** Global Data ****************
Unit* Unit::s_u = 0;
#if defined(THREAD_SAFE_CANTERA)
boost::mutex Unit::units_mutex ;
#endif
mutex_t Unit::units_mutex;
void appdelete()
{

View file

@ -13,13 +13,10 @@
#include "cantera/base/ct_defs.h"
#include "cantera/base/ctexceptions.h"
#include "cantera/base/ct_thread.h"
#include <string>
#if defined(THREAD_SAFE_CANTERA)
#include <boost/thread/mutex.hpp>
#endif
namespace Cantera
{
@ -34,9 +31,7 @@ public:
//! Initialize the static Unit class.
static Unit* units() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(units_mutex) ;
#endif
ScopedLock lock(units_mutex);
if (!s_u) {
s_u = new Unit;
}
@ -48,9 +43,7 @@ public:
* Note this can't be done in a destructor.
*/
static void deleteUnit() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(units_mutex) ;
#endif
ScopedLock lock(units_mutex);
if (s_u) {
delete s_u;
s_u = 0;
@ -172,11 +165,8 @@ private:
*/
std::map<std::string, doublereal> m_act_u;
#if defined(THREAD_SAFE_CANTERA)
//! Decl for static locker for Units singleton
static boost::mutex units_mutex;
#endif
static mutex_t units_mutex;
//! Units class constructor, containing the default mappings between
//! strings and units.

View file

@ -13,10 +13,7 @@ namespace Cantera
{
FalloffFactory* FalloffFactory::s_factory = 0;
#if defined(THREAD_SAFE_CANTERA)
boost::mutex FalloffFactory::falloff_mutex ;
#endif
mutex_t FalloffFactory::falloff_mutex;
//! The 3-parameter Troe falloff parameterization.
/*!

View file

@ -18,9 +18,7 @@ namespace Cantera
{
KineticsFactory* KineticsFactory::s_factory = 0;
#if defined(THREAD_SAFE_CANTERA)
boost::mutex KineticsFactory::kinetics_mutex ;
#endif
mutex_t KineticsFactory::kinetics_mutex;
static int ntypes = 6;
static string _types[] = {"none", "GasKinetics", "GRI30", "Interface", "Edge", "AqueousKinetics"};

View file

@ -35,10 +35,7 @@ namespace Cantera
{
SpeciesThermoFactory* SpeciesThermoFactory::s_factory = 0;
#if defined(THREAD_SAFE_CANTERA)
boost::mutex SpeciesThermoFactory::species_thermo_mutex ;
#endif
mutex_t SpeciesThermoFactory::species_thermo_mutex;
//! Examine the types of species thermo parameterizations,
//! and return a flag indicating the type of reference state thermo manager
@ -115,9 +112,7 @@ static void getSpeciesThermoTypes(std::vector<XML_Node*> & spDataNodeList,
*/
SpeciesThermoFactory* SpeciesThermoFactory::factory()
{
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(species_thermo_mutex);
#endif
ScopedLock lock(species_thermo_mutex);
if (!s_factory) {
s_factory = new SpeciesThermoFactory;
}
@ -132,9 +127,7 @@ SpeciesThermoFactory* SpeciesThermoFactory::factory()
*/
void SpeciesThermoFactory::deleteFactory()
{
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(species_thermo_mutex);
#endif
ScopedLock lock(species_thermo_mutex);
if (s_factory) {
delete s_factory;
s_factory = 0;

View file

@ -61,9 +61,8 @@ namespace Cantera
{
ThermoFactory* ThermoFactory::s_factory = 0;
#if defined(THREAD_SAFE_CANTERA)
boost::mutex ThermoFactory::thermo_mutex;
#endif
mutex_t ThermoFactory::thermo_mutex;
//! Define the number of %ThermoPhase types for use in this factory routine
/*!
* @deprecated This entire structure could be replaced with a std::map

View file

@ -45,12 +45,8 @@ namespace Cantera
VPSSMgrFactory* VPSSMgrFactory::s_factory = 0;
#if defined(THREAD_SAFE_CANTERA)
// Defn of the static mutex variable that locks the
// %VPSSMgr factory singleton
boost::mutex VPSSMgrFactory::vpss_species_thermo_mutex;
#endif
// Defn of the static mutex variable that locks the %VPSSMgr factory singleton
mutex_t VPSSMgrFactory::vpss_species_thermo_mutex;
//! Examine the types of species thermo parameterizations, and return a flag indicating the type of parameterization
//! needed by the species.
@ -201,10 +197,7 @@ static void getVPSSMgrTypes(std::vector<XML_Node*> & spDataNodeList,
*/
void VPSSMgrFactory::deleteFactory()
{
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(vpss_species_thermo_mutex);
#endif
ScopedLock lock(vpss_species_thermo_mutex);
if (s_factory) {
delete s_factory;
s_factory = 0;

View file

@ -16,6 +16,7 @@
#include "cantera/thermo/SpeciesThermo.h"
#include "cantera/base/ctexceptions.h"
#include "cantera/base/FactoryBase.h"
#include "cantera/base/ct_thread.h"
#include "cantera/thermo/VPSSMgr.h"
namespace Cantera
@ -87,9 +88,7 @@ public:
* instance.
*/
static VPSSMgrFactory* factory() {
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(vpss_species_thermo_mutex);
#endif
ScopedLock lock(vpss_species_thermo_mutex);
if (!s_factory) {
s_factory = new VPSSMgrFactory;
}
@ -156,11 +155,9 @@ private:
//! pointer to the sole instance of this class
static VPSSMgrFactory* s_factory;
#if defined(THREAD_SAFE_CANTERA)
//! Decl of the static mutex variable that locks the
//! %VPSSMgr factory singleton
static boost::mutex vpss_species_thermo_mutex;
#endif
static mutex_t vpss_species_thermo_mutex;
//! Constructor. This is made private, so that only the static
//! method factory() can instantiate the class.

View file

@ -52,11 +52,8 @@ const doublereal FiveThirds = 5.0/3.0;
//====================================================================================================================
TransportFactory* TransportFactory::s_factory = 0;
#if defined(THREAD_SAFE_CANTERA)
// declaration of static storage for the mutex
boost::mutex TransportFactory::transport_mutex;
#endif
mutex_t TransportFactory::transport_mutex;
////////////////////////// exceptions /////////////////////////
@ -279,9 +276,7 @@ TransportFactory::~TransportFactory()
// This static function deletes the statically allocated instance.
void TransportFactory::deleteFactory()
{
#if defined(THREAD_SAFE_CANTERA)
boost::mutex::scoped_lock lock(transport_mutex) ;
#endif
ScopedLock transportLock(transport_mutex);
if (s_factory) {
delete s_factory;
s_factory = 0;
@ -685,7 +680,6 @@ void TransportFactory::setupLiquidTransport(std::ostream& flog, thermo_t* thermo
void TransportFactory::initTransport(Transport* tran,
thermo_t* thermo, int mode, int log_level)
{
const std::vector<const XML_Node*> & transport_database = thermo->speciesData();
GasTransportParams trParam;

View file

@ -15,9 +15,7 @@ namespace Cantera
{
ReactorFactory* ReactorFactory::s_factory = 0;
#ifdef THREAD_SAFE_CANTERA
boost::mutex ReactorFactory::reactor_mutex ;
#endif
mutex_t ReactorFactory::reactor_mutex;
static int ntypes = 4;
static string _types[] = {"Reservoir", "Reactor", "ConstPressureReactor",