From ce4f262d2519324e2a5f0dd9d5b786e43c968f96 Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 28 Sep 2008 23:37:39 +0000 Subject: Move Default_Mutex (now Noop_Mutex) into module --- src/libstate.cpp | 8 ++--- src/modules.cpp | 13 +++++++- src/mutex.cpp | 66 --------------------------------------- src/mutex/noop_mutex/modinfo.txt | 10 ++++++ src/mutex/noop_mutex/mux_noop.cpp | 48 ++++++++++++++++++++++++++++ src/mutex/noop_mutex/mux_noop.h | 24 ++++++++++++++ 6 files changed, 98 insertions(+), 71 deletions(-) delete mode 100644 src/mutex.cpp create mode 100644 src/mutex/noop_mutex/modinfo.txt create mode 100644 src/mutex/noop_mutex/mux_noop.cpp create mode 100644 src/mutex/noop_mutex/mux_noop.h (limited to 'src') diff --git a/src/libstate.cpp b/src/libstate.cpp index 141e0d1ec..df9d2b519 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -228,10 +228,10 @@ void Library_State::initialize(const InitializerOptions& args, if(mutex_factory) throw Invalid_State("Library_State has already been initialized"); - if(args.thread_safe()) - mutex_factory = modules.mutex_factory(); - else - mutex_factory = new Default_Mutex_Factory; + mutex_factory = modules.mutex_factory(args.thread_safe()); + + if(!mutex_factory) + throw Invalid_State("Could not acquire a mutex module at init"); allocator_lock = get_mutex(); engine_lock = get_mutex(); diff --git a/src/modules.cpp b/src/modules.cpp index d38a29c9a..08ab0cdbb 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -17,6 +17,10 @@ #include #endif +#if defined(BOTAN_HAS_MUTEX_NOOP) + #include +#endif + #if defined(BOTAN_HAS_ALLOC_MMAP) #include #endif @@ -34,8 +38,15 @@ namespace Botan { /************************************************* * Return a mutex factory, if available * *************************************************/ -Mutex_Factory* Builtin_Modules::mutex_factory() const +Mutex_Factory* Builtin_Modules::mutex_factory(bool thread_safe) const { + if(!thread_safe) + { +#if defined(BOTAN_HAS_MUTEX_NOOP) + return new Noop_Mutex_Factory; +#endif + } + #if defined(BOTAN_HAS_MUTEX_PTHREAD) return new Pthread_Mutex_Factory; #elif defined(BOTAN_HAS_MUTEX_WIN32) diff --git a/src/mutex.cpp b/src/mutex.cpp deleted file mode 100644 index b7de6b8f2..000000000 --- a/src/mutex.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/************************************************* -* Mutex Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include - -namespace Botan { - -/************************************************* -* Mutex_Holder Constructor * -*************************************************/ -Mutex_Holder::Mutex_Holder(Mutex* m) : mux(m) - { - if(!mux) - throw Invalid_Argument("Mutex_Holder: Argument was NULL"); - mux->lock(); - } - -/************************************************* -* Mutex_Holder Destructor * -*************************************************/ -Mutex_Holder::~Mutex_Holder() - { - mux->unlock(); - } - -/************************************************* -* Default Mutex Factory * -*************************************************/ -Mutex* Default_Mutex_Factory::make() - { - class Default_Mutex : public Mutex - { - public: - class Mutex_State_Error : public Internal_Error - { - public: - Mutex_State_Error(const std::string& where) : - Internal_Error("Default_Mutex::" + where + ": " + - "Mutex is already " + where + "ed") {} - }; - - void lock() - { - if(locked) - throw Mutex_State_Error("lock"); - locked = true; - } - - void unlock() - { - if(!locked) - throw Mutex_State_Error("unlock"); - locked = false; - } - - Default_Mutex() { locked = false; } - private: - bool locked; - }; - - return new Default_Mutex; - } - -} diff --git a/src/mutex/noop_mutex/modinfo.txt b/src/mutex/noop_mutex/modinfo.txt new file mode 100644 index 000000000..1f49f5e1c --- /dev/null +++ b/src/mutex/noop_mutex/modinfo.txt @@ -0,0 +1,10 @@ +realname "No-Op Mutex" + +load_on auto + +define MUTEX_NOOP + + +mux_noop.cpp +mux_noop.h + diff --git a/src/mutex/noop_mutex/mux_noop.cpp b/src/mutex/noop_mutex/mux_noop.cpp new file mode 100644 index 000000000..eb3a12702 --- /dev/null +++ b/src/mutex/noop_mutex/mux_noop.cpp @@ -0,0 +1,48 @@ +/************************************************* +* No-Op Mutex Factory Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include + +namespace Botan { + +/************************************************* +* No-Op Mutex Factory * +*************************************************/ +Mutex* Noop_Mutex_Factory::make() + { + class Noop_Mutex : public Mutex + { + public: + class Mutex_State_Error : public Internal_Error + { + public: + Mutex_State_Error(const std::string& where) : + Internal_Error("Noop_Mutex::" + where + ": " + + "Mutex is already " + where + "ed") {} + }; + + void lock() + { + if(locked) + throw Mutex_State_Error("lock"); + locked = true; + } + + void unlock() + { + if(!locked) + throw Mutex_State_Error("unlock"); + locked = false; + } + + Noop_Mutex() { locked = false; } + private: + bool locked; + }; + + return new Noop_Mutex; + } + +} diff --git a/src/mutex/noop_mutex/mux_noop.h b/src/mutex/noop_mutex/mux_noop.h new file mode 100644 index 000000000..a5b802cc0 --- /dev/null +++ b/src/mutex/noop_mutex/mux_noop.h @@ -0,0 +1,24 @@ +/************************************************* +* No-Op Mutex Factory Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_NOOP_MUTEX_FACTORY_H__ +#define BOTAN_NOOP_MUTEX_FACTORY_H__ + +#include + +namespace Botan { + +/************************************************* +* No-Op Mutex Factory * +*************************************************/ +class BOTAN_DLL Noop_Mutex_Factory : public Mutex_Factory + { + public: + Mutex* make(); + }; + +} + +#endif -- cgit v1.2.3