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.

209 lines
6.4KB

  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 "JackMachSemaphore.h"
  16. #include "JackConstants.h"
  17. #include "JackTools.h"
  18. #include "JackError.h"
  19. #include <stdio.h>
  20. namespace Jack
  21. {
  22. void JackMachSemaphore::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_mach_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
  27. }
  28. bool JackMachSemaphore::Signal()
  29. {
  30. if (!fSemaphore) {
  31. jack_error("JackMachSemaphore::Signal name = %s already deallocated!!", fName);
  32. return false;
  33. }
  34. if (fFlush) {
  35. return true;
  36. }
  37. kern_return_t res;
  38. if ((res = semaphore_signal(fSemaphore)) != KERN_SUCCESS) {
  39. jack_error("JackMachSemaphore::Signal name = %s err = %s", fName, mach_error_string(res));
  40. }
  41. return (res == KERN_SUCCESS);
  42. }
  43. bool JackMachSemaphore::SignalAll()
  44. {
  45. if (!fSemaphore) {
  46. jack_error("JackMachSemaphore::SignalAll name = %s already deallocated!!", fName);
  47. return false;
  48. }
  49. if (fFlush) {
  50. return true;
  51. }
  52. kern_return_t res;
  53. // When signaled several times, do not accumulate signals...
  54. if ((res = semaphore_signal_all(fSemaphore)) != KERN_SUCCESS) {
  55. jack_error("JackMachSemaphore::SignalAll name = %s err = %s", fName, mach_error_string(res));
  56. }
  57. return (res == KERN_SUCCESS);
  58. }
  59. bool JackMachSemaphore::Wait()
  60. {
  61. if (!fSemaphore) {
  62. jack_error("JackMachSemaphore::Wait name = %s already deallocated!!", fName);
  63. return false;
  64. }
  65. kern_return_t res;
  66. if ((res = semaphore_wait(fSemaphore)) != KERN_SUCCESS) {
  67. jack_error("JackMachSemaphore::Wait name = %s err = %s", fName, mach_error_string(res));
  68. }
  69. return (res == KERN_SUCCESS);
  70. }
  71. bool JackMachSemaphore::TimedWait(long usec)
  72. {
  73. if (!fSemaphore) {
  74. jack_error("JackMachSemaphore::TimedWait name = %s already deallocated!!", fName);
  75. return false;
  76. }
  77. kern_return_t res;
  78. mach_timespec time;
  79. time.tv_sec = usec / 1000000;
  80. time.tv_nsec = (usec % 1000000) * 1000;
  81. if ((res = semaphore_timedwait(fSemaphore, time)) != KERN_SUCCESS) {
  82. jack_error("JackMachSemaphore::TimedWait name = %s usec = %ld err = %s", fName, usec, mach_error_string(res));
  83. }
  84. return (res == KERN_SUCCESS);
  85. }
  86. // Server side : publish the semaphore in the global namespace
  87. bool JackMachSemaphore::Allocate(const char* name, const char* server_name, int value)
  88. {
  89. BuildName(name, server_name, fName, sizeof(fName));
  90. mach_port_t task = mach_task_self();
  91. kern_return_t res;
  92. if (fBootPort == 0) {
  93. if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
  94. jack_error("Allocate: Can't find bootstrap mach port err = %s", mach_error_string(res));
  95. return false;
  96. }
  97. }
  98. if ((res = semaphore_create(task, &fSemaphore, SYNC_POLICY_FIFO, value)) != KERN_SUCCESS) {
  99. jack_error("Allocate: can create semaphore err = %s", mach_error_string(res));
  100. return false;
  101. }
  102. if ((res = bootstrap_register(fBootPort, fName, fSemaphore)) != KERN_SUCCESS) {
  103. jack_error("Allocate: can't check in mach semaphore name = %s err = %s", fName, mach_error_string(res));
  104. switch (res) {
  105. case BOOTSTRAP_SUCCESS :
  106. /* service not currently registered, "a good thing" (tm) */
  107. break;
  108. case BOOTSTRAP_NOT_PRIVILEGED :
  109. jack_log("bootstrap_register(): bootstrap not privileged");
  110. break;
  111. case BOOTSTRAP_SERVICE_ACTIVE :
  112. jack_log("bootstrap_register(): bootstrap service active");
  113. break;
  114. default :
  115. jack_log("bootstrap_register() err = %s", mach_error_string(res));
  116. break;
  117. }
  118. return false;
  119. }
  120. jack_log("JackMachSemaphore::Allocate name = %s", fName);
  121. return true;
  122. }
  123. // Client side : get the published semaphore from server
  124. bool JackMachSemaphore::ConnectInput(const char* name, const char* server_name)
  125. {
  126. BuildName(name, server_name, fName, sizeof(fName));
  127. kern_return_t res;
  128. if (fBootPort == 0) {
  129. if ((res = task_get_bootstrap_port(mach_task_self(), &fBootPort)) != KERN_SUCCESS) {
  130. jack_error("Connect: can't find bootstrap port err = %s", mach_error_string(res));
  131. return false;
  132. }
  133. }
  134. if ((res = bootstrap_look_up(fBootPort, fName, &fSemaphore)) != KERN_SUCCESS) {
  135. jack_error("Connect: can't find mach semaphore name = %s err = %s", fName, mach_error_string(res));
  136. return false;
  137. }
  138. jack_log("JackMachSemaphore::Connect name = %s ", fName);
  139. return true;
  140. }
  141. bool JackMachSemaphore::Connect(const char* name, const char* server_name)
  142. {
  143. return ConnectInput(name, server_name);
  144. }
  145. bool JackMachSemaphore::ConnectOutput(const char* name, const char* server_name)
  146. {
  147. return ConnectInput(name, server_name);
  148. }
  149. bool JackMachSemaphore::Disconnect()
  150. {
  151. if (fSemaphore > 0) {
  152. jack_log("JackMachSemaphore::Disconnect name = %s", fName);
  153. fSemaphore = 0;
  154. }
  155. // Nothing to do
  156. return true;
  157. }
  158. // Server side : destroy the JackGlobals
  159. void JackMachSemaphore::Destroy()
  160. {
  161. kern_return_t res;
  162. if (fSemaphore > 0) {
  163. jack_log("JackMachSemaphore::Destroy name = %s", fName);
  164. if ((res = semaphore_destroy(mach_task_self(), fSemaphore)) != KERN_SUCCESS) {
  165. jack_error("JackMachSemaphore::Destroy can't destroy semaphore err = %s", mach_error_string(res));
  166. }
  167. fSemaphore = 0;
  168. } else {
  169. jack_error("JackMachSemaphore::Destroy semaphore < 0");
  170. }
  171. }
  172. } // end of namespace