ROS 2 rclcpp + rcl - rolling  rolling-e7363f10
ROS 2 C++ Client Library with ROS Client Library
guard_condition.c
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
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 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
20 #include "rcl/guard_condition.h"
21 
22 #include <stdbool.h>
23 #include <stddef.h>
24 
25 #include "rcl/allocator.h"
26 #include "rcl/error_handling.h"
27 #include "rcl/types.h"
28 
29 #include "rmw/error_handling.h"
30 #include "rmw/ret_types.h"
31 #include "rmw/rmw.h"
32 #include "rmw/types.h"
33 
34 #include "./context_impl.h"
35 #include "./guard_condition_impl.h"
36 
37 
40 {
41  // All members are initialized to 0 or NULL by C99 6.7.8/10.
42  static rcl_guard_condition_t null_guard_condition;
43  return null_guard_condition;
44 }
45 
47 __rcl_guard_condition_init_from_rmw_impl(
48  rcl_guard_condition_t * guard_condition,
49  const rmw_guard_condition_t * rmw_guard_condition,
50  rcl_context_t * context,
51  const rcl_guard_condition_options_t options)
52 {
53  // This function will create an rmw_guard_condition if the parameter is null.
54 
55  // Perform argument validation.
56  const rcl_allocator_t * allocator = &options.allocator;
57  RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
58  RCL_CHECK_ARGUMENT_FOR_NULL(guard_condition, RCL_RET_INVALID_ARGUMENT);
59  // Ensure the guard_condition handle is zero initialized.
60  if (guard_condition->impl) {
61  RCL_SET_ERROR_MSG("guard_condition already initialized, or memory was unintialized");
62  return RCL_RET_ALREADY_INIT;
63  }
64  // Make sure rcl has been initialized.
65  RCL_CHECK_ARGUMENT_FOR_NULL(context, RCL_RET_INVALID_ARGUMENT);
66  if (!rcl_context_is_valid(context)) {
67  RCL_SET_ERROR_MSG(
68  "the given context is not valid, "
69  "either rcl_init() was not called or rcl_shutdown() was called.");
70  return RCL_RET_NOT_INIT;
71  }
72  // Allocate space for the guard condition impl.
73  guard_condition->impl = (rcl_guard_condition_impl_t *)allocator->allocate(
74  sizeof(rcl_guard_condition_impl_t), allocator->state);
75  if (!guard_condition->impl) {
76  RCL_SET_ERROR_MSG("allocating memory failed");
77  return RCL_RET_BAD_ALLOC;
78  }
79  guard_condition->impl->in_use_by_waitset = false;
80  // Create the rmw guard condition.
81  if (rmw_guard_condition) {
82  // If given, just assign (cast away const).
83  guard_condition->impl->rmw_handle = (rmw_guard_condition_t *)rmw_guard_condition;
84  guard_condition->impl->allocated_rmw_guard_condition = false;
85  } else {
86  // Otherwise create one.
87  guard_condition->impl->rmw_handle = rmw_create_guard_condition(&(context->impl->rmw_context));
88  if (!guard_condition->impl->rmw_handle) {
89  // Deallocate impl and exit.
90  allocator->deallocate(guard_condition->impl, allocator->state);
91  guard_condition->impl = NULL;
92  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
93  return RCL_RET_ERROR;
94  }
95  guard_condition->impl->allocated_rmw_guard_condition = true;
96  }
97  // Copy options into impl.
98  guard_condition->impl->options = options;
99  return RCL_RET_OK;
100 }
101 
102 rcl_ret_t
104  rcl_guard_condition_t * guard_condition,
105  rcl_context_t * context,
106  const rcl_guard_condition_options_t options)
107 {
108  // NULL indicates "create a new rmw guard condition".
109  return __rcl_guard_condition_init_from_rmw_impl(guard_condition, NULL, context, options);
110 }
111 
112 rcl_ret_t
114  rcl_guard_condition_t * guard_condition,
115  const rmw_guard_condition_t * rmw_guard_condition,
116  rcl_context_t * context,
117  const rcl_guard_condition_options_t options)
118 {
119  return __rcl_guard_condition_init_from_rmw_impl(
120  guard_condition, rmw_guard_condition, context, options);
121 }
122 
123 rcl_ret_t
125 {
126  // Perform argument validation.
127  RCL_CHECK_ARGUMENT_FOR_NULL(guard_condition, RCL_RET_INVALID_ARGUMENT);
128  rcl_ret_t result = RCL_RET_OK;
129  if (guard_condition->impl) {
130  // assuming the allocator is valid because it is checked in rcl_guard_condition_init()
131  rcl_allocator_t allocator = guard_condition->impl->options.allocator;
132  if (guard_condition->impl->rmw_handle && guard_condition->impl->allocated_rmw_guard_condition) {
133  if (rmw_destroy_guard_condition(guard_condition->impl->rmw_handle) != RMW_RET_OK) {
134  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
135  result = RCL_RET_ERROR;
136  }
137  }
138  allocator.deallocate(guard_condition->impl, allocator.state);
139  guard_condition->impl = NULL;
140  }
141  return result;
142 }
143 
146 {
147  // !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
148  rcl_guard_condition_options_t default_options;
149  default_options.allocator = rcl_get_default_allocator();
150  return default_options;
151 }
152 
153 rcl_ret_t
155 {
156  const rcl_guard_condition_options_t * options = rcl_guard_condition_get_options(guard_condition);
157  if (!options) {
158  return RCL_RET_INVALID_ARGUMENT; // error already set
159  }
160  // Trigger the guard condition.
161  if (rmw_trigger_guard_condition(guard_condition->impl->rmw_handle) != RMW_RET_OK) {
162  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
163  return RCL_RET_ERROR;
164  }
165  return RCL_RET_OK;
166 }
167 
170 {
171  // Perform argument validation.
172  RCL_CHECK_ARGUMENT_FOR_NULL(guard_condition, NULL);
173  RCL_CHECK_FOR_NULL_WITH_MSG(
174  guard_condition->impl,
175  "guard condition implementation is invalid",
176  return NULL);
177  return &guard_condition->impl->options;
178 }
179 
180 rmw_guard_condition_t *
182 {
183  const rcl_guard_condition_options_t * options = rcl_guard_condition_get_options(guard_condition);
184  if (!options) {
185  return NULL; // error already set
186  }
187  return guard_condition->impl->rmw_handle;
188 }
189 
190 #ifdef __cplusplus
191 }
192 #endif
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
Definition: allocator.h:37
#define RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement)
Check that the given allocator is initialized, or fail with a message.
Definition: allocator.h:56
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
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.
Definition: context.c:106
RCL_PUBLIC RCL_WARN_UNUSED const rcl_guard_condition_options_t * rcl_guard_condition_get_options(const rcl_guard_condition_t *guard_condition)
Return the guard condition options.
RCL_PUBLIC RCL_WARN_UNUSED rcl_guard_condition_t rcl_get_zero_initialized_guard_condition(void)
Return a rcl_guard_condition_t struct with members set to NULL.
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.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_trigger_guard_condition(rcl_guard_condition_t *guard_condition)
Trigger a rcl guard condition.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_guard_condition_init(rcl_guard_condition_t *guard_condition, rcl_context_t *context, const rcl_guard_condition_options_t options)
Initialize a rcl guard_condition.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_guard_condition_fini(rcl_guard_condition_t *guard_condition)
Finalize a rcl_guard_condition_t.
RCL_PUBLIC RCL_WARN_UNUSED rcl_guard_condition_options_t rcl_guard_condition_get_default_options(void)
Return the default options in a rcl_guard_condition_options_t struct.
rcl_ret_t rcl_guard_condition_init_from_rmw(rcl_guard_condition_t *guard_condition, const rmw_guard_condition_t *rmw_guard_condition, rcl_context_t *context, const rcl_guard_condition_options_t options)
Same as rcl_guard_condition_init(), but reusing an existing rmw handle.
rmw_context_t rmw_context
rmw context.
Definition: context_impl.h:46
Encapsulates the non-global state of an init/shutdown cycle.
Definition: context.h:117
rcl_context_impl_t * impl
Implementation specific pointer.
Definition: context.h:123
Options available for a rcl guard condition.
rcl_allocator_t allocator
Custom allocator for the guard condition, used for internal allocations.
Handle for a rcl guard condition.
rcl_guard_condition_impl_t * impl
Pointer to the guard condition implementation.
#define RCL_RET_NOT_INIT
rcl_init() not yet called return code.
Definition: types.h:44
#define RCL_RET_ALREADY_INIT
rcl_init() already called return code.
Definition: types.h:42
#define RCL_RET_OK
Success return code.
Definition: types.h:28
#define RCL_RET_BAD_ALLOC
Failed to allocate memory return code.
Definition: types.h:34
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:36
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:30
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:25