Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e33527bb authored by Myles Watson's avatar Myles Watson
Browse files

btif_sock: Use atomic operations for thread_handle

Test: Switch users with Bluetooth enabled
     (Disables/Enables Bluetooth under high load)
Change-Id: I9039f97c090f693e798b8082fbb027e2028c8b40
parent b0540d2b
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@

#define LOG_TAG "bt_btif_sock"

#include <atomic>

#include <base/logging.h>

#include <hardware/bluetooth.h>
@@ -44,7 +46,7 @@ static bt_status_t btsock_connect(const bt_bdaddr_t* bd_addr,

static void btsock_signaled(int fd, int type, int flags, uint32_t user_id);

static int thread_handle = -1;
static std::atomic_int thread_handle{-1};
static thread_t* thread;

btsock_interface_t* btif_sock_get_interface(void) {
@@ -107,16 +109,16 @@ error:;
}

void btif_sock_cleanup(void) {
  if (thread_handle == -1) return;
  int saved_handle = thread_handle;
  if (std::atomic_exchange(&thread_handle, -1) == -1) return;

  thread_stop(thread);
  thread_join(thread);
  btsock_thread_exit(thread_handle);
  btsock_thread_exit(saved_handle);
  btsock_rfc_cleanup();
  btsock_sco_cleanup();
  btsock_l2cap_cleanup();
  thread_free(thread);
  thread_handle = -1;
  thread = NULL;
}