diff options
author | Szczepan Zalega <szczepan@nitrokey.com> | 2017-01-31 18:10:59 +0100 |
---|---|---|
committer | Szczepan Zalega <szczepan@nitrokey.com> | 2017-03-11 15:41:40 +0100 |
commit | cb6b2dd65e1f0132353159b83ae05c944d8e62f0 (patch) | |
tree | ba7f8a91390f15f03a4f90c93ecd45f9e7ae3749 /NitrokeyManager.cc | |
parent | aa668f74e95617fd0544327a2b57bf654a6f9a2d (diff) | |
download | libnitrokey-cb6b2dd65e1f0132353159b83ae05c944d8e62f0.tar.gz libnitrokey-cb6b2dd65e1f0132353159b83ae05c944d8e62f0.tar.bz2 |
Make disconnect thread safe. Check is device actually connected by invoking its checking method
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
Diffstat (limited to 'NitrokeyManager.cc')
-rw-r--r-- | NitrokeyManager.cc | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index ee7ca92..3213417 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -100,16 +100,32 @@ namespace nitrokey{ bool NitrokeyManager::disconnect() { std::lock_guard<std::mutex> lock(mex_dev_com); - if (!is_connected()){ - return false; - } - const auto res = device->disconnect(); - device = nullptr; - return res; + return _disconnect_no_lock(); } - bool NitrokeyManager::is_connected() const throw(){ - return device != nullptr; + bool NitrokeyManager::_disconnect_no_lock() { + //do not use directly without locked mutex, + //used by is_connected, disconnect + if (device == nullptr){ + return false; + } + const auto res = device->disconnect(); + device = nullptr; + return res; + } + + bool NitrokeyManager::is_connected() throw(){ + std::lock_guard<std::mutex> lock(mex_dev_com); + if(device != nullptr){ + auto connected = device->is_connected(); + if(connected){ + return true; + } else { + _disconnect_no_lock(); + return false; + } + } + return false; } void NitrokeyManager::set_debug(bool state) { |