From de3135d1a6adbe5d5f4c1eec6f610009f72f317d Mon Sep 17 00:00:00 2001 From: Myles Watson Date: Tue, 28 Feb 2017 11:01:03 -0800 Subject: [PATCH] osi: Use atomic_exchange to protect is_joined Test: Switch users with Bluetooth enabled (Disables/Enables Bluetooth under high load) Change-Id: I34c5579b75620970f756fff28ccc07cf76599a94 --- osi/src/thread.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/osi/src/thread.cc b/osi/src/thread.cc index 35725f057..586ba7e4f 100644 --- a/osi/src/thread.cc +++ b/osi/src/thread.cc @@ -20,6 +20,8 @@ #include "osi/include/thread.h" +#include + #include #include #include @@ -38,7 +40,7 @@ #include "osi/include/semaphore.h" struct thread_t { - bool is_joined; + std::atomic_bool is_joined{false}; pthread_t pthread; pid_t tid; char name[THREAD_NAME_MAX + 1]; @@ -117,11 +119,8 @@ void thread_free(thread_t* thread) { void thread_join(thread_t* thread) { CHECK(thread != NULL); - // TODO(zachoverflow): use a compare and swap when ready - if (!thread->is_joined) { - thread->is_joined = true; + if (!std::atomic_exchange(&thread->is_joined, true)) pthread_join(thread->pthread, NULL); - } } bool thread_post(thread_t* thread, thread_fn func, void* context) { -- 2.11.0