-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Add new thread_pool component built on top of Task
#673
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
1a4eaba
add thread_pool component
guo-max 9287962
add comments to all the public functions
guo-max d0a79cc
unlock queue mutex when queue is empty.
guo-max ce96a41
fix status comments
guo-max 53c150b
fix stop function comments
guo-max 98d9223
fix readme
guo-max 5da2ee1
add log if task not started
guo-max fa1852e
udpate documentation
guo-max 9df0de9
fix mutex with task_notified.
guo-max 929b02f
use std::move instead of copy for job submit
guo-max 864bb85
update example
guo-max 01138d5
revise per review
guo-max fbe1f50
update based on AI review
guo-max 7c81329
update based on review
guo-max c734cf5
add formatter for the stats struct
guo-max 010b2f2
add more test for all the public functions
guo-max e3ac58f
fix AI review; move format helper to a header file
guo-max 6878774
update doc and fix format
guo-max 165ab76
update example
guo-max 7959f2f
udpate test; update start function; update doc
guo-max acd6f8e
fix component order; fix example; update comments
guo-max 34c6c87
fix typo
guo-max 8c4f0f9
update readme; add example code link
guo-max 6862c78
change signature to only accept r-value; use atomic for stopping and …
guo-max File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| idf_component_register( | ||
| INCLUDE_DIRS "include" | ||
| SRC_DIRS "src" | ||
| REQUIRES base_component task) | ||
|
guo-max marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Thread Pool Component | ||
|
|
||
| [](https://components.espressif.com/components/espp/thread_pool) | ||
|
|
||
| The `ThreadPool` component provides a reusable pool of worker tasks for executing queued jobs asynchronously. | ||
|
|
||
| It is implemented with `espp::Task` workers and `std::condition_variable` synchronization. | ||
|
|
||
| ## Features | ||
|
|
||
| - Configurable worker count | ||
| - Bounded or unbounded queue | ||
| - Optional blocking submit mode for backpressure | ||
| - Manual `start()` / `stop()` control; `start()` returns `true` if all workers launched successfully (or the pool was already running), `false` if any worker failed to start and the pool was rolled back to stopped state | ||
| - Graceful stop (stops workers; queued jobs may not be executed) | ||
|
guo-max marked this conversation as resolved.
|
||
| - Thread-safe stats for submitted / executed / rejected jobs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # The following lines of boilerplate have to be in your project's CMakeLists | ||
| # in this exact order for cmake to work correctly | ||
| cmake_minimum_required(VERSION 3.20) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 20) | ||
|
|
||
| set(ENV{IDF_COMPONENT_MANAGER} "0") | ||
| include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
|
|
||
| # add the component directories that we want to use | ||
| set(EXTRA_COMPONENT_DIRS | ||
| "../../../components/" | ||
| ) | ||
|
|
||
| set( | ||
| COMPONENTS | ||
| "main esptool_py thread_pool" | ||
| CACHE STRING | ||
| "List of components to include" | ||
| ) | ||
|
|
||
| project(thread_pool_example) |
|
guo-max marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Thread Pool Example | ||
|
|
||
| This example is a self-contained test suite that exercises the full public API | ||
| of `espp::ThreadPool` across a range of usage patterns, from basic single-pool | ||
| operation to more advanced concurrent and multi-pool scenarios. | ||
|
|
||
| | # | Test | APIs covered | | ||
| |---|---|---| | ||
| | 1 | Lifecycle | `start()`, `stop()`, `is_running()`, `worker_count()` | | ||
| | 2 | Normal dispatch | `submit()`, `queue_size()`, `stats()` | | ||
| | 3 | Bounded-queue rejection | `try_submit()` with deterministic barrier | | ||
| | 4 | Blocking submit | `submit()` with `block_on_submit_when_full` | | ||
| | 5 | Post-stop rejection | `submit()` after `stop()` | | ||
| | 6 | Concurrent lifecycle | multi-thread `start()` / `stop()` stress | | ||
| | 7 | Concurrent submission | multi-thread `submit()` + `try_submit()` | | ||
| | 8 | Chained pools | a job in `pool_a` submitting work into `pool_b` | | ||
| | 9 | Self-submit | a running job submitting back to its own pool | | ||
|
|
||
| Each test logs individual `PASS` / `FAIL` results inline. At the end of the run | ||
| a summary is printed: | ||
|
|
||
| ``` | ||
| ==================== Results ==================== | ||
| PASS lifecycle: start/stop/is_running/worker_count | ||
| PASS submit: normal dispatch + queue_size + stats | ||
| ... | ||
| ================================================= | ||
| 9/9 tests passed | ||
| All tests passed! | ||
| ``` | ||
|
|
||
| The final summary line (`N/N tests passed` / `N test(s) FAILED`) is the key | ||
| indicator of overall correctness and is suitable for CI log parsing. | ||
|
|
||
| ## Build | ||
|
|
||
| From this directory, run: | ||
|
|
||
| ```bash | ||
| idf.py set-target esp32 | ||
| idf.py build flash monitor | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| idf_component_register(SRC_DIRS "." | ||
| INCLUDE_DIRS ".") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.