uClibc_mutex.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #else
  49. #define __UCLIBC_MUTEX(M) void *__UCLIBC_MUTEX_DUMMY_ ## M
  50. #define __UCLIBC_MUTEX_INIT(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  51. #define __UCLIBC_MUTEX_STATIC(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  52. #define __UCLIBC_MUTEX_EXTERN(M) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
  53. #define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) ((void)0)
  54. #define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) ((void)0)
  55. #define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) (0) /* Always succeed? */
  56. #define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) ((void)0)
  57. #define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) ((void)0)
  58. #define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) ((void)0)
  59. #define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) ((void)0)
  60. #define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) ((void)0)
  61. #define __UCLIBC_MUTEX_LOCK(M) ((void)0)
  62. #define __UCLIBC_MUTEX_UNLOCK(M) ((void)0)
  63. #endif
  64. #endif /* _UCLIBC_MUTEX_H */