| 1 | #ifndef AWS_COMMON_THREAD_H |
| 2 | #define AWS_COMMON_THREAD_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | #include <aws/common/byte_buf.h> |
| 9 | #include <aws/common/string.h> |
| 10 | |
| 11 | #ifndef _WIN32 |
| 12 | # include <pthread.h> |
| 13 | #endif |
| 14 | |
| 15 | AWS_PUSH_SANE_WARNING_LEVEL |
| 16 | |
| 17 | enum aws_thread_detach_state { |
| 18 | AWS_THREAD_NOT_CREATED = 1, |
| 19 | AWS_THREAD_JOINABLE, |
| 20 | AWS_THREAD_JOIN_COMPLETED, |
| 21 | AWS_THREAD_MANAGED, |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * Specifies the join strategy used on an aws_thread, which in turn controls whether or not a thread participates |
| 26 | * in the managed thread system. The managed thread system provides logic to guarantee a join on all participating |
| 27 | * threads at the cost of laziness (the user cannot control when joins happen). |
| 28 | * |
| 29 | * Manual - thread does not participate in the managed thread system; any joins must be done by the user. This |
| 30 | * is the default. The user must call aws_thread_clean_up(), but only after any desired join operation has completed. |
| 31 | * Not doing so will cause the windows handle to leak. |
| 32 | * |
| 33 | * Managed - the managed thread system will automatically perform a join some time after the thread's run function |
| 34 | * has completed. It is an error to call aws_thread_join on a thread configured with the managed join strategy. The |
| 35 | * managed thread system will call aws_thread_clean_up() on the thread after the background join has completed. |
| 36 | * |
| 37 | * Additionally, an API exists, aws_thread_join_all_managed(), which blocks and returns when all outstanding threads |
| 38 | * with the managed strategy have fully joined. This API is useful for tests (rather than waiting for many individual |
| 39 | * signals) and program shutdown or DLL unload. This API is automatically invoked by the common library clean up |
| 40 | * function. If the common library clean up is called from a managed thread, this will cause deadlock. |
| 41 | * |
| 42 | * Lazy thread joining is done only when threads finish their run function or when the user calls |
| 43 | * aws_thread_join_all_managed(). This means it may be a long time between thread function completion and the join |
| 44 | * being applied, but the queue of unjoined threads is always one or fewer so there is no critical resource |
| 45 | * backlog. |
| 46 | * |
| 47 | * Currently, only event loop group async cleanup and host resolver threads participate in the managed thread system. |
| 48 | * Additionally, event loop threads will increment and decrement the pending join count (they are manually joined |
| 49 | * internally) in order to have an accurate view of internal thread usage and also to prevent failure to release |
| 50 | * an event loop group fully from allowing aws_thread_join_all_managed() from running to completion when its |
| 51 | * intent is such that it should block instead. |
| 52 | */ |
| 53 | enum aws_thread_join_strategy { |
| 54 | AWS_TJS_MANUAL = 0, |
| 55 | AWS_TJS_MANAGED, |
| 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * Thread names should be 15 characters or less. |
| 60 | * Longer names will not display on Linux. |
| 61 | * This length does not include a null terminator. |
| 62 | */ |
| 63 | #define AWS_THREAD_NAME_RECOMMENDED_STRLEN 15 |
| 64 | |
| 65 | struct aws_thread_options { |
| 66 | size_t stack_size; |
| 67 | /* default is -1. If you set this to anything >= 0, and the platform supports it, the thread will be pinned to |
| 68 | * that cpu. Also, we assume you're doing this for memory throughput purposes. On unix systems, |
| 69 | * If libnuma.so is available, upon the thread launching, the memory policy for that thread will be set to |
| 70 | * allocate on the numa node that cpu-core is on. |
| 71 | * |
| 72 | * On windows, this will cause the thread affinity to be set, but currently we don't do anything to tell the OS |
| 73 | * how to allocate memory on a node. |
| 74 | * |
| 75 | * On Apple and Android platforms, this setting doesn't do anything at all. |
| 76 | */ |
| 77 | int32_t cpu_id; |
| 78 | |
| 79 | enum aws_thread_join_strategy join_strategy; |
| 80 | |
| 81 | /** |
| 82 | * Thread name, for debugging purpose. |
| 83 | * The length should not exceed AWS_THREAD_NAME_RECOMMENDED_STRLEN(15) |
| 84 | * if you want it to display properly on all platforms. |
| 85 | */ |
| 86 | struct aws_byte_cursor name; |
| 87 | }; |
| 88 | |
| 89 | #ifdef _WIN32 |
| 90 | typedef union { |
| 91 | void *ptr; |
| 92 | } aws_thread_once; |
| 93 | # define AWS_THREAD_ONCE_STATIC_INIT {NULL} |
| 94 | typedef unsigned long aws_thread_id_t; |
| 95 | #else |
| 96 | typedef pthread_once_t aws_thread_once; |
| 97 | # define AWS_THREAD_ONCE_STATIC_INIT PTHREAD_ONCE_INIT |
| 98 | typedef pthread_t aws_thread_id_t; |
| 99 | #endif |
| 100 | |
| 101 | /* |
| 102 | * Buffer size needed to represent aws_thread_id_t as a string (2 hex chars per byte |
| 103 | * plus '\0' terminator). Needed for portable printing because pthread_t is |
| 104 | * opaque. |
| 105 | */ |
| 106 | #define AWS_THREAD_ID_T_REPR_BUFSZ (sizeof(aws_thread_id_t) * 2 + 1) |
| 107 | |
| 108 | struct aws_thread { |
| 109 | struct aws_allocator *allocator; |
| 110 | enum aws_thread_detach_state detach_state; |
| 111 | #ifdef _WIN32 |
| 112 | void *thread_handle; |
| 113 | #endif |
| 114 | aws_thread_id_t thread_id; |
| 115 | }; |
| 116 | |
| 117 | AWS_EXTERN_C_BEGIN |
| 118 | |
| 119 | /** |
| 120 | * Returns an instance of system default thread options. |
| 121 | */ |
| 122 | AWS_COMMON_API |
| 123 | const struct aws_thread_options *aws_default_thread_options(void); |
| 124 | |
| 125 | AWS_COMMON_API void aws_thread_call_once(aws_thread_once *flag, void (*call_once)(void *), void *user_data); |
| 126 | |
| 127 | /** |
| 128 | * Initializes a new platform specific thread object struct (not the os-level |
| 129 | * thread itself). |
| 130 | */ |
| 131 | AWS_COMMON_API |
| 132 | int aws_thread_init(struct aws_thread *thread, struct aws_allocator *allocator); |
| 133 | |
| 134 | /** |
| 135 | * Creates an OS level thread and associates it with func. context will be passed to func when it is executed. |
| 136 | * options will be applied to the thread if they are applicable for the platform. |
| 137 | * |
| 138 | * After launch, you may join on the thread. A successfully launched thread must have clean_up called on it in order |
| 139 | * to avoid a handle leak. If you do not join before calling clean_up, the thread will become detached. |
| 140 | * |
| 141 | * Managed threads must not have join or clean_up called on them by external code. |
| 142 | */ |
| 143 | AWS_COMMON_API |
| 144 | int aws_thread_launch( |
| 145 | struct aws_thread *thread, |
| 146 | void (*func)(void *arg), |
| 147 | void *arg, |
| 148 | const struct aws_thread_options *options); |
| 149 | |
| 150 | /** |
| 151 | * Gets the id of thread |
| 152 | */ |
| 153 | AWS_COMMON_API |
| 154 | aws_thread_id_t aws_thread_get_id(struct aws_thread *thread); |
| 155 | |
| 156 | /** |
| 157 | * Gets the detach state of the thread. For example, is it safe to call join on |
| 158 | * this thread? Has it been detached()? |
| 159 | */ |
| 160 | AWS_COMMON_API |
| 161 | enum aws_thread_detach_state aws_thread_get_detach_state(struct aws_thread *thread); |
| 162 | |
| 163 | /** |
| 164 | * Joins the calling thread to a thread instance. Returns when thread is |
| 165 | * finished. Calling this from the associated OS thread will cause a deadlock. |
| 166 | */ |
| 167 | AWS_COMMON_API |
| 168 | int aws_thread_join(struct aws_thread *thread); |
| 169 | |
| 170 | /** |
| 171 | * Blocking call that waits for all managed threads to complete their join call. This can only be called |
| 172 | * from the main thread or a non-managed thread. |
| 173 | * |
| 174 | * This gets called automatically from library cleanup. |
| 175 | * |
| 176 | * By default the wait is unbounded, but that default can be overridden via aws_thread_set_managed_join_timeout_ns() |
| 177 | */ |
| 178 | AWS_COMMON_API |
| 179 | int aws_thread_join_all_managed(void); |
| 180 | |
| 181 | /** |
| 182 | * Overrides how long, in nanoseconds, that aws_thread_join_all_managed will wait for threads to complete. |
| 183 | * A value of zero will result in an unbounded wait. |
| 184 | */ |
| 185 | AWS_COMMON_API |
| 186 | void aws_thread_set_managed_join_timeout_ns(uint64_t timeout_in_ns); |
| 187 | |
| 188 | /** |
| 189 | * Cleans up the thread handle. Don't call this on a managed thread. If you wish to join the thread, you must join |
| 190 | * before calling this function. |
| 191 | */ |
| 192 | AWS_COMMON_API |
| 193 | void aws_thread_clean_up(struct aws_thread *thread); |
| 194 | |
| 195 | /** |
| 196 | * Returns the thread id of the calling thread. |
| 197 | */ |
| 198 | AWS_COMMON_API |
| 199 | aws_thread_id_t aws_thread_current_thread_id(void); |
| 200 | |
| 201 | /** |
| 202 | * Compare thread ids. |
| 203 | */ |
| 204 | AWS_COMMON_API |
| 205 | bool aws_thread_thread_id_equal(aws_thread_id_t t1, aws_thread_id_t t2); |
| 206 | |
| 207 | /** |
| 208 | * Sleeps the current thread by nanos. |
| 209 | */ |
| 210 | AWS_COMMON_API |
| 211 | void aws_thread_current_sleep(uint64_t nanos); |
| 212 | |
| 213 | typedef void(aws_thread_atexit_fn)(void *user_data); |
| 214 | |
| 215 | /** |
| 216 | * Adds a callback to the chain to be called when the current thread joins. |
| 217 | * Callbacks are called from the current thread, in the reverse order they |
| 218 | * were added, after the thread function returns. |
| 219 | * If not called from within an aws_thread, has no effect. |
| 220 | */ |
| 221 | AWS_COMMON_API |
| 222 | int aws_thread_current_at_exit(aws_thread_atexit_fn *callback, void *user_data); |
| 223 | |
| 224 | /** |
| 225 | * Increments the count of unjoined threads in the managed thread system. Used by managed threads and |
| 226 | * event loop threads. Additional usage requires the user to join corresponding threads themselves and |
| 227 | * correctly increment/decrement even in the face of launch/join errors. |
| 228 | * |
| 229 | * aws_thread_join_all_managed() will not return until this count has gone to zero. |
| 230 | */ |
| 231 | AWS_COMMON_API void aws_thread_increment_unjoined_count(void); |
| 232 | |
| 233 | /** |
| 234 | * Decrements the count of unjoined threads in the managed thread system. Used by managed threads and |
| 235 | * event loop threads. Additional usage requires the user to join corresponding threads themselves and |
| 236 | * correctly increment/decrement even in the face of launch/join errors. |
| 237 | * |
| 238 | * aws_thread_join_all_managed() will not return until this count has gone to zero. |
| 239 | */ |
| 240 | AWS_COMMON_API void aws_thread_decrement_unjoined_count(void); |
| 241 | |
| 242 | /** |
| 243 | * Gets name of the current thread. |
| 244 | * Caller is responsible for destroying returned string. |
| 245 | * If thread does not have a name, AWS_OP_SUCCESS is returned and out_name is |
| 246 | * set to NULL. |
| 247 | * If underlying OS call fails, AWS_ERROR_SYS_CALL_FAILURE will be raised |
| 248 | * If OS does not support getting thread name, AWS_ERROR_PLATFORM_NOT_SUPPORTED |
| 249 | * will be raised |
| 250 | */ |
| 251 | AWS_COMMON_API int aws_thread_current_name(struct aws_allocator *allocator, struct aws_string **out_name); |
| 252 | |
| 253 | /** |
| 254 | * Gets name of the thread. |
| 255 | * Caller is responsible for destroying returned string. |
| 256 | * If thread does not have a name, AWS_OP_SUCCESS is returned and out_name is |
| 257 | * set to NULL. |
| 258 | * If underlying OS call fails, AWS_ERROR_SYS_CALL_FAILURE will be raised |
| 259 | * If OS does not support getting thread name, AWS_ERROR_PLATFORM_NOT_SUPPORTED |
| 260 | * will be raised |
| 261 | */ |
| 262 | AWS_COMMON_API int aws_thread_name( |
| 263 | struct aws_allocator *allocator, |
| 264 | aws_thread_id_t thread_id, |
| 265 | struct aws_string **out_name); |
| 266 | |
| 267 | AWS_EXTERN_C_END |
| 268 | AWS_POP_SANE_WARNING_LEVEL |
| 269 | |
| 270 | #endif /* AWS_COMMON_THREAD_H */ |