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.

162 lines
4.3KB

  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. #include "JackWinSemaphore.h"
  16. #include "JackConstants.h"
  17. #include "JackTools.h"
  18. #include "JackError.h"
  19. #include <stdio.h>
  20. namespace Jack
  21. {
  22. void JackWinSemaphore::BuildName(const char* client_name, const char* server_name, char* res)
  23. {
  24. char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
  25. JackTools::RewriteName(client_name, ext_client_name);
  26. sprintf(res, "jack_pipe.%s_%s", server_name, ext_client_name);
  27. }
  28. bool JackWinSemaphore::Signal()
  29. {
  30. BOOL res;
  31. assert(fSemaphore);
  32. if (fFlush)
  33. return true;
  34. if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
  35. jack_error("JackWinSemaphore::Signal name = %s err = %ld", fName, GetLastError());
  36. }
  37. return res;
  38. }
  39. bool JackWinSemaphore::SignalAll()
  40. {
  41. BOOL res;
  42. assert(fSemaphore);
  43. if (fFlush)
  44. return true;
  45. if (!(res = ReleaseSemaphore(fSemaphore, 1, NULL))) {
  46. jack_error("JackWinSemaphore::SignalAll name = %s err = %ld", fName, GetLastError());
  47. }
  48. return res;
  49. }
  50. bool JackWinSemaphore::Wait()
  51. {
  52. DWORD res;
  53. if ((res = WaitForSingleObject(fSemaphore, INFINITE)) == WAIT_TIMEOUT) {
  54. jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
  55. }
  56. return (res == WAIT_OBJECT_0);
  57. }
  58. bool JackWinSemaphore::TimedWait(long usec)
  59. {
  60. DWORD res;
  61. if ((res = WaitForSingleObject(fSemaphore, usec / 1000)) == WAIT_TIMEOUT) {
  62. jack_error("JackWinSemaphore::TimedWait name = %s time_out", fName);
  63. }
  64. return (res == WAIT_OBJECT_0);
  65. }
  66. // Client side : get the published semaphore from server
  67. bool JackWinSemaphore::ConnectInput(const char* name, const char* server_name)
  68. {
  69. BuildName(name, server_name, fName);
  70. jack_log("JackWinSemaphore::Connect %s", fName);
  71. // Temporary...
  72. if (fSemaphore) {
  73. jack_log("Already connected name = %s", name);
  74. return true;
  75. }
  76. if ((fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS , FALSE, fName)) == NULL) {
  77. jack_error("Connect: can't check in named event name = %s err = %ld", fName, GetLastError());
  78. return false;
  79. } else {
  80. return true;
  81. }
  82. }
  83. bool JackWinSemaphore::Connect(const char* name, const char* server_name)
  84. {
  85. return ConnectInput(name, server_name);
  86. }
  87. bool JackWinSemaphore::ConnectOutput(const char* name, const char* server_name)
  88. {
  89. return ConnectInput(name, server_name);
  90. }
  91. bool JackWinSemaphore::Disconnect()
  92. {
  93. if (fSemaphore) {
  94. jack_log("JackWinSemaphore::Disconnect %s", fName);
  95. CloseHandle(fSemaphore);
  96. fSemaphore = NULL;
  97. return true;
  98. } else {
  99. return false;
  100. }
  101. }
  102. bool JackWinSemaphore::Allocate(const char* name, const char* server_name, int value)
  103. {
  104. BuildName(name, server_name, fName);
  105. jack_log("JackWinSemaphore::Allocate name = %s val = %ld", fName, value);
  106. if ((fSemaphore = CreateSemaphore(NULL, value, 32767, fName)) == NULL) {
  107. jack_error("Allocate: can't check in named semaphore name = %s err = %ld", fName, GetLastError());
  108. return false;
  109. } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
  110. jack_error("Allocate: named semaphore already exist name = %s", fName);
  111. // Try to open it
  112. fSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS, FALSE, fName);
  113. return (fSemaphore != NULL);
  114. } else {
  115. return true;
  116. }
  117. }
  118. void JackWinSemaphore::Destroy()
  119. {
  120. if (fSemaphore != NULL) {
  121. jack_log("JackWinSemaphore::Destroy %s", fName);
  122. CloseHandle(fSemaphore);
  123. fSemaphore = NULL;
  124. } else {
  125. jack_error("JackWinSemaphore::Destroy synchro == NULL");
  126. }
  127. }
  128. } // end of namespace