aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-01-11 20:11:01 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2017-03-11 15:41:37 +0100
commit73eac5050abad1b8f0ddbc7e94a11170a640e130 (patch)
tree67eb6db096a4fb06da3ba19c4bbbcbb71805a90f
parent02189413ce463116694478fcdcb418fed7e42027 (diff)
downloadlibnitrokey-73eac5050abad1b8f0ddbc7e94a11170a640e130.tar.gz
libnitrokey-73eac5050abad1b8f0ddbc7e94a11170a640e130.tar.bz2
Protect concurrent use with lock guard
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--NitrokeyManager.cc11
-rw-r--r--include/NitrokeyManager.h2
2 files changed, 11 insertions, 2 deletions
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc
index 73a704a..b270eb3 100644
--- a/NitrokeyManager.cc
+++ b/NitrokeyManager.cc
@@ -6,9 +6,13 @@
#include <unordered_map>
#include <stick20_commands.h>
#include "include/misc.h"
+#include <mutex>
namespace nitrokey{
+ std::mutex mex_dev_com;
+
+
template <typename T>
void strcpyT(T& dest, const char* src){
@@ -58,6 +62,7 @@ namespace nitrokey{
bool NitrokeyManager::connect() {
this->disconnect();
+ std::lock_guard<std::mutex> lock(mex_dev_com);
vector< shared_ptr<Device> > devices = { make_shared<Stick10>(), make_shared<Stick20>() };
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<std::mutex> lock(mex_dev_com);
switch (device_model[0]){
case 'P':
device = make_shared<Stick10>();
@@ -90,7 +96,10 @@ namespace nitrokey{
return _instance;
}
+
+
bool NitrokeyManager::disconnect() {
+ std::lock_guard<std::mutex> 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;
}
diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h
index d6b70a4..6551c1a 100644
--- a/include/NitrokeyManager.h
+++ b/include/NitrokeyManager.h
@@ -39,7 +39,7 @@ namespace nitrokey {
bool connect(const char *device_model);
bool connect();
bool disconnect();
- bool is_connected();
+ bool is_connected() const throw() ;
DeviceModel get_connected_device_model();
void set_debug(bool state);
stick10::GetStatus::ResponsePayload get_status();