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;
74 return wait_set && wait_set->
impl;
80 size_t number_of_subscriptions,
81 size_t number_of_guard_conditions,
82 size_t number_of_timers,
83 size_t number_of_clients,
84 size_t number_of_services,
85 size_t number_of_events,
89 RCUTILS_LOG_DEBUG_NAMED(
90 ROS_PACKAGE_NAME,
"Initializing wait set with "
91 "'%zu' subscriptions, '%zu' guard conditions, '%zu' timers, '%zu' clients, '%zu' services",
92 number_of_subscriptions, number_of_guard_conditions, number_of_timers, number_of_clients,
99 RCL_SET_ERROR_MSG(
"wait_set already initialized, or memory was uninitialized.");
106 "the given context is not valid, "
107 "either rcl_init() was not called or rcl_shutdown() was called.");
113 RCL_CHECK_FOR_NULL_WITH_MSG(
116 wait_set->
impl->rmw_subscriptions.subscribers = NULL;
117 wait_set->
impl->rmw_subscriptions.subscriber_count = 0;
118 wait_set->
impl->rmw_guard_conditions.guard_conditions = NULL;
119 wait_set->
impl->rmw_guard_conditions.guard_condition_count = 0;
120 wait_set->
impl->rmw_clients.clients = NULL;
121 wait_set->
impl->rmw_clients.client_count = 0;
122 wait_set->
impl->rmw_services.services = NULL;
123 wait_set->
impl->rmw_services.service_count = 0;
124 wait_set->
impl->rmw_events.events = NULL;
125 wait_set->
impl->rmw_events.event_count = 0;
127 wait_set->
impl->context = context;
129 wait_set->
impl->allocator = allocator;
131 size_t num_conditions =
132 (2 * number_of_subscriptions) +
133 number_of_guard_conditions +
138 wait_set->
impl->rmw_wait_set = rmw_create_wait_set(&(context->
impl->
rmw_context), num_conditions);
139 if (!wait_set->
impl->rmw_wait_set) {
146 wait_set, number_of_subscriptions, number_of_guard_conditions, number_of_timers,
147 number_of_clients, number_of_services, number_of_events);
154 if (wait_set->
impl->rmw_wait_set != NULL) {
155 rmw_ret_t rmw_ret = rmw_destroy_wait_set(wait_set->
impl->rmw_wait_set);
156 if (rmw_ret != RMW_RET_OK) {
160 allocator.deallocate(wait_set->
impl, wait_set->
impl->allocator.state);
161 wait_set->
impl = NULL;
173 rmw_ret_t ret = rmw_destroy_wait_set(wait_set->
impl->rmw_wait_set);
174 if (ret != RMW_RET_OK) {
175 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
182 result = resize_result;
184 if (wait_set->
impl) {
185 wait_set->
impl->allocator.deallocate(wait_set->
impl, wait_set->
impl->allocator.state);
186 wait_set->
impl = NULL;
197 RCL_SET_ERROR_MSG(
"wait set is invalid");
201 *allocator = wait_set->
impl->allocator;
205 #define SET_ADD(Type) \
206 RCL_CHECK_ARGUMENT_FOR_NULL(wait_set, RCL_RET_INVALID_ARGUMENT); \
207 if (!wait_set->impl) { \
208 RCL_SET_ERROR_MSG("wait set is invalid"); \
209 return RCL_RET_WAIT_SET_INVALID; \
211 RCL_CHECK_ARGUMENT_FOR_NULL(Type, RCL_RET_INVALID_ARGUMENT); \
212 if (!(wait_set->impl->Type ## _index < wait_set->size_of_ ## Type ## s)) { \
213 RCL_SET_ERROR_MSG(#Type "s set is full"); \
214 return RCL_RET_WAIT_SET_FULL; \
216 size_t current_index = wait_set->impl->Type ## _index++; \
217 wait_set->Type ## s[current_index] = Type; \
219 if (NULL != index) { \
220 *index = current_index; \
223 #define SET_ADD_RMW(Type, RMWStorage, RMWCount) \
225 rmw_ ## Type ## _t * rmw_handle = rcl_ ## Type ## _get_rmw_handle(Type); \
226 RCL_CHECK_FOR_NULL_WITH_MSG( \
227 rmw_handle, rcl_get_error_string().str, return RCL_RET_ERROR); \
228 wait_set->impl->RMWStorage[current_index] = rmw_handle->data; \
229 wait_set->impl->RMWCount++;
231 #define SET_CLEAR(Type) \
233 if (NULL != wait_set->Type ## s) { \
235 (void *)wait_set->Type ## s, \
237 sizeof(rcl_ ## Type ## _t *) * wait_set->size_of_ ## Type ## s); \
238 wait_set->impl->Type ## _index = 0; \
242 #define SET_CLEAR_RMW(Type, RMWStorage, RMWCount) \
244 if (NULL != wait_set->impl->RMWStorage) { \
247 wait_set->impl->RMWStorage, \
249 sizeof(void *) * wait_set->impl->RMWCount); \
250 wait_set->impl->RMWCount = 0; \
254 #define SET_RESIZE(Type, ExtraDealloc, ExtraRealloc) \
256 rcl_allocator_t allocator = wait_set->impl->allocator; \
257 wait_set->size_of_ ## Type ## s = 0; \
258 wait_set->impl->Type ## _index = 0; \
259 if (0 == Type ## s_size) { \
260 if (wait_set->Type ## s) { \
261 allocator.deallocate((void *)wait_set->Type ## s, allocator.state); \
262 wait_set->Type ## s = NULL; \
266 wait_set->Type ## s = (const rcl_ ## Type ## _t **)allocator.reallocate( \
267 (void *)wait_set->Type ## s, sizeof(rcl_ ## Type ## _t *) * Type ## s_size, \
269 RCL_CHECK_FOR_NULL_WITH_MSG( \
270 wait_set->Type ## s, "allocating memory failed", return RCL_RET_BAD_ALLOC); \
271 memset((void *)wait_set->Type ## s, 0, sizeof(rcl_ ## Type ## _t *) * Type ## s_size); \
272 wait_set->size_of_ ## Type ## s = Type ## s_size; \
277 #define SET_RESIZE_RMW_DEALLOC(RMWStorage, RMWCount) \
279 if (wait_set->impl->RMWStorage) { \
280 allocator.deallocate((void *)wait_set->impl->RMWStorage, allocator.state); \
281 wait_set->impl->RMWStorage = NULL; \
282 wait_set->impl->RMWCount = 0; \
285 #define SET_RESIZE_RMW_REALLOC(Type, RMWStorage, RMWCount) \
287 wait_set->impl->RMWCount = 0; \
288 wait_set->impl->RMWStorage = (void **)allocator.reallocate( \
289 wait_set->impl->RMWStorage, sizeof(void *) * Type ## s_size, allocator.state); \
290 if (!wait_set->impl->RMWStorage) { \
291 allocator.deallocate((void *)wait_set->Type ## s, allocator.state); \
292 wait_set->Type ## s = NULL; \
293 wait_set->size_of_ ## Type ## s = 0; \
294 RCL_SET_ERROR_MSG("allocating memory failed"); \
295 return RCL_RET_BAD_ALLOC; \
297 memset(wait_set->impl->RMWStorage, 0, sizeof(void *) * Type ## s_size);
310 SET_ADD(subscription)
311 SET_ADD_RMW(subscription, rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count)
326 SET_CLEAR(subscription);
327 SET_CLEAR(guard_condition);
335 rmw_subscriptions.subscribers,
336 rmw_subscriptions.subscriber_count);
339 rmw_guard_conditions.guard_conditions,
340 rmw_guard_conditions.guard_condition_count);
344 rmw_clients.client_count);
347 rmw_services.services,
348 rmw_services.service_count);
352 rmw_events.event_count);
365 size_t subscriptions_size,
366 size_t guard_conditions_size,
369 size_t services_size,
376 SET_RESIZE_RMW_DEALLOC(
377 rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count),
378 SET_RESIZE_RMW_REALLOC(
379 subscription, rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count)
382 SET_RESIZE(guard_condition,;,;);
385 rmw_guard_conditions_t * rmw_gcs = &(wait_set->
impl->rmw_guard_conditions);
386 const size_t num_rmw_gc = guard_conditions_size + timers_size;
388 rmw_gcs->guard_condition_count = 0u;
389 if (0u == num_rmw_gc) {
390 if (rmw_gcs->guard_conditions) {
391 wait_set->
impl->allocator.deallocate(
392 (
void *)rmw_gcs->guard_conditions, wait_set->
impl->allocator.state);
393 rmw_gcs->guard_conditions = NULL;
396 rmw_gcs->guard_conditions = (
void **)wait_set->
impl->allocator.reallocate(
397 rmw_gcs->guard_conditions,
sizeof(
void *) * num_rmw_gc, wait_set->
impl->allocator.state);
398 if (!rmw_gcs->guard_conditions) {
400 wait_set->
impl->allocator.deallocate(
404 wait_set->
impl->allocator.deallocate(
405 (
void *)wait_set->
timers, wait_set->
impl->allocator.state);
408 RCL_SET_ERROR_MSG(
"allocating memory failed");
411 memset(rmw_gcs->guard_conditions, 0,
sizeof(
void *) * num_rmw_gc);
414 SET_RESIZE(timer,;,;);
417 SET_RESIZE_RMW_DEALLOC(
418 rmw_clients.clients, rmw_clients.client_count),
419 SET_RESIZE_RMW_REALLOC(
420 client, rmw_clients.clients, rmw_clients.client_count)
424 SET_RESIZE_RMW_DEALLOC(
425 rmw_services.services, rmw_services.service_count),
426 SET_RESIZE_RMW_REALLOC(
427 service, rmw_services.services, rmw_services.service_count)
431 SET_RESIZE_RMW_DEALLOC(
432 rmw_events.events, rmw_events.event_count),
433 SET_RESIZE_RMW_REALLOC(
434 event, rmw_events.events, rmw_events.event_count)
446 SET_ADD(guard_condition)
448 guard_condition, rmw_guard_conditions.guard_conditions,
449 rmw_guard_conditions.guard_condition_count)
463 if (NULL != guard_condition) {
467 RCL_CHECK_FOR_NULL_WITH_MSG(
468 rmw_handle, rcl_get_error_string().str,
return RCL_RET_ERROR);
469 wait_set->
impl->rmw_guard_conditions.guard_conditions[index] = rmw_handle->data;
481 SET_ADD_RMW(client, rmw_clients.clients, rmw_clients.client_count)
492 SET_ADD_RMW(service, rmw_services.services, rmw_services.service_count)
503 SET_ADD_RMW(event, rmw_events.events, rmw_events.event_count)
504 wait_set->
impl->rmw_events.events[current_index] = rmw_handle;
513 RCL_SET_ERROR_MSG(
"wait set is invalid");
524 RCL_SET_ERROR_MSG(
"wait set is empty");
529 rmw_time_t * timeout_argument = NULL;
530 rmw_time_t temporary_timeout_storage;
531 bool is_non_blocking = timeout == 0;
533 for (uint64_t t_idx = 0; t_idx < wait_set->
impl->timer_index; ++t_idx) {
534 if (!wait_set->
timers[t_idx]) {
537 rmw_guard_conditions_t * rmw_gcs = &(wait_set->
impl->rmw_guard_conditions);
539 if (NULL != rmw_gcs->guard_conditions[gc_idx]) {
541 rmw_gcs->guard_conditions[rmw_gcs->guard_condition_count] =
542 rmw_gcs->guard_conditions[gc_idx];
543 ++(rmw_gcs->guard_condition_count);
560 if (!is_non_blocking) {
561 for (
size_t t_idx = 0; t_idx < wait_set->
impl->timer_index; ++t_idx) {
562 if (!wait_set->
timers[t_idx]) {
573 bool timer_override_active =
false;
579 if (timer_override_active) {
581 bool override_timer_is_ready =
false;
587 if (override_timer_is_ready) {
589 is_non_blocking =
true;
601 int64_t next_call_time = INT64_MAX;
604 wait_set->
timers[t_idx] = NULL;
610 if (next_call_time < min_next_call_time[clock->
type]) {
611 clocks[clock->
type] = clock;
612 min_next_call_time[clock->
type] = next_call_time;
617 if (is_non_blocking) {
618 temporary_timeout_storage.sec = 0;
619 temporary_timeout_storage.nsec = 0;
620 timeout_argument = &temporary_timeout_storage;
622 bool has_valid_timeout = timeout > 0;
623 int64_t min_timeout = has_valid_timeout ? timeout : INT64_MAX;
627 if (clocks[i] == NULL) {
637 int64_t timer_timeout = min_next_call_time[i] - cur_time;
639 if (timer_timeout <= min_timeout) {
640 has_valid_timeout =
true;
641 min_timeout = timer_timeout;
646 if (min_timeout < 0) {
649 if (has_valid_timeout) {
650 temporary_timeout_storage.sec =
RCL_NS_TO_S(min_timeout);
651 temporary_timeout_storage.nsec = min_timeout % 1000000000;
652 timeout_argument = &temporary_timeout_storage;
657 rmw_ret_t ret = rmw_wait(
658 &wait_set->
impl->rmw_subscriptions,
659 &wait_set->
impl->rmw_guard_conditions,
660 &wait_set->
impl->rmw_services,
661 &wait_set->
impl->rmw_clients,
662 &wait_set->
impl->rmw_events,
663 wait_set->
impl->rmw_wait_set,
669 bool any_timer_is_ready =
false;
674 for (i = 0; i < wait_set->
impl->timer_index; ++i) {
675 if (!wait_set->
timers[i]) {
679 bool current_timer_is_ready =
false;
684 if (!current_timer_is_ready) {
685 wait_set->
timers[i] = NULL;
687 any_timer_is_ready =
true;
691 if (ret != RMW_RET_OK && ret != RMW_RET_TIMEOUT) {
692 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
697 bool is_ready = wait_set->
impl->rmw_subscriptions.subscribers[i] != NULL;
704 bool is_ready = wait_set->
impl->rmw_guard_conditions.guard_conditions[i] != NULL;
711 bool is_ready = wait_set->
impl->rmw_clients.clients[i] != NULL;
718 bool is_ready = wait_set->
impl->rmw_services.services[i] != NULL;
725 bool is_ready = wait_set->
impl->rmw_events.events[i] != NULL;
727 wait_set->
events[i] = NULL;
731 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.
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.