aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--api/direct_bt/DBTManager.hpp6
-rw-r--r--api/direct_bt/GATTHandler.hpp8
-rw-r--r--api/direct_bt/HCIHandler.hpp6
-rw-r--r--src/direct_bt/DBTManager.cpp8
-rw-r--r--src/direct_bt/GATTHandler.cpp6
-rw-r--r--src/direct_bt/HCIHandler.cpp6
6 files changed, 27 insertions, 13 deletions
diff --git a/api/direct_bt/DBTManager.hpp b/api/direct_bt/DBTManager.hpp
index 91e95b44..fa0619e2 100644
--- a/api/direct_bt/DBTManager.hpp
+++ b/api/direct_bt/DBTManager.hpp
@@ -213,16 +213,16 @@ namespace direct_bt {
HCIComm comm;
jau::ringbuffer<std::shared_ptr<MgmtEvent>, nullptr, jau::nsize_t> mgmtEventRing;
- std::atomic<bool> mgmtReaderShallStop;
+ jau::sc_atomic_bool mgmtReaderShallStop;
std::mutex mtx_mgmtReaderLifecycle;
std::condition_variable cv_mgmtReaderInit;
pthread_t mgmtReaderThreadId;
- bool mgmtReaderRunning;
+ jau::relaxed_atomic_bool mgmtReaderRunning;
std::recursive_mutex mtx_sendReply; // for sendWithReply
- std::atomic<bool> allowClose;
+ jau::sc_atomic_bool allowClose;
/** One MgmtAdapterEventCallbackList per event type, allowing multiple callbacks to be invoked for each event */
std::array<MgmtAdapterEventCallbackList, static_cast<uint16_t>(MgmtEvent::Opcode::MGMT_EVENT_TYPE_COUNT)> mgmtAdapterEventCallbackLists;
diff --git a/api/direct_bt/GATTHandler.hpp b/api/direct_bt/GATTHandler.hpp
index fc202ff6..00991339 100644
--- a/api/direct_bt/GATTHandler.hpp
+++ b/api/direct_bt/GATTHandler.hpp
@@ -160,16 +160,16 @@ namespace direct_bt {
POctets rbuffer;
L2CAPComm l2cap;
- std::atomic<bool> is_connected; // reflects state
- std::atomic<bool> has_ioerror; // reflects state
+ jau::sc_atomic_bool is_connected; // reflects state
+ jau::relaxed_atomic_bool has_ioerror; // reflects state
jau::ringbuffer<std::shared_ptr<const AttPDUMsg>, nullptr, jau::nsize_t> attPDURing;
- std::atomic<bool> l2capReaderShallStop;
+ jau::sc_atomic_bool l2capReaderShallStop;
std::mutex mtx_l2capReaderLifecycle;
std::condition_variable cv_l2capReaderInit;
pthread_t l2capReaderThreadId;
- bool l2capReaderRunning;
+ jau::relaxed_atomic_bool l2capReaderRunning;
/** send immediate confirmation of indication events from device, defaults to true. */
jau::relaxed_atomic_bool sendIndicationConfirmation = true;
diff --git a/api/direct_bt/HCIHandler.hpp b/api/direct_bt/HCIHandler.hpp
index 42a304f3..3e5547cb 100644
--- a/api/direct_bt/HCIHandler.hpp
+++ b/api/direct_bt/HCIHandler.hpp
@@ -227,16 +227,16 @@ namespace direct_bt {
inline static void filter_set_opcbit(HCIOpcodeBit opcbit, uint64_t &mask) noexcept { jau::set_bit_uint64(number(opcbit), mask); }
jau::ringbuffer<std::shared_ptr<HCIEvent>, nullptr, jau::nsize_t> hciEventRing;
- std::atomic<bool> hciReaderShallStop;
+ jau::sc_atomic_bool hciReaderShallStop;
std::mutex mtx_hciReaderLifecycle;
std::condition_variable cv_hciReaderInit;
pthread_t hciReaderThreadId;
- bool hciReaderRunning;
+ jau::relaxed_atomic_bool hciReaderRunning;
std::recursive_mutex mtx_sendReply; // for sendWith*Reply, process*Command, ..; Recurses from many..
- std::atomic<bool> allowClose;
+ jau::sc_atomic_bool allowClose;
std::atomic<BTMode> btMode;
std::atomic<ScanType> currentScanType;
diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp
index afea062b..7572e4ba 100644
--- a/src/direct_bt/DBTManager.cpp
+++ b/src/direct_bt/DBTManager.cpp
@@ -37,6 +37,8 @@
// #define PERF3_PRINT_ON 1
#include <jau/debug.hpp>
+#include <jau/basic_algos.hpp>
+
#include "BTIoctl.hpp"
#include "DBTManager.hpp"
@@ -87,6 +89,10 @@ void DBTManager::mgmtReaderThreadImpl() noexcept {
DBG_PRINT("DBTManager::reader: Started");
cv_mgmtReaderInit.notify_all();
}
+ thread_local jau::call_on_release thread_cleanup([&]() {
+ DBG_PRINT("DBTManager::mgmtReaderThreadCleanup: mgmtReaderRunning %d -> 0", mgmtReaderRunning.load());
+ mgmtReaderRunning = false;
+ });
while( !mgmtReaderShallStop ) {
jau::snsize_t len;
@@ -502,7 +508,7 @@ void DBTManager::close() noexcept {
mgmtReaderThreadId = 0;
const bool is_reader = tid_reader == tid_self;
DBG_PRINT("DBTManager::close: mgmtReader[running %d, shallStop %d, isReader %d, tid %p)",
- mgmtReaderRunning, mgmtReaderShallStop.load(), is_reader, (void*)tid_reader);
+ mgmtReaderRunning.load(), mgmtReaderShallStop.load(), is_reader, (void*)tid_reader);
if( mgmtReaderRunning ) {
mgmtReaderShallStop = true;
if( !is_reader && 0 != tid_reader ) {
diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp
index a1a27f96..b08b21ac 100644
--- a/src/direct_bt/GATTHandler.cpp
+++ b/src/direct_bt/GATTHandler.cpp
@@ -196,6 +196,10 @@ void GATTHandler::l2capReaderThreadImpl() {
DBG_PRINT("GATTHandler::reader Started");
cv_l2capReaderInit.notify_all();
}
+ thread_local jau::call_on_release thread_cleanup([&]() {
+ DBG_PRINT("GATTHandler::l2capReaderThreadCleanup: l2capReaderRunning %d -> 0", l2capReaderRunning.load());
+ l2capReaderRunning = false;
+ });
while( !l2capReaderShallStop ) {
jau::snsize_t len;
@@ -370,7 +374,7 @@ bool GATTHandler::disconnect(const bool disconnectDevice, const bool ioErrorCaus
l2capReaderThreadId = 0;
const bool is_l2capReader = tid_l2capReader == tid_self;
DBG_PRINT("GATTHandler.disconnect: l2capReader[running %d, shallStop %d, isReader %d, tid %p)",
- l2capReaderRunning, l2capReaderShallStop.load(), is_l2capReader, (void*)tid_l2capReader);
+ l2capReaderRunning.load(), l2capReaderShallStop.load(), is_l2capReader, (void*)tid_l2capReader);
if( l2capReaderRunning ) {
l2capReaderShallStop = true;
if( !is_l2capReader && 0 != tid_l2capReader ) {
diff --git a/src/direct_bt/HCIHandler.cpp b/src/direct_bt/HCIHandler.cpp
index 92cdfd93..f0c178b4 100644
--- a/src/direct_bt/HCIHandler.cpp
+++ b/src/direct_bt/HCIHandler.cpp
@@ -274,6 +274,10 @@ void HCIHandler::hciReaderThreadImpl() noexcept {
DBG_PRINT("HCIHandler::reader: Started - %s", toString().c_str());
cv_hciReaderInit.notify_all();
}
+ thread_local jau::call_on_release thread_cleanup([&]() {
+ DBG_PRINT("HCIHandler::hciReaderThreadCleanup: hciReaderRunning %d -> 0", hciReaderRunning.load());
+ hciReaderRunning = false;
+ });
while( !hciReaderShallStop ) {
jau::snsize_t len;
@@ -571,7 +575,7 @@ void HCIHandler::close() noexcept {
hciReaderThreadId = 0;
const bool is_reader = tid_reader == tid_self;
DBG_PRINT("HCIHandler::close: hciReader[running %d, shallStop %d, isReader %d, tid %p) - %s",
- hciReaderRunning, hciReaderShallStop.load(), is_reader, (void*)tid_reader, toString().c_str());
+ hciReaderRunning.load(), hciReaderShallStop.load(), is_reader, (void*)tid_reader, toString().c_str());
if( hciReaderRunning ) {
hciReaderShallStop = true;
if( !is_reader && 0 != tid_reader ) {