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.

157 lines
4.0KB

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