summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2017-01-31 18:07:55 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2017-03-11 15:41:40 +0100
commitaa668f74e95617fd0544327a2b57bf654a6f9a2d (patch)
tree1dd7e6b16cf6d88f3a64757fc4f9289e4b41a359
parent48ec48d2c680b9f40d2b038fda9555bfd024bb97 (diff)
downloadlibnitrokey-aa668f74e95617fd0544327a2b57bf654a6f9a2d.tar.gz
libnitrokey-aa668f74e95617fd0544327a2b57bf654a6f9a2d.tar.bz2
Be tread-safe on initializing instance
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--NitrokeyManager.cc3
-rw-r--r--include/DeviceCommunicationExceptions.h31
2 files changed, 33 insertions, 1 deletions
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc
index dc58e4d..ee7ca92 100644
--- a/NitrokeyManager.cc
+++ b/NitrokeyManager.cc
@@ -88,7 +88,8 @@ namespace nitrokey{
}
shared_ptr<NitrokeyManager> NitrokeyManager::instance() {
- //FIXME check thread safety - add atomic for instance, add lock guard
+ static std::mutex mutex;
+ std::lock_guard<std::mutex> lock(mutex);
if (_instance == nullptr){
_instance = make_shared<NitrokeyManager>();
}
diff --git a/include/DeviceCommunicationExceptions.h b/include/DeviceCommunicationExceptions.h
new file mode 100644
index 0000000..78fc625
--- /dev/null
+++ b/include/DeviceCommunicationExceptions.h
@@ -0,0 +1,31 @@
+#ifndef LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H
+#define LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H
+
+#include <exception>
+#include <string>
+//class DeviceCommunicationException: public std::exception {
+class DeviceCommunicationException: public std::runtime_error{
+ std::string message;
+public:
+ DeviceCommunicationException(std::string _msg): runtime_error(_msg), message(_msg){}
+// virtual const char* what() const throw() override {
+// return message.c_str();
+// }
+};
+
+class DeviceNotConnected: public DeviceCommunicationException {
+public:
+ DeviceNotConnected(std::string msg) : DeviceCommunicationException(msg){}
+};
+
+class DeviceSendingFailure: public DeviceCommunicationException {
+public:
+ DeviceSendingFailure(std::string msg) : DeviceCommunicationException(msg){}
+};
+
+class DeviceReceivingFailure: public DeviceCommunicationException {
+public:
+ DeviceReceivingFailure(std::string msg) : DeviceCommunicationException(msg){}
+};
+
+#endif //LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H