Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #ifndef __PTHREAD_H__ |
| 8 | #define __PTHREAD_H__ |
| 9 | |
Youvedeep Singh | c8aa657 | 2017-12-28 14:31:57 +0530 | [diff] [blame] | 10 | #include <kernel.h> |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame^] | 11 | #include <wait_q.h> |
Ramakrishna Pallala | f603e60 | 2018-04-05 22:41:15 +0530 | [diff] [blame] | 12 | #include <posix/time.h> |
| 13 | #include <posix/unistd.h> |
Youvedeep Singh | abc94b8 | 2018-01-03 15:38:02 +0530 | [diff] [blame] | 14 | #include "sys/types.h" |
Youvedeep Singh | 7eabf10 | 2018-01-13 18:51:15 +0530 | [diff] [blame] | 15 | #include "posix_sched.h" |
Youvedeep Singh | c8aa657 | 2017-12-28 14:31:57 +0530 | [diff] [blame] | 16 | |
| 17 | enum pthread_state { |
| 18 | /* The thread is running and joinable. */ |
| 19 | PTHREAD_JOINABLE = 0, |
| 20 | /* The thread is running and detached. */ |
| 21 | PTHREAD_DETACHED, |
| 22 | /* A joinable thread exited and its return code is available. */ |
| 23 | PTHREAD_EXITED, |
| 24 | /* The thread structure is unallocated and available for reuse. */ |
| 25 | PTHREAD_TERMINATED |
| 26 | }; |
| 27 | |
| 28 | struct posix_thread { |
| 29 | struct k_thread thread; |
| 30 | |
| 31 | /* Exit status */ |
| 32 | void *retval; |
| 33 | |
| 34 | /* Pthread cancellation */ |
| 35 | int cancel_state; |
| 36 | int cancel_pending; |
| 37 | struct k_sem cancel_lock_sem; |
| 38 | pthread_mutex_t cancel_lock; |
| 39 | |
| 40 | /* Pthread State */ |
| 41 | enum pthread_state state; |
| 42 | pthread_mutex_t state_lock; |
| 43 | struct k_sem state_lock_sem; |
| 44 | pthread_cond_t state_cond; |
| 45 | }; |
| 46 | |
| 47 | /* Pthread detach/joinable */ |
| 48 | #define PTHREAD_CREATE_JOINABLE 0 |
| 49 | #define PTHREAD_CREATE_DETACHED 1 |
| 50 | |
| 51 | /* Pthread cancellation */ |
| 52 | #define _PTHREAD_CANCEL_POS 0 |
| 53 | #define PTHREAD_CANCEL_ENABLE (0 << _PTHREAD_CANCEL_POS) |
| 54 | #define PTHREAD_CANCEL_DISABLE (1 << _PTHREAD_CANCEL_POS) |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 55 | |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 56 | /** |
| 57 | * @brief Declare a pthread condition variable |
| 58 | * |
| 59 | * Declaration API for a pthread condition variable. This is not a |
| 60 | * POSIX API, it's provided to better conform with Zephyr's allocation |
| 61 | * strategies for kernel objects. |
| 62 | * |
| 63 | * @param name Symbol name of the condition variable |
| 64 | */ |
| 65 | #define PTHREAD_COND_DEFINE(name) \ |
| 66 | struct pthread_cond name = { \ |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame^] | 67 | .wait_q = _WAIT_Q_INIT(&name.wait_q), \ |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @brief POSIX threading compatibility API |
| 72 | * |
| 73 | * See IEEE 1003.1 |
| 74 | */ |
| 75 | static inline int pthread_cond_init(pthread_cond_t *cv, |
| 76 | const pthread_condattr_t *att) |
| 77 | { |
| 78 | ARG_UNUSED(att); |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame^] | 79 | _waitq_init(&cv->wait_q); |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @brief POSIX threading compatibility API |
| 85 | * |
| 86 | * See IEEE 1003.1 |
| 87 | */ |
| 88 | static inline int pthread_cond_destroy(pthread_cond_t *cv) |
| 89 | { |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @brief POSIX threading compatibility API |
| 95 | * |
| 96 | * See IEEE 1003.1 |
| 97 | */ |
| 98 | int pthread_cond_signal(pthread_cond_t *cv); |
| 99 | |
| 100 | /** |
| 101 | * @brief POSIX threading compatibility API |
| 102 | * |
| 103 | * See IEEE 1003.1 |
| 104 | */ |
| 105 | int pthread_cond_broadcast(pthread_cond_t *cv); |
| 106 | |
| 107 | /** |
| 108 | * @brief POSIX threading compatibility API |
| 109 | * |
| 110 | * See IEEE 1003.1 |
| 111 | */ |
| 112 | int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut); |
| 113 | |
| 114 | /** |
| 115 | * @brief POSIX threading compatibility API |
| 116 | * |
| 117 | * See IEEE 1003.1 |
| 118 | */ |
| 119 | int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, |
| 120 | const struct timespec *to); |
| 121 | |
| 122 | /** |
| 123 | * @brief POSIX threading compatibility API |
| 124 | * |
| 125 | * See IEEE 1003.1. |
| 126 | * |
| 127 | * Note that pthread attribute structs are currently noops in Zephyr. |
| 128 | */ |
| 129 | static inline int pthread_condattr_init(pthread_condattr_t *att) |
| 130 | { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @brief POSIX threading compatibility API |
| 136 | * |
| 137 | * See IEEE 1003.1 |
| 138 | * |
| 139 | * Note that pthread attribute structs are currently noops in Zephyr. |
| 140 | */ |
| 141 | static inline int pthread_condattr_destroy(pthread_condattr_t *att) |
| 142 | { |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @brief Declare a pthread mutex |
| 148 | * |
| 149 | * Declaration API for a pthread mutex. This is not a POSIX API, it's |
| 150 | * provided to better conform with Zephyr's allocation strategies for |
| 151 | * kernel objects. |
| 152 | * |
| 153 | * @param name Symbol name of the mutex |
| 154 | */ |
| 155 | #define PTHREAD_MUTEX_DEFINE(name) \ |
| 156 | K_SEM_DEFINE(name##_psem, 1, 1); \ |
| 157 | struct pthread_mutex name = { \ |
| 158 | .sem = &name##_psem, \ |
| 159 | } |
| 160 | |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 161 | /** |
| 162 | * @brief POSIX threading compatibility API |
| 163 | * |
| 164 | * See IEEE 1003.1 |
| 165 | */ |
| 166 | static inline int pthread_mutex_init(pthread_mutex_t *m, |
| 167 | const pthread_mutexattr_t *att) |
| 168 | { |
| 169 | ARG_UNUSED(att); |
| 170 | |
| 171 | k_sem_init(m->sem, 1, 1); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @brief POSIX threading compatibility API |
| 178 | * |
| 179 | * See IEEE 1003.1 |
| 180 | */ |
| 181 | static inline int pthread_mutex_destroy(pthread_mutex_t *m) |
| 182 | { |
| 183 | ARG_UNUSED(m); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @brief POSIX threading compatibility API |
| 190 | * |
| 191 | * See IEEE 1003.1 |
| 192 | */ |
| 193 | static inline int pthread_mutex_lock(pthread_mutex_t *m) |
| 194 | { |
| 195 | return k_sem_take(m->sem, K_FOREVER); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @brief POSIX threading compatibility API |
| 200 | * |
| 201 | * See IEEE 1003.1 |
| 202 | */ |
| 203 | static inline int pthread_mutex_timedlock(pthread_mutex_t *m, |
| 204 | const struct timespec *to) |
| 205 | { |
| 206 | int ret = k_sem_take(m->sem, _ts_to_ms(to)); |
| 207 | |
Youvedeep Singh | 325abfb | 2018-02-08 08:50:06 +0530 | [diff] [blame] | 208 | return ret == 0 ? ret : ETIMEDOUT; |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @brief POSIX threading compatibility API |
| 213 | * |
| 214 | * See IEEE 1003.1 |
| 215 | */ |
| 216 | int pthread_mutex_trylock(pthread_mutex_t *m); |
| 217 | |
| 218 | /** |
| 219 | * @brief POSIX threading compatibility API |
| 220 | * |
| 221 | * See IEEE 1003.1 |
| 222 | */ |
| 223 | static inline int pthread_mutex_unlock(pthread_mutex_t *m) |
| 224 | { |
| 225 | k_sem_give(m->sem); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @brief POSIX threading compatibility API |
| 231 | * |
| 232 | * See IEEE 1003.1 |
| 233 | * |
| 234 | * Note that pthread attribute structs are currently noops in Zephyr. |
| 235 | */ |
| 236 | static inline int pthread_mutexattr_init(pthread_mutexattr_t *m) |
| 237 | { |
| 238 | ARG_UNUSED(m); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @brief POSIX threading compatibility API |
| 245 | * |
| 246 | * See IEEE 1003.1 |
| 247 | * |
| 248 | * Note that pthread attribute structs are currently noops in Zephyr. |
| 249 | */ |
| 250 | static inline int pthread_mutexattr_destroy(pthread_mutexattr_t *m) |
| 251 | { |
| 252 | ARG_UNUSED(m); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* FIXME: these are going to be tricky to implement. Zephyr has (for |
| 258 | * good reason) deprecated its own "initializer" macros in favor of a |
| 259 | * static "declaration" macros instead. Using such a macro inside a |
| 260 | * gcc compound expression to declare and object then reference it |
| 261 | * would work, but gcc limits such expressions to function context |
| 262 | * (because they may need to generate code that runs at assignment |
| 263 | * time) and much real-world use of these initializers is for static |
| 264 | * variables. The best trick I can think of would be to declare it in |
| 265 | * a special section and then initialize that section at runtime |
| 266 | * startup, which sort of defeats the purpose of having these be |
| 267 | * static... |
| 268 | * |
| 269 | * Instead, see the nonstandard PTHREAD_*_DEFINE macros instead, which |
| 270 | * work similarly but conform to Zephyr's paradigms. |
| 271 | */ |
| 272 | /* #define PTHREAD_MUTEX_INITIALIZER */ |
| 273 | /* #define PTHREAD_COND_INITIALIZER */ |
| 274 | |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 275 | /** |
| 276 | * @brief Declare a pthread barrier |
| 277 | * |
| 278 | * Declaration API for a pthread barrier. This is not a |
| 279 | * POSIX API, it's provided to better conform with Zephyr's allocation |
| 280 | * strategies for kernel objects. |
| 281 | * |
| 282 | * @param name Symbol name of the barrier |
| 283 | * @param count Thread count, same as the "count" argument to |
| 284 | * pthread_barrier_init() |
| 285 | */ |
| 286 | #define PTHREAD_BARRIER_DEFINE(name, count) \ |
| 287 | struct pthread_barrier name = { \ |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame^] | 288 | .wait_q = _WAIT_Q_INIT(&name.wait_q), \ |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 289 | .max = count, \ |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * @brief POSIX threading compatibility API |
| 294 | * |
| 295 | * See IEEE 1003.1 |
| 296 | */ |
| 297 | int pthread_barrier_wait(pthread_barrier_t *b); |
| 298 | |
| 299 | /** |
| 300 | * @brief POSIX threading compatibility API |
| 301 | * |
| 302 | * See IEEE 1003.1 |
| 303 | */ |
| 304 | static inline int pthread_barrier_init(pthread_barrier_t *b, |
| 305 | const pthread_barrierattr_t *attr, |
| 306 | unsigned int count) |
| 307 | { |
| 308 | ARG_UNUSED(attr); |
| 309 | |
| 310 | b->max = count; |
| 311 | b->count = 0; |
Andy Ross | ccf3bf7 | 2018-05-10 11:10:34 -0700 | [diff] [blame^] | 312 | _waitq_init(&b->wait_q); |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @brief POSIX threading compatibility API |
| 319 | * |
| 320 | * See IEEE 1003.1 |
| 321 | */ |
| 322 | static inline int pthread_barrier_destroy(pthread_barrier_t *b) |
| 323 | { |
| 324 | ARG_UNUSED(b); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @brief POSIX threading compatibility API |
| 331 | * |
| 332 | * See IEEE 1003.1 |
| 333 | * |
| 334 | * Note that pthread attribute structs are currently noops in Zephyr. |
| 335 | */ |
| 336 | static inline int pthread_barrierattr_init(pthread_barrierattr_t *b) |
| 337 | { |
| 338 | ARG_UNUSED(b); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * @brief POSIX threading compatibility API |
| 345 | * |
| 346 | * See IEEE 1003.1 |
| 347 | * |
| 348 | * Note that pthread attribute structs are currently noops in Zephyr. |
| 349 | */ |
| 350 | static inline int pthread_barrierattr_destroy(pthread_barrierattr_t *b) |
| 351 | { |
| 352 | ARG_UNUSED(b); |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | /* Predicates and setters for various pthread attribute values that we |
| 358 | * don't support (or always support: the "process shared" attribute |
| 359 | * can only be true given the way Zephyr implements these |
| 360 | * objects). Leave these undefined for simplicity instead of defining |
| 361 | * stubs to return an error that would have to be logged and |
| 362 | * interpreted just to figure out that we didn't support it in the |
| 363 | * first place. These APIs are very rarely used even in production |
| 364 | * Unix code. Leave the declarations here so they can be easily |
| 365 | * uncommented and implemented as needed. |
| 366 | |
| 367 | int pthread_condattr_getclock(const pthread_condattr_t * clockid_t *); |
| 368 | int pthread_condattr_getpshared(const pthread_condattr_t * int *); |
| 369 | int pthread_condattr_setclock(pthread_condattr_t *, clockid_t); |
| 370 | int pthread_condattr_setpshared(pthread_condattr_t *, int); |
| 371 | int pthread_mutex_consistent(pthread_mutex_t *); |
| 372 | int pthread_mutex_getprioceiling(const pthread_mutex_t * int *); |
| 373 | int pthread_mutex_setprioceiling(pthread_mutex_t *, int int *); |
| 374 | int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *); |
| 375 | int pthread_mutexattr_getprotocol(const pthread_mutexattr_t * int *); |
| 376 | int pthread_mutexattr_getpshared(const pthread_mutexattr_t * int *); |
| 377 | int pthread_mutexattr_getrobust(const pthread_mutexattr_t * int *); |
| 378 | int pthread_mutexattr_gettype(const pthread_mutexattr_t * int *); |
| 379 | int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int); |
| 380 | int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int); |
| 381 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int); |
| 382 | int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int); |
| 383 | int pthread_mutexattr_settype(pthread_mutexattr_t *, int); |
| 384 | int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *); |
| 385 | int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int); |
| 386 | */ |
| 387 | |
Youvedeep Singh | c8aa657 | 2017-12-28 14:31:57 +0530 | [diff] [blame] | 388 | /* Base Pthread related APIs */ |
| 389 | |
| 390 | /** |
| 391 | * @brief Obtain ID of the calling thread. |
| 392 | * |
| 393 | * The results of calling this API from threads not created with |
| 394 | * pthread_create() are undefined. |
| 395 | * |
| 396 | * See IEEE 1003.1 |
| 397 | */ |
| 398 | static inline pthread_t pthread_self(void) |
| 399 | { |
| 400 | return (pthread_t)k_current_get(); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /** |
| 405 | * @brief Compare thread IDs. |
| 406 | * |
| 407 | * See IEEE 1003.1 |
| 408 | */ |
| 409 | static inline int pthread_equal(pthread_t pt1, pthread_t pt2) |
| 410 | { |
| 411 | return (pt1 == pt2); |
| 412 | } |
| 413 | |
Youvedeep Singh | 216883c | 2018-02-28 15:37:00 +0530 | [diff] [blame] | 414 | /** |
| 415 | * @brief Destroy the read-write lock attributes object. |
| 416 | * |
| 417 | * See IEEE 1003.1 |
| 418 | */ |
| 419 | static inline int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr) |
| 420 | { |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * @brief initialize the read-write lock attributes object. |
| 426 | * |
| 427 | * See IEEE 1003.1 |
| 428 | */ |
| 429 | static inline int pthread_rwlockattr_init(pthread_rwlockattr_t *attr) |
| 430 | { |
| 431 | return 0; |
| 432 | } |
| 433 | |
Youvedeep Singh | c8aa657 | 2017-12-28 14:31:57 +0530 | [diff] [blame] | 434 | int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize); |
| 435 | int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); |
| 436 | int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy); |
| 437 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); |
| 438 | int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate); |
| 439 | int pthread_attr_init(pthread_attr_t *attr); |
| 440 | int pthread_attr_destroy(pthread_attr_t *attr); |
| 441 | int pthread_attr_getschedparam(const pthread_attr_t *attr, |
| 442 | struct sched_param *schedparam); |
| 443 | int pthread_getschedparam(pthread_t pthread, int *policy, |
| 444 | struct sched_param *param); |
| 445 | int pthread_attr_getstack(const pthread_attr_t *attr, |
| 446 | void **stackaddr, size_t *stacksize); |
| 447 | int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, |
| 448 | size_t stacksize); |
| 449 | void pthread_exit(void *retval); |
| 450 | int pthread_join(pthread_t thread, void **status); |
| 451 | int pthread_cancel(pthread_t pthread); |
| 452 | int pthread_detach(pthread_t thread); |
| 453 | int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, |
| 454 | void *(*threadroutine)(void *), void *arg); |
| 455 | int pthread_setcancelstate(int state, int *oldstate); |
| 456 | int pthread_attr_setschedparam(pthread_attr_t *attr, |
| 457 | const struct sched_param *schedparam); |
| 458 | int pthread_setschedparam(pthread_t pthread, int policy, |
| 459 | const struct sched_param *param); |
Youvedeep Singh | 216883c | 2018-02-28 15:37:00 +0530 | [diff] [blame] | 460 | int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); |
| 461 | int pthread_rwlock_init(pthread_rwlock_t *rwlock, |
| 462 | const pthread_rwlockattr_t *attr); |
| 463 | int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); |
| 464 | int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, |
| 465 | const struct timespec *abstime); |
| 466 | int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, |
| 467 | const struct timespec *abstime); |
| 468 | int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); |
| 469 | int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); |
| 470 | int pthread_rwlock_unlock(pthread_rwlock_t *rwlock); |
| 471 | int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); |
Youvedeep Singh | c8aa657 | 2017-12-28 14:31:57 +0530 | [diff] [blame] | 472 | |
Andy Ross | 53c8599 | 2017-07-24 14:59:55 -0700 | [diff] [blame] | 473 | #endif /* __PTHREAD_H__ */ |