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.

163 lines
4.3KB

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