diff options
author | Jack Lloyd <[email protected]> | 2016-10-12 15:32:14 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-10-12 15:32:14 -0400 |
commit | ed9e147695e4c5e800e83654baf365a634f3a2a7 (patch) | |
tree | 59bad402cff7d7af9baa5fd79081d677b60afc83 /src/lib/utils/mutex.h | |
parent | d59b164a2ad2bc2290265530ac1a5c7be7855975 (diff) |
Abstract out mutex type. Make threads optional.
Diffstat (limited to 'src/lib/utils/mutex.h')
-rw-r--r-- | src/lib/utils/mutex.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/lib/utils/mutex.h b/src/lib/utils/mutex.h new file mode 100644 index 000000000..0daea3148 --- /dev/null +++ b/src/lib/utils/mutex.h @@ -0,0 +1,61 @@ +/* +* (C) 2016 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_UTIL_MUTEX_H__ +#define BOTAN_UTIL_MUTEX_H__ + +#include <botan/build.h> +#include <botan/types.h> + +#if defined(BOTAN_TARGET_OS_HAS_THREADS) + +#include <mutex> + +namespace Botan { + +template<typename T> using lock_guard_type = std::lock_guard<T>; +typedef std::mutex mutex_type; + +} + +#elif defined(BOTAN_TARGET_OS_TYPE_IS_UNIKERNEL) + +// No threads + +namespace Botan { + +template<typename Mutex> +class lock_guard + { + public: + explicit lock_guard(Mutex& m) : m_mutex(m) + { m_mutex.lock(); } + + ~lock_guard() { m_mutex.unlock(); } + + lock_guard(const lock_guard& other) = delete; + lock_guard& operator=(const lock_guard& other) = delete; + private: + Mutex& m_mutex; + }; + +class noop_mutex + { + public: + void lock() {} + void unlock() {} + }; + +typedef noop_mutex mutex_type; +template<typename T> using lock_guard_type = lock_guard<T>; + +} + +#else + #error "Threads unexpectedly disabled in non unikernel build" +#endif + +#endif |