diff options
author | lloyd <[email protected]> | 2008-04-02 18:52:13 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-04-02 18:52:13 +0000 |
commit | 8a5ffc5c6294786ad70f7a8fdd7606f411690ce9 (patch) | |
tree | 16b7d8d757e1ac6c8b2460d3e6a42768de466c8c | |
parent | 91c318bbad1fcff1c9b04f1077fb4ec75d59a549 (diff) |
Remove the Named_Mutex_Holder and associated code. Convert all uses to
instead allocate a reference to a mutex locally and use the more typical
Mutex_Holder RAII object.
Named_Mutex_Holder (and in particular the string->mutex mappings contained
in the global state) have been found to be pretty expensive in at least
some situations (see post by Jack Cummings to monotone-devel 2008-03-12),
and doesn't really buy us that much in terms of ease of use. Also, it
relies on the global state object, which has shown itself to be a rich
source of race conditions and locking bugs. The intent is to incrementally
remove all of the shared / global state and require applications to maintain
that state where necessary.
-rw-r--r-- | include/config.h | 9 | ||||
-rw-r--r-- | include/libstate.h | 6 | ||||
-rw-r--r-- | include/mutex.h | 12 | ||||
-rw-r--r-- | src/config.cpp | 38 | ||||
-rw-r--r-- | src/libstate.cpp | 50 | ||||
-rw-r--r-- | src/mutex.cpp | 18 |
6 files changed, 56 insertions, 77 deletions
diff --git a/include/config.h b/include/config.h index 5e3f8ed1c..ad6eafcac 100644 --- a/include/config.h +++ b/include/config.h @@ -6,7 +6,7 @@ #ifndef BOTAN_POLICY_CONF_H__ #define BOTAN_POLICY_CONF_H__ -#include <botan/types.h> +#include <botan/mutex.h> #include <botan/enums.h> #include <string> #include <vector> @@ -20,6 +20,9 @@ namespace Botan { class Config { public: + Config(); + ~Config(); + void load_defaults(); std::string get(const std::string&, const std::string&) const; @@ -43,7 +46,11 @@ class Config static void choose_sig_format(const std::string&, std::string&, Signature_Format&); private: + Config(const Config&) {} + Config& operator=(const Config&) { return (*this); } + std::map<std::string, std::string> settings; + Mutex* mutex; }; /************************************************* diff --git a/include/libstate.h b/include/libstate.h index 666677ff3..297e39479 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -68,7 +68,6 @@ class Library_State class Config& config() const; class Mutex* get_mutex() const; - class Mutex* get_named_mutex(const std::string&); void set_x509_state(class X509_GlobalState*); class X509_GlobalState& x509_state(); @@ -86,11 +85,14 @@ class Library_State class Engine* get_engine_n(u32bit) const; class Mutex_Factory* mutex_factory; + class Mutex* allocator_lock; + class Mutex* engine_lock; + class Mutex* rng_lock; + class Timer* timer; mutable class Config* config_obj; class X509_GlobalState* x509_state_obj; - std::map<std::string, class Mutex*> locks; std::map<std::string, Allocator*> alloc_factory; mutable Allocator* cached_default_allocator; diff --git a/include/mutex.h b/include/mutex.h index ef45a7dce..9f750e526 100644 --- a/include/mutex.h +++ b/include/mutex.h @@ -52,18 +52,6 @@ class Mutex_Holder Mutex* mux; }; -/************************************************* -* Named Mutex Holder * -*************************************************/ -class Named_Mutex_Holder - { - public: - Named_Mutex_Holder(const std::string&); - ~Named_Mutex_Holder(); - private: - const std::string mutex_name; - }; - } #endif diff --git a/src/config.cpp b/src/config.cpp index 3f03c70c0..7706a33f3 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -23,12 +23,36 @@ Config& global_config() } /************************************************* +* Dereference an alias * +*************************************************/ +std::string deref_alias(const std::string& name) + { + return global_state().config().deref_alias(name); + } + +/************************************************* +* Get a configuration value * +*************************************************/ +Config::Config() + { + mutex = global_state().get_mutex(); + } + +/************************************************* +* Get a configuration value * +*************************************************/ +Config::~Config() + { + delete mutex; + } + +/************************************************* * Get a configuration value * *************************************************/ std::string Config::get(const std::string& section, const std::string& key) const { - Named_Mutex_Holder lock("config"); + Mutex_Holder lock(mutex); return search_map<std::string, std::string>(settings, section + "/" + key, ""); @@ -40,7 +64,7 @@ std::string Config::get(const std::string& section, bool Config::is_set(const std::string& section, const std::string& key) const { - Named_Mutex_Holder lock("config"); + Mutex_Holder lock(mutex); return search_map(settings, section + "/" + key, false, true); } @@ -51,7 +75,7 @@ bool Config::is_set(const std::string& section, void Config::set(const std::string& section, const std::string& key, const std::string& value, bool overwrite) { - Named_Mutex_Holder lock("config"); + Mutex_Holder lock(mutex); std::string full_key = section + "/" + key; @@ -192,12 +216,4 @@ void Config::choose_sig_format(const std::string& algo_name, throw Invalid_Argument("Unknown X.509 signing key type: " + algo_name); } -/************************************************* -* Dereference an alias * -*************************************************/ -std::string deref_alias(const std::string& name) - { - return global_config().deref_alias(name); - } - } diff --git a/src/libstate.cpp b/src/libstate.cpp index 7b6a929c5..f6926cdb4 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -72,22 +72,11 @@ Mutex* Library_State::get_mutex() const } /************************************************* -* Get a persistent named mutex object * -*************************************************/ -Mutex* Library_State::get_named_mutex(const std::string& name) - { - Mutex* mux = search_map<std::string, Mutex*>(locks, name, 0); - if(mux) - return mux; - return (locks[name] = get_mutex()); - } - -/************************************************* * Get an allocator by its name * *************************************************/ Allocator* Library_State::get_allocator(const std::string& type) const { - Named_Mutex_Holder lock("allocator"); + Mutex_Holder lock(allocator_lock); if(type != "") return search_map<std::string, Allocator*>(alloc_factory, type, 0); @@ -111,7 +100,7 @@ Allocator* Library_State::get_allocator(const std::string& type) const *************************************************/ void Library_State::add_allocator(Allocator* allocator) { - Named_Mutex_Holder lock("allocator"); + Mutex_Holder lock(allocator_lock); allocator->init(); @@ -124,7 +113,7 @@ void Library_State::add_allocator(Allocator* allocator) *************************************************/ void Library_State::set_default_allocator(const std::string& type) const { - Named_Mutex_Holder lock("allocator"); + Mutex_Holder lock(allocator_lock); if(type == "") return; @@ -155,7 +144,7 @@ u64bit Library_State::system_clock() const *************************************************/ void Library_State::set_prng(RandomNumberGenerator* new_rng) { - Named_Mutex_Holder lock("rng"); + Mutex_Holder lock(rng_lock); delete rng; rng = new_rng; @@ -166,7 +155,7 @@ void Library_State::set_prng(RandomNumberGenerator* new_rng) *************************************************/ void Library_State::randomize(byte out[], u32bit length) { - Named_Mutex_Holder lock("rng"); + Mutex_Holder lock(rng_lock); rng->randomize(out, length); } @@ -176,7 +165,7 @@ void Library_State::randomize(byte out[], u32bit length) *************************************************/ void Library_State::add_entropy_source(EntropySource* src, bool last_in_list) { - Named_Mutex_Holder lock("rng"); + Mutex_Holder lock(rng_lock); if(last_in_list) entropy_sources.push_back(src); @@ -189,7 +178,7 @@ void Library_State::add_entropy_source(EntropySource* src, bool last_in_list) *************************************************/ void Library_State::add_entropy(const byte in[], u32bit length) { - Named_Mutex_Holder lock("rng"); + Mutex_Holder lock(rng_lock); rng->add_entropy(in, length); } @@ -199,7 +188,7 @@ void Library_State::add_entropy(const byte in[], u32bit length) *************************************************/ void Library_State::add_entropy(EntropySource& source, bool slow_poll) { - Named_Mutex_Holder lock("rng"); + Mutex_Holder lock(rng_lock); rng->add_entropy(source, slow_poll); } @@ -209,7 +198,7 @@ void Library_State::add_entropy(EntropySource& source, bool slow_poll) *************************************************/ u32bit Library_State::seed_prng(bool slow_poll, u32bit bits_to_get) { - Named_Mutex_Holder lock("rng"); + Mutex_Holder lock(rng_lock); u32bit bits = 0; for(u32bit j = 0; j != entropy_sources.size(); ++j) @@ -228,7 +217,7 @@ u32bit Library_State::seed_prng(bool slow_poll, u32bit bits_to_get) *************************************************/ Engine* Library_State::get_engine_n(u32bit n) const { - Named_Mutex_Holder lock("engine"); + Mutex_Holder lock(engine_lock); if(n >= engines.size()) return 0; @@ -240,7 +229,7 @@ Engine* Library_State::get_engine_n(u32bit n) const *************************************************/ void Library_State::add_engine(Engine* engine) { - Named_Mutex_Holder lock("engine"); + Mutex_Holder lock(engine_lock); engines.insert(engines.begin(), engine); } @@ -333,6 +322,10 @@ void Library_State::initialize(const InitializerOptions& args, else mutex_factory = new Default_Mutex_Factory; + allocator_lock = get_mutex(); + engine_lock = get_mutex(); + rng_lock = get_mutex(); + cached_default_allocator = 0; x509_state_obj = 0; ui = 0; @@ -340,11 +333,6 @@ void Library_State::initialize(const InitializerOptions& args, timer = modules.timer(); transcoder = modules.transcoder(); - locks["settings"] = get_mutex(); - locks["allocator"] = get_mutex(); - locks["rng"] = get_mutex(); - locks["engine"] = get_mutex(); - std::vector<Allocator*> mod_allocs = modules.allocators(); for(u32bit j = 0; j != mod_allocs.size(); ++j) add_allocator(mod_allocs[j]); @@ -353,10 +341,7 @@ void Library_State::initialize(const InitializerOptions& args, std::vector<Engine*> mod_engines = modules.engines(); for(u32bit j = 0; j != mod_engines.size(); ++j) - { - Named_Mutex_Holder lock("engine"); engines.push_back(mod_engines[j]); - } std::vector<EntropySource*> sources = modules.entropy_sources(); for(u32bit j = 0; j != sources.size(); ++j) @@ -391,6 +376,8 @@ Library_State::Library_State() { mutex_factory = 0; + allocator_lock = engine_lock = rng_lock = 0; + timer = 0; config_obj = 0; x509_state_obj = 0; @@ -426,9 +413,6 @@ Library_State::~Library_State() delete allocators[j]; } - std::for_each(locks.begin(), locks.end(), - delete2nd<std::map<std::string, Mutex*>::value_type>); - delete mutex_factory; } diff --git a/src/mutex.cpp b/src/mutex.cpp index fb2d8308d..387f10e95 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -4,7 +4,6 @@ *************************************************/ #include <botan/mutex.h> -#include <botan/libstate.h> namespace Botan { @@ -27,23 +26,6 @@ Mutex_Holder::~Mutex_Holder() } /************************************************* -* Named_Mutex_Holder Constructor * -*************************************************/ -Named_Mutex_Holder::Named_Mutex_Holder(const std::string& name) : - mutex_name(name) - { - global_state().get_named_mutex(mutex_name)->lock(); - } - -/************************************************* -* Named_Mutex_Holder Destructor * -*************************************************/ -Named_Mutex_Holder::~Named_Mutex_Holder() - { - global_state().get_named_mutex(mutex_name)->unlock(); - } - -/************************************************* * Default Mutex Factory * *************************************************/ Mutex* Default_Mutex_Factory::make() |