Repository navigation
wireless/bluetooth/zblue: Run the work queues as pthreads - #4129
chengkai15 wants to merge 1 commit into
Conversation
|
Outdated: apache/nuttx#22268 now adds This PR depends on apache/nuttx#22268 (
Please merge apache/nuttx#22268 first. The test results in the description are from master with both PRs applied. |
|
765b653 to
6a077ab
Compare
🔗 Cross-repo PR dependenciesThe 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 |
|
Looks good overall: moving every host thread into the application's group fixes the 1. Check the return value of
2. Concurrent I checked
3. PR description / metadata
4. Nits
|
6a077ab to
a98e459
Compare
|
@cederom thanks for the review. Updated in a98e459, rebased on the current master:
Retested on sim:zblue with a CSR controller and a Realtek central: 12/12 GATT checks pass (details in the description). |
🔗 Cross-repo PR dependenciesThe 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>
a98e459 to
aee8742
Compare
Note: Please adhere to Contributing Guidelines.
Depends-On: apache/nuttx/pull/22268
Summary
On current master,
zblue_peripheralon sim:zblue hangs inbt_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:pthread_setspecific()rejects a key that was not created in the group of the caller, so it returnsEINVALin the receive thread. That thread then does not release the cooperative lock while it blocks inread(), and the host TX processor waits on that lock forever.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 callswork_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 insend()again, and the TX work queue and its thread are removed. Eachwrite()carries one complete H4 packet, anduart_bth4sends 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 fromwork_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
zblue_hci_tx).Testing
sim:zblue on Linux with a CSR USB dongle (
CONFIG_SIM_HCISOCKET) as controller. BlueZgatttoolon a Realtek USB dongle is the central.master (nuttx c68c0bfbc1, nuttx-apps fa64592):
bt_enable()hangs, andpsshows the TX processor work queue waiting on a mutex held by the receive thread: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) getsEINVAL: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, soCONFIG_SIM_HCISOCKET_DEVID=2andCONFIG_ZBLUE_HCI_DEVICE="/dev/ttyHCI2"were set locally.All host threads now belong to the application (PID 8):
GATT tests with
gatttoolas central:Not tested: protected and kernel builds (zblue depends on
ARCH_SIM && HOST_LINUX, and the simulator has no protected build), real boards. The libcwork_queue_init()/work_queue_run()used there is tested in apache/nuttx#22268 (mps2-an500:citest_protected and qemu-armv7a:citest_knsh withCONFIG_LIBC_USRWORK=y).