Skip to content

Asynchronous executor#1190

Open
DenisBiryukov91 wants to merge 32 commits intoeclipse-zenoh:mainfrom
DenisBiryukov91:executor
Open

Asynchronous executor#1190
DenisBiryukov91 wants to merge 32 commits intoeclipse-zenoh:mainfrom
DenisBiryukov91:executor

Conversation

@DenisBiryukov91
Copy link
Contributor

@DenisBiryukov91 DenisBiryukov91 commented Mar 20, 2026

Description

This PR introduces asynchronous executor. It allows to run all zenoh-pico tasks (accept, reconnect read, send_join, send_keep_alive, lease, periodic tasks etc) in a single thread, which significantly simplifies task management.

In addition, for single threaded mode zp_spin_once function is provided, which allows to spin executor manually. In particular this trivially extends all previously only multi-threaded features, like p2p, connectivity events, auto-reconnect and advanced pub-sub to single-threaded case.

What does this PR do?

Replaces multiple threads with a single background executor.
Introduces zp_spin_once to allow spinning executor manually in single-threaded mode.
Updates all the examples: removes no-longer needed task start/stop calls from multi-threaded cases, replaces all zp_read/send_keep_alive/send_join with zp_spin_once in single-threaded cases.
Adds platform thread_id getters, and functions to measure elapsed time between 2 z_clock_t instances.

Why is this change needed?

Simplify task management.

Related Issues


🏷️ Label-Based Checklist

Based on the labels applied to this PR, please complete these additional requirements:

Labels: enhancement

✨ Enhancement Requirements

Since this PR enhances existing functionality:

  • Enhancement scope documented - Clear description of what is being improved
  • Minimum necessary code - Implementation is as simple as possible, doesn't overcomplicate the system
  • Backwards compatible - Existing code/APIs still work unchanged
  • No new APIs added - Only improving existing functionality
  • Tests updated - Existing tests pass, new test cases added if needed
  • Performance improvement measured - If applicable, before/after metrics provided
  • Documentation updated - Existing docs updated to reflect improvements
  • User impact documented - How users benefit from this enhancement

Remember: Enhancements should not introduce new APIs or breaking changes.

Instructions:

  1. Check off items as you complete them (change - [ ] to - [x])
  2. The PR checklist CI will verify these are completed

This checklist updates automatically when labels change, but preserves your checked boxes.

@DenisBiryukov91 DenisBiryukov91 added the enhancement Existing things could work better label Mar 20, 2026
@DenisBiryukov91 DenisBiryukov91 marked this pull request as draft March 20, 2026 16:31
_Z_ERROR_RETURN(_Z_ERR_GENERIC);
}
if (fcntl(sock->_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
if (fcntl(sock->_fd, F_SETFL, blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK)) == -1) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

Because of missing configuration, misra checking is incomplete. There can be false negatives! Variable 'O_NONBLOCK' is unknown Warning

Because of missing configuration, misra checking is incomplete. There can be false negatives! Variable 'O_NONBLOCK' is unknown
_Z_ERROR_RETURN(_Z_ERR_GENERIC);
}
if (fcntl(sock->_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
if (fcntl(sock->_fd, F_SETFL, blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK)) == -1) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

Because of missing configuration, misra checking is incomplete. There can be false negatives! Variable 'F_SETFL' is unknown Warning

Because of missing configuration, misra checking is incomplete. There can be false negatives! Variable 'F_SETFL' is unknown
return ts;
}

unsigned long zp_clock_elapsed_us_since(z_clock_t *instant, z_clock_t* epoch) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

Parameter 'instant' can be declared as pointer to const Note

Parameter 'instant' can be declared as pointer to const
return ts;
}

unsigned long zp_clock_elapsed_us_since(z_clock_t *instant, z_clock_t* epoch) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

Parameter 'epoch' can be declared as pointer to const Note

Parameter 'epoch' can be declared as pointer to const
return elapsed > 0 ? (unsigned long)elapsed : 0;
}

unsigned long zp_clock_elapsed_ms_since(z_clock_t *instant, z_clock_t* epoch) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

Parameter 'epoch' can be declared as pointer to const Note

Parameter 'epoch' can be declared as pointer to const
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Cppcheck (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a unified asynchronous executor/runtime to run zenoh-pico background activities (read/lease/accept/periodic-like work) through a single execution model, enabling single-threaded builds to progress networking via a new zp_spin_once() primitive, and refactoring transport/session internals accordingly.

Changes:

  • Replaces per-session/threaded task management with an executor-backed runtime (incl. background executor in multi-thread builds, manual spinning in single-thread builds).
  • Refactors transport read/accept/lease paths to executor “future” task functions and adds connect/accept receive deadlines.
  • Updates tests/examples to remove explicit zp_start_*_task / zp_stop_*_task calls and to use zp_spin_once() in single-threaded examples; adds platform task-id and clock elapsed-since helpers.

Reviewed changes

Copilot reviewed 219 out of 219 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/z_wildcard_subscription_test.c Removes explicit read/lease task start/stop calls.
tests/z_tls_config_test.c Removes explicit task start calls for client/server sessions.
tests/z_test_peer_unicast.c Updates peer test timing/options; removes explicit task start; adjusts waits/asserts.
tests/z_test_peer_multicast.c Updates peer test timing/options; removes explicit task start; adjusts waits/asserts.
tests/z_test_fragment_tx.c Removes explicit read/lease task start.
tests/z_test_fragment_rx.c Removes explicit read/lease task start.
tests/z_session_test.c Removes multi-thread task auto-start behavior tests (file deleted).
tests/z_perf_tx.c Removes explicit task start/stop around perf publisher.
tests/z_perf_rx.c Removes explicit task start around perf subscriber.
tests/z_multi_queryable_test.c Removes explicit task start/stop calls.
tests/z_multi_pubsub_test.c Removes explicit task start/stop calls.
tests/z_collections_test.c Adds deque/pqueue template tests.
tests/z_client_test.c Removes explicit read/lease task start/stop calls.
tests/z_api_source_info_test.c Removes explicit task start/stop calls in source-info tests.
tests/z_api_local_subscriber_test.c Removes explicit task start/stop calls.
tests/z_api_local_queryable_test.c Removes explicit task start/stop calls.
tests/z_api_liveliness_test.c Removes explicit task start/stop and manual join in tests.
tests/z_api_connectivity_test.c Removes task start/stop from session open/close helper.
tests/z_api_cancellation_test.c Removes explicit task start calls across cancellation tests.
tests/z_api_alignment_test.c Removes ZENOH_PICO task start/stop blocks.
tests/z_api_advanced_pubsub_test.c Removes explicit read/lease/periodic task management in advanced pubsub tests.
tests/z_api_admin_space_test.c Removes task start/stop from session open/close helper.
src/transport/unicast/transport.c Removes RX mutex/task fields; adds handshake recv deadlines; updates clear signature.
src/transport/unicast/rx.c Removes transport RX mutex usage in unicast receive path.
src/transport/unicast/read.c Replaces thread task loop with executor future function for unicast reads.
src/transport/transport.c Simplifies _z_transport_clear() signature; sets transport state to closed.
src/transport/raweth/rx.c Removes transport RX mutex usage in raw ethernet receive path.
src/transport/raweth/read.c Replaces raweth read task loop with executor future function.
src/transport/peer.c Adjusts multicast peer copy (removes _next_lease).
src/transport/multicast/transport.c Removes RX mutex/task fields; updates clear signature.
src/transport/multicast/rx.c Removes transport RX mutex usage; removes _next_lease updates.
src/transport/multicast/read.c Replaces multicast read task loop with executor future function.
src/transport/manager.c Passes runtime into peer transport creation; spawns accept via runtime; uses new blocking API.
src/transport/common/transport.c Removes common stop-tasks helper; drops RX mutex teardown.
src/transport/common/rx.c Refactors link receive into flow-specific helpers + deadline-based receive loop.
src/transport/common/read.c Keeps _z_read() but removes legacy _zp_read_task() dispatcher.
src/transport/common/lease.c Adds feature-guarded switch cases for join/keep-alive sending.
src/system/zephyr/system.c Adds task-id helpers and clock elapsed-since helpers.
src/system/zephyr/network.c Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event.
src/system/windows/system.c Adds task-id helpers and clock elapsed-since helpers.
src/system/windows/network.c Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event.
src/system/unix/system.c Adds task-id helpers and clock elapsed-since helpers.
src/system/unix/network.c Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event; tweaks TCP connect loop.
src/system/threadx/stm32/system.c Adds task-id helpers and clock elapsed-since helpers.
src/system/rpi_pico/system.c Adds task-id helpers and clock elapsed-since helpers.
src/system/rpi_pico/network.c Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event.
src/system/mbed/system.cpp Adds task-id helpers and clock elapsed-since helpers.
src/system/mbed/network.cpp Replaces non-blocking setter with _z_socket_set_blocking stub.
src/system/freertos/system.c Adds task-id helpers and clock elapsed-since helpers; rewires elapsed helpers.
src/system/freertos/lwip/network.c Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event.
src/system/freertos/freertos_plus_tcp/network.c Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event.
src/system/flipper/system.c Adds task-id helpers and clock elapsed-since helpers (task-id currently stubbed).
src/system/flipper/network.c Replaces non-blocking setter with _z_socket_set_blocking stub.
src/system/espidf/system.c Adds task-id helpers and clock elapsed-since helpers.
src/system/espidf/network.c Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event.
src/system/emscripten/system.c Adds elapsed-since helpers; refactors time elapsed implementation.
src/system/emscripten/network.c Replaces non-blocking setter with _z_socket_set_blocking stub.
src/system/arduino/opencr/system.c Adds task-id helpers and clock elapsed-since helpers (task-id currently stubbed).
src/system/arduino/opencr/network.cpp Replaces non-blocking setter with _z_socket_set_blocking stub.
src/system/arduino/esp32/system.c Adds task-id helpers and clock elapsed-since helpers.
src/system/arduino/esp32/network.cpp Replaces non-blocking setter with _z_socket_set_blocking; uses mt-lock wrapper in wait_event.
src/session/utils.c Adds runtime init/clear; removes legacy task/periodic scheduler setup/teardown; updates transport clear call.
src/session/query.c Adds executor task fn for periodic pending-query timeout processing.
src/api/advanced_publisher.c Replaces periodic scheduler usage with runtime futures for heartbeat; stores fut handle/period.
include/zenoh-pico/utils/scheduler.h Removes periodic scheduler header (deleted).
include/zenoh-pico/utils/result.h Adds new error codes for spawn failure and RX deadline expiry.
include/zenoh-pico/utils/mutex.h Adds MT-aware recursive-mutex lock/unlock wrappers for single-thread builds.
include/zenoh-pico/transport/unicast/transport.h Updates unicast transport clear signature.
include/zenoh-pico/transport/unicast/read.h Replaces thread task APIs with executor task fn declaration.
include/zenoh-pico/transport/unicast/lease.h Replaces thread task APIs with executor task fn declarations.
include/zenoh-pico/transport/unicast/accept.h Replaces accept task start/stop with executor task fn declaration.
include/zenoh-pico/transport/transport.h Removes RX mutex/task fields; adds transport state; updates clear signature; removes multicast _next_lease.
include/zenoh-pico/transport/raweth/read.h Replaces thread task APIs with executor task fn declaration.
include/zenoh-pico/transport/multicast/transport.h Updates multicast transport clear signature.
include/zenoh-pico/transport/multicast/read.h Replaces thread task APIs with executor task fn declaration.
include/zenoh-pico/transport/multicast/lease.h Replaces thread task APIs with executor task fn declarations.
include/zenoh-pico/transport/manager.h Adds runtime parameter to transport creation API.
include/zenoh-pico/transport/common/transport.h Removes common stop-tasks declaration.
include/zenoh-pico/transport/common/rx.h Updates _z_link_recv_t_msg signature to include recv deadline.
include/zenoh-pico/transport/common/read.h Removes legacy _zp_read_task declaration.
include/zenoh-pico/system/platform/zephyr.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/windows.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/void.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/unix.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/threadx/stm32.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/rpi_pico.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/mbed.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/freertos/lwip.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/freertos/freertos_plus_tcp.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/flipper.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/espidf.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/emscripten.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/arduino/opencr.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/platform/arduino/esp32.h Adds _z_task_id_t typedef.
include/zenoh-pico/system/common/platform.h Adds task-id API + elapsed-since API; renames socket blocking setter.
include/zenoh-pico/session/utils.h Adds platform include.
include/zenoh-pico/session/runtime.h Introduces runtime abstraction over executor/background executor + spawn/cancel/spin helpers.
include/zenoh-pico/session/query.h Declares pending-query timeout executor task fn.
include/zenoh-pico/config.h.in Adds transport accept/connect timeout configuration macros.
include/zenoh-pico/collections/background_executor.h Adds background executor API (multi-thread runtime backend).
include/zenoh-pico/api/types.h Removes periodic-task open option and periodic scheduler options struct.
include/zenoh-pico/api/primitives.h Removes periodic scheduler APIs; adds zp_spin_once for single-thread builds.
include/zenoh-pico/api/advanced_subscriber.h Switches periodic query tracking to a future handle.
include/zenoh-pico/api/advanced_publisher.h Switches heartbeat task tracking to a future handle + stores heartbeat period.
examples/zephyr/z_sub.c Removes explicit read/lease task start.
examples/zephyr/z_queryable.c Removes explicit read/lease task start.
examples/zephyr/z_pull.c Removes explicit read/lease task start.
examples/zephyr/z_pub.c Removes explicit read/lease task start.
examples/zephyr/z_get.c Removes explicit read/lease task start.
examples/windows/z_sub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/windows/z_sub.c Removes explicit read/lease task start.
examples/windows/z_queryable.c Removes explicit read/lease task start.
examples/windows/z_put.c Removes explicit read/lease task start.
examples/windows/z_pull.c Removes explicit read/lease task start.
examples/windows/z_pub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/windows/z_pub.c Removes explicit task start/stop.
examples/windows/z_pong.c Removes explicit read/lease task start.
examples/windows/z_ping.c Removes explicit read/lease task start.
examples/windows/z_info.c Removes explicit read/lease task start.
examples/windows/z_get.c Removes explicit read/lease task start.
examples/unix/c99/z_sub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/unix/c99/z_sub.c Removes explicit read/lease task start.
examples/unix/c99/z_queryable.c Removes explicit read/lease task start.
examples/unix/c99/z_put.c Removes explicit read/lease task start.
examples/unix/c99/z_pull.c Removes explicit read/lease task start.
examples/unix/c99/z_pub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/unix/c99/z_pub.c Removes explicit read/lease task start.
examples/unix/c99/z_pong.c Removes explicit read/lease task start.
examples/unix/c99/z_ping.c Removes explicit read/lease task start.
examples/unix/c99/z_info.c Removes explicit read/lease task start.
examples/unix/c99/z_get.c Removes explicit read/lease task start.
examples/unix/c11/z_sub_tls.c Removes explicit read/lease task start.
examples/unix/c11/z_sub_thr.c Removes explicit read/lease task start.
examples/unix/c11/z_sub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/unix/c11/z_sub_liveliness.c Removes explicit read/lease task start.
examples/unix/c11/z_sub_channel.c Removes explicit read/lease task start.
examples/unix/c11/z_sub_attachment.c Removes explicit read/lease task start.
examples/unix/c11/z_sub.c Removes explicit read/lease task start.
examples/unix/c11/z_queryable_lat.c Removes explicit read/lease task start.
examples/unix/c11/z_queryable_channel.c Removes explicit read/lease task start.
examples/unix/c11/z_queryable_attachment.c Removes explicit read/lease task start.
examples/unix/c11/z_queryable.c Removes explicit read/lease task start.
examples/unix/c11/z_querier.c Removes explicit read/lease task start.
examples/unix/c11/z_put.c Removes explicit read/lease task start.
examples/unix/c11/z_pull.c Removes explicit read/lease task start.
examples/unix/c11/z_pub_tls.c Removes explicit read/lease task start.
examples/unix/c11/z_pub_thr.c Removes explicit read/lease task start.
examples/unix/c11/z_pub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/unix/c11/z_pub_attachment.c Removes explicit read/lease task start.
examples/unix/c11/z_pub.c Removes explicit read/lease task start.
examples/unix/c11/z_pong.c Removes explicit read/lease task start.
examples/unix/c11/z_ping.c Removes explicit read/lease task start.
examples/unix/c11/z_liveliness.c Removes explicit read/lease task start.
examples/unix/c11/z_info.c Removes explicit read/lease task start.
examples/unix/c11/z_get_liveliness.c Removes explicit read/lease task start.
examples/unix/c11/z_get_lat.c Removes explicit read/lease task start.
examples/unix/c11/z_get_channel.c Removes explicit read/lease task start.
examples/unix/c11/z_get_attachment.c Removes explicit read/lease task start.
examples/unix/c11/z_get.c Removes explicit read/lease task start.
examples/unix/c11/z_advanced_sub.c Removes explicit read/lease/periodic scheduler task start.
examples/unix/c11/z_advanced_pub.c Removes explicit read/lease/periodic scheduler task start.
examples/threadx_stm32/z_sub.c Removes explicit read/lease task start.
examples/threadx_stm32/z_pub.c Removes explicit read/lease task start.
examples/rpi_pico/z_sub_thr.c Removes explicit read/lease task start.
examples/rpi_pico/z_sub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/rpi_pico/z_sub.c Removes explicit read/lease task start.
examples/rpi_pico/z_queryable.c Removes explicit read/lease task start.
examples/rpi_pico/z_put.c Removes explicit read/lease task start.
examples/rpi_pico/z_pull.c Removes explicit read/lease task start.
examples/rpi_pico/z_pub_thr.c Removes explicit read/lease task start.
examples/rpi_pico/z_pub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/rpi_pico/z_pub.c Removes explicit read/lease task start.
examples/rpi_pico/z_get.c Removes explicit read/lease task start.
examples/mbed/z_sub.cpp Removes explicit read/lease task start.
examples/mbed/z_queryable.cpp Removes explicit read/lease task start.
examples/mbed/z_pull.cpp Removes explicit read/lease task start.
examples/mbed/z_pub.cpp Removes explicit read/lease task start.
examples/mbed/z_get.cpp Removes explicit read/lease task start.
examples/freertos_plus_tcp/z_sub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/freertos_plus_tcp/z_sub.c Removes explicit read/lease task start.
examples/freertos_plus_tcp/z_queryable.c Removes explicit read/lease task start.
examples/freertos_plus_tcp/z_put.c Removes explicit read/lease task start.
examples/freertos_plus_tcp/z_pull.c Removes explicit read/lease task start.
examples/freertos_plus_tcp/z_pub_st.c Uses zp_spin_once loop for single-thread; tightens feature guard + error text.
examples/freertos_plus_tcp/z_pub.c Removes explicit task creation/start boilerplate for read/lease tasks.
examples/freertos_plus_tcp/z_get.c Removes explicit read/lease task start.
examples/espidf/z_sub.c Removes explicit read/lease task start.
examples/espidf/z_queryable.c Removes explicit read/lease task start.
examples/espidf/z_pull.c Removes explicit read/lease task start.
examples/espidf/z_pub.c Removes explicit read/lease task start.
examples/espidf/z_get.c Removes explicit read/lease task start.
examples/arduino/z_sub.ino Removes explicit read/lease task start.
examples/arduino/z_queryable.ino Removes explicit read/lease task start.
examples/arduino/z_pull.ino Removes explicit read/lease task start.
examples/arduino/z_pub.ino Removes explicit read/lease task start.
examples/arduino/z_get.ino Removes explicit read/lease task start.
docs/config.rst Documents new transport accept/connect timeout options (names currently inconsistent with macros).
docs/api.rst Replaces periodic scheduler docs reference with zp_spin_once.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@DenisBiryukov91 DenisBiryukov91 marked this pull request as ready for review March 20, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Existing things could work better

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants