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.

299 lines
8.8KB

  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 <fcntl.h>
  20. #include <stdio.h>
  21. #include <sys/mman.h>
  22. namespace Jack
  23. {
  24. void JackMachSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
  25. {
  26. char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
  27. JackTools::RewriteName(client_name, ext_client_name);
  28. // make the name as small as possible, as macos has issues with long semaphore names
  29. if (strcmp(server_name, "default") == 0)
  30. server_name = "";
  31. snprintf(res, std::min(size, 32), "js%d.%s%s", JackTools::GetUID(), server_name, ext_client_name);
  32. }
  33. bool JackMachSemaphore::Signal()
  34. {
  35. if (!fSemaphore) {
  36. jack_error("JackMachSemaphore::Signal name = %s already deallocated!!", fName);
  37. return false;
  38. }
  39. if (fFlush) {
  40. return true;
  41. }
  42. kern_return_t res;
  43. if ((res = semaphore_signal(fSemaphore)) != KERN_SUCCESS) {
  44. jack_error("JackMachSemaphore::Signal name = %s err = %s", fName, mach_error_string(res));
  45. }
  46. return (res == KERN_SUCCESS);
  47. }
  48. bool JackMachSemaphore::SignalAll()
  49. {
  50. if (!fSemaphore) {
  51. jack_error("JackMachSemaphore::SignalAll name = %s already deallocated!!", fName);
  52. return false;
  53. }
  54. if (fFlush) {
  55. return true;
  56. }
  57. kern_return_t res;
  58. // When signaled several times, do not accumulate signals...
  59. if ((res = semaphore_signal_all(fSemaphore)) != KERN_SUCCESS) {
  60. jack_error("JackMachSemaphore::SignalAll name = %s err = %s", fName, mach_error_string(res));
  61. }
  62. return (res == KERN_SUCCESS);
  63. }
  64. bool JackMachSemaphore::Wait()
  65. {
  66. if (!fSemaphore) {
  67. jack_error("JackMachSemaphore::Wait name = %s already deallocated!!", fName);
  68. return false;
  69. }
  70. kern_return_t res;
  71. if ((res = semaphore_wait(fSemaphore)) != KERN_SUCCESS) {
  72. jack_error("JackMachSemaphore::Wait name = %s err = %s", fName, mach_error_string(res));
  73. }
  74. return (res == KERN_SUCCESS);
  75. }
  76. bool JackMachSemaphore::TimedWait(long usec)
  77. {
  78. if (!fSemaphore) {
  79. jack_error("JackMachSemaphore::TimedWait name = %s already deallocated!!", fName);
  80. return false;
  81. }
  82. kern_return_t res;
  83. mach_timespec time;
  84. time.tv_sec = usec / 1000000;
  85. time.tv_nsec = (usec % 1000000) * 1000;
  86. if ((res = semaphore_timedwait(fSemaphore, time)) != KERN_SUCCESS) {
  87. jack_error("JackMachSemaphore::TimedWait name = %s usec = %ld err = %s", fName, usec, mach_error_string(res));
  88. }
  89. return (res == KERN_SUCCESS);
  90. }
  91. bool JackMachSemaphore::recursiveBootstrapRegister(int counter)
  92. {
  93. if (counter == 99)
  94. return false;
  95. kern_return_t res;
  96. if ((res = bootstrap_register(fBootPort, fSharedName, fSemaphore)) != KERN_SUCCESS) {
  97. switch (res) {
  98. case BOOTSTRAP_SUCCESS :
  99. break;
  100. case BOOTSTRAP_NOT_PRIVILEGED :
  101. case BOOTSTRAP_NAME_IN_USE :
  102. case BOOTSTRAP_UNKNOWN_SERVICE :
  103. case BOOTSTRAP_SERVICE_ACTIVE :
  104. // try again with next suffix
  105. snprintf(fSharedName, sizeof(fName), "%s-%d", fName, ++counter);
  106. return recursiveBootstrapRegister(counter);
  107. break;
  108. default :
  109. jack_log("bootstrap_register() err = %i:%s", res, bootstrap_strerror(res));
  110. break;
  111. }
  112. jack_error("Allocate: can't check in mach semaphore name = %s err = %i:%s", fName, res, bootstrap_strerror(res));
  113. return false;
  114. }
  115. return true;
  116. }
  117. // Server side : publish the semaphore in the global namespace
  118. bool JackMachSemaphore::Allocate(const char* name, const char* server_name, int value)
  119. {
  120. BuildName(name, server_name, fName, sizeof(fName));
  121. mach_port_t task = mach_task_self();
  122. kern_return_t res;
  123. if (fBootPort == 0) {
  124. if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
  125. jack_error("Allocate: Can't find bootstrap mach port err = %s", mach_error_string(res));
  126. return false;
  127. }
  128. }
  129. if ((fSharedMem = shm_open(fName, O_CREAT | O_RDWR, 0777)) < 0) {
  130. jack_error("Allocate: can't check in mach shared name = %s err = %s", fName, strerror(errno));
  131. return false;
  132. }
  133. struct stat st;
  134. if (fstat(fSharedMem, &st) != -1 && st.st_size == 0) {
  135. if (ftruncate(fSharedMem, SYNC_MAX_NAME_SIZE+1) != 0) {
  136. jack_error("Allocate: can't set shared memory size in mach shared name = %s err = %s", fName, strerror(errno));
  137. return false;
  138. }
  139. }
  140. char* const sharedName = (char*)mmap(NULL, SYNC_MAX_NAME_SIZE+1, PROT_READ|PROT_WRITE, MAP_SHARED, fSharedMem, 0);
  141. if (sharedName == NULL || sharedName == MAP_FAILED) {
  142. jack_error("Allocate: can't check in mach shared name = %s err = %s", fName, strerror(errno));
  143. close(fSharedMem);
  144. fSharedMem = -1;
  145. shm_unlink(fName);
  146. return false;
  147. }
  148. fSharedName = sharedName;
  149. strcpy(fSharedName, fName);
  150. if ((res = semaphore_create(task, &fSemaphore, SYNC_POLICY_FIFO, value)) != KERN_SUCCESS) {
  151. jack_error("Allocate: can create semaphore err = %i:%s", res, mach_error_string(res));
  152. return false;
  153. }
  154. jack_log("JackMachSemaphore::Allocate name = %s", fName);
  155. return recursiveBootstrapRegister(1);
  156. }
  157. // Client side : get the published semaphore from server
  158. bool JackMachSemaphore::ConnectInput(const char* name, const char* server_name)
  159. {
  160. BuildName(name, server_name, fName, sizeof(fName));
  161. kern_return_t res;
  162. // Temporary...
  163. if (fSharedName) {
  164. jack_log("Already connected name = %s", name);
  165. return true;
  166. }
  167. if (fBootPort == 0) {
  168. if ((res = task_get_bootstrap_port(mach_task_self(), &fBootPort)) != KERN_SUCCESS) {
  169. jack_error("Connect: can't find bootstrap port err = %s", mach_error_string(res));
  170. return false;
  171. }
  172. }
  173. if ((fSharedMem = shm_open(fName, O_RDWR, 0)) < 0) {
  174. jack_error("Connect: can't connect mach shared name = %s err = %s", fName, strerror(errno));
  175. return false;
  176. }
  177. char* const sharedName = (char*)mmap(NULL, SYNC_MAX_NAME_SIZE+1, PROT_READ|PROT_WRITE, MAP_SHARED, fSharedMem, 0);
  178. if (sharedName == NULL || sharedName == MAP_FAILED) {
  179. jack_error("Connect: can't connect mach shared name = %s err = %s", fName, strerror(errno));
  180. close(fSharedMem);
  181. fSharedMem = -1;
  182. return false;
  183. }
  184. if ((res = bootstrap_look_up(fBootPort, sharedName, &fSemaphore)) != KERN_SUCCESS) {
  185. jack_error("Connect: can't find mach semaphore name = %s, sname = %s, err = %s", fName, sharedName, bootstrap_strerror(res));
  186. close(fSharedMem);
  187. fSharedMem = -1;
  188. return false;
  189. }
  190. fSharedName = sharedName;
  191. jack_log("JackMachSemaphore::Connect name = %s ", fName);
  192. return true;
  193. }
  194. bool JackMachSemaphore::Connect(const char* name, const char* server_name)
  195. {
  196. return ConnectInput(name, server_name);
  197. }
  198. bool JackMachSemaphore::ConnectOutput(const char* name, const char* server_name)
  199. {
  200. return ConnectInput(name, server_name);
  201. }
  202. bool JackMachSemaphore::Disconnect()
  203. {
  204. if (fSemaphore > 0) {
  205. jack_log("JackMachSemaphore::Disconnect name = %s", fName);
  206. fSemaphore = 0;
  207. }
  208. if (!fSharedName) {
  209. return true;
  210. }
  211. munmap(fSharedName, SYNC_MAX_NAME_SIZE+1);
  212. fSharedName = NULL;
  213. close(fSharedMem);
  214. fSharedMem = -1;
  215. return true;
  216. }
  217. // Server side : destroy the JackGlobals
  218. void JackMachSemaphore::Destroy()
  219. {
  220. kern_return_t res;
  221. if (fSemaphore > 0) {
  222. jack_log("JackMachSemaphore::Destroy name = %s", fName);
  223. if ((res = semaphore_destroy(mach_task_self(), fSemaphore)) != KERN_SUCCESS) {
  224. jack_error("JackMachSemaphore::Destroy can't destroy semaphore err = %s", mach_error_string(res));
  225. }
  226. fSemaphore = 0;
  227. } else {
  228. jack_error("JackMachSemaphore::Destroy semaphore < 0");
  229. }
  230. if (!fSharedName) {
  231. return;
  232. }
  233. munmap(fSharedName, SYNC_MAX_NAME_SIZE+1);
  234. fSharedName = NULL;
  235. close(fSharedMem);
  236. fSharedMem = -1;
  237. shm_unlink(fName);
  238. }
  239. } // end of namespace