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.

135 lines
3.8KB

  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. #include "JackPosixMutex.h"
  18. #include "JackError.h"
  19. namespace Jack
  20. {
  21. JackBasePosixMutex::JackBasePosixMutex(const char* name)
  22. :fOwner(0)
  23. {
  24. int res = pthread_mutex_init(&fMutex, NULL);
  25. ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex"));
  26. }
  27. JackBasePosixMutex::~JackBasePosixMutex()
  28. {
  29. pthread_mutex_destroy(&fMutex);
  30. }
  31. bool JackBasePosixMutex::Lock()
  32. {
  33. pthread_t current_thread = pthread_self();
  34. if (!pthread_equal(current_thread, fOwner)) {
  35. int res = pthread_mutex_lock(&fMutex);
  36. if (res == 0) {
  37. fOwner = current_thread;
  38. return true;
  39. } else {
  40. jack_error("JackBasePosixMutex::Lock res = %d", res);
  41. return false;
  42. }
  43. } else {
  44. return false;
  45. }
  46. }
  47. bool JackBasePosixMutex::Trylock()
  48. {
  49. pthread_t current_thread = pthread_self();
  50. if (!pthread_equal(current_thread, fOwner)) {
  51. int res = pthread_mutex_trylock(&fMutex);
  52. if (res == 0) {
  53. fOwner = current_thread;
  54. return true;
  55. } else {
  56. return false;
  57. }
  58. } else {
  59. return false;
  60. }
  61. }
  62. bool JackBasePosixMutex::Unlock()
  63. {
  64. if (pthread_equal(pthread_self(), fOwner)) {
  65. fOwner = 0;
  66. int res = pthread_mutex_unlock(&fMutex);
  67. if (res == 0) {
  68. return true;
  69. } else {
  70. jack_error("JackBasePosixMutex::Unlock res = %d", res);
  71. return false;
  72. }
  73. } else {
  74. return false;
  75. }
  76. }
  77. JackPosixMutex::JackPosixMutex(const char* name)
  78. {
  79. // Use recursive mutex
  80. pthread_mutexattr_t mutex_attr;
  81. int res;
  82. res = pthread_mutexattr_init(&mutex_attr);
  83. ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex attribute"));
  84. res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
  85. ThrowIf(res != 0, JackException("JackBasePosixMutex: could not settype the mutex"));
  86. res = pthread_mutex_init(&fMutex, &mutex_attr);
  87. ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex"));
  88. pthread_mutexattr_destroy(&mutex_attr);
  89. }
  90. JackPosixMutex::~JackPosixMutex()
  91. {
  92. pthread_mutex_destroy(&fMutex);
  93. }
  94. bool JackPosixMutex::Lock()
  95. {
  96. int res = pthread_mutex_lock(&fMutex);
  97. if (res != 0) {
  98. jack_log("JackPosixMutex::Lock res = %d", res);
  99. }
  100. return (res == 0);
  101. }
  102. bool JackPosixMutex::Trylock()
  103. {
  104. return (pthread_mutex_trylock(&fMutex) == 0);
  105. }
  106. bool JackPosixMutex::Unlock()
  107. {
  108. int res = pthread_mutex_unlock(&fMutex);
  109. if (res != 0) {
  110. jack_log("JackPosixMutex::Unlock res = %d", res);
  111. }
  112. return (res == 0);
  113. }
  114. } // namespace