mutex.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*------------------------------------------------------------------------
  2. * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #ifndef _ZBAR_MUTEX_H_
  24. #define _ZBAR_MUTEX_H_
  25. #include <config.h>
  26. /* simple platform mutex abstraction
  27. */
  28. #if defined(_WIN32)
  29. # include <windows.h>
  30. # define DEBUG_LOCKS
  31. # ifdef DEBUG_LOCKS
  32. typedef struct zbar_mutex_s {
  33. int count;
  34. CRITICAL_SECTION mutex;
  35. } zbar_mutex_t;
  36. static inline int _zbar_mutex_init (zbar_mutex_t *lock)
  37. {
  38. lock->count = 1;
  39. InitializeCriticalSection(&lock->mutex);
  40. return(0);
  41. }
  42. static inline void _zbar_mutex_destroy (zbar_mutex_t *lock)
  43. {
  44. DeleteCriticalSection(&lock->mutex);
  45. }
  46. static inline int _zbar_mutex_lock (zbar_mutex_t *lock)
  47. {
  48. EnterCriticalSection(&lock->mutex);
  49. if(lock->count++ < 1)
  50. assert(0);
  51. return(0);
  52. }
  53. static inline int _zbar_mutex_unlock (zbar_mutex_t *lock)
  54. {
  55. if(lock->count-- <= 1)
  56. assert(0);
  57. LeaveCriticalSection(&lock->mutex);
  58. return(0);
  59. }
  60. # else
  61. typedef CRITICAL_SECTION zbar_mutex_t;
  62. static inline int _zbar_mutex_init (zbar_mutex_t *lock)
  63. {
  64. InitializeCriticalSection(lock);
  65. return(0);
  66. }
  67. static inline void _zbar_mutex_destroy (zbar_mutex_t *lock)
  68. {
  69. DeleteCriticalSection(lock);
  70. }
  71. static inline int _zbar_mutex_lock (zbar_mutex_t *lock)
  72. {
  73. EnterCriticalSection(lock);
  74. return(0);
  75. }
  76. static inline int _zbar_mutex_unlock (zbar_mutex_t *lock)
  77. {
  78. LeaveCriticalSection(lock);
  79. return(0);
  80. }
  81. # endif
  82. #elif defined(HAVE_LIBPTHREAD)
  83. # include <pthread.h>
  84. typedef pthread_mutex_t zbar_mutex_t;
  85. static inline int _zbar_mutex_init (zbar_mutex_t *lock)
  86. {
  87. # ifdef DEBUG_LOCKS
  88. pthread_mutexattr_t attr;
  89. pthread_mutexattr_init(&attr);
  90. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
  91. int rc = pthread_mutex_init(lock, &attr);
  92. pthread_mutexattr_destroy(&attr);
  93. return(rc);
  94. # else
  95. return(pthread_mutex_init(lock, NULL));
  96. # endif
  97. }
  98. static inline void _zbar_mutex_destroy (zbar_mutex_t *lock)
  99. {
  100. pthread_mutex_destroy(lock);
  101. }
  102. static inline int _zbar_mutex_lock (zbar_mutex_t *lock)
  103. {
  104. int rc = pthread_mutex_lock(lock);
  105. # ifdef DEBUG_LOCKS
  106. assert(!rc);
  107. # endif
  108. /* FIXME save system code */
  109. /*rc = err_capture(proc, SEV_ERROR, ZBAR_ERR_LOCKING, __func__,
  110. "unable to lock processor");*/
  111. return(rc);
  112. }
  113. static inline int _zbar_mutex_unlock (zbar_mutex_t *lock)
  114. {
  115. int rc = pthread_mutex_unlock(lock);
  116. # ifdef DEBUG_LOCKS
  117. assert(!rc);
  118. # endif
  119. /* FIXME save system code */
  120. return(rc);
  121. }
  122. #else
  123. typedef int zbar_mutex_t[0];
  124. #define _zbar_mutex_init(l) -1
  125. #define _zbar_mutex_destroy(l)
  126. #define _zbar_mutex_lock(l) 0
  127. #define _zbar_mutex_unlock(l) 0
  128. #endif
  129. #endif