jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.0KB

  1. /*
  2. Copyright (C) 2006 Grame
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
  15. grame@grame.fr
  16. */
  17. #ifndef __JackPosixMutex__
  18. #define __JackPosixMutex__
  19. #include <pthread.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include "JackError.h"
  23. namespace Jack
  24. {
  25. /*!
  26. \brief Mutex abstraction.
  27. */
  28. class JackBasePosixMutex
  29. {
  30. protected:
  31. pthread_mutex_t fMutex;
  32. public:
  33. JackBasePosixMutex()
  34. {
  35. pthread_mutex_init(&fMutex, NULL);
  36. }
  37. virtual ~JackBasePosixMutex()
  38. {
  39. pthread_mutex_destroy(&fMutex);
  40. }
  41. void Lock()
  42. {
  43. int res = pthread_mutex_lock(&fMutex);
  44. if (res != 0)
  45. jack_log("JackBasePosixMutex::Lock res = %d", res);
  46. }
  47. bool Trylock()
  48. {
  49. return (pthread_mutex_trylock(&fMutex) == 0);
  50. }
  51. void Unlock()
  52. {
  53. int res = pthread_mutex_unlock(&fMutex);
  54. if (res != 0)
  55. jack_log("JackBasePosixMutex::Unlock res = %d", res);
  56. }
  57. };
  58. class JackPosixMutex
  59. {
  60. protected:
  61. pthread_mutex_t fMutex;
  62. public:
  63. JackPosixMutex()
  64. {
  65. // Use recursive mutex
  66. pthread_mutexattr_t mutex_attr;
  67. int res;
  68. res = pthread_mutexattr_init(&mutex_attr);
  69. assert(res == 0);
  70. res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
  71. assert(res == 0);
  72. res = pthread_mutex_init(&fMutex, &mutex_attr);
  73. assert(res == 0);
  74. res = pthread_mutexattr_destroy(&mutex_attr);
  75. assert(res == 0);
  76. }
  77. virtual ~JackPosixMutex()
  78. {
  79. pthread_mutex_destroy(&fMutex);
  80. }
  81. bool Lock()
  82. {
  83. int res = pthread_mutex_lock(&fMutex);
  84. if (res != 0)
  85. jack_log("JackPosixMutex::Lock res = %d", res);
  86. return (res == 0);
  87. }
  88. bool Trylock()
  89. {
  90. return (pthread_mutex_trylock(&fMutex) == 0);
  91. }
  92. bool Unlock()
  93. {
  94. int res = pthread_mutex_unlock(&fMutex);
  95. if (res != 0)
  96. jack_log("JackPosixMutex::Unlock res = %d", res);
  97. return (res == 0);
  98. }
  99. };
  100. } // namespace
  101. #endif