This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 94c0f25e8 wireless/bluetooth/zblue: Run the work queues as pthreads
94c0f25e8 is described below
commit 94c0f25e8b92952e9d815bb20a0486542e3f49cf
Author: Kai Cheng <[email protected]>
AuthorDate: Tue Nov 24 22:37:46 2026 +0800
wireless/bluetooth/zblue: Run the work queues as pthreads
In the flat build the Zephyr work queues are kernel threads, created
with work_queue_create(), while the HCI receive thread and the threads
of the application are pthreads of the application. The two kinds of
thread are in different task groups and do not share per-process
resources:
- the thread-specific key of the porting layer is created in the group
of the first thread that uses it. Since pthread_setspecific() rejects
keys that were not created in the group of the caller, the other
threads cannot record their k_thread, the cooperative lock is not
released while the receive thread blocks in read(), and bt_enable()
hangs: HCI_Reset is never sent.
- the file descriptors: the HCI driver needed its own TX work queue,
which opens the device again, to write from the host work queues.
Create each work queue with work_queue_init() and serve it from a zblue
thread, a pthread of the application, that calls work_queue_run(). All
the threads of the host are then pthreads of the application, and
send() writes the packets directly again. The TX work queue and its
thread are removed, and so is the first work item that made the NuttX
worker the queue thread.
The queues are never stopped, so a return from work_queue_run() is logged
as an error and asserted. send() writes each H4 packet with one
write(): uart_bth4 keeps complete packets from different threads apart,
so a short write is now an error instead of being continued.
Assisted-by: Kiro:claude-opus-5-5
Signed-off-by: Kai Cheng <[email protected]>
---
.../bluetooth/zblue/port/include/zephyr/kernel.h | 1 -
wireless/bluetooth/zblue/port/src/hci.c | 143 +++++++--------------
wireless/bluetooth/zblue/port/src/work.c | 47 +++++--
3 files changed, 76 insertions(+), 115 deletions(-)
diff --git a/wireless/bluetooth/zblue/port/include/zephyr/kernel.h
b/wireless/bluetooth/zblue/port/include/zephyr/kernel.h
index 0fd3da178..a15922ed9 100644
--- a/wireless/bluetooth/zblue/port/include/zephyr/kernel.h
+++ b/wireless/bluetooth/zblue/port/include/zephyr/kernel.h
@@ -433,7 +433,6 @@ struct k_work_q
struct k_thread thread;
k_tid_t thread_id;
FAR struct kwork_wqueue_s *wq;
- struct work_s start;
};
extern struct k_work_q k_sys_work_q;
diff --git a/wireless/bluetooth/zblue/port/src/hci.c
b/wireless/bluetooth/zblue/port/src/hci.c
index 444b52e25..7e659b09a 100644
--- a/wireless/bluetooth/zblue/port/src/hci.c
+++ b/wireless/bluetooth/zblue/port/src/hci.c
@@ -23,9 +23,6 @@
/* The HCI device of the host: H4 packets over a NuttX /dev/ttyHCIx
* character device (drivers/serial/uart_bth4.c). It is the zephyr,bt-hci
* chosen node of port/include/zephyr/devicetree_generated.h.
- *
- * The device is opened twice: for reading by the application, which the
- * RX thread shares, and for writing on the TX work queue.
*/
/****************************************************************************
@@ -54,8 +51,7 @@ LOG_MODULE_REGISTER(zblue_hci, CONFIG_LOG_DEFAULT_LEVEL);
struct hci_data
{
struct bt_hci_driver_data common; /* Must be first */
- int fd; /* RX, opened by the application */
- int txfd; /* TX, opened on the TX work queue */
+ int fd;
};
/****************************************************************************
@@ -65,19 +61,12 @@ struct hci_data
static struct hci_data g_hci_data =
{
.fd = -1,
- .txfd = -1,
};
static K_KERNEL_STACK_DEFINE(g_rx_stack, CONFIG_ZBLUE_HCI_RX_STACKSIZE);
static struct k_thread g_rx_thread;
static uint8_t g_rx_frame[BT_BUF_RX_SIZE];
-static K_KERNEL_STACK_DEFINE(g_tx_stack, CONFIG_BT_HCI_TX_STACK_SIZE);
-static struct k_work_q g_tx_queue;
-static struct k_work g_tx_open;
-static struct k_work g_tx_work;
-static K_FIFO_DEFINE(g_tx_fifo);
-
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -259,99 +248,17 @@ static void rx_thread(void *p1, void *p2, void *p3)
}
}
-/* The host sends from any thread, its work queues among them, which are
- * kernel threads in the flat build and do not share the file descriptors
- * of the application. So the TX fd is opened on the TX work queue and
- * all the writes are done there, one after the other.
- */
-
-static void tx_open(struct k_work *work)
-{
- UNUSED(work);
-
- g_hci_data.txfd = open(CONFIG_ZBLUE_HCI_DEVICE, O_WRONLY | O_CLOEXEC);
- if (g_hci_data.txfd < 0)
- {
- g_hci_data.txfd = -errno;
- }
-}
-
-static void tx_work(struct k_work *work)
-{
- struct hci_data *hci = &g_hci_data;
- struct net_buf *buf;
- const uint8_t *data;
- size_t len;
- ssize_t ret;
- int depth;
- int err;
-
- UNUSED(work);
-
- while ((buf = k_fifo_get(&g_tx_fifo, K_NO_WAIT)) != NULL)
- {
- data = buf->data;
- len = buf->len;
-
- while (len > 0)
- {
- depth = z_zblue_block_begin();
- ret = write(hci->txfd, data, len);
- err = errno;
- z_zblue_block_end(depth);
- if (ret < 0)
- {
- if (err == EINTR)
- {
- continue;
- }
-
- LOG_ERR("write failed: %d", err);
- break;
- }
-
- data += ret;
- len -= ret;
- }
-
- net_buf_unref(buf);
- }
-}
-
static int hci_open(const struct device *dev)
{
struct hci_data *hci = dev->data;
- struct k_work_queue_config cfg =
- {
- .name = "zblue_hci_tx",
- };
- struct k_work_sync sync;
-
- hci->fd = open(CONFIG_ZBLUE_HCI_DEVICE, O_RDONLY | O_CLOEXEC);
+ hci->fd = open(CONFIG_ZBLUE_HCI_DEVICE, O_RDWR | O_CLOEXEC);
if (hci->fd < 0)
{
LOG_ERR("cannot open %s: %d", CONFIG_ZBLUE_HCI_DEVICE, errno);
return -errno;
}
- k_work_queue_init(&g_tx_queue);
- k_work_queue_start(&g_tx_queue, g_tx_stack,
- K_KERNEL_STACK_SIZEOF(g_tx_stack),
- K_PRIO_COOP(CONFIG_BT_HCI_TX_PRIO), &cfg);
- k_work_init(&g_tx_open, tx_open);
- k_work_init(&g_tx_work, tx_work);
- k_work_submit_to_queue(&g_tx_queue, &g_tx_open);
- k_work_flush(&g_tx_open, &sync);
- if (hci->txfd < 0)
- {
- LOG_ERR("cannot open %s for TX: %d", CONFIG_ZBLUE_HCI_DEVICE,
- hci->txfd);
- close(hci->fd);
- hci->fd = -1;
- return hci->txfd;
- }
-
k_thread_create(&g_rx_thread, g_rx_stack,
K_KERNEL_STACK_SIZEOF(g_rx_stack), rx_thread,
(void *)dev, NULL, NULL,
@@ -361,8 +268,17 @@ static int hci_open(const struct device *dev)
}
/* buf starts with the H4 packet type, as the upstream drivers expect.
- * It is written by the TX work queue, as upstream H4 queues it for the
- * UART.
+ * The send is synchronous, as in the Zephyr userchan driver. The host
+ * sends from its work queues and, for the host flow control, sends Host
+ * Number of Completed Packets through bt_send() from the thread that
+ * frees an ACL buffer: all are threads of the application and share its
+ * file descriptors.
+ *
+ * Each write() carries one complete H4 packet. uart_bth4 reassembles the
+ * packets in one buffer per device and sends each one under its send
+ * lock, so complete packets from different threads do not mix. A part
+ * of a packet would mix with the packets of other threads, so a short
+ * write is an error and is not continued.
*
* There is no close(): the RX thread cannot be stopped while it waits in
* read(), so bt_disable() returns -ENOSYS, as for upstream drivers
@@ -371,10 +287,37 @@ static int hci_open(const struct device *dev)
static int hci_send(const struct device *dev, struct net_buf *buf)
{
- UNUSED(dev);
+ struct hci_data *hci = dev->data;
+ ssize_t ret;
+ int depth;
+ int err;
+
+ /* The controller driver may block in write(): let the other
+ * cooperative threads, the RX thread among them, run meanwhile.
+ */
+
+ do
+ {
+ depth = z_zblue_block_begin();
+ ret = write(hci->fd, buf->data, buf->len);
+ err = errno;
+ z_zblue_block_end(depth);
+ }
+ while (ret < 0 && err == EINTR);
+
+ if (ret < 0)
+ {
+ LOG_ERR("write failed: %d", err);
+ return -err;
+ }
+
+ if (ret != buf->len)
+ {
+ LOG_ERR("short write: %zd of %u", ret, buf->len);
+ return -EIO;
+ }
- k_fifo_put(&g_tx_fifo, buf);
- k_work_submit_to_queue(&g_tx_queue, &g_tx_work);
+ net_buf_unref(buf);
return 0;
}
diff --git a/wireless/bluetooth/zblue/port/src/work.c
b/wireless/bluetooth/zblue/port/src/work.c
index 61155f564..5fa897542 100644
--- a/wireless/bluetooth/zblue/port/src/work.c
+++ b/wireless/bluetooth/zblue/port/src/work.c
@@ -20,9 +20,11 @@
*
****************************************************************************/
-/* Zephyr work queues on NuttX work queues (work_queue_create()).
+/* Zephyr work queues on NuttX work queues (work_queue_init()).
*
- * Each k_work_q is a NuttX work queue with one thread. The NuttX work
+ * Each k_work_q is a NuttX work queue served by one zblue thread, a
+ * pthread of the application: the work runs with its file descriptors and
+ * its thread-specific data. The NuttX work
* item of a k_work only runs work_entry() on that thread when the item is
* due. The Zephyr state (QUEUED, DELAYED, RUNNING, CANCELING) is kept in
* the k_work flags, under one lock, and gives the Zephyr return values;
@@ -36,9 +38,12 @@
****************************************************************************/
#include <zephyr/kernel.h>
+#include <zephyr/logging/log.h>
#include "port.h"
+LOG_MODULE_REGISTER(zblue_work, CONFIG_LOG_DEFAULT_LEVEL);
+
/****************************************************************************
* Private Data
****************************************************************************/
@@ -296,15 +301,31 @@ static void work_entry(FAR void *arg)
pthread_mutex_unlock(&g_work_lock);
}
-/* First item of every queue: makes k_current_get() the queue thread */
+/* The queue thread runs the NuttX work loop */
-static void queue_start_entry(FAR void *arg)
+static void queue_thread_entry(void *p1, void *p2, void *p3)
{
- struct k_work_q *queue = arg;
+ struct k_work_q *queue = p1;
+ int depth;
+ int ret;
+
+ ARG_UNUSED(p2);
+ ARG_UNUSED(p3);
+
+ /* Waiting for work must not hold the cooperative lock: work_entry()
+ * takes it for each item of a cooperative queue.
+ */
+
+ depth = z_zblue_block_begin();
+ ret = work_queue_run(queue->wq);
+ z_zblue_block_end(depth);
+
+ /* The queues are never stopped, so work_queue_run() must not return:
+ * the work of the queue would then never run.
+ */
- queue->thread.pthread = pthread_self();
- pthread_setname_np(queue->thread.pthread, queue->thread.name);
- z_zblue_set_self(&queue->thread);
+ LOG_ERR("work queue %s stopped: %d", queue->thread.name, ret);
+ __ASSERT(0, "work queue %s stopped: %d", queue->thread.name, ret);
}
/****************************************************************************
@@ -394,15 +415,13 @@ void k_work_queue_start(struct k_work_q *queue,
k_thread_stack_t *stack,
queue->thread.name = (cfg != NULL && cfg->name != NULL) ?
cfg->name : "zblue_wq";
- queue->thread.prio = prio;
- queue->thread.coop = prio < 0;
+ queue->thread.entry = queue_thread_entry;
+ queue->thread.p1 = queue;
queue->thread_id = &queue->thread;
- queue->wq = work_queue_create(queue->thread.name, z_zblue_priority(prio),
- NULL, stack_size + ZBLUE_STACK_EXTRA, 1);
+ queue->wq = work_queue_init(1);
if (queue->wq == NULL ||
- work_queue_wq(queue->wq, &queue->start, queue_start_entry,
- queue, 0) < 0)
+ z_zblue_thread_start(&queue->thread, stack_size, prio) < 0)
{
__ASSERT(0, "cannot start work queue %s", queue->thread.name);
}