|
| 1 | +// Copyright 2020 DCS Corporation, All Rights Reserved. |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// DISTRIBUTION A. Approved for public release; distribution unlimited. |
| 16 | +// OPSEC #4584. |
| 17 | + |
| 18 | +use std::borrow::BorrowMut; |
| 19 | +use std::rc::Weak; |
| 20 | + |
| 21 | +use crate::Handle; |
| 22 | +use crate::{error::*, SubscriptionBase}; |
| 23 | + |
| 24 | +use crate::rcl_bindings::*; |
| 25 | + |
| 26 | +use anyhow::Result; |
| 27 | +use rclrs_common::error::WaitSetError; |
| 28 | + |
| 29 | +pub struct WaitSet { |
| 30 | + pub wait_set: rcl_wait_set_t, |
| 31 | + initialized: bool, |
| 32 | +} |
| 33 | + |
| 34 | +impl WaitSet { |
| 35 | + /// Creates and initializes a new WaitSet object. |
| 36 | + /// |
| 37 | + /// Under the hood, this calls `rcl_get_zero_initialized_wait_set()`, and stores it |
| 38 | + /// within the WaitSet struct, while also noting that the returned value is uninitialized. |
| 39 | + pub fn new( |
| 40 | + number_of_subscriptions: usize, |
| 41 | + number_of_guard_conditions: usize, |
| 42 | + number_of_timers: usize, |
| 43 | + number_of_clients: usize, |
| 44 | + number_of_services: usize, |
| 45 | + number_of_events: usize, |
| 46 | + context: &mut rcl_context_t, |
| 47 | + ) -> Result<Self, WaitSetError> { |
| 48 | + let mut waitset = Self { |
| 49 | + wait_set: unsafe { rcl_get_zero_initialized_wait_set() }, |
| 50 | + initialized: false, |
| 51 | + }; |
| 52 | + unsafe { |
| 53 | + match to_rcl_result(rcl_wait_set_init( |
| 54 | + waitset.wait_set.borrow_mut() as *mut _, |
| 55 | + number_of_subscriptions, |
| 56 | + number_of_guard_conditions, |
| 57 | + number_of_timers, |
| 58 | + number_of_clients, |
| 59 | + number_of_services, |
| 60 | + number_of_events, |
| 61 | + context, |
| 62 | + rcutils_get_default_allocator(), |
| 63 | + )) { |
| 64 | + Ok(()) => { |
| 65 | + waitset.initialized = true; |
| 66 | + Ok(waitset) |
| 67 | + } |
| 68 | + Err(err) => { |
| 69 | + waitset.initialized = false; |
| 70 | + Err(WaitSetError::RclError(err)) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// Removes (sets to NULL) all entities in the WaitSet |
| 77 | + /// |
| 78 | + /// # Errors |
| 79 | + /// - `RclError::InvalidArgument` if any arguments are invalid. |
| 80 | + /// - `RclError::WaitSetInvalid` if the WaitSet is already zero-initialized. |
| 81 | + /// - `RclError::Error` for an unspecified error |
| 82 | + pub fn clear(&mut self) -> Result<(), WaitSetError> { |
| 83 | + if !self.initialized { |
| 84 | + return Err(WaitSetError::RclError(RclError::WaitSetInvalid)); |
| 85 | + } |
| 86 | + unsafe { |
| 87 | + // Whether or not we successfully clear, this WaitSet will count as uninitialized |
| 88 | + self.initialized = false; |
| 89 | + to_rcl_result(rcl_wait_set_clear(self.wait_set.borrow_mut() as *mut _)) |
| 90 | + .map_err(WaitSetError::RclError) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// Adds a subscription to the WaitSet |
| 95 | + /// |
| 96 | + /// # Errors |
| 97 | + /// - `WaitSetError::DroppedSubscription` if the passed weak pointer refers to a dropped subscription |
| 98 | + /// - `WaitSetError::RclError` for any `rcl` errors that occur during the process |
| 99 | + pub fn add_subscription( |
| 100 | + &mut self, |
| 101 | + subscription: &Weak<dyn SubscriptionBase>, |
| 102 | + ) -> Result<(), WaitSetError> { |
| 103 | + if let Some(subscription) = subscription.upgrade() { |
| 104 | + let subscription_handle = &*subscription.handle().get(); |
| 105 | + unsafe { |
| 106 | + return to_rcl_result(rcl_wait_set_add_subscription( |
| 107 | + self.wait_set.borrow_mut() as *mut _, |
| 108 | + subscription_handle as *const _, |
| 109 | + std::ptr::null_mut(), |
| 110 | + )) |
| 111 | + .map_err(WaitSetError::RclError); |
| 112 | + } |
| 113 | + } else { |
| 114 | + Err(WaitSetError::DroppedSubscription) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /// Blocks until the WaitSet is ready, or until the timeout has been exceeded |
| 119 | + /// |
| 120 | + /// This function will collect the items in the rcl_wait_set_t and pass them |
| 121 | + /// to the underlying rmw_wait function. |
| 122 | + /// The items in the wait set will be either left untouched or set to NULL after |
| 123 | + /// this function returns. |
| 124 | + /// Items that are not NULL are ready, where ready means different things based |
| 125 | + /// on the type of the item. |
| 126 | + /// For subscriptions this means there may be messages that can be taken, or |
| 127 | + /// perhaps that the state of the subscriptions has changed, in which case |
| 128 | + /// rcl_take may succeed but return with taken == false. |
| 129 | + /// For guard conditions this means the guard condition was triggered. |
| 130 | + /// |
| 131 | + /// The wait set struct must be allocated, initialized, and should have been |
| 132 | + /// cleared and then filled with items, e.g. subscriptions and guard conditions. |
| 133 | + /// Passing a wait set with no wait-able items in it will fail. |
| 134 | + /// NULL items in the sets are ignored, e.g. it is valid to have as input: |
| 135 | + /// subscriptions[0] = valid pointer |
| 136 | + /// subscriptions[1] = NULL |
| 137 | + /// subscriptions[2] = valid pointer |
| 138 | + /// size_of_subscriptions = 3 |
| 139 | + /// |
| 140 | + /// Passing an uninitialized (zero initialized) wait set struct will fail. |
| 141 | + /// Passing a wait set struct with uninitialized memory is undefined behavior. |
| 142 | + /// For this reason, it is advised to use the WaitSet struct to call `wait`, as it |
| 143 | + /// cannot be created uninitialized. |
| 144 | + /// |
| 145 | + /// The unit of timeout is nanoseconds. |
| 146 | + /// If the timeout is negative then this function will block indefinitely until |
| 147 | + /// something in the wait set is valid or it is interrupted. |
| 148 | + /// If the timeout is 0 then this function will be non-blocking; checking what's |
| 149 | + /// ready now, but not waiting if nothing is ready yet. |
| 150 | + /// If the timeout is greater than 0 then this function will return after |
| 151 | + /// that period of time has elapsed or the wait set becomes ready, which ever |
| 152 | + /// comes first. |
| 153 | + /// Passing a timeout struct with uninitialized memory is undefined behavior. |
| 154 | + /// |
| 155 | + /// This function is thread-safe for unique wait sets with unique contents. |
| 156 | + /// This function cannot operate on the same wait set in multiple threads, and |
| 157 | + /// the wait sets may not share content. |
| 158 | + /// For example, calling wait() in two threads on two different wait sets |
| 159 | + /// that both contain a single, shared guard condition is undefined behavior. |
| 160 | + /// # Errors |
| 161 | + /// - `RclError::InvalidArgument` if an argument was invalid |
| 162 | + /// - `RclError::WaitSetInvalid` if the wait set is zero initialized |
| 163 | + /// - `RclError::WaitSetEmpty` if the wait set contains no items |
| 164 | + /// - `RclError::Timeout` if the timeout expired before something was ready |
| 165 | + /// - `RclError::Error` for an unspecified error |
| 166 | + pub fn wait(&mut self, timeout: i64) -> Result<(), WaitSetError> { |
| 167 | + unsafe { |
| 168 | + to_rcl_result(rcl_wait(self.wait_set.borrow_mut() as *mut _, timeout)) |
| 169 | + .map_err(WaitSetError::RclError) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +impl Drop for WaitSet { |
| 175 | + /// Drops the WaitSet, and clears the memory |
| 176 | + /// |
| 177 | + /// # Panics |
| 178 | + /// A panic is raised if `rcl` is unable to release the waitset for any reason. |
| 179 | + fn drop(&mut self) { |
| 180 | + let handle = &mut *self.wait_set.borrow_mut(); |
| 181 | + unsafe { |
| 182 | + match to_rcl_result(rcl_wait_set_fini(handle as *mut _)) { |
| 183 | + Ok(()) => (), |
| 184 | + Err(err) => { |
| 185 | + panic!("Unable to release WaitSet!! {:?}", err) |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments