uClibc_mutex.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright (C) 2006 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #ifndef _UCLIBC_MUTEX_H
  8. #define _UCLIBC_MUTEX_H
  9. #include <features.h>
  10. #ifdef __UCLIBC_HAS_THREADS__
  11. #include <pthread.h>
  12. #ifdef _LIBC
  13. #include <bits/uClibc_pthread.h>
  14. #endif
  15. #define __UCLIBC_MUTEX_TYPE pthread_mutex_t
  16. #define __UCLIBC_MUTEX(M) pthread_mutex_t M
  17. #define __UCLIBC_MUTEX_INIT(M,I) pthread_mutex_t M = I
  18. #define __UCLIBC_MUTEX_STATIC(M,I) static pthread_mutex_t M = I
  19. #define __UCLIBC_MUTEX_EXTERN(M) extern pthread_mutex_t M
  20. #define __UCLIBC_MUTEX_INIT_VAR(M) \
  21. ((M) = (pthread_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  22. #define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) \
  23. __pthread_mutex_lock(&(M))
  24. #define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) \
  25. __pthread_mutex_unlock(&(M))
  26. #define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) \
  27. __pthread_mutex_trylock(&(M))
  28. #define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) \
  29. do { \
  30. struct _pthread_cleanup_buffer __infunc_pthread_cleanup_buffer; \
  31. int __infunc_need_locking = (C); \
  32. if (__infunc_need_locking) { \
  33. _pthread_cleanup_push_defer(&__infunc_pthread_cleanup_buffer, \
  34. (void (*) (void *))__pthread_mutex_unlock, \
  35. &(M)); \
  36. __pthread_mutex_lock(&(M)); \
  37. } \
  38. ((void)0)
  39. #define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) \
  40. if (__infunc_need_locking) { \
  41. _pthread_cleanup_pop_restore(&__infunc_pthread_cleanup_buffer,1); \
  42. } \
  43. } while (0)
  44. #define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) int A
  45. #define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) \
  46. __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,((A=(V)) == 0))
  47. #define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) \
  48. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,(A == 0))
  49. #define __UCLIBC_MUTEX_LOCK(M) \
  50. __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  51. #define __UCLIBC_MUTEX_UNLOCK(M) \
  52. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  53. #ifdef __USE_STDIO_FUTEXES__
  54. #include <bits/stdio-lock.h>
  55. #define __UCLIBC_IO_MUTEX(M) _IO_lock_t M
  56. #define __UCLIBC_IO_MUTEX_LOCK(M) _IO_lock_lock(M)
  57. #define __UCLIBC_IO_MUTEX_UNLOCK(M) _IO_lock_unlock(M)
  58. #define __UCLIBC_IO_MUTEX_TRYLOCK(M) _IO_lock_trylock(M)
  59. #define __UCLIBC_IO_MUTEX_INIT(M) _IO_lock_t M = _IO_lock_initializer
  60. #define __UCLIBC_IO_MUTEX_EXTERN(M) extern _IO_lock_t M
  61. #define __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,C) \
  62. if (C) { \
  63. _IO_lock_lock(M); \
  64. }
  65. #define __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,C) \
  66. if (C) { \
  67. _IO_lock_unlock(M); \
  68. }
  69. #define __UCLIBC_IO_MUTEX_AUTO_LOCK(M,A,V) \
  70. __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,((A=(V))) == 0)
  71. #define __UCLIBC_IO_MUTEX_AUTO_UNLOCK(M,A) \
  72. __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,((A) == 0))
  73. #define __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(M) _IO_lock_lock(M)
  74. #define __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(M) _IO_lock_unlock(M)
  75. #else /* of __USE_STDIO_FUTEXES__ */
  76. #define __UCLIBC_IO_MUTEX(M) __UCLIBC_MUTEX(M)
  77. #define __UCLIBC_IO_MUTEX_LOCK(M) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  78. #define __UCLIBC_IO_MUTEX_UNLOCK(M) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  79. #define __UCLIBC_IO_MUTEX_TRYLOCK(M) __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M)
  80. #define __UCLIBC_IO_MUTEX_INIT(M) __UCLIBC_MUTEX_INIT(M, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  81. #define __UCLIBC_IO_MUTEX_EXTERN(M) __UCLIBC_MUTEX_EXTERN(M)
  82. #define __UCLIBC_IO_MUTEX_AUTO_LOCK(M,A,V) __UCLIBC_MUTEX_AUTO_LOCK(M,A,V)
  83. #define __UCLIBC_IO_MUTEX_AUTO_UNLOCK(M,A) __UCLIBC_MUTEX_AUTO_UNLOCK(M,A)
  84. #define __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M)
  85. #define __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M)
  86. #define __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C)
  87. #define __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C)
  88. #endif /* of __USE_STDIO_FUTEXES__ */
  89. #else /* of __UCLIBC_HAS_THREADS__ */
  90. #define __UCLIBC_MUTEX(M) void *__UCLIBC_MUTEX_DUMMY_ ## M
  91. #define __UCLIBC_MUTEX_INIT(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  92. #define __UCLIBC_MUTEX_STATIC(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  93. #define __UCLIBC_MUTEX_EXTERN(M) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  94. #define __UCLIBC_MUTEX_INIT_VAR(M) ((void)0)
  95. #define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) ((void)0)
  96. #define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) ((void)0)
  97. #define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) (0) /* Always succeed? */
  98. #define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) ((void)0)
  99. #define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) ((void)0)
  100. #define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) ((void)0)
  101. #define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) ((void)0)
  102. #define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) ((void)0)
  103. #define __UCLIBC_MUTEX_LOCK(M) ((void)0)
  104. #define __UCLIBC_MUTEX_UNLOCK(M) ((void)0)
  105. #define __UCLIBC_IO_MUTEX(M) __UCLIBC_MUTEX(M)
  106. #define __UCLIBC_IO_MUTEX_LOCK(M) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  107. #define __UCLIBC_IO_MUTEX_UNLOCK(M) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  108. #define __UCLIBC_IO_MUTEX_TRYLOCK(M) __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M)
  109. #define __UCLIBC_IO_MUTEX_INIT(M) __UCLIBC_MUTEX_INIT(M, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  110. #define __UCLIBC_IO_MUTEX_EXTERN(M) __UCLIBC_MUTEX_EXTERN(M)
  111. #define __UCLIBC_IO_MUTEX_AUTO_LOCK(M,A,V) __UCLIBC_MUTEX_AUTO_LOCK(M,A,V)
  112. #define __UCLIBC_IO_MUTEX_AUTO_UNLOCK(M,A) __UCLIBC_MUTEX_AUTO_UNLOCK(M,A)
  113. #define __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M)
  114. #define __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M)
  115. #define __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C)
  116. #define __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C)
  117. #endif /* of __UCLIBC_HAS_THREADS__ */
  118. #define __UCLIBC_IO_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) \
  119. __UCLIBC_IO_MUTEX_TRYLOCK(M)
  120. #endif /* _UCLIBC_MUTEX_H */