Skip to content

Commit 964b9aa

Browse files
committed
Bluetooth: Host: Add bt_taskq workqueue for quick non-blocking tasks
Add a new workqueue bt_taskq specifically designed for quick non-blocking work items in the Bluetooth subsystem. This workqueue is always available and does not depend on any Kconfig option. Signed-off-by: Aleksander Wasaznik <[email protected]>
1 parent c8d9103 commit 964b9aa

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

subsys/bluetooth/host/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources_ifdef(CONFIG_BT_MONITOR monitor.c)
1111
zephyr_library_sources_ifdef(CONFIG_BT_SETTINGS settings.c)
1212
zephyr_library_sources_ifdef(CONFIG_BT_HOST_CCM aes_ccm.c)
1313
zephyr_library_sources_ifdef(CONFIG_BT_LONG_WQ long_wq.c)
14+
zephyr_library_sources(bt_taskq.c)
1415

1516
if(CONFIG_BT_HCI_HOST)
1617
zephyr_library_sources(

subsys/bluetooth/host/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ config BT_LONG_WQ_INIT_PRIO
3535

3636
endif # BT_LONG_WQ
3737

38+
config BT_TASKQ_STACK_SIZE_WITH_PROMPT
39+
bool "Override BT taskq thread stack size"
40+
41+
config BT_TASKQ_STACK_SIZE
42+
int
43+
prompt "BT taskq thread stack size" if BT_HCI_TX_STACK_SIZE_WITH_PROMPT
44+
default 1024
45+
46+
config BT_TASKQ_THREAD_PRIO
47+
int
48+
# -1 is the least urgent cooperative priority.
49+
# tx_processor() needs a cooperative thread for now.
50+
default -1
51+
3852
config BT_HCI_HOST
3953
# Hidden option to make the conditions more intuitive
4054
bool

subsys/bluetooth/host/bt_taskq.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* bt_taskq.c - Workqueue for quick non-blocking Bluetooth tasks */
2+
3+
/*
4+
* Copyright (c) 2025 Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <zephyr/autoconf.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/init.h>
12+
#include <zephyr/kernel/thread_stack.h>
13+
14+
static K_THREAD_STACK_DEFINE(bt_taskq_stack, CONFIG_BT_TASKQ_STACK_SIZE);
15+
struct k_work_q bt_taskq;
16+
17+
static int bt_taskq_init(void)
18+
{
19+
const struct k_work_queue_config cfg = {
20+
.name = "bt_taskq",
21+
/* Enable CONFIG_WORKQUEUE_WORK_TIMEOUT to detect tasks that take too long. */
22+
.work_timeout_ms = 10,
23+
};
24+
25+
k_work_queue_start(&bt_taskq, bt_taskq_stack, K_THREAD_STACK_SIZEOF(bt_taskq_stack),
26+
CONFIG_BT_TASKQ_THREAD_PRIO, &cfg);
27+
28+
return 0;
29+
}
30+
31+
/* The init priority is set to POST_KERNEL 999, the last level
32+
* before APPLICATION.
33+
*/
34+
SYS_INIT(bt_taskq_init, POST_KERNEL, 999);

subsys/bluetooth/host/bt_taskq.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* bt_taskq.h - Workqueue for quick non-blocking Bluetooth tasks */
2+
3+
/*
4+
* Copyright (c) 2025 Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <zephyr/kernel.h>
10+
11+
/**
12+
* @brief Bluetooth task workqueue
13+
*
14+
* bt_taskq is a workqueue intended for quick non-blocking work
15+
* items ("tasks") in the Bluetooth subsystem. This workqueue
16+
* must always exist and is not controlled by any Kconfig
17+
* option.
18+
*
19+
* Blocking means "waiting for something while running". A
20+
* task is NOT allowed to block. If a task need to "wait", it
21+
* should instead return immediately and schedule itself to run
22+
* later.
23+
*
24+
* Work items submitted to this queue should be:
25+
* - Quick to execute (non-blocking).
26+
* - Not perform long-running operations.
27+
* - Not block.
28+
*
29+
* @warning Non-blocking violation pitfalls:
30+
* - net_buf_unref() on a foreign buffer could have a blocking
31+
* destroy callback
32+
* - Any user-defined callback might be blocking
33+
* - Avoid any operations that could sleep or block the thread
34+
*
35+
* Use bt_long_wq for long-running or potentially blocking
36+
* operations instead.
37+
*
38+
* Available in APPLICATION initialization level and later.
39+
*/
40+
extern struct k_work_q bt_taskq;

subsys/bluetooth/host/hci_core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ void bt_send_one_host_num_completed_packets(uint16_t handle)
282282
BT_ASSERT_MSG(err == 0, "Unable to send Host NCP (err %d)", err);
283283
}
284284

285+
#include "bt_taskq.h"
286+
285287
#if defined(CONFIG_BT_TESTING)
286288
__weak void bt_testing_trace_event_acl_pool_destroy(struct net_buf *buf)
287289
{

0 commit comments

Comments
 (0)