aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/libstate.h3
-rw-r--r--include/mutex.h20
-rw-r--r--modules/eng_aep/aep_conn.cpp17
-rw-r--r--modules/eng_aep/aep_conn.h1
-rw-r--r--src/init_def.cpp2
-rw-r--r--src/libstate.cpp72
-rw-r--r--src/mutex.cpp18
7 files changed, 73 insertions, 60 deletions
diff --git a/include/libstate.h b/include/libstate.h
index 9cb9f81d5..b2879c7d9 100644
--- a/include/libstate.h
+++ b/include/libstate.h
@@ -51,6 +51,7 @@ class Library_State
void add_engine(class Engine*);
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();
@@ -58,6 +59,7 @@ class Library_State
void set_transcoder(class Charset_Transcoder*);
std::string transcode(const std::string,
Character_Set, Character_Set) const;
+ void set_default_policy();
Library_State(class Mutex_Factory*);
~Library_State();
@@ -66,7 +68,6 @@ class Library_State
Library_State& operator=(const Library_State&) { return (*this); }
class Engine* get_engine_n(u32bit) const;
- void set_default_policy();
class Mutex_Factory* mutex_factory;
class Timer* timer;
diff --git a/include/mutex.h b/include/mutex.h
index 30e7b231f..48d1ea637 100644
--- a/include/mutex.h
+++ b/include/mutex.h
@@ -22,6 +22,16 @@ class Mutex
};
/*************************************************
+* Mutex Factory *
+*************************************************/
+class Mutex_Factory
+ {
+ public:
+ virtual Mutex* make();
+ virtual ~Mutex_Factory() {}
+ };
+
+/*************************************************
* Mutex Holding Class *
*************************************************/
class Mutex_Holder
@@ -34,13 +44,15 @@ class Mutex_Holder
};
/*************************************************
-* Mutex Factory *
+* Named Mutex Holder *
*************************************************/
-class Mutex_Factory
+class Named_Mutex_Holder
{
public:
- virtual Mutex* make();
- virtual ~Mutex_Factory() {}
+ Named_Mutex_Holder(const std::string&);
+ ~Named_Mutex_Holder();
+ private:
+ const std::string mutex_name;
};
}
diff --git a/modules/eng_aep/aep_conn.cpp b/modules/eng_aep/aep_conn.cpp
index 9293f45fc..6175bc5ae 100644
--- a/modules/eng_aep/aep_conn.cpp
+++ b/modules/eng_aep/aep_conn.cpp
@@ -14,20 +14,19 @@ namespace Botan {
* Persistent connection pool *
*************************************************/
std::vector<AEP_Connection::Connection_Info> AEP_Connection::pool;
-Mutex* AEP_Connection::guard = 0;
/*************************************************
* Close all currently open connections *
*************************************************/
void AEP_Connection::close_all_connections()
{
- guard->lock();
+ Mutex* mutex = global_state().get_named_mutex("aep");
+
+ mutex->lock();
for(u32bit j = 0; j != pool.size(); j++)
AEP::AEP_CloseConnection(pool[j].id);
pool.clear();
- guard->unlock();
- delete guard;
- guard = 0;
+ mutex->unlock();
}
/*************************************************
@@ -35,11 +34,7 @@ void AEP_Connection::close_all_connections()
*************************************************/
AEP_Connection::AEP_Connection()
{
- // FIXME: race condition
- if(!guard)
- guard = global_state().get_mutex();
-
- Mutex_Holder lock(guard);
+ Named_Mutex_Holder lock("aep");
this_connection = 0;
@@ -73,7 +68,7 @@ AEP_Connection::AEP_Connection()
*************************************************/
AEP_Connection::~AEP_Connection()
{
- Mutex_Holder lock(guard);
+ Named_Mutex_Holder lock("aep");
for(u32bit j = 0; j != pool.size(); j++)
{
diff --git a/modules/eng_aep/aep_conn.h b/modules/eng_aep/aep_conn.h
index 18b8c76db..9b1d97e58 100644
--- a/modules/eng_aep/aep_conn.h
+++ b/modules/eng_aep/aep_conn.h
@@ -28,7 +28,6 @@ class AEP_Connection
static const u32bit MAX_CACHED_CONNECTIONS = 8;
static std::vector<Connection_Info> pool;
- static Mutex* guard;
u32bit this_connection;
};
diff --git a/src/init_def.cpp b/src/init_def.cpp
index 6a5074665..17d8899e2 100644
--- a/src/init_def.cpp
+++ b/src/init_def.cpp
@@ -51,6 +51,8 @@ void initialize(const std::string& arg_string)
set_global_state(new Library_State(mutex_factory));
+ global_state().set_default_policy();
+
global_state().set_timer(modules.timer());
std::vector<Allocator*> allocators = modules.allocators();
diff --git a/src/libstate.cpp b/src/libstate.cpp
index a7a6b5f6f..d5841d924 100644
--- a/src/libstate.cpp
+++ b/src/libstate.cpp
@@ -25,7 +25,10 @@ Library_State* global_lib_state = 0;
Library_State& global_state()
{
if(!global_lib_state)
- throw Invalid_State("Library was not intialized correctly");
+ {
+ abort();
+ throw Invalid_State("Library was not initialized correctly");
+ }
return (*global_lib_state);
}
@@ -41,32 +44,6 @@ Library_State* swap_global_state(Library_State* new_state)
return old_state;
}
-namespace {
-
-/*************************************************
-* Named Mutex Holder *
-*************************************************/
-class Named_Mutex_Holder
- {
- public:
- Named_Mutex_Holder(const std::map<std::string, Mutex*>& mutexes,
- const std::string& name)
- {
- mux = search_map<std::string, Mutex*>(mutexes, name, 0);
-
- if(!mux)
- throw Invalid_Argument("Named_Mutex_Holder: mutex not found");
-
- mux->lock();
- }
-
- ~Named_Mutex_Holder() { mux->unlock(); }
- private:
- Mutex* mux;
- };
-
-}
-
/*************************************************
* Increment the Engine iterator *
*************************************************/
@@ -84,11 +61,22 @@ 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(locks, "allocator");
+ Named_Mutex_Holder lock("allocator");
if(type != "")
return search_map<std::string, Allocator*>(alloc_factory, type, 0);
@@ -97,7 +85,7 @@ Allocator* Library_State::get_allocator(const std::string& type) const
{
const std::string key_name = "conf/base/default_allocator";
- Named_Mutex_Holder lock(locks, "settings");
+ Named_Mutex_Holder lock("settings");
std::string chosen = search_map(settings, key_name);
if(chosen == "")
@@ -115,7 +103,7 @@ Allocator* Library_State::get_allocator(const std::string& type) const
*************************************************/
void Library_State::add_allocator(Allocator* allocator)
{
- Named_Mutex_Holder lock(locks, "allocator");
+ Named_Mutex_Holder lock("allocator");
allocator->init();
@@ -151,7 +139,7 @@ u64bit Library_State::system_clock() const
*************************************************/
void Library_State::set_prng(RandomNumberGenerator* new_rng)
{
- Named_Mutex_Holder lock(locks, "rng");
+ Named_Mutex_Holder lock("rng");
delete rng;
rng = new_rng;
@@ -162,7 +150,7 @@ void Library_State::set_prng(RandomNumberGenerator* new_rng)
*************************************************/
void Library_State::randomize(byte out[], u32bit length)
{
- Named_Mutex_Holder lock(locks, "rng");
+ Named_Mutex_Holder lock("rng");
rng->randomize(out, length);
}
@@ -172,7 +160,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(locks, "rng");
+ Named_Mutex_Holder lock("rng");
if(last_in_list)
entropy_sources.push_back(src);
@@ -185,7 +173,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(locks, "rng");
+ Named_Mutex_Holder lock("rng");
rng->add_entropy(in, length);
}
@@ -195,7 +183,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(locks, "rng");
+ Named_Mutex_Holder lock("rng");
rng->add_entropy(source, slow_poll);
}
@@ -205,7 +193,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(locks, "rng");
+ Named_Mutex_Holder lock("rng");
u32bit bits = 0;
for(u32bit j = 0; j != entropy_sources.size(); ++j)
@@ -227,7 +215,7 @@ void Library_State::set_option(const std::string& section,
const std::string& value,
bool overwrite)
{
- Named_Mutex_Holder lock(locks, "settings");
+ Named_Mutex_Holder lock("settings");
std::map<std::string, std::string>::const_iterator i = settings.find(name);
@@ -247,7 +235,7 @@ void Library_State::set_option(const std::string& section,
std::string Library_State::get_option(const std::string& section,
const std::string& name) const
{
- Named_Mutex_Holder lock(locks, "settings");
+ Named_Mutex_Holder lock("settings");
return search_map<std::string, std::string>(settings,
section + "/" + name, "");
@@ -259,7 +247,7 @@ std::string Library_State::get_option(const std::string& section,
bool Library_State::option_set(const std::string& section,
const std::string& name) const
{
- Named_Mutex_Holder lock(locks, "settings");
+ Named_Mutex_Holder lock("settings");
return search_map(settings, section + "/" + name, false, true);
}
@@ -269,7 +257,7 @@ bool Library_State::option_set(const std::string& section,
*************************************************/
Engine* Library_State::get_engine_n(u32bit n) const
{
- Named_Mutex_Holder lock(locks, "engine");
+ Named_Mutex_Holder lock("engine");
if(n >= engines.size())
return 0;
@@ -281,7 +269,7 @@ Engine* Library_State::get_engine_n(u32bit n) const
*************************************************/
void Library_State::add_engine(Engine* engine)
{
- Named_Mutex_Holder lock(locks, "engine");
+ Named_Mutex_Holder lock("engine");
engines.push_back(engine);
}
@@ -347,8 +335,6 @@ Library_State::Library_State(Mutex_Factory* mutex_factory)
rng = 0;
cached_default_allocator = 0;
x509_state_obj = 0;
-
- set_default_policy();
}
/*************************************************
diff --git a/src/mutex.cpp b/src/mutex.cpp
index 5514922b2..a6d18a44c 100644
--- a/src/mutex.cpp
+++ b/src/mutex.cpp
@@ -4,6 +4,7 @@
*************************************************/
#include <botan/mutex.h>
+#include <botan/libstate.h>
namespace Botan {
@@ -26,6 +27,23 @@ 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* Mutex_Factory::make()