Fix non-standard pthread mutex interactions.
pthread_mutex_t is intended to be opaque.
pthread_attr_t is intended to be opaque.
Change-Id: Ie0f5558a96f8e5fc8b2f59763acbfe5340fa81a4
diff --git a/btif/include/btif_sock_util.h b/btif/include/btif_sock_util.h
index 0659930..eff18d2 100644
--- a/btif/include/btif_sock_util.h
+++ b/btif/include/btif_sock_util.h
@@ -27,36 +27,8 @@
#ifndef BTIF_SOCK_UTIL_H
#define BTIF_SOCK_UTIL_H
-#include <pthread.h>
-
#include "osi/include/log.h"
-/*******************************************************************************
-** Functions
-********************************************************************************/
-
-static inline void init_slot_lock( pthread_mutex_t* mutex)
-{
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
- pthread_mutex_init(mutex, &attr);
-}
-
-static inline void lock_slot(pthread_mutex_t* mutex)
-{
- if(mutex->value)
- pthread_mutex_lock(mutex);
- else LOG_ERROR("mutex: %p is not initialized", mutex);
-}
-
-static inline void unlock_slot(pthread_mutex_t* mutex)
-{
- if(mutex->value)
- pthread_mutex_unlock(mutex);
- else LOG_ERROR("mutex: %p is not initialized", mutex);
-}
-
void dump_bin(const char* title, const char* data, int size);
int sock_send_fd(int sock_fd, const uint8_t* buffer, int len, int send_fd);
diff --git a/btif/src/btif_sock_rfc.c b/btif/src/btif_sock_rfc.c
index 4b9040e..b2b1eb1 100644
--- a/btif/src/btif_sock_rfc.c
+++ b/btif/src/btif_sock_rfc.c
@@ -20,6 +20,7 @@
#include <assert.h>
#include <errno.h>
+#include <features.h>
#include <hardware/bluetooth.h>
#include <hardware/bt_sock.h>
#include <string.h>
@@ -81,7 +82,11 @@
static rfc_slot_t rfc_slots[MAX_RFC_CHANNEL];
static uint32_t rfc_slot_id;
static volatile int pth = -1; // poll thread handle
-static pthread_mutex_t slot_lock;
+#if __GLIBC__
+static pthread_mutex_t slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+#else
+static pthread_mutex_t slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
+#endif
static rfc_slot_t *find_free_slot(void);
static void cleanup_rfc_slot(rfc_slot_t *rs);
@@ -107,7 +112,6 @@
}
BTA_JvEnable(jv_dm_cback);
- init_slot_lock(&slot_lock);
return BT_STATUS_SUCCESS;
}
@@ -115,13 +119,13 @@
void btsock_rfc_cleanup(void) {
pth = -1;
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
for (size_t i = 0; i < ARRAY_SIZE(rfc_slots); ++i) {
if (rfc_slots[i].id)
cleanup_rfc_slot(&rfc_slots[i]);
list_free(rfc_slots[i].incoming_queue);
}
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
static rfc_slot_t *find_free_slot(void) {
@@ -255,7 +259,7 @@
}
int status = BT_STATUS_FAIL;
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = alloc_rfc_slot(NULL, service_name, service_uuid, channel, flags, true);
if (!slot) {
@@ -271,7 +275,7 @@
status = BT_STATUS_SUCCESS;
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
return status;
}
@@ -287,7 +291,7 @@
return BT_STATUS_NOT_READY;
int status = BT_STATUS_FAIL;
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = alloc_rfc_slot(bd_addr, NULL, service_uuid, channel, flags, false);
if (!slot) {
@@ -329,7 +333,7 @@
status = BT_STATUS_SUCCESS;
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
return status;
}
@@ -410,7 +414,7 @@
}
static void on_cl_rfc_init(tBTA_JV_RFCOMM_CL_INIT *p_init, uint32_t id) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (!slot)
@@ -422,11 +426,11 @@
cleanup_rfc_slot(slot);
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
static void on_srv_rfc_listen_started(tBTA_JV_RFCOMM_START *p_start, uint32_t id) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (!slot)
@@ -443,12 +447,12 @@
cleanup_rfc_slot(slot);
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
static uint32_t on_srv_rfc_connect(tBTA_JV_RFCOMM_SRV_OPEN *p_open, uint32_t id) {
uint32_t new_listen_slot_id = 0;
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *srv_rs = find_rfc_slot_by_id(id);
if (!srv_rs)
@@ -466,12 +470,12 @@
new_listen_slot_id = srv_rs->id;
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
return new_listen_slot_id;
}
static void on_cli_rfc_connect(tBTA_JV_RFCOMM_OPEN *p_open, uint32_t id) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (!slot)
@@ -491,32 +495,32 @@
LOG_ERROR("%s unable to send connect completion signal to caller.", __func__);
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
static void on_rfc_close(UNUSED_ATTR tBTA_JV_RFCOMM_CLOSE *p_close, uint32_t id) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
// rfc_handle already closed when receiving rfcomm close event from stack.
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (slot)
cleanup_rfc_slot(slot);
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
static void on_rfc_write_done(UNUSED_ATTR tBTA_JV_RFCOMM_WRITE *p, uint32_t id) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (slot && !slot->f.outgoing_congest)
btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
static void on_rfc_outgoing_congest(tBTA_JV_RFCOMM_CONG *p, uint32_t id) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (slot) {
@@ -525,7 +529,7 @@
btsock_thread_add_fd(pth, slot->fd, BTSOCK_RFCOMM, SOCK_THREAD_FD_RD, slot->id);
}
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
static void *rfcomm_cback(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data) {
@@ -569,7 +573,7 @@
break;
default:
- LOG_ERROR("%s unhandled event %d, slot id: %d", __func__, event, (uintptr_t)user_data);
+ LOG_ERROR("%s unhandled event %d, slot id: %zi", __func__, event, (uintptr_t)user_data);
break;
}
return new_user_data;
@@ -579,7 +583,7 @@
uint32_t id = (uintptr_t)user_data;
switch(event) {
case BTA_JV_CREATE_RECORD_EVT: {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (slot && create_server_sdp_record(slot)) {
@@ -590,12 +594,12 @@
cleanup_rfc_slot(slot);
}
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
break;
}
case BTA_JV_DISCOVERY_COMP_EVT: {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(id);
if (p_data->disc_comp.status == BTA_JV_SUCCESS && p_data->disc_comp.scn) {
if (slot && slot->f.doing_sdp_request) {
@@ -627,7 +631,7 @@
slot->f.doing_sdp_request = true;
}
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
break;
}
@@ -698,7 +702,7 @@
}
void btsock_rfc_signaled(UNUSED_ATTR int fd, int flags, uint32_t user_id) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
rfc_slot_t *slot = find_rfc_slot_by_id(user_id);
if (!slot)
@@ -735,11 +739,11 @@
}
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
}
int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
int ret = 0;
uint32_t id = (uintptr_t)user_data;
@@ -770,12 +774,12 @@
}
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
return ret; // Return 0 to disable data flow.
}
int bta_co_rfc_data_outgoing_size(void *user_data, int *size) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
uint32_t id = (uintptr_t)user_data;
int ret = false;
@@ -792,12 +796,12 @@
}
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
return ret;
}
int bta_co_rfc_data_outgoing(void *user_data, uint8_t *buf, uint16_t size) {
- lock_slot(&slot_lock);
+ pthread_mutex_lock(&slot_lock);
uint32_t id = (uintptr_t)user_data;
int ret = false;
@@ -814,6 +818,6 @@
}
out:;
- unlock_slot(&slot_lock);
+ pthread_mutex_unlock(&slot_lock);
return ret;
}
diff --git a/btif/src/btif_sock_thread.c b/btif/src/btif_sock_thread.c
index 7e4e839..5d44a4d 100644
--- a/btif/src/btif_sock_thread.c
+++ b/btif/src/btif_sock_thread.c
@@ -32,6 +32,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
+#include <features.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -108,8 +109,11 @@
static inline void add_poll(int h, int fd, int type, int flags, uint32_t user_id);
-static pthread_mutex_t thread_slot_lock;
-
+#if __GLIBC__
+static pthread_mutex_t thread_slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+#else
+static pthread_mutex_t thread_slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
+#endif
static inline void set_socket_blocking(int s, int blocking)
{
@@ -206,7 +210,6 @@
if(!initialized)
{
initialized = 1;
- init_slot_lock(&thread_slot_lock);
int h;
for(h = 0; h < MAX_THREAD; h++)
{
@@ -224,9 +227,9 @@
{
int ret = FALSE;
asrt(callback || cmd_callback);
- lock_slot(&thread_slot_lock);
+ pthread_mutex_lock(&thread_slot_lock);
int h = alloc_thread_slot();
- unlock_slot(&thread_slot_lock);
+ pthread_mutex_unlock(&thread_slot_lock);
APPL_TRACE_DEBUG("alloc_thread_slot ret:%d", h);
if(h >= 0)
{
@@ -390,9 +393,9 @@
if(send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd))
{
pthread_join(ts[h].thread_id, 0);
- lock_slot(&thread_slot_lock);
+ pthread_mutex_lock(&thread_slot_lock);
free_thread_slot(h);
- unlock_slot(&thread_slot_lock);
+ pthread_mutex_unlock(&thread_slot_lock);
return TRUE;
}
return FALSE;
diff --git a/udrv/ulinux/uipc.c b/udrv/ulinux/uipc.c
index 7619d2c..fb2a094 100644
--- a/udrv/ulinux/uipc.c
+++ b/udrv/ulinux/uipc.c
@@ -210,7 +210,9 @@
static int uipc_main_init(void)
{
int i;
- const pthread_mutexattr_t attr = PTHREAD_MUTEX_RECURSIVE;
+ 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 ###");