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.

197 lines
5.6KB

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