|
| 1 | +// Copyright 2023 eSOL Co.,Ltd. |
| 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 | +#ifndef RCUTILS__THREAD_ATTR_H_ |
| 16 | +#define RCUTILS__THREAD_ATTR_H_ |
| 17 | + |
| 18 | +#include "rcutils/visibility_control.h" |
| 19 | + |
| 20 | +#include "rcutils/allocator.h" |
| 21 | +#include "rcutils/macros.h" |
| 22 | +#include "rcutils/types/rcutils_ret.h" |
| 23 | + |
| 24 | +#ifdef __cplusplus |
| 25 | +extern "C" |
| 26 | +{ |
| 27 | +#endif |
| 28 | + |
| 29 | +typedef enum rcutils_thread_scheduling_policy_e |
| 30 | +{ |
| 31 | + RCUTILS_THREAD_SCHEDULING_POLICY_UNKNOWN = 0, |
| 32 | + RCUTILS_THREAD_SCHEDULING_POLICY_FIFO = 1, |
| 33 | + RCUTILS_THREAD_SCHEDULING_POLICY_RR = 2, |
| 34 | + RCUTILS_THREAD_SCHEDULING_POLICY_SPORADIC = 3, |
| 35 | + RCUTILS_THREAD_SCHEDULING_POLICY_OTHER = 4, |
| 36 | + RCUTILS_THREAD_SCHEDULING_POLICY_IDLE = 5, |
| 37 | + RCUTILS_THREAD_SCHEDULING_POLICY_BATCH = 6, |
| 38 | + RCUTILS_THREAD_SCHEDULING_POLICY_DEADLINE = 7 |
| 39 | +} rcutils_thread_scheduling_policy_t; |
| 40 | + |
| 41 | +typedef struct rcutils_thread_attr_s |
| 42 | +{ |
| 43 | + /// Thread core affinity |
| 44 | + int core_affinity; |
| 45 | + /// Thread scheduling policy. |
| 46 | + rcutils_thread_scheduling_policy_t scheduling_policy; |
| 47 | + /// Thread priority. |
| 48 | + int priority; |
| 49 | + /// Thread name |
| 50 | + char const * name; |
| 51 | +} rcutils_thread_attr_t; |
| 52 | + |
| 53 | +/// Hold thread attribute rules. |
| 54 | +typedef struct rcutils_thread_attrs_s |
| 55 | +{ |
| 56 | + /// Private implementation array. |
| 57 | + rcutils_thread_attr_t * attributes; |
| 58 | + /// Number of threads attribute |
| 59 | + size_t num_attributes; |
| 60 | + /// Number of threads attribute capacity |
| 61 | + size_t capacity_attributes; |
| 62 | + /// Allocator used to allocate objects in this struct |
| 63 | + rcutils_allocator_t allocator; |
| 64 | +} rcutils_thread_attrs_t; |
| 65 | + |
| 66 | +/** |
| 67 | + * \brief Return a rcutils_thread_attrs_t struct with members initialized to zero value. |
| 68 | + * \return a rcutils_thread_attrs_t struct with members initialized to zero value. |
| 69 | + */ |
| 70 | +RCUTILS_PUBLIC |
| 71 | +RCUTILS_WARN_UNUSED |
| 72 | +rcutils_thread_attrs_t |
| 73 | +rcutils_get_zero_initialized_thread_attrs(void); |
| 74 | + |
| 75 | +/** |
| 76 | + * \brief Initialize list of thread attributes. |
| 77 | + * \param[out] thread_attrs list of thread attributes to be initialized |
| 78 | + * \param[in] allocator memory allocator to be used |
| 79 | + * \return #RCUTILS_RET_OK if the structure was initialized succeessfully, or |
| 80 | + * \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid, or |
| 81 | + * \return #RCUTILS_RET_BAD_ALLOC if allocating memory failed, or |
| 82 | + * \return #RCUTILS_RET_ERROR an unspecified error occur. |
| 83 | + */ |
| 84 | +RCUTILS_PUBLIC |
| 85 | +RCUTILS_WARN_UNUSED |
| 86 | +rcutils_ret_t |
| 87 | +rcutils_thread_attrs_init( |
| 88 | + rcutils_thread_attrs_t * thread_attrs, |
| 89 | + rcutils_allocator_t allocator); |
| 90 | + |
| 91 | +/** |
| 92 | + * \brief Initialize list of thread attributes with a capacity. |
| 93 | + * \param[out] thread_attrs list of thread attributes to be initialized |
| 94 | + * \param[in] allocator memory allocator to be used |
| 95 | + * \return #RCUTILS_RET_OK if the structure was initialized succeessfully, or |
| 96 | + * \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid, or |
| 97 | + * \return #RCUTILS_RET_BAD_ALLOC if allocating memory failed, or |
| 98 | + * \return #RCUTILS_RET_ERROR an unspecified error occur. |
| 99 | + */ |
| 100 | +RCUTILS_PUBLIC |
| 101 | +RCUTILS_WARN_UNUSED |
| 102 | +rcutils_ret_t |
| 103 | +rcutils_thread_attrs_init_with_capacity( |
| 104 | + rcutils_thread_attrs_t * thread_attrs, |
| 105 | + rcutils_allocator_t allocator, |
| 106 | + size_t capacity); |
| 107 | + |
| 108 | +/** |
| 109 | + * \brief Free list of thread attributes |
| 110 | + * \param[in] thread_attrs structure to be deallocated. |
| 111 | + * \return #RCUTILS_RET_OK if the memory was successfully freed, or |
| 112 | + * \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid |
| 113 | + */ |
| 114 | +RCUTILS_PUBLIC |
| 115 | +RCUTILS_WARN_UNUSED |
| 116 | +rcutils_ret_t |
| 117 | +rcutils_thread_attrs_fini( |
| 118 | + rcutils_thread_attrs_t * thread_attrs); |
| 119 | + |
| 120 | +/** |
| 121 | + * \brief Add thread attribute to the list of thread attributes. |
| 122 | + * \param[inout] thread_attrs list of thread attributes to add a thread attribute to |
| 123 | + * \param[in] sched_policy thread scheduling policy of adding attribute |
| 124 | + * \param[in] core_affinity thread core affinity of adding attribute |
| 125 | + * \param[in] priority thread priority of adding attribute |
| 126 | + * \param[in] name thread name of adding attribute |
| 127 | + * \return #RCUTILS_RET_OK if the thread attribute was successfully added, or |
| 128 | + * \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid, or |
| 129 | + * \return #RCUTILS_RET_BAD_ALLOC if allocating memory failed, or |
| 130 | + * \return #RCUTILS_RET_ERROR an unspecified error occur. |
| 131 | + */ |
| 132 | +RCUTILS_PUBLIC |
| 133 | +RCUTILS_WARN_UNUSED |
| 134 | +rcutils_ret_t |
| 135 | +rcutils_thread_attrs_add_attr( |
| 136 | + rcutils_thread_attrs_t * thread_attrs, |
| 137 | + rcutils_thread_scheduling_policy_t sched_policy, |
| 138 | + int core_affinity, |
| 139 | + int priority, |
| 140 | + char const * name); |
| 141 | + |
| 142 | +#ifdef __cplusplus |
| 143 | +} |
| 144 | +#endif |
| 145 | + |
| 146 | +#endif // RCUTILS__THREAD_ATTR_H_ |
0 commit comments