uClibc_mutex.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include <bits/uClibc_pthread.h>
  13. #define __UCLIBC_MUTEX_TYPE pthread_mutex_t
  14. #define __UCLIBC_MUTEX(M) pthread_mutex_t M
  15. #define __UCLIBC_MUTEX_INIT(M,I) pthread_mutex_t M = I
  16. #define __UCLIBC_MUTEX_STATIC(M,I) static pthread_mutex_t M = I
  17. #define __UCLIBC_MUTEX_EXTERN(M) extern pthread_mutex_t M
  18. #define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) \
  19. __pthread_mutex_lock(&(M))
  20. #define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) \
  21. __pthread_mutex_unlock(&(M))
  22. #define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) \
  23. __pthread_mutex_trylock(&(M))
  24. #define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) \
  25. do { \
  26. struct _pthread_cleanup_buffer __infunc_pthread_cleanup_buffer; \
  27. if (C) { \
  28. _pthread_cleanup_push_defer(&__infunc_pthread_cleanup_buffer, \
  29. (void (*) (void *))__pthread_mutex_unlock, \
  30. &(M)); \
  31. __pthread_mutex_lock(&(M)); \
  32. } \
  33. ((void)0)
  34. #define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) \
  35. if (C) { \
  36. _pthread_cleanup_pop_restore(&__infunc_pthread_cleanup_buffer,1); \
  37. } \
  38. } while (0)
  39. #define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) int A
  40. #define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) \
  41. __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,((A=(V)) == 0))
  42. #define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) \
  43. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,(A == 0))
  44. #define __UCLIBC_MUTEX_LOCK(M) \
  45. __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  46. #define __UCLIBC_MUTEX_UNLOCK(M) \
  47. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  48. #ifdef __USE_STDIO_FUTEXES__
  49. #include <bits/stdio-lock.h>
  50. #define __UCLIBC_IO_MUTEX(M) _IO_lock_t M
  51. #define __UCLIBC_IO_MUTEX_LOCK(M) _IO_lock_lock(M)
  52. #define __UCLIBC_IO_MUTEX_UNLOCK(M) _IO_lock_unlock(M)
  53. #define __UCLIBC_IO_MUTEX_TRYLOCK(M) _IO_lock_trylock(M)
  54. #define __UCLIBC_IO_MUTEX_INIT(M) _IO_lock_t M = _IO_lock_initializer
  55. #define __UCLIBC_IO_MUTEX_EXTERN(M) extern _IO_lock_t M
  56. #define __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,C) \
  57. if (C) { \
  58. _IO_lock_lock(M); \
  59. }
  60. #define __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,C) \
  61. if (C) { \
  62. _IO_lock_unlock(M); \
  63. }
  64. #define __UCLIBC_IO_MUTEX_AUTO_LOCK(M,A,V) \
  65. __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,((A=(V))) == 0)
  66. #define __UCLIBC_IO_MUTEX_AUTO_UNLOCK(M,A) \
  67. __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,((A) == 0))
  68. #define __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(M) _IO_lock_lock(M)
  69. #define __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(M) _IO_lock_unlock(M)
  70. #else /* of __USE_STDIO_FUTEXES__ */
  71. #define __UCLIBC_IO_MUTEX(M) __UCLIBC_MUTEX(M)
  72. #define __UCLIBC_IO_MUTEX_LOCK(M) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  73. #define __UCLIBC_IO_MUTEX_UNLOCK(M) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  74. #define __UCLIBC_IO_MUTEX_TRYLOCK(M) __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M)
  75. #define __UCLIBC_IO_MUTEX_INIT(M) __UCLIBC_MUTEX_INIT(M, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  76. #define __UCLIBC_IO_MUTEX_EXTERN(M) __UCLIBC_MUTEX_EXTERN(M)
  77. #define __UCLIBC_IO_MUTEX_AUTO_LOCK(M,A,V) __UCLIBC_MUTEX_AUTO_LOCK(M,A,V)
  78. #define __UCLIBC_IO_MUTEX_AUTO_UNLOCK(M,A) __UCLIBC_MUTEX_AUTO_UNLOCK(M,A)
  79. #define __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M)
  80. #define __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M)
  81. #define __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  82. #define __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  83. #endif /* of __USE_STDIO_FUTEXES__ */
  84. #else /* of __UCLIBC_HAS_THREADS__ */
  85. #define __UCLIBC_MUTEX(M) void *__UCLIBC_MUTEX_DUMMY_ ## M
  86. #define __UCLIBC_MUTEX_INIT(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  87. #define __UCLIBC_MUTEX_STATIC(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  88. #define __UCLIBC_MUTEX_EXTERN(M) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  89. #define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) ((void)0)
  90. #define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) ((void)0)
  91. #define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) (0) /* Always succeed? */
  92. #define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) ((void)0)
  93. #define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) ((void)0)
  94. #define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) ((void)0)
  95. #define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) ((void)0)
  96. #define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) ((void)0)
  97. #define __UCLIBC_MUTEX_LOCK(M) ((void)0)
  98. #define __UCLIBC_MUTEX_UNLOCK(M) ((void)0)
  99. #define __UCLIBC_IO_MUTEX(M) __UCLIBC_MUTEX(M)
  100. #define __UCLIBC_IO_MUTEX_LOCK(M) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  101. #define __UCLIBC_IO_MUTEX_UNLOCK(M) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  102. #define __UCLIBC_IO_MUTEX_TRYLOCK(M) __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M)
  103. #define __UCLIBC_IO_MUTEX_INIT(M) __UCLIBC_MUTEX_INIT(M, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
  104. #define __UCLIBC_IO_MUTEX_EXTERN(M) __UCLIBC_MUTEX_EXTERN(M)
  105. #define __UCLIBC_IO_MUTEX_AUTO_LOCK(M,A,V) __UCLIBC_MUTEX_AUTO_LOCK(M,A,V)
  106. #define __UCLIBC_IO_MUTEX_AUTO_UNLOCK(M,A) __UCLIBC_MUTEX_AUTO_UNLOCK(M,A)
  107. #define __UCLIBC_IO_MUTEX_LOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M)
  108. #define __UCLIBC_IO_MUTEX_UNLOCK_CANCEL_UNSAFE(M) __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M)
  109. #define __UCLIBC_IO_MUTEX_CONDITIONAL_LOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
  110. #define __UCLIBC_IO_MUTEX_CONDITIONAL_UNLOCK(M,C) __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
  111. #endif /* of __UCLIBC_HAS_THREADS__ */
  112. #define __UCLIBC_IO_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) \
  113. __UCLIBC_IO_MUTEX_TRYLOCK(M)
  114. #endif /* _UCLIBC_MUTEX_H */