aboutsummaryrefslogtreecommitdiffstats
path: root/src/direct_bt/DBTManager.cpp
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-10-27 13:21:51 +0100
committerSven Gothel <[email protected]>2020-10-27 13:21:51 +0100
commitd134152b4f8c279f8a4f8714f7ca8671fea04492 (patch)
treeeaf4ed21935108fcfc269f7da5a4214c5fa5205e /src/direct_bt/DBTManager.cpp
parent0daeb8396d96abba46f66a01c9eb050a740b3316 (diff)
DBTManager, HCIHandler, GATTHandler: Add reader thread cleanup, setting <bla>Running flag (now atomic) to false @ thread exit
We have experienced a rare deadlock in GATTHandler::disconnect() waiting for l2capReaderRunning=false, as the l2capReaderThread ended abnormal. The abormal thread exit prohibited the well formulated exit-handshake via mtx_l2capReaderLifecycle and l2capReaderRunning kept true. This 'thread_local jau::call_on_release thread_cleanup' instance takes care of these situation, having our custom cleanup lambda called at its destruction, i.e. thread-local-dtor (thread exit).
Diffstat (limited to 'src/direct_bt/DBTManager.cpp')
-rw-r--r--src/direct_bt/DBTManager.cpp8
1 files changed, 7 insertions, 1 deletions
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 ) {