diff options
Diffstat (limited to 'src')
30 files changed, 162 insertions, 62 deletions
diff --git a/src/lib/asn1/oid_lookup/oids.cpp b/src/lib/asn1/oid_lookup/oids.cpp index cdb863494..f49a3daf3 100644 --- a/src/lib/asn1/oid_lookup/oids.cpp +++ b/src/lib/asn1/oid_lookup/oids.cpp @@ -7,7 +7,7 @@ #include <botan/oids.h> #include <botan/parsing.h> -#include <mutex> +#include <botan/mutex.h> #include <sstream> namespace Botan { diff --git a/src/lib/base/algo_registry.h b/src/lib/base/algo_registry.h index f7e66b3e2..bca3715b6 100644 --- a/src/lib/base/algo_registry.h +++ b/src/lib/base/algo_registry.h @@ -13,7 +13,7 @@ #include <botan/exceptn.h> #include <botan/scan_name.h> #include <functional> -#include <mutex> +#include <botan/mutex.h> #include <vector> #include <map> #include <string> @@ -77,14 +77,14 @@ class Algo_Registry void add(const std::string& name, const std::string& provider, maker_fn fn, byte pref) { - std::lock_guard<mutex> lock(m_mutex); + lock_guard_type<af_mutex_type> lock(m_mutex); if(!m_algo_info[name].add_provider(provider, fn, pref)) throw Exception("Duplicated registration of " + name + "/" + provider); } std::vector<std::string> providers_of(const Spec& spec) { - std::lock_guard<mutex> lock(m_mutex); + lock_guard_type<af_mutex_type> lock(m_mutex); auto i = m_algo_info.find(spec.algo_name()); if(i != m_algo_info.end()) return i->second.providers(); @@ -93,7 +93,7 @@ class Algo_Registry void set_provider_preference(const Spec& spec, const std::string& provider, byte pref) { - std::lock_guard<mutex> lock(m_mutex); + lock_guard_type<af_mutex_type> lock(m_mutex); auto i = m_algo_info.find(spec.algo_name()); if(i != m_algo_info.end()) i->second.set_pref(provider, pref); @@ -137,16 +137,16 @@ class Algo_Registry private: #if defined(BOTAN_WORKAROUND_GH_321) - using mutex = WinCS_Mutex; + using af_mutex_type = WinCS_Mutex; #else - using mutex = std::mutex; + using af_mutex_type = mutex_type; #endif Algo_Registry() { } std::vector<maker_fn> get_makers(const Spec& spec, const std::string& provider) { - std::lock_guard<mutex> lock(m_mutex); + lock_guard_type<af_mutex_type> lock(m_mutex); return m_algo_info[spec.algo_name()].get_makers(provider); } @@ -208,7 +208,7 @@ class Algo_Registry std::unordered_map<std::string, maker_fn> m_maker_fns; }; - mutex m_mutex; + af_mutex_type m_mutex; std::unordered_map<std::string, Algo_Info> m_algo_info; }; diff --git a/src/lib/base/scan_name.cpp b/src/lib/base/scan_name.cpp index 08f5e8702..76d9b1a17 100644 --- a/src/lib/base/scan_name.cpp +++ b/src/lib/base/scan_name.cpp @@ -172,7 +172,7 @@ size_t SCAN_Name::arg_as_integer(size_t i, size_t def_value) const return to_u32bit(m_args[i]); } -std::mutex SCAN_Name::g_alias_map_mutex; +mutex_type SCAN_Name::g_alias_map_mutex; std::map<std::string, std::string> SCAN_Name::g_alias_map = { { "3DES", "TripleDES" }, { "ARC4", "RC4" }, @@ -197,7 +197,7 @@ std::map<std::string, std::string> SCAN_Name::g_alias_map = { void SCAN_Name::add_alias(const std::string& alias, const std::string& basename) { - std::lock_guard<std::mutex> lock(g_alias_map_mutex); + lock_guard_type<mutex_type> lock(g_alias_map_mutex); if(g_alias_map.find(alias) == g_alias_map.end()) g_alias_map[alias] = basename; @@ -205,7 +205,7 @@ void SCAN_Name::add_alias(const std::string& alias, const std::string& basename) std::string SCAN_Name::deref_alias(const std::string& alias) { - std::lock_guard<std::mutex> lock(g_alias_map_mutex); + lock_guard_type<mutex_type> lock(g_alias_map_mutex); std::string name = alias; diff --git a/src/lib/base/scan_name.h b/src/lib/base/scan_name.h index d59d5889e..1d9132729 100644 --- a/src/lib/base/scan_name.h +++ b/src/lib/base/scan_name.h @@ -11,7 +11,7 @@ #include <botan/types.h> #include <string> #include <vector> -#include <mutex> +#include <botan/mutex.h> #include <map> namespace Botan { @@ -107,7 +107,7 @@ class BOTAN_DLL SCAN_Name static std::string deref_alias(const std::string& alias); private: - static std::mutex g_alias_map_mutex; + static mutex_type g_alias_map_mutex; static std::map<std::string, std::string> g_alias_map; std::string m_orig_algo_spec; diff --git a/src/lib/cert/x509/certstor_sql/certstor_sql.h b/src/lib/cert/x509/certstor_sql/certstor_sql.h index 096426b7a..281090542 100644 --- a/src/lib/cert/x509/certstor_sql/certstor_sql.h +++ b/src/lib/cert/x509/certstor_sql/certstor_sql.h @@ -86,7 +86,7 @@ class BOTAN_DLL Certificate_Store_In_SQL : public Certificate_Store std::shared_ptr<SQL_Database> m_database; std::string m_prefix; std::string m_password; - std::mutex m_mutex; + mutex_type m_mutex; }; } diff --git a/src/lib/cert/x509/x509path.cpp b/src/lib/cert/x509/x509path.cpp index 65ab3eac1..29853bb4a 100644 --- a/src/lib/cert/x509/x509path.cpp +++ b/src/lib/cert/x509/x509path.cpp @@ -15,6 +15,7 @@ #include <chrono> #include <vector> #include <set> +#include <future> namespace Botan { diff --git a/src/lib/entropy/egd/es_egd.cpp b/src/lib/entropy/egd/es_egd.cpp index 384516aa8..fdc1c9a0f 100644 --- a/src/lib/entropy/egd/es_egd.cpp +++ b/src/lib/entropy/egd/es_egd.cpp @@ -136,7 +136,7 @@ EGD_EntropySource::~EGD_EntropySource() */ size_t EGD_EntropySource::poll(RandomNumberGenerator& rng) { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); secure_vector<byte> buf(BOTAN_SYSTEM_RNG_POLL_REQUEST); diff --git a/src/lib/entropy/egd/es_egd.h b/src/lib/entropy/egd/es_egd.h index 04b4591e3..e0fb9c2d5 100644 --- a/src/lib/entropy/egd/es_egd.h +++ b/src/lib/entropy/egd/es_egd.h @@ -11,7 +11,7 @@ #include <botan/entropy_src.h> #include <string> #include <vector> -#include <mutex> +#include <botan/mutex.h> namespace Botan { @@ -42,7 +42,7 @@ class EGD_EntropySource final : public Entropy_Source int m_fd; // cached fd }; - std::mutex m_mutex; + mutex_type m_mutex; std::vector<EGD_Socket> m_sockets; secure_vector<uint8_t> m_io_buf; }; diff --git a/src/lib/entropy/proc_walk/proc_walk.cpp b/src/lib/entropy/proc_walk/proc_walk.cpp index beaa57308..5132ac951 100644 --- a/src/lib/entropy/proc_walk/proc_walk.cpp +++ b/src/lib/entropy/proc_walk/proc_walk.cpp @@ -114,7 +114,7 @@ size_t ProcWalking_EntropySource::poll(RandomNumberGenerator& rng) { const size_t MAX_FILES_READ_PER_POLL = 2048; - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); if(!m_dir) m_dir.reset(new Directory_Walker(m_path)); diff --git a/src/lib/entropy/proc_walk/proc_walk.h b/src/lib/entropy/proc_walk/proc_walk.h index 369b52699..4e9562c88 100644 --- a/src/lib/entropy/proc_walk/proc_walk.h +++ b/src/lib/entropy/proc_walk/proc_walk.h @@ -9,7 +9,7 @@ #define BOTAN_ENTROPY_SRC_PROC_WALK_H__ #include <botan/entropy_src.h> -#include <mutex> +#include <botan/mutex.h> namespace Botan { @@ -35,7 +35,7 @@ class ProcWalking_EntropySource final : public Entropy_Source private: const std::string m_path; - std::mutex m_mutex; + mutex_type m_mutex; std::unique_ptr<File_Descriptor_Source> m_dir; secure_vector<byte> m_buf; }; diff --git a/src/lib/entropy/unix_procs/unix_procs.cpp b/src/lib/entropy/unix_procs/unix_procs.cpp index 8f885cfcf..d8dceba52 100644 --- a/src/lib/entropy/unix_procs/unix_procs.cpp +++ b/src/lib/entropy/unix_procs/unix_procs.cpp @@ -209,7 +209,7 @@ size_t Unix_EntropySource::poll(RandomNumberGenerator& rng) if((getuid() != geteuid()) || (getgid() != getegid()) || (geteuid() == 0)) return 0; - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); if(m_sources.empty()) { diff --git a/src/lib/entropy/unix_procs/unix_procs.h b/src/lib/entropy/unix_procs/unix_procs.h index 27f7ab5bb..f87881d54 100644 --- a/src/lib/entropy/unix_procs/unix_procs.h +++ b/src/lib/entropy/unix_procs/unix_procs.h @@ -10,7 +10,7 @@ #include <botan/entropy_src.h> #include <vector> -#include <mutex> +#include <botan/mutex.h> namespace Botan { @@ -67,7 +67,7 @@ class Unix_EntropySource final : public Entropy_Source const std::vector<std::string>& next_source(); - std::mutex m_mutex; + mutex_type m_mutex; const std::vector<std::string> m_trusted_paths; const size_t m_concurrent; diff --git a/src/lib/filters/basefilt.h b/src/lib/filters/basefilt.h index c065fae0e..d803542a1 100644 --- a/src/lib/filters/basefilt.h +++ b/src/lib/filters/basefilt.h @@ -10,7 +10,10 @@ #define BOTAN_BASEFILT_H__ #include <botan/filter.h> + +#if defined(BOTAN_TARGET_OS_HAS_THREADS) #include <thread> +#endif namespace Botan { @@ -78,6 +81,8 @@ class BOTAN_DLL Fork : public Fanout_Filter Fork(Filter* filter_arr[], size_t length); }; +#if defined(BOTAN_TARGET_OS_HAS_THREADS) + /** * This class is a threaded version of the Fork filter. While this uses * threads, the class itself is NOT thread-safe. This is meant as a drop- @@ -113,6 +118,7 @@ class BOTAN_DLL Threaded_Fork : public Fork std::vector<std::shared_ptr<std::thread>> m_threads; std::unique_ptr<struct Threaded_Fork_Data> m_thread_data; }; +#endif } diff --git a/src/lib/filters/threaded_fork.cpp b/src/lib/filters/threaded_fork.cpp index 5ee802593..f558ac9c2 100644 --- a/src/lib/filters/threaded_fork.cpp +++ b/src/lib/filters/threaded_fork.cpp @@ -7,6 +7,9 @@ */ #include <botan/basefilt.h> + +#if defined(BOTAN_TARGET_OS_HAS_THREADS) + #include <botan/internal/semaphore.h> namespace Botan { @@ -144,3 +147,5 @@ void Threaded_Fork::thread_entry(Filter* filter) } } + +#endif diff --git a/src/lib/pubkey/dsa/dsa.cpp b/src/lib/pubkey/dsa/dsa.cpp index 15dc45373..9c8ae0821 100644 --- a/src/lib/pubkey/dsa/dsa.cpp +++ b/src/lib/pubkey/dsa/dsa.cpp @@ -17,7 +17,9 @@ #include <botan/rfc6979.h> #endif -#include <future> +#if defined(BOTAN_TARGET_OS_HAS_THREADS) + #include <future> +#endif namespace Botan { @@ -124,11 +126,17 @@ DSA_Signature_Operation::raw_sign(const byte msg[], size_t msg_len, const BigInt k = BigInt::random_integer(rng, 1, m_q); #endif +#if defined(BOTAN_TARGET_OS_HAS_THREADS) auto future_r = std::async(std::launch::async, [&]() { return m_mod_q.reduce(m_powermod_g_p(k)); }); BigInt s = inverse_mod(k, m_q); const BigInt r = future_r.get(); +#else + BigInt s = inverse_mod(k, m_q); + const BigInt r = m_mod_q.reduce(m_powermod_g_p(k)); +#endif + s = m_mod_q.multiply(s, mul_add(m_x, r, i)); // With overwhelming probability, a bug rather than actual zero r/s @@ -184,11 +192,16 @@ bool DSA_Verification_Operation::verify(const byte msg[], size_t msg_len, s = inverse_mod(s, m_q); +#if defined(BOTAN_TARGET_OS_HAS_THREADS) auto future_s_i = std::async(std::launch::async, [&]() { return m_powermod_g_p(m_mod_q.multiply(s, i)); }); BigInt s_r = m_powermod_y_p(m_mod_q.multiply(s, r)); BigInt s_i = future_s_i.get(); +#else + BigInt s_r = m_powermod_y_p(m_mod_q.multiply(s, r)); + BigInt s_i = m_powermod_g_p(m_mod_q.multiply(s, i)); +#endif s = m_mod_p.multiply(s_i, s_r); diff --git a/src/lib/pubkey/rsa/rsa.cpp b/src/lib/pubkey/rsa/rsa.cpp index b40f485e3..d201ca277 100644 --- a/src/lib/pubkey/rsa/rsa.cpp +++ b/src/lib/pubkey/rsa/rsa.cpp @@ -14,12 +14,16 @@ #include <botan/workfactor.h> #include <botan/der_enc.h> #include <botan/ber_dec.h> -#include <future> #if defined(BOTAN_HAS_OPENSSL) #include <botan/internal/openssl.h> #endif +#if defined(BOTAN_TARGET_OS_HAS_THREADS) +#include <future> +#endif + + namespace Botan { size_t RSA_PublicKey::estimated_strength() const @@ -218,9 +222,14 @@ class RSA_Private_Operation BigInt private_op(const BigInt& m) const { +#if defined(BOTAN_TARGET_OS_HAS_THREADS) auto future_j1 = std::async(std::launch::async, m_powermod_d1_p, m); BigInt j2 = m_powermod_d2_q(m); BigInt j1 = future_j1.get(); +#else + BigInt j1 = m_powermod_d1_p(m); + BigInt j2 = m_powermod_d2_q(m); +#endif j1 = m_mod_p.reduce(sub_mul(j1, j2, m_c)); diff --git a/src/lib/rng/rng.cpp b/src/lib/rng/rng.cpp index 8c2982312..a2dd3d3a4 100644 --- a/src/lib/rng/rng.cpp +++ b/src/lib/rng/rng.cpp @@ -57,6 +57,8 @@ RandomNumberGenerator* RandomNumberGenerator::make_rng() #endif } +#if defined(BOTAN_TARGET_OS_HAS_THREADS) Serialized_RNG::Serialized_RNG() : m_rng(RandomNumberGenerator::make_rng()) {} +#endif } diff --git a/src/lib/rng/rng.h b/src/lib/rng/rng.h index d1cdcfff2..e3640a32f 100644 --- a/src/lib/rng/rng.h +++ b/src/lib/rng/rng.h @@ -11,9 +11,9 @@ #include <botan/entropy_src.h> #include <botan/secmem.h> #include <botan/exceptn.h> +#include <botan/mutex.h> #include <chrono> #include <string> -#include <mutex> namespace Botan { @@ -193,6 +193,7 @@ class BOTAN_DLL Null_RNG final : public RandomNumberGenerator std::string name() const override { return "Null_RNG"; } }; +#if defined(BOTAN_TARGET_OS_HAS_THREADS) /** * Wraps access to a RNG in a mutex */ @@ -201,25 +202,25 @@ class BOTAN_DLL Serialized_RNG final : public RandomNumberGenerator public: void randomize(byte out[], size_t len) override { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); m_rng->randomize(out, len); } bool is_seeded() const override { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); return m_rng->is_seeded(); } void clear() override { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); m_rng->clear(); } std::string name() const override { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); return m_rng->name(); } @@ -227,23 +228,24 @@ class BOTAN_DLL Serialized_RNG final : public RandomNumberGenerator size_t poll_bits = BOTAN_RNG_RESEED_POLL_BITS, std::chrono::milliseconds poll_timeout = BOTAN_RNG_RESEED_DEFAULT_TIMEOUT) override { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); return m_rng->reseed(src, poll_bits, poll_timeout); } void add_entropy(const byte in[], size_t len) override { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); m_rng->add_entropy(in, len); } - BOTAN_DEPRECATED("Create an AutoSeeded_RNG for other constructor") Serialized_RNG(); + BOTAN_DEPRECATED("Use Serialized_RNG(new AutoSeeded_RNG)") Serialized_RNG(); explicit Serialized_RNG(RandomNumberGenerator* rng) : m_rng(rng) {} private: - mutable std::mutex m_mutex; + mutable mutex_type m_mutex; std::unique_ptr<RandomNumberGenerator> m_rng; }; +#endif } diff --git a/src/lib/tls/tls_session_manager.h b/src/lib/tls/tls_session_manager.h index e01462f66..49f4925d8 100644 --- a/src/lib/tls/tls_session_manager.h +++ b/src/lib/tls/tls_session_manager.h @@ -9,7 +9,7 @@ #define BOTAN_TLS_SESSION_MANAGER_H__ #include <botan/tls_session.h> -#include <mutex> +#include <botan/mutex.h> #include <chrono> #include <map> @@ -138,7 +138,7 @@ class BOTAN_DLL Session_Manager_In_Memory : public Session_Manager bool load_from_session_str(const std::string& session_str, Session& session); - std::mutex m_mutex; + mutex_type m_mutex; size_t m_max_sessions; diff --git a/src/lib/tls/tls_session_manager_memory.cpp b/src/lib/tls/tls_session_manager_memory.cpp index 37019c943..d0866b37a 100644 --- a/src/lib/tls/tls_session_manager_memory.cpp +++ b/src/lib/tls/tls_session_manager_memory.cpp @@ -57,7 +57,7 @@ bool Session_Manager_In_Memory::load_from_session_str( bool Session_Manager_In_Memory::load_from_session_id( const std::vector<byte>& session_id, Session& session) { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); return load_from_session_str(hex_encode(session_id), session); } @@ -65,7 +65,7 @@ bool Session_Manager_In_Memory::load_from_session_id( bool Session_Manager_In_Memory::load_from_server_info( const Server_Information& info, Session& session) { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); auto i = m_info_sessions.find(info); @@ -87,7 +87,7 @@ bool Session_Manager_In_Memory::load_from_server_info( void Session_Manager_In_Memory::remove_entry( const std::vector<byte>& session_id) { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); auto i = m_sessions.find(hex_encode(session_id)); @@ -106,7 +106,7 @@ size_t Session_Manager_In_Memory::remove_all() void Session_Manager_In_Memory::save(const Session& session) { - std::lock_guard<std::mutex> lock(m_mutex); + lock_guard_type<mutex_type> lock(m_mutex); if(m_max_sessions != 0) { 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 } |