BMutex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  32. #include <pthread.h>
  33. #endif
  34. #include <misc/debug.h>
  35. #include <base/DebugObject.h>
  36. typedef struct {
  37. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  38. pthread_mutex_t pthread_mutex;
  39. DebugObject d_obj;
  40. #endif
  41. } BMutex;
  42. static int BMutex_Init (BMutex *o) WARN_UNUSED;
  43. static void BMutex_Free (BMutex *o);
  44. static void BMutex_Lock (BMutex *o);
  45. static void BMutex_Unlock (BMutex *o);
  46. static int BMutex_Init (BMutex *o)
  47. {
  48. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  49. if (pthread_mutex_init(&o->pthread_mutex, NULL) != 0) {
  50. return 0;
  51. }
  52. DebugObject_Init(&o->d_obj);
  53. return 1;
  54. #else
  55. ASSERT(0)
  56. return 0;
  57. #endif
  58. }
  59. static void BMutex_Free (BMutex *o)
  60. {
  61. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  62. DebugObject_Free(&o->d_obj);
  63. int res = pthread_mutex_destroy(&o->pthread_mutex);
  64. B_USE(res)
  65. ASSERT(res == 0)
  66. #else
  67. ASSERT(0)
  68. #endif
  69. }
  70. static void BMutex_Lock (BMutex *o)
  71. {
  72. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  73. DebugObject_Access(&o->d_obj);
  74. int res = pthread_mutex_lock(&o->pthread_mutex);
  75. B_USE(res)
  76. ASSERT(res == 0)
  77. #else
  78. ASSERT(0)
  79. #endif
  80. }
  81. static void BMutex_Unlock (BMutex *o)
  82. {
  83. #ifdef BADVPN_THREADWORK_USE_PTHREAD
  84. DebugObject_Access(&o->d_obj);
  85. int res = pthread_mutex_unlock(&o->pthread_mutex);
  86. B_USE(res)
  87. ASSERT(res == 0)
  88. #else
  89. ASSERT(0)
  90. #endif
  91. }
  92. #endif