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:
parent
76789883e2
commit
fae964c008
2 changed files with 19 additions and 4 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue