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.

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