aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-10-17 03:11:14 -0400
committerJack Lloyd <[email protected]>2016-10-17 03:11:14 -0400
commit0b353965a56dabf7528eecf672cc627304dbb8e1 (patch)
treededfd4db3cc140f4a4c2fbba8a566742bc735fd2 /src/lib/utils
parenta816a52612cd8e9cf12bfdccaacc5ce7960b2700 (diff)
parent8b3bda479efecef760f052cc055d3d6d98bf0637 (diff)
Merge GH #665 Add IncludeOS target, make filesystem/threads optional
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/calendar.cpp4
-rw-r--r--src/lib/utils/data_src.cpp9
-rw-r--r--src/lib/utils/data_src.h4
-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.cpp4
-rw-r--r--src/lib/utils/semaphore.cpp8
-rw-r--r--src/lib/utils/semaphore.h9
12 files changed, 97 insertions, 22 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/data_src.cpp b/src/lib/utils/data_src.cpp
index 0c06f2fe4..7da94d7c0 100644
--- a/src/lib/utils/data_src.cpp
+++ b/src/lib/utils/data_src.cpp
@@ -8,9 +8,12 @@
#include <botan/data_src.h>
#include <botan/exceptn.h>
-#include <fstream>
#include <algorithm>
+#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
+ #include <fstream>
+#endif
+
namespace Botan {
/*
@@ -98,6 +101,8 @@ DataSource_Memory::DataSource_Memory(const std::string& in) :
{
}
+#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
+
/*
* Read from a stream
*/
@@ -209,4 +214,6 @@ DataSource_Stream::~DataSource_Stream()
delete m_source_p;
}
+#endif
+
}
diff --git a/src/lib/utils/data_src.h b/src/lib/utils/data_src.h
index 6a100ce63..b24fd75a4 100644
--- a/src/lib/utils/data_src.h
+++ b/src/lib/utils/data_src.h
@@ -138,6 +138,8 @@ class BOTAN_DLL DataSource_Memory : public DataSource
size_t m_offset;
};
+#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
+
/**
* This class represents a Stream-Based DataSource.
*/
@@ -175,6 +177,8 @@ class BOTAN_DLL DataSource_Stream : public DataSource
size_t m_total_read;
};
+#endif
+
}
#endif
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 2f17da952..4020a4be3 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -33,8 +33,10 @@ uint32_t get_process_id()
return ::getpid();
#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; // truly no meaningful value
#else
- throw Exception("get_process_id not supported");
+ #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
}