Repository navigation
Closed
Labels
enhancementNew feature or request
Description
Feature request
This is a request for an API on top of the current waitset api that considers groups of waitable entities.
Feature description
An API that covers groups of entities that can be waited on would be immediately useful for actions. A server and client consists of several waitable entities (servers, clients, maybe a timer). The API would allow a client library to add a group of waitable entities to the wait set, and then notify the action server when one of them becomes available.
Implementation considerations
This could be implemented in a package rcl_grouped_waitset on top of the existing rcl API
Structs and methods to be added.
struct rcl_grouped_waitset
{
rcl_waitset_group_t * groups;
size_t num_groups;
rcl_waitset_group_impl_t * impl;
}
struct rcl_waitset_group_t
{
rcl_subscription_t * subscriptions;
rcl_guard_condition_t * guard_conditions;
rcl_timer_t * timers;
rcl_client_t * clients;
rcl_service_t * servers;
size_t num_subscriptions;
size_t num_guard_conditions;
size_t num_timers;
size_t num_clients;
size_t num_servers;
}
rcl_waitset_group_get_zero_initialized();
// User won't call these, but rcl_grouped_waitset_add and rcl_grouped_waitset_fini might
// rcl_waitset_group_init(rcl_waitset_group_t *, num_subs, subs, ..., rcl_allocator_t alloc);
// rcl_waitset_group_fini(rcl_waitset_group_t * g);
rcl_grouped_waitset_get_zero_initialized();
rcl_grouped_waitset_init(rcl_grouped_waitset_t * gws, size_t num_groups, rcl_allocator_t alloc);
rcl_grouped_waitset_fini(rcl_grouped_waitset_t * gws);
// Add a group waitable entities to the wait set
// This returns a waitset group that can be used to tell which entities in the group woke the wait set
// The returned group isn't usable until `rcl_grouped_waitset_build()` is called. The returned group never needs to be cleaned up by the user.
// subs, gcs, timers, etc pointers are borrowed and must be valid until `rcl_grouped_waitset_build()` is called
rcl_grouped_waitset_add(rcl_grouped_waitset_t * gws, rcl_waitset_group_t * g, size_t num_subs, rcl_subscription_t * subs, size_t num_guard_conditions, , rcl_guard_condition_t * gcs, size_t num_timers, rcl_timer_t * timers, size_t num_clients, rcl_client_t * clients, size_t num_servers, rcl_server_t * servers);
// Called after all groups have been added so allocation of the wait set only happens once
rcl_grouped_waitset_build();
// Calls `rcl_wait(rcl_waitset_t ws,...)` internally using a waitset built in `_build()` and stored in the implementation of the group object.
rcl_grouped_wait(rcl_grouped_waitset_t * gws, int64_t timeout);Example use
rcl_waitset_group_t gws = rcl_grouped_waitset_get_zero_initialized();
rcl_grouped_waitset_init(&gws, num_groups, alloc);
// Client library builds a group with all of the users subs, clients, services, timers, etc
rcl_waitset_group_t cl_group = rcl_waitset_group_get_zero_initialized();
rcl_grouped_waitset_add(&gws, &cl_group, ...);
// For each action server client library asks it to add itself to the waitset group
{
rcl_waitset_group group = rcl_waitset_group_get_zero_initialized();
// This calls rcl_grouped_waitset_add() internally
rcl_action_server_add_to_grouped_waitset(&as, &gws, &group);
}
rcl_grouped_wait(gws, ...)
// Entities are set to NULL within the added groups
for (size_t g = 0; g < gws.num_groups; ++g)
{
const rcl_waitset_group_t * group = gws.groups[g];
for (size_t s = 0; s < group->num_subscriptions; ++s)
{
// client library does right thing
// First group could be user defined stuff,
// Following groups could be for action servers, action clients, etc
}
}
// When done finalizing the grouped waitset also cleans up the groups
// This works because rcl_grouped_waitset_build() set the pointers to the entities to be internal to an `rcl_waitset_t1
rcl_grouped_waitset_fini(gws);connects to #307
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
jacobperron commentedon Nov 14, 2018
@sloretz
It looks to me that the definition of
rcl_waitset_group_tfrom your example very closely resemblesrcl_wait_set_t:rcl/rcl/include/rcl/wait.h
Lines 38 to 57 in a2546fa
Rather than defining a new concept of a wait set group, maybe we could instead implement a second wait function that takes an array of
rcl_wait_set_t, e.g.I think this gives us the same capabilities but with less plumbing. What do you think?
sloretz commentedon Nov 14, 2018
@jacobperron That could work. One thing to be aware of is the wait sets need to be consolidated before being passed to
rmw_wait(). Theimplpointer on the grouped waitset was a place to store those lists.rcl/rcl/src/rcl/wait.c
Lines 543 to 549 in a2546fa
Maybe
rcl_wait_multiple()could pick one of the wait sets to use as storage for the lists passed tormw_wait()?jacobperron commentedon Nov 14, 2018
I guess we shouldn't modify the size of one of the input wait sets since the caller will check that after waiting. It doesn't seem like too much overhead to create a new wait set for the scope of the function where we collect all entities. Then we can call
rcl_wait()on the collated wait set and propagate the update back to the input list.hidmic commentedon Nov 16, 2018
@jacobperron @sloretz I was just looking into
rclcppinternals in preparation for the client implementation, and I started wondering if we'd be better off with just wait set grouping and ungrouping functions instead of adding anotherrcl_wait*()variant. Something like (or whatever name you guys prefer):Essentially splitting up the function @jacobperron wrote in three separate steps: grouping, waiting, ungrouping. I believe it'll make adapting client libraries simpler, see here and here.
jacobperron commentedon Nov 16, 2018
@hidmic is it possible to provide an example/pseudocode of how you imagine these functions could be used in the context of actions?
hidmic commentedon Nov 16, 2018
@jacobperron so, for a generic example, I'll just reuse the one you wrote for
rcl_wait_multiple(), it's basically the same but in more steps:The real benefit comes when updating
rclcppto work with action clients and servers. There, the grouped wait set is the wait set being passed in toadd_handles_to_wait_set()and toremove_null_handles().Actually, we can keep
rcl_wait_multiple()(it's way more comfortable e.g. for anrclcuser that just wants the thing to work) by implementing it in terms of these ones, like:hidmic commentedon Nov 16, 2018
Not a something we must do, I just thought it may be helpful considering API freeze is next Wednesday.
jacobperron commentedon Nov 16, 2018
Yeah, it doesn't seem like a bad idea. It might help with readability too since
rcl_wait_mutliple()can reuse the grouping functions.jacobperron commentedon Nov 16, 2018
@hidmic
How can the implementation ofrcl_wait_set_ungroup()know what entities belong to which wait set?Nevermind, I guess it only works if the input array of wait sets is the same as when rcl_wait_set_group was called.
hidmic commentedon Nov 18, 2018
Yeap, it builds on top of the same underlying assumptions as
rcl_wait_multiple()'s current implementation.jacobperron commentedon Nov 19, 2018
As an alternative solution for working with actions and wait sets, we can instead modify the existing wait set API to return the index of the entity in the wait set when it is added. For example, adding a service:
Then, the action server/client can have an implementation like so: