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.

147 lines
3.6KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program 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
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackWinMutex__
  16. #define __JackWinMutex__
  17. #include <windows.h>
  18. namespace Jack
  19. {
  20. /*!
  21. \brief Mutex abstraction.
  22. */
  23. class JackBaseWinMutex
  24. {
  25. protected:
  26. HANDLE fMutex;
  27. DWORD fOwner;
  28. public:
  29. JackBaseWinMutex():fOwner(0)
  30. {
  31. // In recursive mode by default
  32. fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
  33. ThrowIf(fMutex == 0, JackException("JackWinMutex: could not init the mutex"));
  34. }
  35. virtual ~JackBaseWinMutex()
  36. {
  37. CloseHandle(fMutex);
  38. }
  39. bool Lock()
  40. {
  41. if (fOwner != GetCurrentThreadId()) {
  42. DWORD res = WaitForSingleObject(fMutex, INFINITE);
  43. if (res == WAIT_OBJECT_0) {
  44. fOwner = GetCurrentThreadId();
  45. return true;
  46. } else {
  47. jack_log("JackWinMutex::Lock res = %d", res);
  48. return false;
  49. }
  50. } else {
  51. jack_error("JackWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
  52. return false;
  53. }
  54. }
  55. bool Trylock()
  56. {
  57. if (fOwner != GetCurrentThreadId()) {
  58. DWORD res = WaitForSingleObject(fMutex, 0);
  59. if (res == WAIT_OBJECT_0) {
  60. fOwner = GetCurrentThreadId();
  61. return true;
  62. } else {
  63. jack_log("JackWinMutex::Trylock res = %d", res);
  64. return false;
  65. }
  66. } else {
  67. jack_error("JackWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
  68. return false;
  69. }
  70. }
  71. bool Unlock()
  72. {
  73. if (fOwner == GetCurrentThreadId()) {
  74. fOwner = 0;
  75. int res = ReleaseMutex(fMutex);
  76. if (res != 0) {
  77. return true;
  78. } else {
  79. jack_log("JackWinMutex::Unlock res = %d", res);
  80. return false;
  81. }
  82. } else {
  83. jack_error("JackWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
  84. return false;
  85. }
  86. }
  87. };
  88. class JackWinMutex
  89. {
  90. protected:
  91. HANDLE fMutex;
  92. public:
  93. JackWinMutex()
  94. {
  95. // In recursive mode by default
  96. fMutex = (HANDLE)CreateMutex(0, FALSE, 0);
  97. }
  98. virtual ~JackWinMutex()
  99. {
  100. CloseHandle(fMutex);
  101. }
  102. bool Lock()
  103. {
  104. return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, INFINITE));
  105. }
  106. bool Trylock()
  107. {
  108. return (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0));
  109. }
  110. bool Unlock()
  111. {
  112. return(ReleaseMutex(fMutex) != 0);
  113. }
  114. };
  115. } // namespace
  116. #endif