mirror of https://github.com/labapart/gattlib
Consolidate device mutex
parent
ce52533f39
commit
fab0e8fa67
|
@ -49,10 +49,10 @@ void gattlib_on_disconnected_device(gatt_connection_t* connection) {
|
|||
}
|
||||
|
||||
// Signal the device is now disconnected
|
||||
g_mutex_lock(&connection->disconnection_wait.lock);
|
||||
g_mutex_lock(&connection->device_mutex);
|
||||
connection->disconnection_wait.value = true;
|
||||
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
|
||||
gattlib_connection_free(connection);
|
||||
|
|
|
@ -52,13 +52,11 @@ struct _gattlib_device {
|
|||
// Context specific to the backend implementation (eg: dbus backend)
|
||||
void* context;
|
||||
|
||||
GMutex connection_mutex;
|
||||
GMutex device_mutex;
|
||||
|
||||
struct {
|
||||
// Used by gattlib_disconnection when we want to wait for the disconnection to be effective
|
||||
GCond condition;
|
||||
// Mutex used for disconnection_condition synchronization
|
||||
GMutex lock;
|
||||
// Used to avoid spurious or stolen wakeup
|
||||
bool value;
|
||||
} disconnection_wait;
|
||||
|
|
|
@ -290,7 +290,7 @@ FREE_CONN_CONTEXT:
|
|||
void gattlib_connection_free(gatt_connection_t* connection) {
|
||||
gattlib_context_t* conn_context;
|
||||
|
||||
g_mutex_lock(&connection->connection_mutex);
|
||||
g_mutex_lock(&connection->device_mutex);
|
||||
conn_context = connection->context;
|
||||
|
||||
// Remove signal
|
||||
|
@ -326,7 +326,7 @@ void gattlib_connection_free(gatt_connection_t* connection) {
|
|||
free(connection->context);
|
||||
connection->context = NULL;
|
||||
|
||||
g_mutex_unlock(&connection->connection_mutex);
|
||||
g_mutex_unlock(&connection->device_mutex);
|
||||
|
||||
// And finally free the connection
|
||||
free(connection);
|
||||
|
@ -342,7 +342,7 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
|
|||
return GATTLIB_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
g_mutex_lock(&connection->connection_mutex);
|
||||
g_mutex_lock(&connection->device_mutex);
|
||||
conn_context = connection->context;
|
||||
|
||||
if (conn_context == NULL) {
|
||||
|
@ -365,21 +365,18 @@ int gattlib_disconnect(gatt_connection_t* connection, bool wait_disconnection) {
|
|||
if (wait_disconnection) {
|
||||
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;
|
||||
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_mutex_unlock(&connection->disconnection_wait.lock);
|
||||
}
|
||||
|
||||
EXIT:
|
||||
g_mutex_unlock(&connection->connection_mutex);
|
||||
g_mutex_unlock(&connection->device_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue