aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-10-12 15:32:14 -0400
committerJack Lloyd <[email protected]>2016-10-12 15:32:14 -0400
commited9e147695e4c5e800e83654baf365a634f3a2a7 (patch)
tree59bad402cff7d7af9baa5fd79081d677b60afc83 /src/lib/utils
parentd59b164a2ad2bc2290265530ac1a5c7be7855975 (diff)
Abstract out mutex type. Make threads optional.
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/calendar.cpp4
-rw-r--r--src/lib/utils/http_util/http_util.cpp5
-rw-r--r--src/lib/utils/http_util/http_util.h4
-rw-r--r--src/lib/utils/info.txt1
-rw-r--r--src/lib/utils/locking_allocator/locking_allocator.cpp6
-rw-r--r--src/lib/utils/locking_allocator/locking_allocator.h4
-rw-r--r--src/lib/utils/mutex.h61
-rw-r--r--src/lib/utils/os_utils.cpp2
-rw-r--r--src/lib/utils/semaphore.cpp8
-rw-r--r--src/lib/utils/semaphore.h9
10 files changed, 83 insertions, 21 deletions
diff --git a/src/lib/utils/calendar.cpp b/src/lib/utils/calendar.cpp
index 2ed90486a..4f6c835d1 100644
--- a/src/lib/utils/calendar.cpp
+++ b/src/lib/utils/calendar.cpp
@@ -11,7 +11,7 @@
#include <ctime>
#include <sstream>
#include <iomanip>
-#include <mutex>
+#include <botan/mutex.h>
#include <stdlib.h>
#if defined(BOTAN_HAS_BOOST_DATETIME)
@@ -72,7 +72,7 @@ std::time_t boost_timegm(std::tm *tm)
#pragma message "Caution! A fallback version of timegm() is used which is not thread-safe"
-std::mutex ENV_TZ;
+mutex_type ENV_TZ;
std::time_t fallback_timegm(std::tm *tm)
{
diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp
index 1286e4026..5624f2e4f 100644
--- a/src/lib/utils/http_util/http_util.cpp
+++ b/src/lib/utils/http_util/http_util.cpp
@@ -226,11 +226,6 @@ Response POST_sync(const std::string& url,
return http_sync("POST", url, content_type, body, allowable_redirects);
}
-std::future<Response> GET_async(const std::string& url, size_t allowable_redirects)
- {
- return std::async(std::launch::async, GET_sync, url, allowable_redirects);
- }
-
}
}
diff --git a/src/lib/utils/http_util/http_util.h b/src/lib/utils/http_util/http_util.h
index 6688285c6..918dfc269 100644
--- a/src/lib/utils/http_util/http_util.h
+++ b/src/lib/utils/http_util/http_util.h
@@ -10,7 +10,6 @@
#include <botan/types.h>
#include <botan/exceptn.h>
-#include <future>
#include <vector>
#include <map>
#include <chrono>
@@ -88,9 +87,6 @@ BOTAN_DLL Response POST_sync(const std::string& url,
const std::vector<byte>& body,
size_t allowable_redirects = 1);
-std::future<Response> BOTAN_DLL GET_async(const std::string& url,
- size_t allowable_redirects = 1);
-
BOTAN_DLL std::string url_encode(const std::string& url);
}
diff --git a/src/lib/utils/info.txt b/src/lib/utils/info.txt
index 511e6b0e8..189b2da1f 100644
--- a/src/lib/utils/info.txt
+++ b/src/lib/utils/info.txt
@@ -14,6 +14,7 @@ exceptn.h
loadstor.h
mem_ops.h
mul128.h
+mutex.h
parsing.h
rotate.h
types.h
diff --git a/src/lib/utils/locking_allocator/locking_allocator.cpp b/src/lib/utils/locking_allocator/locking_allocator.cpp
index c6181d4c0..770464d92 100644
--- a/src/lib/utils/locking_allocator/locking_allocator.cpp
+++ b/src/lib/utils/locking_allocator/locking_allocator.cpp
@@ -11,7 +11,7 @@
#include <algorithm>
#include <cstdlib>
#include <string>
-#include <mutex>
+#include <botan/mutex.h>
namespace Botan {
@@ -58,7 +58,7 @@ void* mlock_allocator::allocate(size_t num_elems, size_t elem_size)
if(n < BOTAN_MLOCK_ALLOCATOR_MIN_ALLOCATION || n > BOTAN_MLOCK_ALLOCATOR_MAX_ALLOCATION)
return nullptr;
- std::lock_guard<std::mutex> lock(m_mutex);
+ lock_guard_type<mutex_type> lock(m_mutex);
auto best_fit = m_freelist.end();
@@ -146,7 +146,7 @@ bool mlock_allocator::deallocate(void* p, size_t num_elems, size_t elem_size)
if(!ptr_in_pool(m_pool, m_poolsize, p, n))
return false;
- std::lock_guard<std::mutex> lock(m_mutex);
+ lock_guard_type<mutex_type> lock(m_mutex);
const size_t start = static_cast<byte*>(p) - m_pool;
diff --git a/src/lib/utils/locking_allocator/locking_allocator.h b/src/lib/utils/locking_allocator/locking_allocator.h
index db75bc02a..5c19852c0 100644
--- a/src/lib/utils/locking_allocator/locking_allocator.h
+++ b/src/lib/utils/locking_allocator/locking_allocator.h
@@ -10,7 +10,7 @@
#include <botan/types.h>
#include <vector>
-#include <mutex>
+#include <botan/mutex.h>
namespace Botan {
@@ -32,7 +32,7 @@ class BOTAN_DLL mlock_allocator
~mlock_allocator();
- std::mutex m_mutex;
+ mutex_type m_mutex;
std::vector<std::pair<size_t, size_t>> m_freelist;
byte* m_pool = nullptr;
size_t m_poolsize = 0;
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
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index 56ec59cc0..4020a4be3 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -34,7 +34,7 @@ uint32_t get_process_id()
#elif defined(BOTAN_TARGET_OS_IS_WINDOWS) || defined(BOTAN_TARGET_OS_IS_MINGW)
return ::GetCurrentProcessId();
#elif defined(BOTAN_TARGET_OS_TYPE_IS_UNIKERNEL)
- return 0;
+ return 0; // truly no meaningful value
#else
#error "Missing get_process_id"
#endif
diff --git a/src/lib/utils/semaphore.cpp b/src/lib/utils/semaphore.cpp
index f30c43661..62c31d3e3 100644
--- a/src/lib/utils/semaphore.cpp
+++ b/src/lib/utils/semaphore.cpp
@@ -7,6 +7,8 @@
#include <botan/internal/semaphore.h>
+#if defined(BOTAN_TARGET_OS_HAS_THREADS)
+
// Based on code by Pierre Gaston (http://p9as.blogspot.com/2012/06/c11-semaphores.html)
namespace Botan {
@@ -15,7 +17,7 @@ void Semaphore::release(size_t n)
{
for(size_t i = 0; i != n; ++i)
{
- std::lock_guard<std::mutex> lock(m_mutex);
+ lock_guard_type<mutex_type> lock(m_mutex);
++m_value;
@@ -29,7 +31,7 @@ void Semaphore::release(size_t n)
void Semaphore::acquire()
{
- std::unique_lock<std::mutex> lock(m_mutex);
+ std::unique_lock<mutex_type> lock(m_mutex);
--m_value;
if(m_value < 0)
{
@@ -39,3 +41,5 @@ void Semaphore::acquire()
}
}
+
+#endif
diff --git a/src/lib/utils/semaphore.h b/src/lib/utils/semaphore.h
index 994a15f21..87e6b84fe 100644
--- a/src/lib/utils/semaphore.h
+++ b/src/lib/utils/semaphore.h
@@ -8,11 +8,15 @@
#ifndef BOTAN_SEMAPHORE_H__
#define BOTAN_SEMAPHORE_H__
-#include <mutex>
+#include <botan/mutex.h>
+
+#if defined(BOTAN_TARGET_OS_HAS_THREADS)
#include <condition_variable>
+#endif
namespace Botan {
+#if defined(BOTAN_TARGET_OS_HAS_THREADS)
class Semaphore
{
public:
@@ -25,9 +29,10 @@ class Semaphore
private:
int m_value;
int m_wakeups;
- std::mutex m_mutex;
+ mutex_type m_mutex;
std::condition_variable m_cond;
};
+#endif
}