Replace pthread_mutex with std::mutex
In an effort to simplify and reduce errors, replace pthread_mutexes
with std equivalents.
Test: run unit tests & manual sanity checks
Change-Id: I5c33e0b4f4c85133ae4f7415cd19372fc4c7ca1a
diff --git a/audio_a2dp_hw/audio_a2dp_hw.cc b/audio_a2dp_hw/audio_a2dp_hw.cc
index 30b2876..a1e4a20 100644
--- a/audio_a2dp_hw/audio_a2dp_hw.cc
+++ b/audio_a2dp_hw/audio_a2dp_hw.cc
@@ -29,7 +29,6 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
-#include <pthread.h>
#include <stdint.h>
#include <sys/errno.h>
#include <sys/socket.h>
@@ -38,6 +37,8 @@
#include <sys/un.h>
#include <unistd.h>
+#include <mutex>
+
#include <hardware/audio.h>
#include <hardware/hardware.h>
#include <system/audio.h>
@@ -107,7 +108,7 @@
/* move ctrl_fd outside output stream and keep open until HAL unloaded ? */
struct a2dp_stream_common {
- pthread_mutex_t lock;
+ std::recursive_mutex* mutex;
int ctrl_fd;
int audio_fd;
size_t buffer_sz;
@@ -551,13 +552,9 @@
****************************************************************************/
static void a2dp_stream_common_init(struct a2dp_stream_common* common) {
- pthread_mutexattr_t lock_attr;
-
FNLOG();
- pthread_mutexattr_init(&lock_attr);
- pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&common->lock, &lock_attr);
+ common->mutex = new std::recursive_mutex;
common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
common->audio_fd = AUDIO_SKT_DISCONNECTED;
@@ -567,6 +564,13 @@
common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ;
}
+static void a2dp_stream_common_destroy(struct a2dp_stream_common* common) {
+ FNLOG();
+
+ delete common->mutex;
+ common->mutex = NULL;
+}
+
static int start_audio_datapath(struct a2dp_stream_common* common) {
INFO("state %d", common->state);
@@ -656,7 +660,7 @@
DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd);
- pthread_mutex_lock(&out->common.lock);
+ std::unique_lock<std::recursive_mutex> lock(*out->common.mutex);
if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED ||
out->common.state == AUDIO_A2DP_STATE_STOPPING) {
DEBUG("stream suspended or closing");
@@ -674,9 +678,9 @@
goto finish;
}
- pthread_mutex_unlock(&out->common.lock);
+ lock.unlock();
sent = skt_write(out->common.audio_fd, buffer, bytes);
- pthread_mutex_lock(&out->common.lock);
+ lock.lock();
if (sent == -1) {
skt_disconnect(out->common.audio_fd);
@@ -694,7 +698,7 @@
const size_t frames = bytes / audio_stream_out_frame_size(stream);
out->frames_rendered += frames;
out->frames_presented += frames;
- pthread_mutex_unlock(&out->common.lock);
+ lock.unlock();
// If send didn't work out, sleep to emulate write delay.
if (sent == -1) {
@@ -766,12 +770,11 @@
FNLOG();
- pthread_mutex_lock(&out->common.lock);
+ std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
// Do nothing in SUSPENDED state.
if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED)
retVal = suspend_audio_datapath(&out->common, true);
out->frames_rendered = 0; // rendered is reset, presented is not
- pthread_mutex_unlock(&out->common.lock);
return retVal;
}
@@ -794,7 +797,7 @@
if (params.empty()) return status;
- pthread_mutex_lock(&out->common.lock);
+ std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
/* dump params */
hash_map_utils_dump_string_keys_string_values(params);
@@ -816,8 +819,6 @@
/* Irrespective of the state, return 0 */
}
- pthread_mutex_unlock(&out->common.lock);
-
return status;
}
@@ -833,7 +834,7 @@
if (params.empty()) return strdup("");
- pthread_mutex_lock(&out->common.lock);
+ std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
if (a2dp_read_output_audio_config(&out->common) < 0) {
ERROR("a2dp_read_output_audio_config failed");
@@ -904,8 +905,6 @@
}
done:
- pthread_mutex_unlock(&out->common.lock);
-
std::string result;
for (const auto& ptr : return_params) {
result += ptr.first + "=" + ptr.second + ";";
@@ -949,7 +948,7 @@
if (stream == NULL || frames == NULL || timestamp == NULL) return -EINVAL;
int ret = -EWOULDBLOCK;
- pthread_mutex_lock(&out->common.lock);
+ std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
uint64_t latency_frames =
(uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000;
if (out->frames_presented >= latency_frames) {
@@ -958,7 +957,6 @@
timestamp); // could also be associated with out_write().
ret = 0;
}
- pthread_mutex_unlock(&out->common.lock);
return ret;
}
@@ -969,7 +967,7 @@
FNLOG();
if (stream == NULL || dsp_frames == NULL) return -EINVAL;
- pthread_mutex_lock(&out->common.lock);
+ std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
uint64_t latency_frames =
(uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000;
if (out->frames_rendered >= latency_frames) {
@@ -977,7 +975,6 @@
} else {
*dsp_frames = 0;
}
- pthread_mutex_unlock(&out->common.lock);
return 0;
}
@@ -1081,7 +1078,7 @@
DEBUG("read %zu bytes, state: %d", bytes, in->common.state);
- pthread_mutex_lock(&in->common.lock);
+ std::unique_lock<std::recursive_mutex> lock(*in->common.mutex);
if (in->common.state == AUDIO_A2DP_STATE_SUSPENDED ||
in->common.state == AUDIO_A2DP_STATE_STOPPING) {
DEBUG("stream suspended");
@@ -1099,9 +1096,9 @@
goto error;
}
- pthread_mutex_unlock(&in->common.lock);
+ lock.unlock();
read = skt_read(in->common.audio_fd, buffer, bytes);
- pthread_mutex_lock(&in->common.lock);
+ lock.lock();
if (read == -1) {
skt_disconnect(in->common.audio_fd);
in->common.audio_fd = AUDIO_SKT_DISCONNECTED;
@@ -1117,13 +1114,12 @@
memset(buffer, 0, bytes);
read = bytes;
}
- pthread_mutex_unlock(&in->common.lock);
+ lock.unlock();
DEBUG("read %d bytes out of %zu bytes", read, bytes);
return read;
error:
- pthread_mutex_unlock(&in->common.lock);
memset(buffer, 0, bytes);
us_delay = calc_audiotime_usec(in->common.cfg, bytes);
DEBUG("emulate a2dp read delay (%d us)", us_delay);
@@ -1222,6 +1218,7 @@
return 0;
err_open:
+ a2dp_stream_common_destroy(&out->common);
free(out);
*stream_out = NULL;
a2dp_dev->output = NULL;
@@ -1236,7 +1233,7 @@
INFO("closing output (state %d)", out->common.state);
- pthread_mutex_lock(&out->common.lock);
+ std::unique_lock<std::recursive_mutex> lock(*out->common.mutex);
if ((out->common.state == AUDIO_A2DP_STATE_STARTED) ||
(out->common.state == AUDIO_A2DP_STATE_STOPPING)) {
stop_audio_datapath(&out->common);
@@ -1244,9 +1241,10 @@
skt_disconnect(out->common.ctrl_fd);
out->common.ctrl_fd = AUDIO_SKT_DISCONNECTED;
+ lock.unlock();
+ a2dp_stream_common_destroy(&out->common);
free(stream);
a2dp_dev->output = NULL;
- pthread_mutex_unlock(&out->common.lock);
DEBUG("done");
}
@@ -1384,6 +1382,7 @@
return 0;
err_open:
+ a2dp_stream_common_destroy(&in->common);
free(in);
*stream_in = NULL;
a2dp_dev->input = NULL;
@@ -1405,6 +1404,7 @@
skt_disconnect(in->common.ctrl_fd);
in->common.ctrl_fd = AUDIO_SKT_DISCONNECTED;
+ a2dp_stream_common_destroy(&in->common);
free(stream);
a2dp_dev->input = NULL;
diff --git a/btcore/src/module.cc b/btcore/src/module.cc
index d3e5c93..ea66fab 100644
--- a/btcore/src/module.cc
+++ b/btcore/src/module.cc
@@ -20,8 +20,9 @@
#include <assert.h>
#include <dlfcn.h>
-#include <pthread.h>
#include <string.h>
+
+#include <mutex>
#include <unordered_map>
#include "btcore/include/module.h"
@@ -37,20 +38,17 @@
static std::unordered_map<const module_t*, module_state_t> metadata;
-// Include this lock for now for correctness, while the startup sequence is
-// being refactored
-static pthread_mutex_t metadata_lock;
+// TODO(jamuraa): remove this lock after the startup sequence is clean
+static std::mutex metadata_mutex;
static bool call_lifecycle_function(module_lifecycle_fn function);
static module_state_t get_module_state(const module_t* module);
static void set_module_state(const module_t* module, module_state_t state);
-void module_management_start(void) { pthread_mutex_init(&metadata_lock, NULL); }
+void module_management_start(void) {}
void module_management_stop(void) {
metadata.clear();
-
- pthread_mutex_destroy(&metadata_lock);
}
const module_t* get_module(const char* name) {
@@ -150,17 +148,15 @@
}
static module_state_t get_module_state(const module_t* module) {
- pthread_mutex_lock(&metadata_lock);
+ std::lock_guard<std::mutex> lock(metadata_mutex);
auto map_ptr = metadata.find(module);
- pthread_mutex_unlock(&metadata_lock);
return (map_ptr != metadata.end()) ? map_ptr->second : MODULE_STATE_NONE;
}
static void set_module_state(const module_t* module, module_state_t state) {
- pthread_mutex_lock(&metadata_lock);
+ std::lock_guard<std::mutex> lock(metadata_mutex);
metadata[module] = state;
- pthread_mutex_unlock(&metadata_lock);
}
// TODO(zachoverflow): remove when everything modulized
diff --git a/btcore/src/osi_module.cc b/btcore/src/osi_module.cc
index 3bcf039..1938d3f 100644
--- a/btcore/src/osi_module.cc
+++ b/btcore/src/osi_module.cc
@@ -23,19 +23,16 @@
#include "osi/include/alarm.h"
#include "osi/include/future.h"
#include "osi/include/log.h"
-#include "osi/include/mutex.h"
#include "osi/include/osi.h"
#include "osi/include/wakelock.h"
future_t* osi_init(void) {
- mutex_init();
return future_new_immediate(FUTURE_SUCCESS);
}
future_t* osi_clean_up(void) {
alarm_cleanup();
wakelock_cleanup();
- mutex_cleanup();
return future_new_immediate(FUTURE_SUCCESS);
}
diff --git a/hci/src/btsnoop_net.cc b/hci/src/btsnoop_net.cc
index bce7885..deb703c 100644
--- a/hci/src/btsnoop_net.cc
+++ b/hci/src/btsnoop_net.cc
@@ -30,6 +30,8 @@
#include <sys/types.h>
#include <unistd.h>
+#include <mutex>
+
#include "osi/include/log.h"
#include "osi/include/osi.h"
@@ -42,7 +44,7 @@
static pthread_t listen_thread_;
static bool listen_thread_valid_ = false;
-static pthread_mutex_t client_socket_lock_ = PTHREAD_MUTEX_INITIALIZER;
+static std::mutex client_socket_mutex_;
static int listen_socket_ = -1;
static int client_socket_ = -1;
@@ -79,7 +81,7 @@
return; // Disable using network sockets for security reasons
#endif
- pthread_mutex_lock(&client_socket_lock_);
+ std::lock_guard<std::mutex> lock(client_socket_mutex_);
if (client_socket_ != -1) {
ssize_t ret;
OSI_NO_INTR(ret = send(client_socket_, data, length, 0));
@@ -88,7 +90,6 @@
safe_close_(&client_socket_);
}
}
- pthread_mutex_unlock(&client_socket_lock_);
}
static void* listen_fn_(UNUSED_ATTR void* context) {
@@ -139,12 +140,11 @@
/* When a new client connects, we have to send the btsnoop file header. This
* allows a decoder to treat the session as a new, valid btsnoop file. */
- pthread_mutex_lock(&client_socket_lock_);
+ std::lock_guard<std::mutex> lock(client_socket_mutex_);
safe_close_(&client_socket_);
client_socket_ = client_socket;
OSI_NO_INTR(send(client_socket_, "btsnoop\0\0\0\0\1\0\0\x3\xea", 16, 0));
- pthread_mutex_unlock(&client_socket_lock_);
}
cleanup:
diff --git a/hci/src/hci_layer.cc b/hci/src/hci_layer.cc
index 5a87bab..b8ac022 100644
--- a/hci/src/hci_layer.cc
+++ b/hci/src/hci_layer.cc
@@ -21,12 +21,13 @@
#include "hci_layer.h"
#include <assert.h>
-#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
+#include <mutex>
+
#include "btcore/include/module.h"
#include "btsnoop.h"
#include "buffer_allocator.h"
@@ -152,7 +153,7 @@
// Inbound-related
static alarm_t* command_response_timer;
static list_t* commands_pending_response;
-static pthread_mutex_t commands_pending_response_lock;
+static std::mutex commands_pending_response_mutex;
static packet_receive_data_t incoming_packets[INBOUND_PACKET_TYPE_COUNT];
// The hand-off point for data going to a higher layer, set by the higher layer
@@ -173,8 +174,6 @@
command_credits = 1;
firmware_is_configured = false;
- pthread_mutex_init(&commands_pending_response_lock, NULL);
-
// For now, always use the default timeout on non-Android builds.
period_ms_t startup_timeout_ms = DEFAULT_STARTUP_TIMEOUT_MS;
@@ -303,8 +302,6 @@
list_free(commands_pending_response);
commands_pending_response = NULL;
- pthread_mutex_destroy(&commands_pending_response_lock);
-
packet_fragmenter->cleanup();
// Free the timers
@@ -410,27 +407,23 @@
alarm_cancel(startup_timer);
- pthread_mutex_lock(&commands_pending_response_lock);
+ std::lock_guard<std::mutex> lock(commands_pending_response_mutex);
if (startup_future == NULL) {
// The firmware configuration took too long - ignore the callback
- pthread_mutex_unlock(&commands_pending_response_lock);
return;
}
firmware_is_configured = success;
future_ready(startup_future, success ? FUTURE_SUCCESS : FUTURE_FAIL);
startup_future = NULL;
-
- pthread_mutex_unlock(&commands_pending_response_lock);
}
static void startup_timer_expired(UNUSED_ATTR void* context) {
LOG_ERROR(LOG_TAG, "%s", __func__);
- pthread_mutex_lock(&commands_pending_response_lock);
+ std::lock_guard<std::mutex> lock(commands_pending_response_mutex);
future_ready(startup_future, FUTURE_FAIL);
startup_future = NULL;
- pthread_mutex_unlock(&commands_pending_response_lock);
}
// Postload functions
@@ -475,9 +468,10 @@
command_credits--;
// Move it to the list of commands awaiting response
- pthread_mutex_lock(&commands_pending_response_lock);
- list_append(commands_pending_response, wait_entry);
- pthread_mutex_unlock(&commands_pending_response_lock);
+ {
+ std::lock_guard<std::mutex> lock(commands_pending_response_mutex);
+ list_append(commands_pending_response, wait_entry);
+ }
// Send it off
low_power_manager->wake_assert();
@@ -524,14 +518,14 @@
}
static void command_timed_out(UNUSED_ATTR void* context) {
- pthread_mutex_lock(&commands_pending_response_lock);
+ std::unique_lock<std::mutex> lock(commands_pending_response_mutex);
if (list_is_empty(commands_pending_response)) {
LOG_ERROR(LOG_TAG, "%s with no commands pending response", __func__);
} else {
waiting_command_t* wait_entry =
static_cast<waiting_command_t*>(list_front(commands_pending_response));
- pthread_mutex_unlock(&commands_pending_response_lock);
+ lock.unlock();
// We shouldn't try to recover the stack from this command timeout.
// If it's caused by a software bug, fix it. If it's a hardware bug, fix it.
@@ -779,7 +773,7 @@
}
static waiting_command_t* get_waiting_command(command_opcode_t opcode) {
- pthread_mutex_lock(&commands_pending_response_lock);
+ std::lock_guard<std::mutex> lock(commands_pending_response_mutex);
for (const list_node_t* node = list_begin(commands_pending_response);
node != list_end(commands_pending_response); node = list_next(node)) {
@@ -790,11 +784,9 @@
list_remove(commands_pending_response, wait_entry);
- pthread_mutex_unlock(&commands_pending_response_lock);
return wait_entry;
}
- pthread_mutex_unlock(&commands_pending_response_lock);
return NULL;
}
diff --git a/osi/include/mutex.h b/osi/include/mutex.h
index 3d1b306..81a98d2 100644
--- a/osi/include/mutex.h
+++ b/osi/include/mutex.h
@@ -24,12 +24,6 @@
extern "C" {
#endif
-// Mutex-related state init
-void mutex_init(void);
-
-// Mutex-related state cleanup
-void mutex_cleanup(void);
-
// Lock the global mutex
void mutex_global_lock(void);
diff --git a/osi/src/alarm.cc b/osi/src/alarm.cc
index dfe7a85..4eacc22 100644
--- a/osi/src/alarm.cc
+++ b/osi/src/alarm.cc
@@ -27,13 +27,14 @@
#include <fcntl.h>
#include <inttypes.h>
#include <malloc.h>
-#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <hardware/bluetooth.h>
+#include <mutex>
+
#include "osi/include/allocator.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/list.h"
@@ -70,12 +71,12 @@
} alarm_stats_t;
struct alarm_t {
- // The lock is held while the callback for this alarm is being executed.
+ // The mutex is held while the callback for this alarm is being executed.
// It allows us to release the coarse-grained monitor lock while a
// potentially long-running callback is executing. |alarm_cancel| uses this
- // lock to provide a guarantee to its caller that the callback will not be
+ // mutex to provide a guarantee to its caller that the callback will not be
// in progress when it returns.
- pthread_mutex_t callback_lock;
+ std::recursive_mutex* callback_mutex;
period_ms_t creation_time;
period_ms_t period;
period_ms_t deadline;
@@ -104,7 +105,7 @@
// This mutex ensures that the |alarm_set|, |alarm_cancel|, and alarm callback
// functions execute serially and not concurrently. As a result, this mutex
// also protects the |alarms| list.
-static pthread_mutex_t monitor;
+static std::mutex alarms_mutex;
static list_t* alarms;
static timer_t timer;
static timer_t wakeup_timer;
@@ -156,45 +157,21 @@
return NULL;
}
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
-
alarm_t* ret = static_cast<alarm_t*>(osi_calloc(sizeof(alarm_t)));
- // Make this a recursive mutex to make it safe to call |alarm_cancel| from
- // within the callback function of the alarm.
- int error = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- if (error) {
- LOG_ERROR(LOG_TAG, "%s unable to create a recursive mutex: %s", __func__,
- strerror(error));
- goto error;
- }
-
- error = pthread_mutex_init(&ret->callback_lock, &attr);
- if (error) {
- LOG_ERROR(LOG_TAG, "%s unable to initialize mutex: %s", __func__,
- strerror(error));
- goto error;
- }
-
+ ret->callback_mutex = new std::recursive_mutex;
ret->is_periodic = is_periodic;
ret->stats.name = osi_strdup(name);
// NOTE: The stats were reset by osi_calloc() above
- pthread_mutexattr_destroy(&attr);
return ret;
-
-error:
- pthread_mutexattr_destroy(&attr);
- osi_free(ret);
- return NULL;
}
void alarm_free(alarm_t* alarm) {
if (!alarm) return;
alarm_cancel(alarm);
- pthread_mutex_destroy(&alarm->callback_lock);
+ delete alarm->callback_mutex;
osi_free((void*)alarm->stats.name);
osi_free(alarm);
}
@@ -204,9 +181,8 @@
period_ms_t remaining_ms = 0;
period_ms_t just_now = now();
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(alarms_mutex);
if (alarm->deadline > just_now) remaining_ms = alarm->deadline - just_now;
- pthread_mutex_unlock(&monitor);
return remaining_ms;
}
@@ -230,7 +206,7 @@
assert(alarm != NULL);
assert(cb != NULL);
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(alarms_mutex);
alarm->creation_time = now();
alarm->period = period;
@@ -240,25 +216,23 @@
schedule_next_instance(alarm);
alarm->stats.scheduled_count++;
-
- pthread_mutex_unlock(&monitor);
}
void alarm_cancel(alarm_t* alarm) {
assert(alarms != NULL);
if (!alarm) return;
- pthread_mutex_lock(&monitor);
- alarm_cancel_internal(alarm);
- pthread_mutex_unlock(&monitor);
+ {
+ std::lock_guard<std::mutex> lock(alarms_mutex);
+ alarm_cancel_internal(alarm);
+ }
// If the callback for |alarm| is in progress, wait here until it completes.
- pthread_mutex_lock(&alarm->callback_lock);
- pthread_mutex_unlock(&alarm->callback_lock);
+ std::lock_guard<std::recursive_mutex> lock(*alarm->callback_mutex);
}
// Internal implementation of canceling an alarm.
-// The caller must hold the |monitor| lock.
+// The caller must hold the |alarms_mutex|
static void alarm_cancel_internal(alarm_t* alarm) {
bool needs_reschedule =
(!list_is_empty(alarms) && list_front(alarms) == alarm);
@@ -289,7 +263,7 @@
thread_free(dispatcher_thread);
dispatcher_thread = NULL;
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(alarms_mutex);
fixed_queue_free(default_callback_queue, NULL);
default_callback_queue = NULL;
@@ -303,9 +277,6 @@
list_free(alarms);
alarms = NULL;
-
- pthread_mutex_unlock(&monitor);
- pthread_mutex_destroy(&monitor);
}
static bool lazy_initialize(void) {
@@ -316,7 +287,7 @@
bool timer_initialized = false;
bool wakeup_timer_initialized = false;
- pthread_mutex_init(&monitor, NULL);
+ std::lock_guard<std::mutex> lock(alarms_mutex);
alarms = list_new(NULL);
if (!alarms) {
@@ -385,8 +356,6 @@
list_free(alarms);
alarms = NULL;
- pthread_mutex_destroy(&monitor);
-
return false;
}
@@ -404,7 +373,7 @@
}
// Remove alarm from internal alarm list and the processing queue
-// The caller must hold the |monitor| lock.
+// The caller must hold the |alarms_mutex|
static void remove_pending_alarm(alarm_t* alarm) {
list_remove(alarms, alarm);
while (fixed_queue_try_remove_from_queue(alarm->queue, alarm) != NULL) {
@@ -413,7 +382,7 @@
}
}
-// Must be called with monitor held
+// Must be called with |alarms_mutex| held
static void schedule_next_instance(alarm_t* alarm) {
// If the alarm is currently set and it's at the start of the list,
// we'll need to re-schedule since we've adjusted the earliest deadline.
@@ -452,7 +421,7 @@
}
}
-// NOTE: must be called with monitor lock.
+// NOTE: must be called with |alarms_mutex| held
static void reschedule_root_alarm(void) {
assert(alarms != NULL);
@@ -558,7 +527,7 @@
fixed_queue_unregister_dequeue(queue);
// Cancel all alarms that are using this queue
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(alarms_mutex);
for (list_node_t* node = list_begin(alarms); node != list_end(alarms);) {
alarm_t* alarm = (alarm_t*)list_node(node);
node = list_next(node);
@@ -567,16 +536,14 @@
// an assert.
if (alarm->queue == queue) alarm_cancel_internal(alarm);
}
- pthread_mutex_unlock(&monitor);
}
static void alarm_queue_ready(fixed_queue_t* queue, UNUSED_ATTR void* context) {
assert(queue != NULL);
- pthread_mutex_lock(&monitor);
+ std::unique_lock<std::mutex> lock(alarms_mutex);
alarm_t* alarm = (alarm_t*)fixed_queue_try_dequeue(queue);
if (alarm == NULL) {
- pthread_mutex_unlock(&monitor);
return; // The alarm was probably canceled
}
@@ -598,8 +565,8 @@
alarm->data = NULL;
}
- pthread_mutex_lock(&alarm->callback_lock);
- pthread_mutex_unlock(&monitor);
+ std::lock_guard<std::recursive_mutex> cb_lock(*alarm->callback_mutex);
+ lock.unlock();
period_ms_t t0 = now();
callback(data);
@@ -609,8 +576,6 @@
assert(t1 >= t0);
period_ms_t delta = t1 - t0;
update_scheduling_stats(&alarm->stats, t0, deadline, delta);
-
- pthread_mutex_unlock(&alarm->callback_lock);
}
// Callback function for wake alarms and our posix timer
@@ -627,17 +592,15 @@
semaphore_wait(alarm_expired);
if (!dispatcher_thread_active) break;
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(alarms_mutex);
alarm_t* alarm;
// Take into account that the alarm may get cancelled before we get to it.
// We're done here if there are no alarms or the alarm at the front is in
- // the future. Release the monitor lock and exit right away since there's
- // nothing left to do.
+ // the future. Exit right away since there's nothing left to do.
if (list_is_empty(alarms) ||
(alarm = static_cast<alarm_t*>(list_front(alarms)))->deadline > now()) {
reschedule_root_alarm();
- pthread_mutex_unlock(&monitor);
continue;
}
@@ -652,8 +615,6 @@
// Enqueue the alarm for processing
fixed_queue_enqueue(alarm->queue, alarm);
-
- pthread_mutex_unlock(&monitor);
}
LOG_DEBUG(LOG_TAG, "%s Callback thread exited", __func__);
@@ -716,10 +677,9 @@
void alarm_debug_dump(int fd) {
dprintf(fd, "\nBluetooth Alarms Statistics:\n");
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(alarms_mutex);
if (alarms == NULL) {
- pthread_mutex_unlock(&monitor);
dprintf(fd, " None\n");
return;
}
@@ -763,5 +723,4 @@
dprintf(fd, "\n");
}
- pthread_mutex_unlock(&monitor);
}
diff --git a/osi/src/fixed_queue.cc b/osi/src/fixed_queue.cc
index a4cd08d..bc5da34 100644
--- a/osi/src/fixed_queue.cc
+++ b/osi/src/fixed_queue.cc
@@ -17,9 +17,10 @@
******************************************************************************/
#include <assert.h>
-#include <pthread.h>
#include <string.h>
+#include <mutex>
+
#include "osi/include/allocator.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/list.h"
@@ -31,7 +32,7 @@
list_t* list;
semaphore_t* enqueue_sem;
semaphore_t* dequeue_sem;
- pthread_mutex_t lock;
+ std::mutex* mutex;
size_t capacity;
reactor_object_t* dequeue_object;
@@ -45,7 +46,7 @@
fixed_queue_t* ret =
static_cast<fixed_queue_t*>(osi_calloc(sizeof(fixed_queue_t)));
- pthread_mutex_init(&ret->lock, NULL);
+ ret->mutex = new std::mutex;
ret->capacity = capacity;
ret->list = list_new(NULL);
@@ -77,7 +78,7 @@
list_free(queue->list);
semaphore_free(queue->enqueue_sem);
semaphore_free(queue->dequeue_sem);
- pthread_mutex_destroy(&queue->lock);
+ delete queue->mutex;
osi_free(queue);
}
@@ -95,21 +96,15 @@
bool fixed_queue_is_empty(fixed_queue_t* queue) {
if (queue == NULL) return true;
- pthread_mutex_lock(&queue->lock);
- bool is_empty = list_is_empty(queue->list);
- pthread_mutex_unlock(&queue->lock);
-
- return is_empty;
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ return list_is_empty(queue->list);
}
size_t fixed_queue_length(fixed_queue_t* queue) {
if (queue == NULL) return 0;
- pthread_mutex_lock(&queue->lock);
- size_t length = list_length(queue->list);
- pthread_mutex_unlock(&queue->lock);
-
- return length;
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ return list_length(queue->list);
}
size_t fixed_queue_capacity(fixed_queue_t* queue) {
@@ -124,9 +119,10 @@
semaphore_wait(queue->enqueue_sem);
- pthread_mutex_lock(&queue->lock);
- list_append(queue->list, data);
- pthread_mutex_unlock(&queue->lock);
+ {
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ list_append(queue->list, data);
+ }
semaphore_post(queue->dequeue_sem);
}
@@ -136,10 +132,12 @@
semaphore_wait(queue->dequeue_sem);
- pthread_mutex_lock(&queue->lock);
- void* ret = list_front(queue->list);
- list_remove(queue->list, ret);
- pthread_mutex_unlock(&queue->lock);
+ void* ret = NULL;
+ {
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ ret = list_front(queue->list);
+ list_remove(queue->list, ret);
+ }
semaphore_post(queue->enqueue_sem);
@@ -152,9 +150,10 @@
if (!semaphore_try_wait(queue->enqueue_sem)) return false;
- pthread_mutex_lock(&queue->lock);
- list_append(queue->list, data);
- pthread_mutex_unlock(&queue->lock);
+ {
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ list_append(queue->list, data);
+ }
semaphore_post(queue->dequeue_sem);
return true;
@@ -165,10 +164,12 @@
if (!semaphore_try_wait(queue->dequeue_sem)) return NULL;
- pthread_mutex_lock(&queue->lock);
- void* ret = list_front(queue->list);
- list_remove(queue->list, ret);
- pthread_mutex_unlock(&queue->lock);
+ void* ret = NULL;
+ {
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ ret = list_front(queue->list);
+ list_remove(queue->list, ret);
+ }
semaphore_post(queue->enqueue_sem);
@@ -178,34 +179,29 @@
void* fixed_queue_try_peek_first(fixed_queue_t* queue) {
if (queue == NULL) return NULL;
- pthread_mutex_lock(&queue->lock);
- void* ret = list_is_empty(queue->list) ? NULL : list_front(queue->list);
- pthread_mutex_unlock(&queue->lock);
-
- return ret;
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ return list_is_empty(queue->list) ? NULL : list_front(queue->list);
}
void* fixed_queue_try_peek_last(fixed_queue_t* queue) {
if (queue == NULL) return NULL;
- pthread_mutex_lock(&queue->lock);
- void* ret = list_is_empty(queue->list) ? NULL : list_back(queue->list);
- pthread_mutex_unlock(&queue->lock);
-
- return ret;
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ return list_is_empty(queue->list) ? NULL : list_back(queue->list);
}
void* fixed_queue_try_remove_from_queue(fixed_queue_t* queue, void* data) {
if (queue == NULL) return NULL;
bool removed = false;
- pthread_mutex_lock(&queue->lock);
- if (list_contains(queue->list, data) &&
- semaphore_try_wait(queue->dequeue_sem)) {
- removed = list_remove(queue->list, data);
- assert(removed);
+ {
+ std::lock_guard<std::mutex> lock(*queue->mutex);
+ if (list_contains(queue->list, data) &&
+ semaphore_try_wait(queue->dequeue_sem)) {
+ removed = list_remove(queue->list, data);
+ assert(removed);
+ }
}
- pthread_mutex_unlock(&queue->lock);
if (removed) {
semaphore_post(queue->enqueue_sem);
@@ -217,8 +213,9 @@
list_t* fixed_queue_get_list(fixed_queue_t* queue) {
assert(queue != NULL);
- // NOTE: This function is not thread safe, and there is no point for
- // calling pthread_mutex_lock() / pthread_mutex_unlock()
+ // NOTE: Using the list in this way is not thread-safe.
+ // Using this list in any context where threads can call other functions
+ // to the queue can break our assumptions and the queue in general.
return queue->list;
}
diff --git a/osi/src/mutex.cc b/osi/src/mutex.cc
index 4cbb45f..fbbef40 100644
--- a/osi/src/mutex.cc
+++ b/osi/src/mutex.cc
@@ -18,21 +18,12 @@
#define LOG_TAG "bt_osi_mutex"
-#include <pthread.h>
+#include <mutex>
#include "osi/include/mutex.h"
-static pthread_mutex_t global_lock;
+static std::recursive_mutex global_mutex;
-void mutex_init(void) {
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
- pthread_mutex_init(&global_lock, &attr);
-}
+void mutex_global_lock(void) { global_mutex.lock(); }
-void mutex_cleanup(void) { pthread_mutex_destroy(&global_lock); }
-
-void mutex_global_lock(void) { pthread_mutex_lock(&global_lock); }
-
-void mutex_global_unlock(void) { pthread_mutex_unlock(&global_lock); }
+void mutex_global_unlock(void) { global_mutex.unlock(); }
diff --git a/osi/src/reactor.cc b/osi/src/reactor.cc
index b77809a..9b4286b 100644
--- a/osi/src/reactor.cc
+++ b/osi/src/reactor.cc
@@ -29,6 +29,8 @@
#include <sys/eventfd.h>
#include <unistd.h>
+#include <mutex>
+
#include "osi/include/allocator.h"
#include "osi/include/list.h"
#include "osi/include/log.h"
@@ -40,7 +42,7 @@
struct reactor_t {
int epoll_fd;
int event_fd;
- pthread_mutex_t list_lock; // protects invalidation_list.
+ std::mutex* list_mutex;
list_t* invalidation_list; // reactor objects that have been unregistered.
pthread_t run_thread; // the pthread on which reactor_run is executing.
bool is_running; // indicates whether |run_thread| is valid.
@@ -51,8 +53,7 @@
int fd; // the file descriptor to monitor for events.
void* context; // a context that's passed back to the *_ready functions.
reactor_t* reactor; // the reactor instance this object is registered with.
- pthread_mutex_t
- lock; // protects the lifetime of this object and all variables.
+ std::mutex* mutex; // protects the lifetime of this object and all variables.
void (*read_ready)(void* context); // function to call when the file
// descriptor becomes readable.
@@ -85,7 +86,7 @@
goto error;
}
- pthread_mutex_init(&ret->list_lock, NULL);
+ ret->list_mutex = new std::mutex;
ret->invalidation_list = list_new(NULL);
if (!ret->invalidation_list) {
LOG_ERROR(LOG_TAG, "%s unable to allocate object invalidation list.",
@@ -149,7 +150,7 @@
object->context = context;
object->read_ready = read_ready;
object->write_ready = write_ready;
- pthread_mutex_init(&object->lock, NULL);
+ object->mutex = new std::mutex;
struct epoll_event event;
memset(&event, 0, sizeof(event));
@@ -160,7 +161,7 @@
if (epoll_ctl(reactor->epoll_fd, EPOLL_CTL_ADD, fd, &event) == -1) {
LOG_ERROR(LOG_TAG, "%s unable to register fd %d to epoll set: %s", __func__,
fd, strerror(errno));
- pthread_mutex_destroy(&object->lock);
+ delete object->mutex;
osi_free(object);
return NULL;
}
@@ -186,10 +187,9 @@
return false;
}
- pthread_mutex_lock(&object->lock);
+ std::lock_guard<std::mutex> lock(*object->mutex);
object->read_ready = read_ready;
object->write_ready = write_ready;
- pthread_mutex_unlock(&object->lock);
return true;
}
@@ -209,9 +209,10 @@
return;
}
- pthread_mutex_lock(&reactor->list_lock);
- list_append(reactor->invalidation_list, obj);
- pthread_mutex_unlock(&reactor->list_lock);
+ {
+ std::unique_lock<std::mutex> lock(*reactor->list_mutex);
+ list_append(reactor->invalidation_list, obj);
+ }
// Taking the object lock here makes sure a callback for |obj| isn't
// currently executing. The reactor thread must then either be before
@@ -221,9 +222,9 @@
// invalidation_list and find it in there. So by taking this lock, we
// are waiting until the reactor thread drops all references to |obj|.
// One the wait completes, we can unlock and destroy |obj| safely.
- pthread_mutex_lock(&obj->lock);
- pthread_mutex_unlock(&obj->lock);
- pthread_mutex_destroy(&obj->lock);
+ obj->mutex->lock();
+ obj->mutex->unlock();
+ delete obj->mutex;
osi_free(obj);
}
@@ -238,9 +239,10 @@
struct epoll_event events[MAX_EVENTS];
for (int i = 0; iterations == 0 || i < iterations; ++i) {
- pthread_mutex_lock(&reactor->list_lock);
- list_clear(reactor->invalidation_list);
- pthread_mutex_unlock(&reactor->list_lock);
+ {
+ std::lock_guard<std::mutex> lock(*reactor->list_mutex);
+ list_clear(reactor->invalidation_list);
+ }
int ret;
OSI_NO_INTR(ret = epoll_wait(reactor->epoll_fd, events, MAX_EVENTS, -1));
@@ -264,27 +266,27 @@
reactor_object_t* object = (reactor_object_t*)events[j].data.ptr;
- pthread_mutex_lock(&reactor->list_lock);
+ std::unique_lock<std::mutex> lock(*reactor->list_mutex);
if (list_contains(reactor->invalidation_list, object)) {
- pthread_mutex_unlock(&reactor->list_lock);
continue;
}
// Downgrade the list lock to an object lock.
- pthread_mutex_lock(&object->lock);
- pthread_mutex_unlock(&reactor->list_lock);
+ {
+ std::lock_guard<std::mutex> obj_lock(*object->mutex);
+ lock.unlock();
- reactor->object_removed = false;
- if (events[j].events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR) &&
- object->read_ready)
- object->read_ready(object->context);
- if (!reactor->object_removed && events[j].events & EPOLLOUT &&
- object->write_ready)
- object->write_ready(object->context);
- pthread_mutex_unlock(&object->lock);
+ reactor->object_removed = false;
+ if (events[j].events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR) &&
+ object->read_ready)
+ object->read_ready(object->context);
+ if (!reactor->object_removed && events[j].events & EPOLLOUT &&
+ object->write_ready)
+ object->write_ready(object->context);
+ }
if (reactor->object_removed) {
- pthread_mutex_destroy(&object->lock);
+ delete object->mutex;
osi_free(object);
}
}
diff --git a/osi/src/wakelock.cc b/osi/src/wakelock.cc
index 777aa8c..8e1a739 100644
--- a/osi/src/wakelock.cc
+++ b/osi/src/wakelock.cc
@@ -31,6 +31,7 @@
#include <time.h>
#include <unistd.h>
+#include <mutex>
#include <string>
#include "osi/include/alarm.h"
@@ -77,7 +78,7 @@
// This mutex ensures that the functions that update and dump the statistics
// are executed serially.
-static pthread_mutex_t monitor;
+static std::mutex stats_mutex;
static bt_status_t wakelock_acquire_callout(void);
static bt_status_t wakelock_acquire_native(void);
@@ -182,7 +183,6 @@
}
static void wakelock_initialize(void) {
- pthread_mutex_init(&monitor, NULL);
reset_wakelock_stats();
if (is_native) wakelock_initialize_native();
@@ -212,7 +212,6 @@
wake_lock_path.clear();
wake_unlock_path.clear();
initialized = PTHREAD_ONCE_INIT;
- pthread_mutex_destroy(&monitor);
}
void wakelock_set_paths(const char* lock_path, const char* unlock_path) {
@@ -235,7 +234,7 @@
// Reset the Bluetooth wakelock statistics.
// This function is thread-safe.
static void reset_wakelock_stats(void) {
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(stats_mutex);
wakelock_stats.is_acquired = false;
wakelock_stats.acquired_count = 0;
@@ -249,8 +248,6 @@
wakelock_stats.last_acquired_timestamp_ms = 0;
wakelock_stats.last_released_timestamp_ms = 0;
wakelock_stats.last_reset_timestamp_ms = now();
-
- pthread_mutex_unlock(&monitor);
}
//
@@ -264,7 +261,7 @@
static void update_wakelock_acquired_stats(bt_status_t acquired_status) {
const period_ms_t now_ms = now();
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(stats_mutex);
if (acquired_status != BT_STATUS_SUCCESS) {
wakelock_stats.acquired_errors++;
@@ -272,7 +269,6 @@
}
if (wakelock_stats.is_acquired) {
- pthread_mutex_unlock(&monitor);
return;
}
@@ -280,8 +276,6 @@
wakelock_stats.acquired_count++;
wakelock_stats.last_acquired_timestamp_ms = now_ms;
- pthread_mutex_unlock(&monitor);
-
metrics_wake_event(WAKE_EVENT_ACQUIRED, NULL, WAKE_LOCK_ID, now_ms);
}
@@ -296,7 +290,7 @@
static void update_wakelock_released_stats(bt_status_t released_status) {
const period_ms_t now_ms = now();
- pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(stats_mutex);
if (released_status != BT_STATUS_SUCCESS) {
wakelock_stats.released_errors++;
@@ -304,7 +298,6 @@
}
if (!wakelock_stats.is_acquired) {
- pthread_mutex_unlock(&monitor);
return;
}
@@ -324,17 +317,13 @@
wakelock_stats.last_acquired_interval_ms = delta_ms;
wakelock_stats.total_acquired_interval_ms += delta_ms;
- pthread_mutex_unlock(&monitor);
-
metrics_wake_event(WAKE_EVENT_RELEASED, NULL, WAKE_LOCK_ID, now_ms);
}
void wakelock_debug_dump(int fd) {
const period_ms_t now_ms = now();
- // Need to keep track for lock errors - e.g., the "monitor" mutex
- // might not be initialized
- const int lock_error = pthread_mutex_lock(&monitor);
+ std::lock_guard<std::mutex> lock(stats_mutex);
// Compute the last acquired interval if the wakelock is still acquired
period_ms_t delta_ms = 0;
@@ -375,6 +364,4 @@
dprintf(
fd, " Total run time (ms) : %llu\n",
(unsigned long long)(now_ms - wakelock_stats.last_reset_timestamp_ms));
-
- if (lock_error == 0) pthread_mutex_unlock(&monitor);
}
diff --git a/udrv/ulinux/uipc.cc b/udrv/ulinux/uipc.cc
index 4d394c2..2bfbd8e 100644
--- a/udrv/ulinux/uipc.cc
+++ b/udrv/ulinux/uipc.cc
@@ -20,13 +20,12 @@
*
* Filename: uipc.cc
*
- * Description: UIPC implementation for bluedroid
+ * Description: UIPC implementation for fluoride
*
*****************************************************************************/
#include <errno.h>
#include <fcntl.h>
-#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -39,6 +38,7 @@
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
+#include <mutex>
#include "audio_a2dp_hw.h"
#include "bt_common.h"
@@ -62,11 +62,6 @@
#define UIPC_DISCONNECTED (-1)
-#define UIPC_LOCK() /*BTIF_TRACE_EVENT(" %s lock", __func__);*/ \
- pthread_mutex_lock(&uipc_main.mutex);
-#define UIPC_UNLOCK() /*BTIF_TRACE_EVENT("%s unlock", __func__);*/ \
- pthread_mutex_unlock(&uipc_main.mutex);
-
#define SAFE_FD_ISSET(fd, set) (((fd) == -1) ? false : FD_ISSET((fd), (set)))
#define UIPC_FLUSH_BUFFER_SIZE 1024
@@ -84,16 +79,13 @@
int fd;
int read_poll_tmo_ms;
int task_evt_flags; /* event flags pending to be processed in read task */
- tUIPC_EVENT cond_flags;
- pthread_mutex_t cond_mutex;
- pthread_cond_t cond;
tUIPC_RCV_CBACK* cback;
} tUIPC_CHAN;
typedef struct {
pthread_t tid; /* main thread id */
int running;
- pthread_mutex_t mutex;
+ std::recursive_mutex mutex;
fd_set active_set;
fd_set read_set;
@@ -215,13 +207,17 @@
static int uipc_main_init(void) {
int i;
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&uipc_main.mutex, &attr);
BTIF_TRACE_EVENT("### uipc_main_init ###");
+ uipc_main.tid = 0;
+ uipc_main.running = 0;
+ memset(&uipc_main.active_set, 0, sizeof(uipc_main.active_set));
+ memset(&uipc_main.read_set, 0, sizeof(uipc_main.read_set));
+ uipc_main.max_fd = 0;
+ memset(&uipc_main.signal_fds, 0, sizeof(uipc_main.signal_fds));
+ memset(&uipc_main.ch, 0, sizeof(uipc_main.ch));
+
/* setup interrupt socket pair */
if (socketpair(AF_UNIX, SOCK_STREAM, 0, uipc_main.signal_fds) < 0) {
return -1;
@@ -235,8 +231,6 @@
p->srvfd = UIPC_DISCONNECTED;
p->fd = UIPC_DISCONNECTED;
p->task_evt_flags = 0;
- pthread_cond_init(&p->cond, NULL);
- pthread_mutex_init(&p->cond_mutex, NULL);
p->cback = NULL;
}
@@ -260,8 +254,6 @@
int i;
for (i = 0; i < UIPC_CH_NUM; i++) {
- // BTIF_TRACE_EVENT("CHECK TASK FLAGS %x %x",
- // uipc_main.ch[i].task_evt_flags, UIPC_TASK_FLAG_DISCONNECT_CHAN);
if (uipc_main.ch[i].task_evt_flags & UIPC_TASK_FLAG_DISCONNECT_CHAN) {
uipc_main.ch[i].task_evt_flags &= ~UIPC_TASK_FLAG_DISCONNECT_CHAN;
uipc_close_ch_locked(i);
@@ -335,13 +327,12 @@
if (ch_id >= UIPC_CH_NUM) return -1;
- UIPC_LOCK();
+ std::lock_guard<std::recursive_mutex> guard(uipc_main.mutex);
fd = create_server_socket(name);
if (fd < 0) {
BTIF_TRACE_ERROR("failed to setup %s", name, strerror(errno));
- UIPC_UNLOCK();
return -1;
}
@@ -356,8 +347,6 @@
/* trigger main thread to update read set */
uipc_wakeup_locked();
- UIPC_UNLOCK();
-
return 0;
}
@@ -477,27 +466,29 @@
continue;
}
if (result < 0) {
- if (errno != EINTR) BTIF_TRACE_EVENT("select failed %s", strerror(errno));
+ if (errno != EINTR) {
+ BTIF_TRACE_EVENT("select failed %s", strerror(errno));
+ }
continue;
}
- UIPC_LOCK();
+ {
+ std::lock_guard<std::recursive_mutex> guard(uipc_main.mutex);
- /* clear any wakeup interrupt */
- uipc_check_interrupt_locked();
+ /* clear any wakeup interrupt */
+ uipc_check_interrupt_locked();
- /* check pending task events */
- uipc_check_task_flags_locked();
+ /* check pending task events */
+ uipc_check_task_flags_locked();
- /* make sure we service audio channel first */
- uipc_check_fd_locked(UIPC_CH_ID_AV_AUDIO);
+ /* make sure we service audio channel first */
+ uipc_check_fd_locked(UIPC_CH_ID_AV_AUDIO);
- /* check for other connections */
- for (ch_id = 0; ch_id < UIPC_CH_NUM; ch_id++) {
- if (ch_id != UIPC_CH_ID_AV_AUDIO) uipc_check_fd_locked(ch_id);
+ /* check for other connections */
+ for (ch_id = 0; ch_id < UIPC_CH_NUM; ch_id++) {
+ if (ch_id != UIPC_CH_ID_AV_AUDIO) uipc_check_fd_locked(ch_id);
+ }
}
-
- UIPC_UNLOCK();
}
BTIF_TRACE_EVENT("UIPC READ THREAD EXITING");
@@ -526,10 +517,11 @@
/* blocking call */
void uipc_stop_main_server_thread(void) {
/* request shutdown of read thread */
- UIPC_LOCK();
- uipc_main.running = 0;
- uipc_wakeup_locked();
- UIPC_UNLOCK();
+ {
+ std::lock_guard<std::recursive_mutex> lock(uipc_main.mutex);
+ uipc_main.running = 0;
+ uipc_wakeup_locked();
+ }
/* wait until read thread is fully terminated */
/* tid might hold pointer value where it's value
@@ -551,10 +543,7 @@
void UIPC_Init(UNUSED_ATTR void* p_data) {
BTIF_TRACE_DEBUG("UIPC_Init");
- memset(&uipc_main, 0, sizeof(tUIPC_MAIN));
-
uipc_main_init();
-
uipc_start_main_server_thread();
}
@@ -570,16 +559,14 @@
bool UIPC_Open(tUIPC_CH_ID ch_id, tUIPC_RCV_CBACK* p_cback) {
BTIF_TRACE_DEBUG("UIPC_Open : ch_id %d, p_cback %x", ch_id, p_cback);
- UIPC_LOCK();
+ std::lock_guard<std::recursive_mutex> lock(uipc_main.mutex);
if (ch_id >= UIPC_CH_NUM) {
- UIPC_UNLOCK();
return false;
}
if (uipc_main.ch[ch_id].srvfd != UIPC_DISCONNECTED) {
BTIF_TRACE_EVENT("CHANNEL %d ALREADY OPEN", ch_id);
- UIPC_UNLOCK();
return 0;
}
@@ -593,8 +580,6 @@
break;
}
- UIPC_UNLOCK();
-
return true;
}
@@ -613,14 +598,13 @@
/* special case handling uipc shutdown */
if (ch_id != UIPC_CH_ID_ALL) {
- UIPC_LOCK();
+ std::lock_guard<std::recursive_mutex> lock(uipc_main.mutex);
uipc_close_locked(ch_id);
- UIPC_UNLOCK();
- } else {
- BTIF_TRACE_DEBUG("UIPC_Close : waiting for shutdown to complete");
- uipc_stop_main_server_thread();
- BTIF_TRACE_DEBUG("UIPC_Close : shutdown complete");
+ return;
}
+ BTIF_TRACE_DEBUG("UIPC_Close : waiting for shutdown to complete");
+ uipc_stop_main_server_thread();
+ BTIF_TRACE_DEBUG("UIPC_Close : shutdown complete");
}
/*******************************************************************************
@@ -636,7 +620,7 @@
uint16_t msglen) {
BTIF_TRACE_DEBUG("UIPC_Send : ch_id:%d %d bytes", ch_id, msglen);
- UIPC_LOCK();
+ std::lock_guard<std::recursive_mutex> lock(uipc_main.mutex);
ssize_t ret;
OSI_NO_INTR(ret = write(uipc_main.ch[ch_id].fd, p_buf, msglen));
@@ -644,8 +628,6 @@
BTIF_TRACE_ERROR("failed to write (%s)", strerror(errno));
}
- UIPC_UNLOCK();
-
return false;
}
@@ -675,9 +657,6 @@
return 0;
}
- // BTIF_TRACE_DEBUG("UIPC_Read : ch_id %d, len %d, fd %d, polltmo %d",
- // ch_id, len, fd, uipc_main.ch[ch_id].read_poll_tmo_ms);
-
while (n_read < (int)len) {
pfd.fd = fd;
pfd.events = POLLIN | POLLHUP;
@@ -702,9 +681,8 @@
if (pfd.revents & (POLLHUP | POLLNVAL)) {
BTIF_TRACE_WARNING("poll : channel detached remotely");
- UIPC_LOCK();
+ std::lock_guard<std::recursive_mutex> lock(uipc_main.mutex);
uipc_close_locked(ch_id);
- UIPC_UNLOCK();
return 0;
}
@@ -715,9 +693,8 @@
if (n == 0) {
BTIF_TRACE_WARNING("UIPC_Read : channel detached remotely");
- UIPC_LOCK();
+ std::lock_guard<std::recursive_mutex> lock(uipc_main.mutex);
uipc_close_locked(ch_id);
- UIPC_UNLOCK();
return 0;
}
@@ -745,8 +722,7 @@
extern bool UIPC_Ioctl(tUIPC_CH_ID ch_id, uint32_t request, void* param) {
BTIF_TRACE_DEBUG("#### UIPC_Ioctl : ch_id %d, request %d ####", ch_id,
request);
-
- UIPC_LOCK();
+ std::lock_guard<std::recursive_mutex> lock(uipc_main.mutex);
switch (request) {
case UIPC_REQ_RX_FLUSH:
@@ -760,7 +736,6 @@
break;
case UIPC_REG_REMOVE_ACTIVE_READSET:
-
/* user will read data directly and not use select loop */
if (uipc_main.ch[ch_id].fd != UIPC_DISCONNECTED) {
/* remove this channel from active set */
@@ -782,7 +757,5 @@
break;
}
- UIPC_UNLOCK();
-
return false;
}
diff --git a/utils/src/bt_utils.cc b/utils/src/bt_utils.cc
index a8cb824..d7b8ebd 100644
--- a/utils/src/bt_utils.cc
+++ b/utils/src/bt_utils.cc
@@ -35,6 +35,7 @@
#include <stdlib.h>
#include <sys/resource.h>
#include <unistd.h>
+#include <mutex>
#ifdef OS_GENERIC
#define ANDROID_PRIORITY_AUDIO -16
@@ -55,14 +56,13 @@
******************************************************************************/
static pthread_once_t g_DoSchedulingGroupOnce[TASK_HIGH_MAX];
static bool g_DoSchedulingGroup[TASK_HIGH_MAX];
-static pthread_mutex_t gIdxLock;
+static std::mutex gIdxLock;
static int g_TaskIdx;
static int g_TaskIDs[TASK_HIGH_MAX];
#define INVALID_TASK_ID (-1)
static future_t* init(void) {
int i;
- pthread_mutexattr_t lock_attr;
for (i = 0; i < TASK_HIGH_MAX; i++) {
g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
@@ -70,13 +70,10 @@
g_TaskIDs[i] = INVALID_TASK_ID;
}
- pthread_mutexattr_init(&lock_attr);
- pthread_mutex_init(&gIdxLock, &lock_attr);
return NULL;
}
static future_t* clean_up(void) {
- pthread_mutex_destroy(&gIdxLock);
return NULL;
}
@@ -121,23 +118,25 @@
int tid = gettid();
int priority = ANDROID_PRIORITY_AUDIO;
- pthread_mutex_lock(&gIdxLock);
- g_TaskIdx = high_task;
+ {
+ std::lock_guard<std::mutex> lock(gIdxLock);
+ g_TaskIdx = high_task;
// TODO(armansito): Remove this conditional check once we find a solution
// for system/core on non-Android platforms.
#if defined(OS_GENERIC)
- rc = -1;
+ rc = -1;
#else // !defined(OS_GENERIC)
- pthread_once(&g_DoSchedulingGroupOnce[g_TaskIdx], check_do_scheduling_group);
- if (g_DoSchedulingGroup[g_TaskIdx]) {
- // set_sched_policy does not support tid == 0
- rc = set_sched_policy(tid, SP_AUDIO_SYS);
- }
+ pthread_once(&g_DoSchedulingGroupOnce[g_TaskIdx],
+ check_do_scheduling_group);
+ if (g_DoSchedulingGroup[g_TaskIdx]) {
+ // set_sched_policy does not support tid == 0
+ rc = set_sched_policy(tid, SP_AUDIO_SYS);
+ }
#endif // defined(OS_GENERIC)
- g_TaskIDs[high_task] = tid;
- pthread_mutex_unlock(&gIdxLock);
+ g_TaskIDs[high_task] = tid;
+ }
if (rc) {
LOG_WARN(LOG_TAG, "failed to change sched policy, tid %d, err: %d", tid,