Skip to content

wireless/bluetooth/zblue: Run the work queues as pthreads - #4129

Open
chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:zblue-pthread-wq
Open

chengkai15 wants to merge 1 commit into
apache:masterfrom
chengkai15:zblue-pthread-wq

Conversation

@chengkai15

@chengkai15 chengkai15 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Depends-On: apache/nuttx/pull/22268

Summary

On current master, zblue_peripheral on sim:zblue hangs in bt_enable(): HCI_Reset is never sent.

In the flat build the Zephyr work queues are kernel threads (work_queue_create()), while the HCI receive thread and the application threads are pthreads of the application. They are in different task groups:

  • The thread-specific key of the porting layer is created by the first thread that uses it, a work queue (kernel thread). Since apache/nuttx 0bc013bb2b, pthread_setspecific() rejects a key that was not created in the group of the caller, so it returns EINVAL in the receive thread. That thread then does not release the cooperative lock while it blocks in read(), and the host TX processor waits on that lock forever.
  • File descriptors are not shared either, which is why the HCI driver had its own TX work queue that opens the device a second time.

This PR creates each work queue with work_queue_init() (apache/nuttx#22268) and serves it from a zblue thread, a pthread of the application, that calls work_queue_run(). The thread gives up the cooperative lock while it waits for work; work_entry() takes it for each item of a cooperative queue, as before. All host threads are then pthreads of the application. The HCI driver writes in send() again, and the TX work queue and its thread are removed. Each write() carries one complete H4 packet, and uart_bth4 sends each packet whole under its send lock, so packets from different threads do not interleave; a short write is an error. The send is synchronous, as in the Zephyr userchan driver. A return from work_queue_run() is logged and asserted, since the queues are never stopped. The first work item that made the NuttX worker the queue thread is removed too.

Impact

Testing

sim:zblue on Linux with a CSR USB dongle (CONFIG_SIM_HCISOCKET) as controller. BlueZ gatttool on a Realtek USB dongle is the central.

master (nuttx c68c0bfbc1, nuttx-apps fa64592): bt_enable() hangs, and ps shows the TX processor work queue waiting on a mutex held by the receive thread:

nsh> zblue_peripheral &
zblue_peripheral [8:100]
  TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK COMMAND
    8     8     7 100 FIFO     Task      - Waiting  Semaphore 0000000000000000 0067456 zblue_peripheral
    9     0     8 121 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0071520 sysworkq 0x7caf25724010 0x7caf25724098
   10     0     8 110 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0070768 BT LW WQ 0x7caf25724280 0x7caf25724308
   11     0     8 121 FIFO     Kthread   - Waiting  Mutex:14  0000000000000000 0070496 zblue_wq 0x7caf257244f0 0x7caf25724578
   12     0     8 128 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0070672 BT RX WQ 0x7caf25724760 0x7caf257247e8
   13     0     8 129 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0070496 zblue_hci_tx 0x7caf25724a00 0x7caf25724a88
   14     8     7 130 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0071600 zblue_hci_rx 0x400cc998 0x4023c440
nsh> W: bt_hci_core: Dropping queued command 0x0c03: HCI transport closed

With a debug print in z_zblue_set_self(), the work queues (pid 0) set the key, and the receive thread of the application (pid 8) gets EINVAL:

DBG set_self sysworkq pid=0 ret=0
DBG set_self BT LW WQ pid=0 ret=0
DBG set_self zblue_wq pid=0 ret=0
DBG set_self zblue_wq pid=0 ret=0
DBG set_self zblue_hci_tx pid=0 ret=0
DBG set_self (null) pid=8 ret=22

This PR + apache/nuttx#22268 (nuttx e9ebe71ec2 on b5db91fe0d, nuttx-apps aee8742 on 9a1d292): sim:zblue with a CSR dongle (HCI 4.2) as controller and a Realtek dongle as central. On this host the CSR dongle is hci2, so CONFIG_SIM_HCISOCKET_DEVID=2 and CONFIG_ZBLUE_HCI_DEVICE="/dev/ttyHCI2" were set locally.

I: bt_hci_core: Identity: P:00:1B:DC:F4:B4:8E
I: bt_hci_core: HCI: version 4.2 (0x08) revision 0x30e8, manufacturer 0x000a
I: bt_hci_core: LMP: version 4.2 (0x08) subver 0x30e8
zblue: Bluetooth ready, identity P:00:1B:DC:F4:B4:8E
zblue: advertising as "NuttX zblue"
zblue: connected P:08:BE:AC:48:26:13

All host threads now belong to the application (PID 8):

    8     8     7 100 FIFO     Task      - Waiting  Semaphore 0000000000000000 0067456 zblue_peripheral
    9     8     7 121 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0071600 sysworkq 0x400cc97d 0x4023a640
   10     8     7 110 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0070848 BT LW WQ 0x400cc97d 0x4023abc0
   11     8     7 121 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0070576 zblue_wq 0x400cc97d 0x4023ed60
   12     8     7 128 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0070752 BT RX WQ 0x400cc97d 0x4023f4c0
   13     8     7 130 FIFO     pthread   - Waiting  Semaphore 0000000000000000 0071600 zblue_hci_rx 0x400cc97d 0x40239440

GATT tests with gatttool as central:

[PASS] bt_enable + advertising
[PASS] primary service discovery (GATT 0x1801, GAP 0x1800)
[PASS] characteristic discovery (Device Name 0x2a00)
[PASS] connect
[PASS] ATT MTU exchange mtu=23
[PASS] write request (Client Supported Features 0x0006)
[PASS] read back 0x0006 01
[PASS] 200 reads on one connection 200/200, 30.0s
[PASS] 10 reads, each on a new connection 10/10
[PASS] no zblue_hci_tx thread
[PASS] all host threads in the application (PID 8)
[PASS] no write failed / error log
SUMMARY 12 / 12

Not tested: protected and kernel builds (zblue depends on ARCH_SIM && HOST_LINUX, and the simulator has no protected build), real boards. The libc work_queue_init() / work_queue_run() used there is tested in apache/nuttx#22268 (mps2-an500:citest_protected and qemu-armv7a:citest_knsh with CONFIG_LIBC_USRWORK=y).

@chengkai15

chengkai15 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Outdated: apache/nuttx#22268 now adds work_queue_init() / work_queue_run() instead of work_queue_create_pthread(), and this PR uses them. The dependency still holds: merge apache/nuttx#22268 first.


This PR depends on apache/nuttx#22268 (sched/wqueue: Add work_queue_create_pthread()), which is not merged yet.

work.c calls work_queue_create_pthread(), which that PR adds. Until it is merged:

  • this PR does not build without it (work_queue_create_pthread() is not declared or defined), also in CI;
  • current master still hangs in bt_enable() on sim:zblue (see the description).

Please merge apache/nuttx#22268 first. The test results in the description are from master with both PRs applied.

@github-actions

Copy link
Copy Markdown

⚠️ depends-on could not be parsed

A depends-on: line was found in the PR description, but no valid dependency was parsed. Supported repositories: apache/nuttx, apache/nuttx-apps; the PR id must be numeric.

Expected format:

depends-on: [apache/nuttx/pull/<N> apache/nuttx-apps/pull/<M>]

CI run: https://github.com/apache/nuttx-apps/actions/runs/45418995878

@chengkai15
chengkai15 marked this pull request as draft November 25, 2026 13:18
@chengkai15
chengkai15 marked this pull request as ready for review November 25, 2026 13:33
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/45624885864

@cederom

cederom commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Looks good overall: moving every host thread into the application's group fixes the pthread_setspecific() / EINVAL hang at the root, and it removes the second open(). Comments on 6a077ab:

1. Check the return value of work_queue_run()

queue_thread_entry() ignores it. These queues are never freed, so any return is a bug (-EBUSY, -EINVAL, or an -ESHUTDOWN that should be impossible). The thread would then exit quietly, and every work item on that queue would hang, which is the same symptom this PR fixes. Suggest:

ret = work_queue_run(queue->wq);
__ASSERT(0, "work queue %s stopped: %d", queue->thread.name, ret);

2. Concurrent hci_send()

I checked uart_bth4_write(). It holds sendlock across reassembly and drv->send() and returns either buflen or an error, so one write() per complete H4 packet can't interleave. Two notes:

  • The reassembly buffer is per device and shared across fds/threads. Safety depends on every write() carrying a whole packet, so please say "one write() per H4 packet" explicitly in the comment.
  • hci_send() is now synchronous and gives up the coop lock in the caller's thread, where the old path queued and returned, as upstream H4 does. This matches Zephyr's userchan driver, so the host should tolerate it. Please confirm it's intended for the host flow-control path (Number of Completed Packets sent from buffer release, possibly on the RX thread).

3. PR description / metadata

4. Nits

  • CONFIG_BT_HCI_TX_STACK_SIZE / CONFIG_BT_HCI_TX_PRIO are no longer used by the port.
  • Protected/kernel builds go through the libc implementation (LIBC_USRWORK) and are untested per the description. A build-only CI run for one protected config would be good.

@chengkai15

Copy link
Copy Markdown
Contributor Author

@cederom thanks for the review. Updated in a98e459, rebased on the current master:

  1. work_queue_run() return value: queue_thread_entry() now logs work queue %s stopped: %d with LOG_ERR and then asserts. The log line is there because __ASSERT() is compiled out in sim:zblue (CONFIG_ASSERT_MODULE_DEFAULT_LEVEL=0), so an assert alone would leave the failure silent there.
  2. Concurrent hci_send():
    • The comment now says that each write() carries one complete H4 packet, and why. I also dropped the loop that continued a short write: the rest of the packet would go out in a second write() after the cooperative lock was given up, so another thread could slip a packet in between. uart_bth4_write() never returns a short count, so a short write is now -EIO. EINTR still retries the whole packet, which is safe because uart_bth4 resets sendlen on error.
    • Host flow control: yes, this is intended, and it is exercised. In the btmon capture of the run in the description, the host sends Set Controller To Host Flow Control (ACL) and Host Buffer Size with only 10 ACL buffers. The controller sent 221 ACL packets, and the host answered each with a Host Number of Completed Packets (221). Those go through bt_hci_cmd_send() -> bt_send() directly, not through cmd_tx_queue, from the thread that frees the buffer (normally BT RX WQ). Without them, the 200 reads on one connection would stall after 10.
  3. Description / depends-on: the description had already been updated on 11-25, and the bot parsed the Depends-On line in its second run. The stale work_queue_create_pthread() text was in my earlier comment, which is now marked outdated. The description now has the results of this push against sched/wqueue: Add work_queue_init(), work_queue_run() and work_queue_stop() nuttx#22268 @ 24bf49ebb9.
  4. Nits:
    • CONFIG_BT_HCI_TX_STACK_SIZE / CONFIG_BT_HCI_TX_PRIO are Zephyr symbols from subsys/bluetooth/host/Kconfig. They are not set in zblue.conf or defined by the port. BT_HCI_TX_PRIO is still used by the upstream common/dummy.c that zblue builds (BUILD_ASSERT(CONFIG_BT_DRIVER_RX_HIGH_PRIO < CONFIG_BT_HCI_TX_PRIO)), so there is nothing to remove on the port side.
    • Protected build: zblue depends on ARCH_SIM && HOST_LINUX, and the simulator has no protected build, so zblue itself cannot be built there. The libc work_queue_init() / work_queue_run() path is build-tested in sched/wqueue: Add work_queue_init(), work_queue_run() and work_queue_stop() nuttx#22268 (stm32f4discovery:kostest with CONFIG_LIBC_USRWORK=y).

Retested on sim:zblue with a CSR controller and a Realtek central: 12/12 GATT checks pass (details in the description).

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/45955680431

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 <chengkai@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants