From d53921627fc4ccfedb33ca17d6a22cab06cd93bd Mon Sep 17 00:00:00 2001 From: pierantoniomerlino Date: Mon, 14 May 2018 15:23:26 +0200 Subject: [PATCH] Added mutex to BluetoothManager Signed-off-by: pierantoniomerlino --- api/tinyb/BluetoothManager.hpp | 4 ++++ src/BluetoothManager.cpp | 1 + 2 files changed, 5 insertions(+) diff --git a/api/tinyb/BluetoothManager.hpp b/api/tinyb/BluetoothManager.hpp index fd833cfc..21423619 100644 --- a/api/tinyb/BluetoothManager.hpp +++ b/api/tinyb/BluetoothManager.hpp @@ -41,6 +41,7 @@ friend class BluetoothEventManager; std::unique_ptr default_adapter; static BluetoothManager *bluetooth_manager; std::list> event_list; + std::mutex lock; BluetoothManager(); BluetoothManager(const BluetoothManager &object); @@ -74,16 +75,19 @@ friend class BluetoothEventManager; * matches an incoming event its' callback will be triggered. Events can be * the addition of a new Device, GattService, GattCharacteristic, etc. */ void add_event(std::shared_ptr &event) { + std::lock_guard guard(lock); event_list.push_back(event); } /** Remove event to checked against events generated by BlueZ. */ void remove_event(std::shared_ptr &event) { + std::lock_guard guard(lock); event_list.remove(event); } void remove_event(BluetoothEvent &event) { + std::lock_guard guard(lock); for(auto it = event_list.begin(); it != event_list.end(); ++it) { if ((*it).get() == &event) { event_list.remove(*it); diff --git a/src/BluetoothManager.cpp b/src/BluetoothManager.cpp index 1a05776e..87a4af20 100644 --- a/src/BluetoothManager.cpp +++ b/src/BluetoothManager.cpp @@ -206,6 +206,7 @@ std::weak_ptr BluetoothManager::find(BluetoothType type, void BluetoothManager::handle_event(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent, BluetoothObject &object) { + std::lock_guard guard(lock); for (auto it = event_list.begin(); it != event_list.end();) { if ((*it)->get_type() != BluetoothType::NONE && ((*it)->get_type()) != type) {