Skip to content

Commit

Permalink
Bluetooth: RFCOMM: Remove reference counting
Browse files Browse the repository at this point in the history
The reference counting has been an internal trick to handle the TX
fiber, but it's not really needed since we can do the same thing with
the help of the state value.

Change-Id: I9cdaed9afb0b0c07e23d599637328cb863c123b3
Signed-off-by: Johan Hedberg <[email protected]>
  • Loading branch information
Johan Hedberg committed Oct 4, 2016
1 parent 7fb0894 commit ea55761
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
1 change: 0 additions & 1 deletion include/bluetooth/rfcomm.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ struct bt_rfcomm_dlc {
struct nano_fifo tx_queue;
/** TX credits */
struct nano_sem tx_credits;
atomic_t ref;
struct bt_rfcomm_session *session;
struct bt_rfcomm_dlc_ops *ops;
struct bt_rfcomm_dlc *_next;
Expand Down
38 changes: 10 additions & 28 deletions net/bluetooth/rfcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,32 +236,16 @@ static void rfcomm_dlc_destroy(struct bt_rfcomm_dlc *dlc)
}
}

static void rfcomm_dlc_unref(struct bt_rfcomm_dlc *dlc)
{
atomic_dec(&dlc->ref);

BT_DBG("dlc %p ref %u", dlc, atomic_get(&dlc->ref));

if (!atomic_get(&dlc->ref)) {
rfcomm_dlc_destroy(dlc);
}
}

static struct bt_rfcomm_dlc *rfcomm_dlc_ref(struct bt_rfcomm_dlc *dlc)
{
atomic_inc(&dlc->ref);

BT_DBG("dlc %p ref %u", dlc, atomic_get(&dlc->ref));

return dlc;
}

static void rfcomm_dlc_disconnected(struct bt_rfcomm_dlc *dlc)
static void rfcomm_dlc_disconnect(struct bt_rfcomm_dlc *dlc)
{
uint8_t old_state = dlc->state;

BT_DBG("dlc %p", dlc);

if (dlc->state == BT_RFCOMM_STATE_DISCONNECTED) {
return;
}

dlc->state = BT_RFCOMM_STATE_DISCONNECTED;

switch (old_state) {
Expand All @@ -280,10 +264,9 @@ static void rfcomm_dlc_disconnected(struct bt_rfcomm_dlc *dlc)

break;
default:
rfcomm_dlc_destroy(dlc);
break;
}

rfcomm_dlc_unref(dlc);
}

static void rfcomm_session_disconnected(struct bt_rfcomm_session *session)
Expand All @@ -303,7 +286,7 @@ static void rfcomm_session_disconnected(struct bt_rfcomm_session *session)
next = dlc->_next;
dlc->_next = NULL;

rfcomm_dlc_disconnected(dlc);
rfcomm_dlc_disconnect(dlc);

dlc = next;
}
Expand Down Expand Up @@ -407,7 +390,6 @@ static struct bt_rfcomm_dlc *rfcomm_dlc_accept(struct bt_rfcomm_session *session
dlc->rx_credit = RFCOMM_DEFAULT_CREDIT;
dlc->state = BT_RFCOMM_STATE_INIT;
dlc->mtu = min(dlc->mtu, session->mtu);
atomic_set(&dlc->ref, 1);
nano_sem_init(&dlc->tx_credits);

return dlc;
Expand Down Expand Up @@ -476,7 +458,7 @@ static void rfcomm_dlc_tx_fiber(int arg1, int arg2)
net_buf_unref(buf);
}

rfcomm_dlc_unref(dlc);
rfcomm_dlc_destroy(dlc);

BT_DBG("dlc %p exiting", dlc);
}
Expand Down Expand Up @@ -535,7 +517,7 @@ static void rfcomm_dlc_connected(struct bt_rfcomm_dlc *dlc)

nano_fifo_init(&dlc->tx_queue);
fiber_start(dlc->stack, sizeof(dlc->stack), rfcomm_dlc_tx_fiber,
(int)rfcomm_dlc_ref(dlc), 0, 7, 0);
(int)dlc, 0, 7, 0);

if (dlc->ops && dlc->ops->connected) {
dlc->ops->connected(dlc);
Expand Down Expand Up @@ -694,7 +676,7 @@ static void rfcomm_handle_disc(struct bt_rfcomm_session *session, uint8_t dlci)
}

rfcomm_send_ua(session, dlci);
rfcomm_dlc_disconnected(dlc);
rfcomm_dlc_disconnect(dlc);
} else {
rfcomm_send_ua(session, 0);
rfcomm_session_disconnected(session);
Expand Down

0 comments on commit ea55761

Please sign in to comment.