From 73eac5050abad1b8f0ddbc7e94a11170a640e130 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 20:11:01 +0100 Subject: Protect concurrent use with lock guard Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'NitrokeyManager.cc') diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 73a704a..b270eb3 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -6,9 +6,13 @@ #include #include #include "include/misc.h" +#include namespace nitrokey{ + std::mutex mex_dev_com; + + template void strcpyT(T& dest, const char* src){ @@ -58,6 +62,7 @@ namespace nitrokey{ bool NitrokeyManager::connect() { this->disconnect(); + std::lock_guard lock(mex_dev_com); vector< shared_ptr > devices = { make_shared(), make_shared() }; for( auto & d : devices ){ if (d->connect()){ @@ -70,6 +75,7 @@ namespace nitrokey{ bool NitrokeyManager::connect(const char *device_model) { this->disconnect(); + std::lock_guard lock(mex_dev_com); switch (device_model[0]){ case 'P': device = make_shared(); @@ -90,7 +96,10 @@ namespace nitrokey{ return _instance; } + + bool NitrokeyManager::disconnect() { + std::lock_guard lock(mex_dev_com); if (!is_connected()){ return false; } @@ -99,7 +108,7 @@ namespace nitrokey{ return res; } - bool NitrokeyManager::is_connected(){ + bool NitrokeyManager::is_connected() const throw(){ return device != nullptr; } -- cgit v1.2.1