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

Commit 81800ad3 authored by Myles Watson's avatar Myles Watson
Browse files

osi: Use atomic_exchange to protect is_joined

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


#include "osi/include/thread.h"
#include "osi/include/thread.h"


#include <atomic>

#include <base/logging.h>
#include <base/logging.h>
#include <errno.h>
#include <errno.h>
#include <malloc.h>
#include <malloc.h>
@@ -38,7 +40,7 @@
#include "osi/include/semaphore.h"
#include "osi/include/semaphore.h"


struct thread_t {
struct thread_t {
  bool is_joined;
  std::atomic_bool is_joined{false};
  pthread_t pthread;
  pthread_t pthread;
  pid_t tid;
  pid_t tid;
  char name[THREAD_NAME_MAX + 1];
  char name[THREAD_NAME_MAX + 1];
@@ -117,12 +119,9 @@ void thread_free(thread_t* thread) {
void thread_join(thread_t* thread) {
void thread_join(thread_t* thread) {
  CHECK(thread != NULL);
  CHECK(thread != NULL);


  // TODO(zachoverflow): use a compare and swap when ready
  if (!std::atomic_exchange(&thread->is_joined, true))
  if (!thread->is_joined) {
    thread->is_joined = true;
    pthread_join(thread->pthread, NULL);
    pthread_join(thread->pthread, NULL);
}
}
}


bool thread_post(thread_t* thread, thread_fn func, void* context) {
bool thread_post(thread_t* thread, thread_fn func, void* context) {
  CHECK(thread != NULL);
  CHECK(thread != NULL);