Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use low priority workqueue for underglow and battery reporting #1870

Merged
merged 2 commits into from
Jul 25, 2023

Conversation

xudongzheng
Copy link
Contributor

Blocking operations on the high priority system workqueue may result in deadlocks, particularly when Bluetooth is in use.

app/Kconfig Outdated Show resolved Hide resolved
app/src/rgb_underglow.c Outdated Show resolved Hide resolved
@xudongzheng
Copy link
Contributor Author

@caksoylar Thanks for the suggestions, both have been addressed.

I've also confirmed that the underglow part works on RP2040.

Copy link
Contributor

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple thoughts on the implementation. Thanks for working on this!

app/Kconfig Outdated
Comment on lines 121 to 128
config ZMK_LOW_PRIORITY_THREAD_STACK_SIZE
int "Low priority thread stack size"
default 768

config ZMK_LOW_PRIORITY_THREAD_PRIORITY
int "Low priority thread priority"
default 10

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This config is all reasonable, but I don't love where you're dropping it in this file. I would nest this under the "Advanced" menu somewhere.

@@ -0,0 +1 @@
extern struct k_work_q lowprio_work_q;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love exposing this as an extern here. I'd vote for either a function (that would get inlined probably) or a C-macro/define at least.

In particular, if we have a space constrained device (RAM wise) we might want to just-reuse the system workqueue instead of adding a second queue, and using the define/function will let use swap those out w/o the consumer caring as much.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've introduced zmk_lowprio_work_q() but generally I'm hesitant to make things like this configurable unless if there's a good concrete reason - ie 1KB is the difference between fitting and not fitting on a chip.

Furthermore currently the purpose of the low priority workqueue is not well defined. It may end up being used for other tasks that must run with a lower priority than the system workqueue on low resource MCUs, similar to how battery notification is for BLE.

}

k_timer_start(&battery_timer, K_MINUTES(1), K_SECONDS(CONFIG_ZMK_BATTERY_REPORT_INTERVAL));
k_timer_start(&battery_timer, K_NO_WAIT, K_SECONDS(CONFIG_ZMK_BATTERY_REPORT_INTERVAL));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@petejohanson petejohanson self-assigned this Jul 17, 2023
@petejohanson petejohanson added enhancement New feature or request core Core functionality/behavior of ZMK bluetooth Bluetooth related items lighting labels Jul 17, 2023
@xudongzheng xudongzheng force-pushed the custom-wq-pr branch 2 times, most recently from 8b63db2 to 9ebf522 Compare July 17, 2023 21:16
@petejohanson
Copy link
Contributor

Code looks reasonable, will test locally soon.

@xudongzheng
Copy link
Contributor Author

A separate commit has been added for turning underglow off using the low priority work queue as well.

Interestingly I observed quite a bit of LED flickering on RP2040 when led_strip_update_rgb() is called from the system workqueue. It's practically non-existent while running on the low priority workqueue.

@@ -0,0 +1 @@
struct k_work_q *zmk_lowprio_work_q();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionality tested w/o issue, but as I peek at this code one more time, it seems this naming should be adjusted to be "namespaced" to match the header, e.g.:

Suggested change
struct k_work_q *zmk_lowprio_work_q();
struct k_work_q *zmk_workqueue_lowprio_work_q();

Blocking operations on the high priority system workqueue may result in
deadlocks, particularly when Bluetooth is in use.
Copy link
Contributor

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @Nicell Any concerns with the underglow bits?

@petejohanson petejohanson requested a review from Nicell July 23, 2023 23:40
Copy link
Member

@Nicell Nicell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No concerns about the underglow parts, looks good!

@petejohanson petejohanson merged commit cb9c573 into zmkfirmware:main Jul 25, 2023
41 checks passed
@xudongzheng xudongzheng deleted the custom-wq-pr branch July 25, 2023 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bluetooth Bluetooth related items core Core functionality/behavior of ZMK enhancement New feature or request lighting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants