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.

130 lines
2.7KB

  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 "JackCompilerDeps.h"
  18. #include "JackException.h"
  19. #ifdef __MINGW32__
  20. #include <winsock2.h>
  21. #endif
  22. #include <windows.h>
  23. #include <stdio.h>
  24. namespace Jack
  25. {
  26. /*!
  27. \brief Mutex abstraction.
  28. */
  29. class SERVER_EXPORT JackBaseWinMutex
  30. {
  31. protected:
  32. HANDLE fMutex;
  33. DWORD fOwner;
  34. public:
  35. JackBaseWinMutex():fOwner(0)
  36. {
  37. // In recursive mode by default
  38. fMutex = CreateMutex(NULL, FALSE, NULL);
  39. ThrowIf((fMutex == 0), JackException("JackBaseWinMutex: could not init the mutex"));
  40. }
  41. virtual ~JackBaseWinMutex()
  42. {
  43. CloseHandle(fMutex);
  44. }
  45. bool Lock();
  46. bool Trylock();
  47. bool Unlock();
  48. };
  49. class SERVER_EXPORT JackWinMutex
  50. {
  51. protected:
  52. HANDLE fMutex;
  53. public:
  54. JackWinMutex(const char* name = NULL)
  55. {
  56. // In recursive mode by default
  57. if (name) {
  58. char buffer[MAX_PATH];
  59. snprintf(buffer, sizeof(buffer), "%s_%s", "JackWinMutex", name);
  60. fMutex = CreateMutex(NULL, FALSE, buffer);
  61. } else {
  62. fMutex = CreateMutex(NULL, FALSE, NULL);
  63. }
  64. ThrowIf((fMutex == 0), JackException("JackWinMutex: could not init the mutex"));
  65. }
  66. virtual ~JackWinMutex()
  67. {
  68. CloseHandle(fMutex);
  69. }
  70. bool Lock();
  71. bool Trylock();
  72. bool Unlock();
  73. };
  74. class SERVER_EXPORT JackWinCriticalSection
  75. {
  76. protected:
  77. CRITICAL_SECTION fSection;
  78. public:
  79. JackWinCriticalSection(const char* name = NULL)
  80. {
  81. InitializeCriticalSection(&fSection);
  82. }
  83. virtual ~JackWinCriticalSection()
  84. {
  85. DeleteCriticalSection(&fSection);
  86. }
  87. bool Lock();
  88. bool Trylock();
  89. bool Unlock();
  90. };
  91. } // namespace
  92. #endif