@@ -68,21 +68,70 @@ 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, allowing 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+ // / @note With Z_FEATURE_PERIODIC_TASKS enabled only.
84+ bool auto_start_periodic_task = true ;
85+ #endif
86+ // / @brief Create default auto-start settings.
87+ static BackgroundTasksAutoStartOptions create_default () { return {}; }
88+ };
89+
7290 // / @brief If ``true``, start background threads which handle the network
7391 // / traffic. If false, the threads should be called manually with ``Session::start_read_task``,
7492 // / ``Session::start_lease_task`` and ``Session::start_periodic_scheduler_task``
7593 // / or methods ``Session::read``, ``Session::send_keep_alive``,
7694 // / ``Session::send_join`` and ``Session::process_periodic_tasks`` should be called in loop.
77- // / @note Zenoh-pico only.
78- bool start_background_tasks = true ;
95+ // / If contains ``BackgroundTasksAutoStartOptions`` value, only enabled tasks will start and the
96+ // / remaining ones will need to be started or triggered manually.
97+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD enabled only.
98+ std::variant<bool , BackgroundTasksAutoStartOptions> start_background_tasks = true ;
7999#endif
100+ // / @name Methods
101+
102+ // / @brief Create default option settings.
80103 static SessionOptions create_default () { return {}; }
104+
105+ private:
106+ friend struct interop ::detail::Converter;
107+ ::z_open_options_t to_c_opts () {
108+ z_open_options_t opts;
109+ z_open_options_default (&opts);
110+ #if defined(ZENOHCXX_ZENOHPICO) && Z_FEATURE_MULTI_THREAD == 1
111+ std::visit (
112+ detail::commons::overloaded{[&opts](const SessionOptions::BackgroundTasksAutoStartOptions& tasks) {
113+ opts.auto_start_read_task = tasks.auto_start_read_task ;
114+ opts.auto_start_lease_task = tasks.auto_start_lease_task ;
115+ #if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
116+ opts.auto_start_periodic_task = tasks.auto_start_periodic_task ;
117+ #endif
118+ },
119+ [&opts](const bool & start_all) {
120+ opts.auto_start_read_task = start_all;
121+ opts.auto_start_lease_task = start_all;
122+ #if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
123+ opts.auto_start_periodic_task = start_all;
124+ #endif
125+ }},
126+ start_background_tasks);
127+ #endif
128+ return opts;
129+ }
81130 };
82131
83132 // / @brief Options to be passed when closing a ``Session``.
84133 struct SessionCloseOptions {
85- // / @name Fields
134+ // / @name Methods
86135 static SessionCloseOptions create_default () { return {}; }
87136 };
88137
@@ -95,28 +144,9 @@ class Session : public Owned<::z_owned_session_t> {
95144 // / thrown in case of error.
96145 Session (Config&& config, SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr )
97146 : Owned(nullptr ) {
98- __ZENOH_RESULT_CHECK (::z_open (&this ->_0 , interop::as_moved_c_ptr (config), nullptr ), err,
147+ z_open_options_t opts = interop::detail::Converter::to_c_opts (options);
148+ __ZENOH_RESULT_CHECK (::z_open (&this ->_0 , interop::as_moved_c_ptr (config), &opts), err,
99149 " 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
120150 }
121151
122152#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API)
@@ -875,11 +905,12 @@ class Session : public Owned<::z_owned_session_t> {
875905 " Failed to fetch peer Ids" );
876906 return out;
877907 }
878- #ifdef ZENOHCXX_ZENOHPICO
908+ #if defined(ZENOHCXX_ZENOHPICO)
909+ #if Z_FEATURE_MULTI_THREAD == 1
879910 // / @brief Start a separate task to read from the network and process the messages as soon as they are received.
880911 // / @param err if not null, the result code will be written to this location, otherwise ZException exception will be
881912 // / thrown in case of error.
882- // / @note Zenoh-pico only.
913+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD enabled only.
883914 void start_read_task (ZResult* err = nullptr ) {
884915 __ZENOH_RESULT_CHECK (zp_start_read_task (interop::as_loaned_c_ptr (*this ), nullptr ), err,
885916 " Failed to start read task" );
@@ -888,7 +919,7 @@ class Session : public Owned<::z_owned_session_t> {
888919 // / @brief Stop the read task.
889920 // / @param err if not null, the result code will be written to this location, otherwise ZException exception will be
890921 // / thrown in case of error.
891- // / @note Zenoh-pico only.
922+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD enabled only.
892923 void stop_read_task (ZResult* err = nullptr ) {
893924 __ZENOH_RESULT_CHECK (zp_stop_read_task (interop::as_loaned_c_ptr (*this )), err, " Failed to stop read task" );
894925 }
@@ -898,7 +929,7 @@ class Session : public Owned<::z_owned_session_t> {
898929 // / periodically sends the Join messages.
899930 // / @param err if not null, the result code will be written to this location, otherwise ZException exception will be
900931 // / thrown in case of error.
901- // / @note Zenoh-pico only.
932+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD enabled only.
902933 void start_lease_task (ZResult* err = nullptr ) {
903934 __ZENOH_RESULT_CHECK (zp_start_lease_task (interop::as_loaned_c_ptr (*this ), NULL ), err,
904935 " Failed to start lease task" );
@@ -907,17 +938,27 @@ class Session : public Owned<::z_owned_session_t> {
907938 // / @brief Stop the lease task.
908939 // / @param err if not null, the result code will be written to this location, otherwise ZException exception will be
909940 // / thrown in case of error.
910- // / @note Zenoh-pico only.
941+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD enabled only.
911942 void stop_lease_task (ZResult* err = nullptr ) {
912943 __ZENOH_RESULT_CHECK (zp_stop_lease_task (interop::as_loaned_c_ptr (*this )), err, " Failed to stop lease task" );
913944 }
914945
946+ // / @brief Verify if read task is currently running.
947+ // / @return ``true`` if read task is running, ``false`` otherwise.
948+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD enabled only.
949+ bool is_read_task_running () const { return zp_read_task_is_running (interop::as_loaned_c_ptr (*this )); }
950+
951+ // / @brief Verify if lease task is currently running.
952+ // / @return ``true`` if read task is running, ``false`` otherwise.
953+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD enabled only.
954+ bool is_lease_task_running () const { return zp_lease_task_is_running (interop::as_loaned_c_ptr (*this )); }
955+
915956#if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
916957 // / @brief Start the periodic scheduler task. The periodic scheduler task executes registered periodic jobs
917958 // / according to their configured intervals. Jobs are added and removed via the scheduler API.
918959 // / @param err if not null, the result code will be written to this location, otherwise ZException exception will be
919960 // / thrown in case of error.
920- // / @note Zenoh-pico only.
961+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD and Z_FEATURE_PERIODIC_TASKS enabled only.
921962 void start_periodic_scheduler_task (ZResult* err = nullptr ) {
922963 __ZENOH_RESULT_CHECK (zp_start_periodic_scheduler_task (interop::as_loaned_c_ptr (*this ), NULL ), err,
923964 " Failed to start periodic scheduler task" );
@@ -926,15 +967,26 @@ class Session : public Owned<::z_owned_session_t> {
926967 // / @brief Stop the periodic scheduler task.
927968 // / @param err if not null, the result code will be written to this location, otherwise ZException exception will be
928969 // / thrown in case of error.
929- // / @note Zenoh-pico only.
970+ // / @note Zenoh-pico with Z_FEATURE_MULTI_THREAD and Z_FEATURE_PERIODIC_TASKS enabled only.
930971 void stop_periodic_scheduler_task (ZResult* err = nullptr ) {
931972 __ZENOH_RESULT_CHECK (zp_stop_periodic_scheduler_task (interop::as_loaned_c_ptr (*this )), err,
932973 " Failed to stop periodic scheduler task" );
933974 }
934975
976+ // / @brief Verify if periodic scheduler task is currently running.
977+ // / @return ``true`` if read task is running, ``false`` otherwise.
978+ // / @note Zenoh-pico with _FEATURE_MULTI_THREAD and Z_FEATURE_PERIODIC_TASKS enabled only.
979+ bool is_periodic_scheduler_task_running () const {
980+ return zp_periodic_scheduler_task_is_running (interop::as_loaned_c_ptr (*this ));
981+ }
982+ #endif
983+ #endif
984+
985+ #if defined(Z_FEATURE_UNSTABLE_API) && Z_FEATURE_PERIODIC_TASKS == 1
935986 // / @brief Process outstanding periodic tasks.
936987 // / @param err if not null, the result code will be written to this location, otherwise ZException exception will be
937988 // / thrown in case of error.
989+ // / @note Zenoh-pico with Z_FEATURE_PERIODIC_TASKS enabled only.
938990 void process_periodic_tasks (ZResult* err = nullptr ) {
939991 __ZENOH_RESULT_CHECK (zp_process_periodic_tasks (interop::as_loaned_c_ptr (*this )), err,
940992 " Failed to process periodic tasks" );
0 commit comments