Simplify mutex code by providing stubs that are do nothing during

CHAISCRIPT_NO_THREADS builds.
This commit is contained in:
Jason Turner 2011-03-25 22:49:17 -06:00
parent 58e5df0a9a
commit 92c836c58a
6 changed files with 106 additions and 103 deletions

View File

@ -7,15 +7,28 @@
#ifndef CHAISCRIPT_HPP_ #ifndef CHAISCRIPT_HPP_
#define CHAISCRIPT_HPP_ #define CHAISCRIPT_HPP_
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <stdexcept>
#include <iostream> /// \mainpage
#include <map> /// <a href="http://www.chaiscript.com">ChaiScript</a> is a scripting language designed specifically for integration with C++. It provides
#include <fstream> /// seamless integration with C++ on all levels, including shared_ptr objects, functors and exceptions.
#include <boost/shared_ptr.hpp> ///
/// The parts of the ChaiScript API that the average user will be concerned with are contained in the
/// chaiscript namespace and the chaiscript::ChaiScript class.
///
/// The end user parts of the API are extremely simple both in size and ease of use.
///
/// Currently, all source control and project management aspects of ChaiScript occur on <a href="http://www.github.com">github</a>.
///
/// \sa chaiscript
/// \sa chaiscript::ChaiScript
/// \sa http://www.chaiscript.com
/// \sa http://www.github.com/ChaiScript/ChaiScript
/// \namespace chaiscript
/// The chaiscript namespace contains every API call that the average user will be concerned with.
#include "dispatchkit/dispatchkit.hpp" #include "dispatchkit/dispatchkit.hpp"
#include "dispatchkit/bootstrap.hpp" #include "dispatchkit/bootstrap.hpp"

View File

@ -13,6 +13,14 @@
#pragma message ("ChaiScript is compiling without thread safety.") #pragma message ("ChaiScript is compiling without thread safety.")
#endif #endif
/// \file
///
/// This file contains code necessary for thread support in ChaiScript.
/// If the compiler definition CHAISCRIPT_NO_THREADS is defined then thread safety
/// is disabled in ChaiScript. This has the result that some code is faster, because mutex locks are not required.
/// It also has the side effect that the chaiscript::ChaiScript object may not be accessed from more than
/// one thread simultaneously.
namespace chaiscript namespace chaiscript
{ {
namespace detail namespace detail
@ -21,6 +29,12 @@ namespace chaiscript
{ {
#ifndef CHAISCRIPT_NO_THREADS #ifndef CHAISCRIPT_NO_THREADS
using boost::unique_lock;
using boost::shared_lock;
using boost::lock_guard;
using boost::shared_mutex;
using boost::recursive_mutex;
template<typename T> template<typename T>
class Thread_Storage class Thread_Storage
@ -51,6 +65,32 @@ namespace chaiscript
}; };
#else #else
template<typename T>
class unique_lock
{
public:
unique_lock(T &) {}
};
template<typename T>
class shared_lock
{
public:
shared_lock(T &) {}
void unlock() {}
};
template<typename T>
class lock_guard
{
public:
lock_guard(T &) {}
};
class shared_mutex { };
class recursive_mutex {};
template<typename T> template<typename T>
class Thread_Storage class Thread_Storage

View File

@ -5,10 +5,13 @@
// http://www.chaiscript.com // http://www.chaiscript.com
/** /**
* This file contains utility functions for registration of STL container * \file
* classes. The methodology used is based on the SGI STL concepts. * This file contains utility functions for registration of STL container
* http://www.sgi.com/tech/stl/table_of_contents.html * classes. The methodology used is based on the SGI STL concepts.
*/ * http://www.sgi.com/tech/stl/table_of_contents.html
*/
#ifndef CHAISCRIPT_BOOTSTRAP_STL_HPP_ #ifndef CHAISCRIPT_BOOTSTRAP_STL_HPP_
#define CHAISCRIPT_BOOTSTRAP_STL_HPP_ #define CHAISCRIPT_BOOTSTRAP_STL_HPP_

View File

@ -395,9 +395,7 @@ namespace chaiscript
throw exception::global_non_const(); throw exception::global_non_const();
} }
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_global_object_mutex);
boost::unique_lock<boost::shared_mutex> l(m_global_object_mutex);
#endif
m_state.m_global_objects[name] = obj; m_state.m_global_objects[name] = obj;
} }
@ -476,9 +474,7 @@ namespace chaiscript
// Is the value we are looking for a global? // Is the value we are looking for a global?
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_global_object_mutex);
boost::shared_lock<boost::shared_mutex> l(m_global_object_mutex);
#endif
std::map<std::string, Boxed_Value>::const_iterator itr = m_state.m_global_objects.find(name); std::map<std::string, Boxed_Value>::const_iterator itr = m_state.m_global_objects.find(name);
if (itr != m_state.m_global_objects.end()) if (itr != m_state.m_global_objects.end())
@ -512,9 +508,7 @@ namespace chaiscript
{ {
add_global_const(const_var(ti), name + "_type"); add_global_const(const_var(ti), name + "_type");
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::unique_lock<boost::shared_mutex> l(m_mutex);
#endif
m_state.m_types.insert(std::make_pair(name, ti)); m_state.m_types.insert(std::make_pair(name, ti));
} }
@ -524,9 +518,7 @@ namespace chaiscript
*/ */
Type_Info get_type(const std::string &name) const Type_Info get_type(const std::string &name) const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
Type_Name_Map::const_iterator itr = m_state.m_types.find(name); Type_Name_Map::const_iterator itr = m_state.m_types.find(name);
@ -545,9 +537,7 @@ namespace chaiscript
*/ */
std::string get_type_name(const Type_Info &ti) const std::string get_type_name(const Type_Info &ti) const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
for (Type_Name_Map::const_iterator itr = m_state.m_types.begin(); for (Type_Name_Map::const_iterator itr = m_state.m_types.begin();
itr != m_state.m_types.end(); itr != m_state.m_types.end();
@ -567,9 +557,7 @@ namespace chaiscript
*/ */
std::vector<std::pair<std::string, Type_Info> > get_types() const std::vector<std::pair<std::string, Type_Info> > get_types() const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
return std::vector<std::pair<std::string, Type_Info> >(m_state.m_types.begin(), m_state.m_types.end()); return std::vector<std::pair<std::string, Type_Info> >(m_state.m_types.begin(), m_state.m_types.end());
} }
@ -580,9 +568,7 @@ namespace chaiscript
std::vector< Proxy_Function > std::vector< Proxy_Function >
get_function(const std::string &t_name) const get_function(const std::string &t_name) const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
const std::map<std::string, std::vector<Proxy_Function> > &funs = get_functions_int(); const std::map<std::string, std::vector<Proxy_Function> > &funs = get_functions_int();
@ -603,9 +589,7 @@ namespace chaiscript
*/ */
bool function_exists(const std::string &name) const bool function_exists(const std::string &name) const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
const std::map<std::string, std::vector<Proxy_Function> > &functions = get_functions_int(); const std::map<std::string, std::vector<Proxy_Function> > &functions = get_functions_int();
return functions.find(name) != functions.end(); return functions.find(name) != functions.end();
@ -616,9 +600,8 @@ namespace chaiscript
*/ */
std::vector<std::pair<std::string, Proxy_Function > > get_functions() const std::vector<std::pair<std::string, Proxy_Function > > get_functions() const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
std::vector<std::pair<std::string, Proxy_Function> > rets; std::vector<std::pair<std::string, Proxy_Function> > rets;
const std::map<std::string, std::vector<Proxy_Function> > &functions = get_functions_int(); const std::map<std::string, std::vector<Proxy_Function> > &functions = get_functions_int();
@ -640,9 +623,7 @@ namespace chaiscript
void add_reserved_word(const std::string &name) void add_reserved_word(const std::string &name)
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::unique_lock<boost::shared_mutex> l(m_mutex);
#endif
m_state.m_reserved_words.insert(name); m_state.m_reserved_words.insert(name);
} }
@ -779,20 +760,16 @@ namespace chaiscript
State get_state() State get_state()
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::unique_lock<boost::shared_mutex> l(m_mutex); chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_global_object_mutex);
boost::unique_lock<boost::shared_mutex> l2(m_global_object_mutex);
#endif
return m_state; return m_state;
} }
void set_state(const State &t_state) void set_state(const State &t_state)
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::unique_lock<boost::shared_mutex> l(m_mutex); chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l2(m_global_object_mutex);
boost::unique_lock<boost::shared_mutex> l2(m_global_object_mutex);
#endif
m_state = t_state; m_state = t_state;
} }
@ -918,9 +895,7 @@ namespace chaiscript
*/ */
void validate_object_name(const std::string &name) const void validate_object_name(const std::string &name) const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
if (m_state.m_reserved_words.find(name) != m_state.m_reserved_words.end()) if (m_state.m_reserved_words.find(name) != m_state.m_reserved_words.end())
{ {
@ -935,9 +910,7 @@ namespace chaiscript
*/ */
bool add_function(const Proxy_Function &t_f, const std::string &t_name) bool add_function(const Proxy_Function &t_f, const std::string &t_name)
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::unique_lock<boost::shared_mutex> l(m_mutex);
#endif
std::map<std::string, std::vector<Proxy_Function> > &funcs = get_functions_int(); std::map<std::string, std::vector<Proxy_Function> > &funcs = get_functions_int();
@ -968,10 +941,8 @@ namespace chaiscript
return true; return true;
} }
#ifndef CHAISCRIPT_NO_THREADS mutable chaiscript::detail::threading::shared_mutex m_mutex;
mutable boost::shared_mutex m_mutex; mutable chaiscript::detail::threading::shared_mutex m_global_object_mutex;
mutable boost::shared_mutex m_global_object_mutex;
#endif
struct Stack_Holder struct Stack_Holder
{ {

View File

@ -15,10 +15,6 @@
#include <boost/type_traits/is_polymorphic.hpp> #include <boost/type_traits/is_polymorphic.hpp>
#include <boost/type_traits/is_base_of.hpp> #include <boost/type_traits/is_base_of.hpp>
#ifndef CHAISCRIPT_NO_THREADS
#include <boost/thread/shared_mutex.hpp>
#endif
namespace chaiscript namespace chaiscript
{ {
namespace exception namespace exception
@ -156,9 +152,7 @@ namespace chaiscript
template<typename InItr> template<typename InItr>
void cleanup(InItr begin, const InItr &end) void cleanup(InItr begin, const InItr &end)
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::unique_lock<boost::shared_mutex> l(m_mutex);
#endif
while (begin != end) while (begin != end)
{ {
@ -173,28 +167,22 @@ namespace chaiscript
void add_conversion(const boost::shared_ptr<Dynamic_Conversion> &conversion) void add_conversion(const boost::shared_ptr<Dynamic_Conversion> &conversion)
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::unique_lock<boost::shared_mutex> l(m_mutex);
#endif
m_conversions.insert(conversion.get()); m_conversions.insert(conversion.get());
} }
bool has_conversion(const Type_Info &base, const Type_Info &derived) bool has_conversion(const Type_Info &base, const Type_Info &derived) const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
return find(base, derived) != m_conversions.end(); return find(base, derived) != m_conversions.end();
} }
Dynamic_Conversion *get_conversion(const Type_Info &base, const Type_Info &derived) Dynamic_Conversion *get_conversion(const Type_Info &base, const Type_Info &derived) const
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
boost::shared_lock<boost::shared_mutex> l(m_mutex);
#endif
std::set<Dynamic_Conversion *>::const_iterator itr = std::set<Dynamic_Conversion *>::const_iterator itr =
find(base, derived); find(base, derived);
@ -211,7 +199,7 @@ namespace chaiscript
Dynamic_Conversions() {} Dynamic_Conversions() {}
std::set<Dynamic_Conversion *>::const_iterator find( std::set<Dynamic_Conversion *>::const_iterator find(
const Type_Info &base, const Type_Info &derived) const Type_Info &base, const Type_Info &derived) const
{ {
for (std::set<Dynamic_Conversion *>::const_iterator itr = m_conversions.begin(); for (std::set<Dynamic_Conversion *>::const_iterator itr = m_conversions.begin();
itr != m_conversions.end(); itr != m_conversions.end();
@ -225,9 +213,8 @@ namespace chaiscript
return m_conversions.end(); return m_conversions.end();
} }
#ifndef CHAISCRIPT_NO_THREADS
boost::shared_mutex m_mutex; mutable chaiscript::detail::threading::shared_mutex m_mutex;
#endif
std::set<Dynamic_Conversion *> m_conversions; std::set<Dynamic_Conversion *> m_conversions;
}; };
} }

View File

@ -223,10 +223,9 @@ namespace chaiscript
} }
class ChaiScript { class ChaiScript {
#ifndef CHAISCRIPT_NO_THREADS
mutable boost::shared_mutex m_mutex; mutable chaiscript::detail::threading::shared_mutex m_mutex;
mutable boost::recursive_mutex m_use_mutex; mutable chaiscript::detail::threading::recursive_mutex m_use_mutex;
#endif
std::set<std::string> m_used_files; std::set<std::string> m_used_files;
std::map<std::string, detail::Loadable_Module_Ptr> m_loaded_modules; std::map<std::string, detail::Loadable_Module_Ptr> m_loaded_modules;
@ -280,17 +279,13 @@ namespace chaiscript
try { try {
const std::string appendedpath = m_usepaths[i] + t_filename; const std::string appendedpath = m_usepaths[i] + t_filename;
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex); chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
boost::shared_lock<boost::shared_mutex> l2(m_mutex);
#endif
if (m_used_files.count(appendedpath) == 0) if (m_used_files.count(appendedpath) == 0)
{ {
#ifndef CHAISCRIPT_NO_THREADS
m_used_files.insert(appendedpath); m_used_files.insert(appendedpath);
l2.unlock(); l2.unlock();
#endif
eval_file(appendedpath); eval_file(appendedpath);
} }
} catch (const exception::file_not_found_error &) { } catch (const exception::file_not_found_error &) {
@ -403,10 +398,8 @@ namespace chaiscript
*/ */
State get_state() State get_state()
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex); chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
boost::shared_lock<boost::shared_mutex> l2(m_mutex);
#endif
State s; State s;
s.used_files = m_used_files; s.used_files = m_used_files;
@ -420,10 +413,8 @@ namespace chaiscript
*/ */
void set_state(const State &t_state) void set_state(const State &t_state)
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex); chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
boost::shared_lock<boost::shared_mutex> l2(m_mutex);
#endif
m_used_files = t_state.used_files; m_used_files = t_state.used_files;
m_active_loaded_modules = t_state.active_loaded_modules; m_active_loaded_modules = t_state.active_loaded_modules;
@ -505,9 +496,7 @@ namespace chaiscript
*/ */
void load_module(const std::string &t_module_name, const std::string &t_filename) void load_module(const std::string &t_module_name, const std::string &t_filename)
{ {
#ifndef CHAISCRIPT_NO_THREADS chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex);
#endif
if (m_loaded_modules.count(t_module_name) == 0) if (m_loaded_modules.count(t_module_name) == 0)
{ {