27 #include "rcl/error_handling.h"
29 #include "rcutils/logging_macros.h"
30 #include "rmw/error_handling.h"
32 #include "rmw/event.h"
34 #include "./context_impl.h"
39 size_t subscription_index;
40 rmw_subscriptions_t rmw_subscriptions;
42 size_t guard_condition_index;
43 rmw_guard_conditions_t rmw_guard_conditions;
46 rmw_clients_t rmw_clients;
49 rmw_services_t rmw_services;
52 rmw_events_t rmw_events;
54 rmw_wait_set_t * rmw_wait_set;
68 .size_of_subscriptions = 0,
69 .guard_conditions = NULL,
70 .size_of_guard_conditions = 0,
74 .size_of_services = 0,
87 return wait_set && wait_set->
impl;
97 wait_set->
impl->allocator.deallocate(wait_set->
impl, wait_set->
impl->allocator.state);
98 wait_set->
impl = NULL;
105 size_t number_of_subscriptions,
106 size_t number_of_guard_conditions,
107 size_t number_of_timers,
108 size_t number_of_clients,
109 size_t number_of_services,
110 size_t number_of_events,
114 RCUTILS_LOG_DEBUG_NAMED(
115 ROS_PACKAGE_NAME,
"Initializing wait set with "
116 "'%zu' subscriptions, '%zu' guard conditions, '%zu' timers, '%zu' clients, '%zu' services",
117 number_of_subscriptions, number_of_guard_conditions, number_of_timers, number_of_clients,
124 RCL_SET_ERROR_MSG(
"wait_set already initialized, or memory was uninitialized.");
131 "the given context is not valid, "
132 "either rcl_init() was not called or rcl_shutdown() was called.");
138 RCL_CHECK_FOR_NULL_WITH_MSG(
141 wait_set->
impl->rmw_subscriptions.subscribers = NULL;
142 wait_set->
impl->rmw_subscriptions.subscriber_count = 0;
143 wait_set->
impl->rmw_guard_conditions.guard_conditions = NULL;
144 wait_set->
impl->rmw_guard_conditions.guard_condition_count = 0;
145 wait_set->
impl->rmw_clients.clients = NULL;
146 wait_set->
impl->rmw_clients.client_count = 0;
147 wait_set->
impl->rmw_services.services = NULL;
148 wait_set->
impl->rmw_services.service_count = 0;
149 wait_set->
impl->rmw_events.events = NULL;
150 wait_set->
impl->rmw_events.event_count = 0;
152 wait_set->
impl->context = context;
154 wait_set->
impl->allocator = allocator;
156 size_t num_conditions =
157 (2 * number_of_subscriptions) +
158 number_of_guard_conditions +
163 wait_set->
impl->rmw_wait_set = rmw_create_wait_set(&(context->
impl->
rmw_context), num_conditions);
164 if (!wait_set->
impl->rmw_wait_set) {
170 wait_set, number_of_subscriptions, number_of_guard_conditions, number_of_timers,
171 number_of_clients, number_of_services, number_of_events);
179 rmw_ret_t ret = rmw_destroy_wait_set(wait_set->
impl->rmw_wait_set);
180 if (ret != RMW_RET_OK) {
184 __wait_set_clean_up(wait_set);
195 rmw_ret_t ret = rmw_destroy_wait_set(wait_set->
impl->rmw_wait_set);
196 if (ret != RMW_RET_OK) {
197 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
200 __wait_set_clean_up(wait_set);
210 RCL_SET_ERROR_MSG(
"wait set is invalid");
214 *allocator = wait_set->
impl->allocator;
218 #define SET_ADD(Type) \
219 RCL_CHECK_ARGUMENT_FOR_NULL(wait_set, RCL_RET_INVALID_ARGUMENT); \
220 if (!wait_set->impl) { \
221 RCL_SET_ERROR_MSG("wait set is invalid"); \
222 return RCL_RET_WAIT_SET_INVALID; \
224 RCL_CHECK_ARGUMENT_FOR_NULL(Type, RCL_RET_INVALID_ARGUMENT); \
225 if (!(wait_set->impl->Type ## _index < wait_set->size_of_ ## Type ## s)) { \
226 RCL_SET_ERROR_MSG(#Type "s set is full"); \
227 return RCL_RET_WAIT_SET_FULL; \
229 size_t current_index = wait_set->impl->Type ## _index++; \
230 wait_set->Type ## s[current_index] = Type; \
232 if (NULL != index) { \
233 *index = current_index; \
236 #define SET_ADD_RMW(Type, RMWStorage, RMWCount) \
238 rmw_ ## Type ## _t * rmw_handle = rcl_ ## Type ## _get_rmw_handle(Type); \
239 RCL_CHECK_FOR_NULL_WITH_MSG( \
240 rmw_handle, rcl_get_error_string().str, return RCL_RET_ERROR); \
241 wait_set->impl->RMWStorage[current_index] = rmw_handle->data; \
242 wait_set->impl->RMWCount++;
244 #define SET_CLEAR(Type) \
246 if (NULL != wait_set->Type ## s) { \
248 (void *)wait_set->Type ## s, \
250 sizeof(rcl_ ## Type ## _t *) * wait_set->size_of_ ## Type ## s); \
251 wait_set->impl->Type ## _index = 0; \
255 #define SET_CLEAR_RMW(Type, RMWStorage, RMWCount) \
257 if (NULL != wait_set->impl->RMWStorage) { \
260 wait_set->impl->RMWStorage, \
262 sizeof(void *) * wait_set->impl->RMWCount); \
263 wait_set->impl->RMWCount = 0; \
267 #define SET_RESIZE(Type, ExtraDealloc, ExtraRealloc) \
269 rcl_allocator_t allocator = wait_set->impl->allocator; \
270 wait_set->size_of_ ## Type ## s = 0; \
271 wait_set->impl->Type ## _index = 0; \
272 if (0 == Type ## s_size) { \
273 if (wait_set->Type ## s) { \
274 allocator.deallocate((void *)wait_set->Type ## s, allocator.state); \
275 wait_set->Type ## s = NULL; \
279 wait_set->Type ## s = (const rcl_ ## Type ## _t **)allocator.reallocate( \
280 (void *)wait_set->Type ## s, sizeof(rcl_ ## Type ## _t *) * Type ## s_size, \
282 RCL_CHECK_FOR_NULL_WITH_MSG( \
283 wait_set->Type ## s, "allocating memory failed", return RCL_RET_BAD_ALLOC); \
284 memset((void *)wait_set->Type ## s, 0, sizeof(rcl_ ## Type ## _t *) * Type ## s_size); \
285 wait_set->size_of_ ## Type ## s = Type ## s_size; \
290 #define SET_RESIZE_RMW_DEALLOC(RMWStorage, RMWCount) \
292 if (wait_set->impl->RMWStorage) { \
293 allocator.deallocate((void *)wait_set->impl->RMWStorage, allocator.state); \
294 wait_set->impl->RMWStorage = NULL; \
295 wait_set->impl->RMWCount = 0; \
298 #define SET_RESIZE_RMW_REALLOC(Type, RMWStorage, RMWCount) \
300 wait_set->impl->RMWCount = 0; \
301 wait_set->impl->RMWStorage = (void **)allocator.reallocate( \
302 wait_set->impl->RMWStorage, sizeof(void *) * Type ## s_size, allocator.state); \
303 if (!wait_set->impl->RMWStorage) { \
304 allocator.deallocate((void *)wait_set->Type ## s, allocator.state); \
305 wait_set->Type ## s = NULL; \
306 wait_set->size_of_ ## Type ## s = 0; \
307 RCL_SET_ERROR_MSG("allocating memory failed"); \
308 return RCL_RET_BAD_ALLOC; \
310 memset(wait_set->impl->RMWStorage, 0, sizeof(void *) * Type ## s_size);
323 SET_ADD(subscription)
324 SET_ADD_RMW(subscription, rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count)
339 SET_CLEAR(subscription);
340 SET_CLEAR(guard_condition);
348 rmw_subscriptions.subscribers,
349 rmw_subscriptions.subscriber_count);
352 rmw_guard_conditions.guard_conditions,
353 rmw_guard_conditions.guard_condition_count);
357 rmw_clients.client_count);
360 rmw_services.services,
361 rmw_services.service_count);
365 rmw_events.event_count);
378 size_t subscriptions_size,
379 size_t guard_conditions_size,
382 size_t services_size,
389 SET_RESIZE_RMW_DEALLOC(
390 rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count),
391 SET_RESIZE_RMW_REALLOC(
392 subscription, rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count)
395 SET_RESIZE(guard_condition,;,;);
398 rmw_guard_conditions_t * rmw_gcs = &(wait_set->
impl->rmw_guard_conditions);
399 const size_t num_rmw_gc = guard_conditions_size + timers_size;
401 rmw_gcs->guard_condition_count = 0u;
402 if (0u == num_rmw_gc) {
403 if (rmw_gcs->guard_conditions) {
404 wait_set->
impl->allocator.deallocate(
405 (
void *)rmw_gcs->guard_conditions, wait_set->
impl->allocator.state);
406 rmw_gcs->guard_conditions = NULL;
409 rmw_gcs->guard_conditions = (
void **)wait_set->
impl->allocator.reallocate(
410 rmw_gcs->guard_conditions,
sizeof(
void *) * num_rmw_gc, wait_set->
impl->allocator.state);
411 if (!rmw_gcs->guard_conditions) {
413 wait_set->
impl->allocator.deallocate(
417 wait_set->
impl->allocator.deallocate(
418 (
void *)wait_set->
timers, wait_set->
impl->allocator.state);
421 RCL_SET_ERROR_MSG(
"allocating memory failed");
424 memset(rmw_gcs->guard_conditions, 0,
sizeof(
void *) * num_rmw_gc);
427 SET_RESIZE(timer,;,;);
430 SET_RESIZE_RMW_DEALLOC(
431 rmw_clients.clients, rmw_clients.client_count),
432 SET_RESIZE_RMW_REALLOC(
433 client, rmw_clients.clients, rmw_clients.client_count)
437 SET_RESIZE_RMW_DEALLOC(
438 rmw_services.services, rmw_services.service_count),
439 SET_RESIZE_RMW_REALLOC(
440 service, rmw_services.services, rmw_services.service_count)
444 SET_RESIZE_RMW_DEALLOC(
445 rmw_events.events, rmw_events.event_count),
446 SET_RESIZE_RMW_REALLOC(
447 event, rmw_events.events, rmw_events.event_count)
459 SET_ADD(guard_condition)
461 guard_condition, rmw_guard_conditions.guard_conditions,
462 rmw_guard_conditions.guard_condition_count)
476 if (NULL != guard_condition) {
480 RCL_CHECK_FOR_NULL_WITH_MSG(
481 rmw_handle, rcl_get_error_string().str,
return RCL_RET_ERROR);
482 wait_set->
impl->rmw_guard_conditions.guard_conditions[index] = rmw_handle->data;
494 SET_ADD_RMW(client, rmw_clients.clients, rmw_clients.client_count)
505 SET_ADD_RMW(service, rmw_services.services, rmw_services.service_count)
516 SET_ADD_RMW(event, rmw_events.events, rmw_events.event_count)
517 wait_set->
impl->rmw_events.events[current_index] = rmw_handle;
526 RCL_SET_ERROR_MSG(
"wait set is invalid");
537 RCL_SET_ERROR_MSG(
"wait set is empty");
542 rmw_time_t * timeout_argument = NULL;
543 rmw_time_t temporary_timeout_storage;
544 bool is_non_blocking = timeout == 0;
546 for (uint64_t t_idx = 0; t_idx < wait_set->
impl->timer_index; ++t_idx) {
547 if (!wait_set->
timers[t_idx]) {
550 rmw_guard_conditions_t * rmw_gcs = &(wait_set->
impl->rmw_guard_conditions);
552 if (NULL != rmw_gcs->guard_conditions[gc_idx]) {
554 rmw_gcs->guard_conditions[rmw_gcs->guard_condition_count] =
555 rmw_gcs->guard_conditions[gc_idx];
556 ++(rmw_gcs->guard_condition_count);
573 if (!is_non_blocking) {
574 for (
size_t t_idx = 0; t_idx < wait_set->
impl->timer_index; ++t_idx) {
575 if (!wait_set->
timers[t_idx]) {
586 bool timer_override_active =
false;
592 if (timer_override_active) {
594 bool override_timer_is_ready =
false;
600 if (override_timer_is_ready) {
602 is_non_blocking =
true;
614 int64_t next_call_time = INT64_MAX;
617 wait_set->
timers[t_idx] = NULL;
623 if (next_call_time < min_next_call_time[clock->
type]) {
624 clocks[clock->
type] = clock;
625 min_next_call_time[clock->
type] = next_call_time;
630 if (is_non_blocking) {
631 temporary_timeout_storage.sec = 0;
632 temporary_timeout_storage.nsec = 0;
633 timeout_argument = &temporary_timeout_storage;
635 bool has_valid_timeout = timeout > 0;
636 int64_t min_timeout = has_valid_timeout ? timeout : INT64_MAX;
640 if (clocks[i] == NULL) {
650 int64_t timer_timeout = min_next_call_time[i] - cur_time;
652 if (timer_timeout <= min_timeout) {
653 has_valid_timeout =
true;
654 min_timeout = timer_timeout;
659 if (min_timeout < 0) {
662 if (has_valid_timeout) {
663 temporary_timeout_storage.sec =
RCL_NS_TO_S(min_timeout);
664 temporary_timeout_storage.nsec = min_timeout % 1000000000;
665 timeout_argument = &temporary_timeout_storage;
670 rmw_ret_t ret = rmw_wait(
671 &wait_set->
impl->rmw_subscriptions,
672 &wait_set->
impl->rmw_guard_conditions,
673 &wait_set->
impl->rmw_services,
674 &wait_set->
impl->rmw_clients,
675 &wait_set->
impl->rmw_events,
676 wait_set->
impl->rmw_wait_set,
682 bool any_timer_is_ready =
false;
687 for (i = 0; i < wait_set->
impl->timer_index; ++i) {
688 if (!wait_set->
timers[i]) {
692 bool current_timer_is_ready =
false;
697 if (!current_timer_is_ready) {
698 wait_set->
timers[i] = NULL;
700 any_timer_is_ready =
true;
704 if (ret != RMW_RET_OK && ret != RMW_RET_TIMEOUT) {
705 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
710 bool is_ready = wait_set->
impl->rmw_subscriptions.subscribers[i] != NULL;
717 bool is_ready = wait_set->
impl->rmw_guard_conditions.guard_conditions[i] != NULL;
724 bool is_ready = wait_set->
impl->rmw_clients.clients[i] != NULL;
731 bool is_ready = wait_set->
impl->rmw_services.services[i] != NULL;
738 bool is_ready = wait_set->
impl->rmw_events.events[i] != NULL;
740 wait_set->
events[i] = NULL;
744 if (RMW_RET_TIMEOUT == ret && !any_timer_is_ready) {
#define RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement)
Check that the given allocator is initialized, or fail with a message.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_context_is_valid(const rcl_context_t *context)
Return true if the given context is currently valid, otherwise false.
RCL_PUBLIC RCL_WARN_UNUSED rmw_guard_condition_t * rcl_guard_condition_get_rmw_handle(const rcl_guard_condition_t *guard_condition)
Return the rmw guard condition handle.
Structure which encapsulates a ROS Client.
Encapsulation of a time source.
rcl_clock_type_t type
Clock type.
rmw_context_t rmw_context
rmw context.
Encapsulates the non-global state of an init/shutdown cycle.
rcl_context_impl_t * impl
Implementation specific pointer.
Structure which encapsulates a ROS QoS event handle.
Handle for a rcl guard condition.
Structure which encapsulates a ROS Service.
Structure which encapsulates a ROS Subscription.
rcl_subscription_impl_t * impl
Pointer to the subscription implementation.
Structure which encapsulates a ROS Timer.
Container for subscription's, guard condition's, etc to be waited on.
const rcl_event_t ** events
Storage for event pointers.
const rcl_timer_t ** timers
Storage for timer pointers.
size_t size_of_timers
Number of timers.
size_t size_of_events
Number of events.
size_t size_of_subscriptions
Number of subscriptions.
const rcl_service_t ** services
Storage for service pointers.
const rcl_client_t ** clients
Storage for client pointers.
const rcl_subscription_t ** subscriptions
Storage for subscription pointers.
size_t size_of_guard_conditions
Number of guard_conditions.
rcl_wait_set_impl_t * impl
Implementation specific storage.
size_t size_of_clients
Number of clients.
const rcl_guard_condition_t ** guard_conditions
Storage for guard condition pointers.
size_t size_of_services
Number of services.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_get_now(rcl_clock_t *clock, rcl_time_point_value_t *time_point_value)
Fill the time point value with the current value of the associated clock.
#define RCL_NS_TO_S
Convenience macro to convert nanoseconds to seconds.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_is_enabled_ros_time_override(rcl_clock_t *clock, bool *is_enabled)
Check if the RCL_ROS_TIME time source has the override enabled.
@ RCL_ROS_TIME
Use ROS time.
@ RCL_SYSTEM_TIME
Use system time.
@ RCL_STEADY_TIME
Use a steady clock time.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_get_next_call_time(const rcl_timer_t *timer, int64_t *next_call_time)
Retrieve the time when the next call to rcl_timer_call() shall occur.
RCL_PUBLIC RCL_WARN_UNUSED rcl_guard_condition_t * rcl_timer_get_guard_condition(const rcl_timer_t *timer)
Retrieve a guard condition used by the timer to wake the waitset when using ROSTime.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_clock(const rcl_timer_t *timer, rcl_clock_t **clock)
Retrieve the clock of the timer.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_timer_is_ready(const rcl_timer_t *timer, bool *is_ready)
Calculates whether or not the timer should be called.
#define RCL_RET_WAIT_SET_EMPTY
Given rcl_wait_set_t is empty return code.
#define RCL_RET_WAIT_SET_INVALID
Invalid rcl_wait_set_t given return code.
#define RCL_RET_NOT_INIT
rcl_init() not yet called return code.
#define RCL_RET_ALREADY_INIT
rcl_init() already called return code.
#define RCL_RET_OK
Success return code.
#define RCL_RET_BAD_ALLOC
Failed to allocate memory return code.
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
#define RCL_RET_ERROR
Unspecified error return code.
#define RCL_RET_TIMER_CANCELED
Given timer was canceled return code.
#define RCL_RET_TIMEOUT
Timeout occurred return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_subscription(rcl_wait_set_t *wait_set, const rcl_subscription_t *subscription, size_t *index)
Store a pointer to the given subscription in the next empty spot in the set.
RCL_PUBLIC bool rcl_wait_set_is_valid(const rcl_wait_set_t *wait_set)
Return true if the wait set is valid, else false.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_service(rcl_wait_set_t *wait_set, const rcl_service_t *service, size_t *index)
Store a pointer to the service in the next empty spot in the set.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_init(rcl_wait_set_t *wait_set, size_t number_of_subscriptions, size_t number_of_guard_conditions, size_t number_of_timers, size_t number_of_clients, size_t number_of_services, size_t number_of_events, rcl_context_t *context, rcl_allocator_t allocator)
Initialize a rcl wait set with space for items to be waited on.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_clear(rcl_wait_set_t *wait_set)
Remove (sets to NULL) all entities in the wait set.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_timer(rcl_wait_set_t *wait_set, const rcl_timer_t *timer, size_t *index)
Store a pointer to the timer in the next empty spot in the set.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_client(rcl_wait_set_t *wait_set, const rcl_client_t *client, size_t *index)
Store a pointer to the client in the next empty spot in the set.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_fini(rcl_wait_set_t *wait_set)
Finalize a rcl wait set.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait(rcl_wait_set_t *wait_set, int64_t timeout)
Block until the wait set is ready or until the timeout has been exceeded.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_resize(rcl_wait_set_t *wait_set, size_t subscriptions_size, size_t guard_conditions_size, size_t timers_size, size_t clients_size, size_t services_size, size_t events_size)
Reallocate space for entities in the wait set.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_get_allocator(const rcl_wait_set_t *wait_set, rcl_allocator_t *allocator)
Retrieve the wait set's allocator.
RCL_PUBLIC RCL_WARN_UNUSED rcl_wait_set_t rcl_get_zero_initialized_wait_set(void)
Return a rcl_wait_set_t struct with members set to NULL.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_event(rcl_wait_set_t *wait_set, const rcl_event_t *event, size_t *index)
Store a pointer to the event in the next empty spot in the set.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_guard_condition(rcl_wait_set_t *wait_set, const rcl_guard_condition_t *guard_condition, size_t *index)
Store a pointer to the guard condition in the next empty spot in the set.