xrp_thread_impl.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2018 Cadence Design Systems Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef _XRP_THREAD_PTHREAD_IMPL_H
  24. #define _XRP_THREAD_PTHREAD_IMPL_H
  25. #include <pthread.h>
  26. typedef pthread_t xrp_thread;
  27. typedef pthread_mutex_t xrp_mutex;
  28. typedef struct xrp_cond {
  29. pthread_mutex_t mutex;
  30. pthread_cond_t cond;
  31. } xrp_cond;
  32. static inline void xrp_mutex_init(xrp_mutex *p)
  33. {
  34. pthread_mutex_init(p, NULL);
  35. }
  36. static inline void xrp_mutex_lock(xrp_mutex *p)
  37. {
  38. pthread_mutex_lock(p);
  39. }
  40. static inline void xrp_mutex_unlock(xrp_mutex *p)
  41. {
  42. pthread_mutex_unlock(p);
  43. }
  44. static inline void xrp_mutex_destroy(xrp_mutex *p)
  45. {
  46. pthread_mutex_destroy(p);
  47. }
  48. static inline void xrp_cond_init(xrp_cond *p)
  49. {
  50. pthread_mutex_init(&p->mutex, NULL);
  51. pthread_cond_init(&p->cond, NULL);
  52. }
  53. static inline void xrp_cond_lock(xrp_cond *p)
  54. {
  55. pthread_mutex_lock(&p->mutex);
  56. }
  57. static inline void xrp_cond_unlock(xrp_cond *p)
  58. {
  59. pthread_mutex_unlock(&p->mutex);
  60. }
  61. static inline void xrp_cond_broadcast(xrp_cond *p)
  62. {
  63. pthread_cond_broadcast(&p->cond);
  64. }
  65. static inline void xrp_cond_wait(xrp_cond *p)
  66. {
  67. pthread_cond_wait(&p->cond, &p->mutex);
  68. }
  69. static inline void xrp_cond_destroy(xrp_cond *p)
  70. {
  71. pthread_mutex_destroy(&p->mutex);
  72. pthread_cond_destroy(&p->cond);
  73. }
  74. static inline int xrp_thread_create(xrp_thread *thread, int priority,
  75. void *(*thread_func)(void *),
  76. void *p)
  77. {
  78. (void)priority;
  79. return pthread_create(thread, NULL, thread_func, p) == 0;
  80. }
  81. static inline int xrp_thread_join(xrp_thread *thread)
  82. {
  83. return pthread_join(*thread, NULL) == 0;
  84. }
  85. static inline int xrp_thread_detach(xrp_thread *thread)
  86. {
  87. return pthread_detach(*thread) == 0;
  88. }
  89. #endif