Consolidate device mutex

latest-fixes
Olivier Martin 2024-04-03 15:11:00 +02:00
parent ce52533f39
commit fab0e8fa67
3 changed files with 8 additions and 13 deletions

View File

@ -49,10 +49,10 @@ void gattlib_on_disconnected_device(gatt_connection_t* connection) {
} }
// Signal the device is now disconnected // Signal the device is now disconnected
g_mutex_lock(&connection->disconnection_wait.lock); g_mutex_lock(&connection->device_mutex);
connection->disconnection_wait.value = true; connection->disconnection_wait.value = true;
g_cond_broadcast(&connection->disconnection_wait.condition); g_cond_broadcast(&connection->disconnection_wait.condition);
g_mutex_unlock(&connection->disconnection_wait.lock); g_mutex_unlock(&connection->device_mutex);
// Clean GATTLIB connection on disconnection // Clean GATTLIB connection on disconnection
gattlib_connection_free(connection); gattlib_connection_free(connection);

View File

@ -52,13 +52,11 @@ struct _gattlib_device {
// Context specific to the backend implementation (eg: dbus backend) // Context specific to the backend implementation (eg: dbus backend)
void* context; void* context;
GMutex connection_mutex; GMutex device_mutex;
struct { struct {
// Used by gattlib_disconnection when we want to wait for the disconnection to be effective // Used by gattlib_disconnection when we want to wait for the disconnection to be effective
GCond condition; GCond condition;
// Mutex used for disconnection_condition synchronization
GMutex lock;
// Used to avoid spurious or stolen wakeup // Used to avoid spurious or stolen wakeup
bool value; bool value;
} disconnection_wait; } disconnection_wait;

View File

@ -290,7 +290,7 @@ FREE_CONN_CONTEXT:
void gattlib_connection_free(gatt_connection_t* connection) { void gattlib_connection_free(gatt_connection_t* connection) {
gattlib_context_t* conn_context; gattlib_context_t* conn_context;
g_mutex_lock(&connection->connection_mutex); g_mutex_lock(&connection->device_mutex);
conn_context = connection->context; conn_context = connection->context;
// Remove signal // Remove signal
@ -326,7 +326,7 @@ void gattlib_connection_free(gatt_connection_t* connection) {
free(connection->context); free(connection->context);
connection->context = NULL; connection->context = NULL;
g_mutex_unlock(&connection->connection_mutex); g_mutex_unlock(&connection->device_mutex);
// And finally free the connection // And finally free the connection
free(connection); free(connection);
@ -342,7 +342,7 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
return GATTLIB_INVALID_PARAMETER; return GATTLIB_INVALID_PARAMETER;
} }
g_mutex_lock(&connection->connection_mutex); g_mutex_lock(&connection->device_mutex);
conn_context = connection->context; conn_context = connection->context;
if (conn_context == NULL) { if (conn_context == NULL) {
@ -365,21 +365,18 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
if (wait_disconnection) { if (wait_disconnection) {
gint64 end_time; gint64 end_time;
g_mutex_lock(&connection->disconnection_wait.lock);
end_time = g_get_monotonic_time() + GATTLIB_DISCONNECTION_WAIT_TIMEOUT_SEC * G_TIME_SPAN_SECOND; end_time = g_get_monotonic_time() + GATTLIB_DISCONNECTION_WAIT_TIMEOUT_SEC * G_TIME_SPAN_SECOND;
while (!connection->disconnection_wait.value) { while (!connection->disconnection_wait.value) {
if (!g_cond_wait_until(&connection->disconnection_wait.condition, &connection->disconnection_wait.lock, end_time)) { if (!g_cond_wait_until(&connection->disconnection_wait.condition, &connection->device_mutex, end_time)) {
ret = GATTLIB_TIMEOUT; ret = GATTLIB_TIMEOUT;
break; break;
} }
} }
g_mutex_unlock(&connection->disconnection_wait.lock);
} }
EXIT: EXIT:
g_mutex_unlock(&connection->connection_mutex); g_mutex_unlock(&connection->device_mutex);
return ret; return ret;
} }