Bluetooth: host: Make bt_conn_ref return NULL if the ref count is zero

Make bt_conn_ref return NULL if the reference count has reached zero.
This makes it possible to re-use bt_conn_ref internally to re-use the
reference count mechanism to check if the reference is in use.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
This commit is contained in:
Joakim Andersson 2020-11-30 13:38:49 +01:00 committed by Carles Cufí
commit fae964c008
2 changed files with 19 additions and 4 deletions

View file

@ -197,9 +197,12 @@ struct bt_conn_le_data_len_param {
*
* Increment the reference count of a connection object.
*
* @note Will return NULL if the reference count is zero.
*
* @param conn Connection object.
*
* @return Connection object with incremented reference count.
* @return Connection object with incremented reference count, or NULL if the
* reference count is zero.
*/
struct bt_conn *bt_conn_ref(struct bt_conn *conn);

View file

@ -1820,10 +1820,22 @@ void bt_conn_foreach(int type, void (*func)(struct bt_conn *conn, void *data),
struct bt_conn *bt_conn_ref(struct bt_conn *conn)
{
atomic_val_t old = atomic_inc(&conn->ref);
atomic_val_t old;
BT_DBG("handle %u ref %u -> %u", conn->handle, old,
atomic_get(&conn->ref));
/* Reference counter must be checked to avoid incrementing ref from
* zero, then we should return NULL instead.
* Loop on clear-and-set in case someone has modified the reference
* count since the read, and start over again when that happens.
*/
do {
old = atomic_get(&conn->ref);
if (!old) {
return NULL;
}
} while (!atomic_cas(&conn->ref, old, old + 1));
BT_DBG("handle %u ref %u -> %u", conn->handle, old, old + 1);
return conn;
}