BMutex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @file BMutex.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_BMUTEX_H
  30. #define BADVPN_BMUTEX_H
  31. #if !defined(BADVPN_THREAD_SAFE) || (BADVPN_THREAD_SAFE != 0 && BADVPN_THREAD_SAFE != 1)
  32. #error BADVPN_THREAD_SAFE is not defined or incorrect
  33. #endif
  34. #if BADVPN_THREAD_SAFE
  35. #include <pthread.h>
  36. #endif
  37. #include <misc/debug.h>
  38. #include <base/DebugObject.h>
  39. typedef struct {
  40. #if BADVPN_THREAD_SAFE
  41. pthread_mutex_t pthread_mutex;
  42. #endif
  43. DebugObject d_obj;
  44. } BMutex;
  45. static int BMutex_Init (BMutex *o) WARN_UNUSED;
  46. static void BMutex_Free (BMutex *o);
  47. static void BMutex_Lock (BMutex *o);
  48. static void BMutex_Unlock (BMutex *o);
  49. static int BMutex_Init (BMutex *o)
  50. {
  51. #if BADVPN_THREAD_SAFE
  52. if (pthread_mutex_init(&o->pthread_mutex, NULL) != 0) {
  53. return 0;
  54. }
  55. #endif
  56. DebugObject_Init(&o->d_obj);
  57. return 1;
  58. }
  59. static void BMutex_Free (BMutex *o)
  60. {
  61. DebugObject_Free(&o->d_obj);
  62. #if BADVPN_THREAD_SAFE
  63. int res = pthread_mutex_destroy(&o->pthread_mutex);
  64. B_USE(res)
  65. ASSERT(res == 0)
  66. #endif
  67. }
  68. static void BMutex_Lock (BMutex *o)
  69. {
  70. DebugObject_Access(&o->d_obj);
  71. #if BADVPN_THREAD_SAFE
  72. int res = pthread_mutex_lock(&o->pthread_mutex);
  73. B_USE(res)
  74. ASSERT(res == 0)
  75. #endif
  76. }
  77. static void BMutex_Unlock (BMutex *o)
  78. {
  79. DebugObject_Access(&o->d_obj);
  80. #if BADVPN_THREAD_SAFE
  81. int res = pthread_mutex_unlock(&o->pthread_mutex);
  82. B_USE(res)
  83. ASSERT(res == 0)
  84. #endif
  85. }
  86. #endif