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.

161 lines
4.1KB

  1. /*
  2. Copyright (C) 2004-2005 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include "JackWinSemaphore.h"
  19. #include "JackError.h"
  20. namespace Jack
  21. {
  22. void JackWinSemaphore::BuildName(const char* name, const char* server_name, char* res)
  23. {
  24. sprintf(res, "jack_pipe.%s_%s", server_name, name);
  25. }
  26. bool JackWinSemaphore::Signal()
  27. {
  28. BOOL res;
  29. assert(fSemaphore);
  30. if (fFlush)
  31. return true;
  32. if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
  33. jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
  34. }
  35. return res;
  36. }
  37. bool JackWinSemaphore::SignalAll()
  38. {
  39. BOOL res;
  40. assert(fSemaphore);
  41. if (fFlush)
  42. return true;
  43. if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
  44. jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
  45. }
  46. return res;
  47. }
  48. bool JackWinSemaphore::Wait()
  49. {
  50. DWORD res;
  51. if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
  52. jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
  53. }
  54. return (res == WAIT_OBJECT_0);
  55. }
  56. bool JackWinSemaphore::TimedWait(long usec)
  57. {
  58. DWORD res;
  59. if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
  60. jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
  61. }
  62. return (res == WAIT_OBJECT_0);
  63. }
  64. // Client side : get the published semaphore from server
  65. bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name)
  66. {
  67. BuildName(name, server_name, fName);
  68. jack_log("JackWinSemaphore::Connect %s", fName);
  69. // Temporary...
  70. if (fSemaphore) {
  71. jack_log("Already connected name = %s", name);
  72. return true;
  73. }
  74. if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) {
  75. jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
  76. return false;
  77. } else {
  78. return true;
  79. }
  80. }
  81. bool JackWinSemaphore::Connect(const char* name, const char* server_name)
  82. {
  83. return ConnectInput(name, server_name);
  84. }
  85. bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name)
  86. {
  87. return ConnectInput(name, server_name);
  88. }
  89. bool JackWinSemaphore::Disconnect()
  90. {
  91. if (fSemaphore) {
  92. jack_log("JackWinSemaphore::Disconnect %s", fName);
  93. CloseHandle(fSemaphore);
  94. fSemaphore = NULL;
  95. return true;
  96. } else {
  97. return false;
  98. }
  99. }
  100. bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value)
  101. {
  102. BuildName(name, server_name, fName);
  103. jack_log("JackWinSemaphore::Allocate name = %s val = %ld", fName, value);
  104. if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) {
  105. jack_error("Allocate: can't check in named semaphore name = %s err = %ld", fName, GetLastError());
  106. return false;
  107. } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
  108. jack_error("Allocate: named semaphore already exist name = %s", fName);
  109. CloseHandle(fSemaphore);
  110. fSemaphore = NULL;
  111. return false;
  112. } else {
  113. return true;
  114. }
  115. }
  116. void JackWinSemaphore::Destroy()
  117. {
  118. if (fSemaphore != NULL) {
  119. jack_log("JackWinSemaphore::Destroy %s", fName);
  120. CloseHandle(fSemaphore);
  121. fSemaphore = NULL;
  122. } else {
  123. jack_error("JackWinSemaphore::Destroy synchro == NULL");
  124. }
  125. }
  126. } // end of namespace