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.

127 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. #include "JackWinMutex.h"
  16. #include "JackError.h"
  17. namespace Jack
  18. {
  19. bool JackBaseWinMutex::Lock()
  20. {
  21. if (fOwner != GetCurrentThreadId()) {
  22. DWORD res = WaitForSingleObject(fMutex, INFINITE);
  23. if (res == WAIT_OBJECT_0) {
  24. fOwner = GetCurrentThreadId();
  25. return true;
  26. } else {
  27. jack_log("JackBaseWinMutex::Lock res = %d", res);
  28. return false;
  29. }
  30. } else {
  31. jack_error("JackBaseWinMutex::Lock mutex already locked by thread = %d", GetCurrentThreadId());
  32. return false;
  33. }
  34. }
  35. bool JackBaseWinMutex::Trylock()
  36. {
  37. if (fOwner != GetCurrentThreadId()) {
  38. DWORD res = WaitForSingleObject(fMutex, 0);
  39. if (res == WAIT_OBJECT_0) {
  40. fOwner = GetCurrentThreadId();
  41. return true;
  42. } else {
  43. jack_log("JackBaseWinMutex::Trylock res = %d", res);
  44. return false;
  45. }
  46. } else {
  47. jack_error("JackBaseWinMutex::Trylock mutex already locked by thread = %d", GetCurrentThreadId());
  48. return false;
  49. }
  50. }
  51. bool JackBaseWinMutex::Unlock()
  52. {
  53. if (fOwner == GetCurrentThreadId()) {
  54. fOwner = 0;
  55. int res = ReleaseMutex(fMutex);
  56. if (res != 0) {
  57. return true;
  58. } else {
  59. jack_log("JackBaseWinMutex::Unlock res = %d", res);
  60. return false;
  61. }
  62. } else {
  63. jack_error("JackBaseWinMutex::Unlock mutex not locked by thread = %d", GetCurrentThreadId());
  64. return false;
  65. }
  66. }
  67. bool JackWinMutex::Lock()
  68. {
  69. if (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, INFINITE)) {
  70. return true;
  71. } else {
  72. jack_log("JackWinProcessSync::Lock WaitForSingleObject err = %d", GetLastError());
  73. return false;
  74. }
  75. }
  76. bool JackWinMutex::Trylock()
  77. {
  78. if (WAIT_OBJECT_0 == WaitForSingleObject(fMutex, 0)) {
  79. return true;
  80. } else {
  81. jack_log("JackWinProcessSync::Trylock WaitForSingleObject err = %d", GetLastError());
  82. return false;
  83. }
  84. }
  85. bool JackWinMutex::Unlock()
  86. {
  87. if (!ReleaseMutex(fMutex)) {
  88. jack_log("JackWinProcessSync::Unlock ReleaseMutex err = %d", GetLastError());
  89. return false;
  90. } else {
  91. return true;
  92. }
  93. }
  94. bool JackWinCriticalSection::Lock()
  95. {
  96. EnterCriticalSection(&fSection);
  97. return true;
  98. }
  99. bool JackWinCriticalSection::Trylock()
  100. {
  101. return (TryEnterCriticalSection(&fSection));
  102. }
  103. bool JackWinCriticalSection::Unlock()
  104. {
  105. LeaveCriticalSection(&fSection);
  106. return true;
  107. }
  108. } // namespace