Skip to content

Commit 4172a9d

Browse files
add support for task auto start using zenoh-pico options
1 parent e600b43 commit 4172a9d

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

docs/Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2353,7 +2353,7 @@ INCLUDE_FILE_PATTERNS =
23532353
# recursively expanded use the := operator instead of the = operator.
23542354
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
23552355

2356-
PREDEFINED = ZENOHCXX_ZENOHPICO ZENOHCXX_ZENOHC Z_FEATURE_SHARED_MEMORY Z_FEATURE_UNSTABLE_API __DOXYGEN__
2356+
PREDEFINED = ZENOHCXX_ZENOHPICO ZENOHCXX_ZENOHC Z_FEATURE_SHARED_MEMORY Z_FEATURE_UNSTABLE_API Z_FEATURE_MULTI_THREAD=1 __DOXYGEN__
23572357

23582358
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
23592359
# tag can be used to specify a list of macro names that should be expanded. The

include/zenoh/api/session.hxx

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,69 @@ class Session : public Owned<::z_owned_session_t> {
6868
/// @brief Options to be passed when opening a ``Session``.
6969
struct SessionOptions {
7070
/// @name Fields
71-
#ifdef ZENOHCXX_ZENOHPICO
71+
#if defined(ZENOHCXX_ZENOHPICO) && Z_FEATURE_MULTI_THREAD == 1
72+
/// @brief List of background tasks to auto-start, enabling per task granularity.
73+
/// @note Zenoh-pico only.
74+
struct BackgroundTasksAutoStartOptions {
75+
/// @name Fields
76+
77+
/// Auto-start read task
78+
bool auto_start_read_task = true;
79+
/// Auto-start lease task
80+
bool auto_start_lease_task = true;
81+
#if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
82+
/// Auto-start periodic scheduler task
83+
bool auto_start_periodic_task = true;
84+
#endif
85+
/// @brief Create default auto-start settings.
86+
static BackgroundTasksAutoStartOptions create_default() { return {}; }
87+
};
88+
7289
/// @brief If ``true``, start background threads which handle the network
7390
/// traffic. If false, the threads should be called manually with ``Session::start_read_task``,
7491
/// ``Session::start_lease_task`` and ``Session::start_periodic_scheduler_task``
7592
/// or methods ``Session::read``, ``Session::send_keep_alive``,
7693
/// ``Session::send_join`` and ``Session::process_periodic_tasks`` should be called in loop.
94+
/// If contains ``BackgroundTasksAutoStartOptions`` value, only enabled tasks will start and the
95+
/// remaining ones will need to be started or triggered manually.
7796
/// @note Zenoh-pico only.
78-
bool start_background_tasks = true;
97+
std::variant<bool, BackgroundTasksAutoStartOptions> start_background_tasks = true;
7998
#endif
99+
/// @name Methods
100+
101+
/// @brief Create default option settings.
80102
static SessionOptions create_default() { return {}; }
103+
104+
private:
105+
friend struct interop::detail::Converter;
106+
::z_open_options_t to_c_opts() {
107+
z_open_options_t opts;
108+
z_open_options_default(&opts);
109+
#if defined(ZENOHCXX_ZENOHPICO) && Z_FEATURE_MULTI_THREAD == 1
110+
std::visit(
111+
detail::commons::overloaded{[&opts](const SessionOptions::BackgroundTasksAutoStartOptions& tasks) {
112+
opts.auto_start_read_task = tasks.auto_start_read_task;
113+
opts.auto_start_lease_task = tasks.auto_start_lease_task;
114+
#if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
115+
opts.auto_start_periodic_task = tasks.auto_start_periodic_task;
116+
#endif
117+
},
118+
[&opts](const bool& start_all) {
119+
opts.auto_start_read_task = start_all;
120+
opts.auto_start_lease_task = start_all;
121+
#if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
122+
opts.auto_start_periodic_task = start_all;
123+
#endif
124+
}},
125+
start_background_tasks);
126+
#endif
127+
return opts;
128+
}
81129
};
82130

83131
/// @brief Options to be passed when closing a ``Session``.
84132
struct SessionCloseOptions {
85-
/// @name Fields
133+
/// @name Methods
86134
static SessionCloseOptions create_default() { return {}; }
87135
};
88136

@@ -95,28 +143,9 @@ class Session : public Owned<::z_owned_session_t> {
95143
/// thrown in case of error.
96144
Session(Config&& config, SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr)
97145
: Owned(nullptr) {
98-
__ZENOH_RESULT_CHECK(::z_open(&this->_0, interop::as_moved_c_ptr(config), nullptr), err,
146+
z_open_options_t opts = interop::detail::Converter::to_c_opts(options);
147+
__ZENOH_RESULT_CHECK(::z_open(&this->_0, interop::as_moved_c_ptr(config), &opts), err,
99148
"Failed to open session");
100-
#ifdef ZENOHCXX_ZENOHPICO
101-
if (err != nullptr && *err != Z_OK) return;
102-
if (options.start_background_tasks) {
103-
ZResult err_inner;
104-
this->start_read_task(&err_inner);
105-
if (err_inner == Z_OK) {
106-
this->start_lease_task(&err_inner);
107-
}
108-
#if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
109-
if (err_inner == Z_OK) {
110-
this->start_periodic_scheduler_task(&err_inner);
111-
}
112-
#endif
113-
if (err_inner == Z_OK) return;
114-
::z_drop(::z_move(this->_0));
115-
__ZENOH_RESULT_CHECK(err_inner, err, "Failed to start background tasks");
116-
}
117-
#else
118-
(void)options;
119-
#endif
120149
}
121150

122151
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API)

include/zenoh/detail/commons.hxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ auto make_transform_iterator(It const& it, F&& f) {
5454
return TransformIterator<It, F>(it, std::forward<F>(f));
5555
}
5656

57+
template <class... Ts>
58+
struct overloaded : Ts... {
59+
using Ts::operator()...;
60+
};
61+
62+
// Some compilers might require this explicit deduction guide
63+
template <class... Ts>
64+
overloaded(Ts...) -> overloaded<Ts...>;
65+
5766
} // namespace zenoh::detail::commons

0 commit comments

Comments
 (0)