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"
35 #include "./client_impl.h"
36 #include "./guard_condition_impl.h"
37 #include "./service_impl.h"
38 #include "./subscription_impl.h"
39 #include "./timer_impl.h"
44 size_t subscription_index;
45 rmw_subscriptions_t rmw_subscriptions;
47 size_t guard_condition_index;
48 rmw_guard_conditions_t rmw_guard_conditions;
51 rmw_clients_t rmw_clients;
54 rmw_services_t rmw_services;
57 rmw_events_t rmw_events;
59 rmw_wait_set_t * rmw_wait_set;
79 return wait_set && wait_set->
impl;
85 size_t number_of_subscriptions,
86 size_t number_of_guard_conditions,
87 size_t number_of_timers,
88 size_t number_of_clients,
89 size_t number_of_services,
90 size_t number_of_events,
94 RCUTILS_LOG_DEBUG_NAMED(
95 ROS_PACKAGE_NAME,
"Initializing wait set with "
96 "'%zu' subscriptions, '%zu' guard conditions, '%zu' timers, '%zu' clients, '%zu' services",
97 number_of_subscriptions, number_of_guard_conditions, number_of_timers, number_of_clients,
104 RCL_SET_ERROR_MSG(
"wait_set already initialized, or memory was uninitialized.");
111 "the given context is not valid, "
112 "either rcl_init() was not called or rcl_shutdown() was called.");
118 RCL_CHECK_FOR_NULL_WITH_MSG(
121 wait_set->
impl->rmw_subscriptions.subscribers = NULL;
122 wait_set->
impl->rmw_subscriptions.subscriber_count = 0;
123 wait_set->
impl->rmw_guard_conditions.guard_conditions = NULL;
124 wait_set->
impl->rmw_guard_conditions.guard_condition_count = 0;
125 wait_set->
impl->rmw_clients.clients = NULL;
126 wait_set->
impl->rmw_clients.client_count = 0;
127 wait_set->
impl->rmw_services.services = NULL;
128 wait_set->
impl->rmw_services.service_count = 0;
129 wait_set->
impl->rmw_events.events = NULL;
130 wait_set->
impl->rmw_events.event_count = 0;
132 wait_set->
impl->context = context;
134 wait_set->
impl->allocator = allocator;
136 size_t num_conditions =
137 (2 * number_of_subscriptions) +
138 number_of_guard_conditions +
143 wait_set->
impl->rmw_wait_set = rmw_create_wait_set(&(context->
impl->
rmw_context), num_conditions);
144 if (!wait_set->
impl->rmw_wait_set) {
151 wait_set, number_of_subscriptions, number_of_guard_conditions, number_of_timers,
152 number_of_clients, number_of_services, number_of_events);
159 if (wait_set->
impl->rmw_wait_set != NULL) {
160 rmw_ret_t rmw_ret = rmw_destroy_wait_set(wait_set->
impl->rmw_wait_set);
161 if (rmw_ret != RMW_RET_OK) {
165 allocator.deallocate(wait_set->
impl, wait_set->
impl->allocator.state);
166 wait_set->
impl = NULL;
178 rmw_ret_t ret = rmw_destroy_wait_set(wait_set->
impl->rmw_wait_set);
179 if (ret != RMW_RET_OK) {
180 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
187 result = resize_result;
189 if (wait_set->
impl) {
190 wait_set->
impl->allocator.deallocate(wait_set->
impl, wait_set->
impl->allocator.state);
191 wait_set->
impl = NULL;
202 RCL_SET_ERROR_MSG(
"wait set is invalid");
206 *allocator = wait_set->
impl->allocator;
210 #define SET_ADD(Type) \
211 RCL_CHECK_ARGUMENT_FOR_NULL(wait_set, RCL_RET_INVALID_ARGUMENT); \
212 if (!wait_set->impl) { \
213 RCL_SET_ERROR_MSG("wait set is invalid"); \
214 return RCL_RET_WAIT_SET_INVALID; \
216 RCL_CHECK_ARGUMENT_FOR_NULL(Type, RCL_RET_INVALID_ARGUMENT); \
217 if (!(wait_set->impl->Type ## _index < wait_set->size_of_ ## Type ## s)) { \
218 RCL_SET_ERROR_MSG(#Type "s set is full"); \
219 return RCL_RET_WAIT_SET_FULL; \
221 size_t current_index = wait_set->impl->Type ## _index++; \
222 wait_set->Type ## s[current_index] = Type; \
224 if (NULL != index) { \
225 *index = current_index; \
228 #define SET_ADD_RMW(Type, RMWStorage, RMWCount) \
230 rmw_ ## Type ## _t * rmw_handle = rcl_ ## Type ## _get_rmw_handle(Type); \
231 RCL_CHECK_FOR_NULL_WITH_MSG( \
232 rmw_handle, rcl_get_error_string().str, return RCL_RET_ERROR); \
233 wait_set->impl->RMWStorage[current_index] = rmw_handle->data; \
234 wait_set->impl->RMWCount++;
236 #define SET_CLEAR(Type) \
238 if (NULL != wait_set->Type ## s) { \
240 (void *)wait_set->Type ## s, \
242 sizeof(rcl_ ## Type ## _t *) * wait_set->size_of_ ## Type ## s); \
243 wait_set->impl->Type ## _index = 0; \
247 #define SET_CLEAR_RMW(Type, RMWStorage, RMWCount) \
249 if (NULL != wait_set->impl->RMWStorage) { \
252 wait_set->impl->RMWStorage, \
254 sizeof(void *) * wait_set->impl->RMWCount); \
255 wait_set->impl->RMWCount = 0; \
259 #define SET_RESIZE(Type, ExtraDealloc, ExtraRealloc) \
261 rcl_allocator_t allocator = wait_set->impl->allocator; \
262 wait_set->size_of_ ## Type ## s = 0; \
263 wait_set->impl->Type ## _index = 0; \
264 if (0 == Type ## s_size) { \
265 if (wait_set->Type ## s) { \
266 allocator.deallocate((void *)wait_set->Type ## s, allocator.state); \
267 wait_set->Type ## s = NULL; \
271 wait_set->Type ## s = (const rcl_ ## Type ## _t **)allocator.reallocate( \
272 (void *)wait_set->Type ## s, sizeof(rcl_ ## Type ## _t *) * Type ## s_size, \
274 RCL_CHECK_FOR_NULL_WITH_MSG( \
275 wait_set->Type ## s, "allocating memory failed", return RCL_RET_BAD_ALLOC); \
276 memset((void *)wait_set->Type ## s, 0, sizeof(rcl_ ## Type ## _t *) * Type ## s_size); \
277 wait_set->size_of_ ## Type ## s = Type ## s_size; \
282 #define SET_RESIZE_RMW_DEALLOC(RMWStorage, RMWCount) \
284 if (wait_set->impl->RMWStorage) { \
285 allocator.deallocate((void *)wait_set->impl->RMWStorage, allocator.state); \
286 wait_set->impl->RMWStorage = NULL; \
287 wait_set->impl->RMWCount = 0; \
290 #define SET_RESIZE_RMW_REALLOC(Type, RMWStorage, RMWCount) \
292 wait_set->impl->RMWCount = 0; \
293 wait_set->impl->RMWStorage = (void **)allocator.reallocate( \
294 wait_set->impl->RMWStorage, sizeof(void *) * Type ## s_size, allocator.state); \
295 if (!wait_set->impl->RMWStorage) { \
296 allocator.deallocate((void *)wait_set->Type ## s, allocator.state); \
297 wait_set->Type ## s = NULL; \
298 wait_set->size_of_ ## Type ## s = 0; \
299 RCL_SET_ERROR_MSG("allocating memory failed"); \
300 return RCL_RET_BAD_ALLOC; \
302 memset(wait_set->impl->RMWStorage, 0, sizeof(void *) * Type ## s_size);
312 #define CHECK_DOUBLE_USAGE(Type) \
313 for (size_t idx = 0; idx < wait_set->size_of_ ## Type ## s; idx++) { \
314 if (wait_set->Type ## s[idx]) { \
315 wait_set->Type ## s[idx]->impl->in_use_by_waitset = false; \
318 for (size_t idx = 0; idx < wait_set->size_of_ ## Type ## s; idx++) { \
319 if (wait_set->Type ## s[idx]) { \
320 if(wait_set->Type ## s[idx]->impl->in_use_by_waitset) { \
321 RCL_SET_ERROR_MSG("Entitiy of type " #Type " added multiple times to waitset."); \
322 return RCL_RET_WAIT_SET_INVALID; \
324 wait_set->Type ## s[idx]->impl->in_use_by_waitset = true; \
339 SET_ADD(subscription)
340 SET_ADD_RMW(subscription, rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count)
355 SET_CLEAR(subscription);
356 SET_CLEAR(guard_condition);
364 rmw_subscriptions.subscribers,
365 rmw_subscriptions.subscriber_count);
368 rmw_guard_conditions.guard_conditions,
369 rmw_guard_conditions.guard_condition_count);
373 rmw_clients.client_count);
376 rmw_services.services,
377 rmw_services.service_count);
381 rmw_events.event_count);
394 size_t subscriptions_size,
395 size_t guard_conditions_size,
398 size_t services_size,
405 SET_RESIZE_RMW_DEALLOC(
406 rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count),
407 SET_RESIZE_RMW_REALLOC(
408 subscription, rmw_subscriptions.subscribers, rmw_subscriptions.subscriber_count)
411 SET_RESIZE(guard_condition,;,;);
414 rmw_guard_conditions_t * rmw_gcs = &(wait_set->
impl->rmw_guard_conditions);
415 const size_t num_rmw_gc = guard_conditions_size + timers_size;
417 rmw_gcs->guard_condition_count = 0u;
418 if (0u == num_rmw_gc) {
419 if (rmw_gcs->guard_conditions) {
420 wait_set->
impl->allocator.deallocate(
421 (
void *)rmw_gcs->guard_conditions, wait_set->
impl->allocator.state);
422 rmw_gcs->guard_conditions = NULL;
425 rmw_gcs->guard_conditions = (
void **)wait_set->
impl->allocator.reallocate(
426 rmw_gcs->guard_conditions,
sizeof(
void *) * num_rmw_gc, wait_set->
impl->allocator.state);
427 if (!rmw_gcs->guard_conditions) {
429 wait_set->
impl->allocator.deallocate(
433 wait_set->
impl->allocator.deallocate(
434 (
void *)wait_set->
timers, wait_set->
impl->allocator.state);
437 RCL_SET_ERROR_MSG(
"allocating memory failed");
440 memset(rmw_gcs->guard_conditions, 0,
sizeof(
void *) * num_rmw_gc);
443 SET_RESIZE(timer,;,;);
446 SET_RESIZE_RMW_DEALLOC(
447 rmw_clients.clients, rmw_clients.client_count),
448 SET_RESIZE_RMW_REALLOC(
449 client, rmw_clients.clients, rmw_clients.client_count)
453 SET_RESIZE_RMW_DEALLOC(
454 rmw_services.services, rmw_services.service_count),
455 SET_RESIZE_RMW_REALLOC(
456 service, rmw_services.services, rmw_services.service_count)
460 SET_RESIZE_RMW_DEALLOC(
461 rmw_events.events, rmw_events.event_count),
462 SET_RESIZE_RMW_REALLOC(
463 event, rmw_events.events, rmw_events.event_count)
475 SET_ADD(guard_condition)
477 guard_condition, rmw_guard_conditions.guard_conditions,
478 rmw_guard_conditions.guard_condition_count)
492 if (NULL != guard_condition) {
496 RCL_CHECK_FOR_NULL_WITH_MSG(
497 rmw_handle, rcl_get_error_string().str,
return RCL_RET_ERROR);
498 wait_set->
impl->rmw_guard_conditions.guard_conditions[index] = rmw_handle->data;
510 SET_ADD_RMW(client, rmw_clients.clients, rmw_clients.client_count)
521 SET_ADD_RMW(service, rmw_services.services, rmw_services.service_count)
532 SET_ADD_RMW(event, rmw_events.events, rmw_events.event_count)
533 wait_set->
impl->rmw_events.events[current_index] = rmw_handle;
542 RCL_SET_ERROR_MSG(
"wait set is invalid");
553 RCL_SET_ERROR_MSG(
"wait set is empty");
557 CHECK_DOUBLE_USAGE(client);
558 CHECK_DOUBLE_USAGE(guard_condition);
559 CHECK_DOUBLE_USAGE(service);
560 CHECK_DOUBLE_USAGE(subscription);
561 CHECK_DOUBLE_USAGE(timer);
565 rmw_time_t * timeout_argument = NULL;
566 rmw_time_t temporary_timeout_storage;
567 bool is_non_blocking = timeout == 0;
569 for (uint64_t t_idx = 0; t_idx < wait_set->
impl->timer_index; ++t_idx) {
570 if (!wait_set->
timers[t_idx]) {
573 rmw_guard_conditions_t * rmw_gcs = &(wait_set->
impl->rmw_guard_conditions);
575 if (NULL != rmw_gcs->guard_conditions[gc_idx]) {
577 rmw_gcs->guard_conditions[rmw_gcs->guard_condition_count] =
578 rmw_gcs->guard_conditions[gc_idx];
579 ++(rmw_gcs->guard_condition_count);
596 if (!is_non_blocking) {
597 for (
size_t t_idx = 0; t_idx < wait_set->
impl->timer_index; ++t_idx) {
598 if (!wait_set->
timers[t_idx]) {
606 RCL_EXPECT_ERROR_IS_SET(ret);
611 bool timer_override_active =
false;
615 RCL_EXPECT_ERROR_IS_SET(ret);
619 if (timer_override_active) {
621 bool override_timer_is_ready =
false;
625 RCL_EXPECT_ERROR_IS_SET(ret);
629 if (override_timer_is_ready) {
631 is_non_blocking =
true;
643 int64_t next_call_time = INT64_MAX;
646 wait_set->
timers[t_idx] = NULL;
650 RCL_EXPECT_ERROR_IS_SET(ret);
653 if (next_call_time < min_next_call_time[clock->
type]) {
654 clocks[clock->
type] = clock;
655 min_next_call_time[clock->
type] = next_call_time;
660 if (is_non_blocking) {
661 temporary_timeout_storage.sec = 0;
662 temporary_timeout_storage.nsec = 0;
663 timeout_argument = &temporary_timeout_storage;
665 bool has_valid_timeout = timeout > 0;
666 int64_t min_timeout = has_valid_timeout ? timeout : INT64_MAX;
670 if (clocks[i] == NULL) {
677 RCL_EXPECT_ERROR_IS_SET(ret);
681 int64_t timer_timeout = min_next_call_time[i] - cur_time;
683 if (timer_timeout <= min_timeout) {
684 has_valid_timeout =
true;
685 min_timeout = timer_timeout;
690 if (min_timeout < 0) {
693 if (has_valid_timeout) {
694 temporary_timeout_storage.sec =
RCL_NS_TO_S(min_timeout);
695 temporary_timeout_storage.nsec = min_timeout % 1000000000;
696 timeout_argument = &temporary_timeout_storage;
701 rmw_ret_t ret = rmw_wait(
702 &wait_set->
impl->rmw_subscriptions,
703 &wait_set->
impl->rmw_guard_conditions,
704 &wait_set->
impl->rmw_services,
705 &wait_set->
impl->rmw_clients,
706 &wait_set->
impl->rmw_events,
707 wait_set->
impl->rmw_wait_set,
713 bool any_timer_is_ready =
false;
718 for (i = 0; i < wait_set->
impl->timer_index; ++i) {
719 if (!wait_set->
timers[i]) {
723 bool current_timer_is_ready =
false;
726 RCL_EXPECT_ERROR_IS_SET(ret);
729 if (!current_timer_is_ready) {
730 wait_set->
timers[i] = NULL;
732 any_timer_is_ready =
true;
736 if (ret != RMW_RET_OK && ret != RMW_RET_TIMEOUT) {
737 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
738 "Error from rmw_wait(): %d %s", ret, rmw_get_error_string().str);
743 bool is_ready = wait_set->
impl->rmw_subscriptions.subscribers[i] != NULL;
750 bool is_ready = wait_set->
impl->rmw_guard_conditions.guard_conditions[i] != NULL;
757 bool is_ready = wait_set->
impl->rmw_clients.clients[i] != NULL;
764 bool is_ready = wait_set->
impl->rmw_services.services[i] != NULL;
771 bool is_ready = wait_set->
impl->rmw_events.events[i] != NULL;
773 wait_set->
events[i] = NULL;
777 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.