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.

307 lines
8.4KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. Copyright (C) 2016-2024 Filipe Coelho
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackMachFutex.h"
  17. #include "JackTools.h"
  18. #include "JackConstants.h"
  19. #include "JackError.h"
  20. #include "promiscuous.h"
  21. #include <cerrno>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <sys/mman.h>
  25. #define UL_COMPARE_AND_WAIT 1
  26. #define UL_COMPARE_AND_WAIT_SHARED 3
  27. #define ULF_WAKE_ALL 0x00000100
  28. #define ULF_NO_ERRNO 0x01000000
  29. extern "C" {
  30. int __ulock_wait(uint32_t operation, void* addr, uint64_t value, uint32_t timeout_us);
  31. int __ulock_wake(uint32_t operation, void* addr, uint64_t value);
  32. }
  33. namespace Jack
  34. {
  35. JackMachFutex::JackMachFutex() : JackSynchro(), fSharedMem(-1), fFutex(NULL), fPrivate(false)
  36. {
  37. const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
  38. fPromiscuous = (promiscuous != NULL);
  39. fPromiscuousGid = jack_group2gid(promiscuous);
  40. }
  41. void JackMachFutex::BuildName(const char* client_name, const char* server_name, char* res, int size)
  42. {
  43. char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
  44. JackTools::RewriteName(client_name, ext_client_name);
  45. // make the name as small as possible, as macos has issues with long semaphore names
  46. if (strcmp(server_name, "default") == 0)
  47. server_name = "";
  48. else if (strcmp(server_name, "mod-desktop") == 0)
  49. server_name = "mdsk.";
  50. if (fPromiscuous) {
  51. snprintf(res, std::min(size, 32), "js.%s%s", server_name, ext_client_name);
  52. } else {
  53. snprintf(res, std::min(size, 32), "js%d.%s%s", JackTools::GetUID(), server_name, ext_client_name);
  54. }
  55. }
  56. bool JackMachFutex::Signal()
  57. {
  58. if (!fFutex) {
  59. jack_error("JackMachFutex::Signal name = %s already deallocated!!", fName);
  60. return false;
  61. }
  62. if (fFlush) {
  63. return true;
  64. }
  65. if (! __sync_bool_compare_and_swap(&fFutex->futex, 0, 1))
  66. {
  67. // already unlocked, do not wake futex
  68. if (! fFutex->internal) return true;
  69. }
  70. const uint32_t operation = ULF_NO_ERRNO | (fFutex->internal ? UL_COMPARE_AND_WAIT : UL_COMPARE_AND_WAIT_SHARED);
  71. __ulock_wake(operation, fFutex, 0);
  72. return true;
  73. }
  74. bool JackMachFutex::SignalAll()
  75. {
  76. if (!fFutex) {
  77. jack_error("JackMachFutex::SignalAll name = %s already deallocated!!", fName);
  78. return false;
  79. }
  80. if (fFlush) {
  81. return true;
  82. }
  83. const uint32_t operation = ULF_NO_ERRNO | ULF_WAKE_ALL | (fFutex->internal ? UL_COMPARE_AND_WAIT : UL_COMPARE_AND_WAIT_SHARED);
  84. __ulock_wake(operation, fFutex, 0);
  85. return true;
  86. }
  87. bool JackMachFutex::Wait()
  88. {
  89. if (!fFutex) {
  90. jack_error("JackMachFutex::Wait name = %s already deallocated!!", fName);
  91. return false;
  92. }
  93. if (fFutex->needsChange)
  94. {
  95. fFutex->needsChange = false;
  96. fFutex->internal = !fFutex->internal;
  97. }
  98. const uint32_t operation = fFutex->internal ? UL_COMPARE_AND_WAIT : UL_COMPARE_AND_WAIT_SHARED;
  99. for (;;)
  100. {
  101. if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
  102. return true;
  103. if (__ulock_wait(operation, fFutex, 0, UINT32_MAX) != 0)
  104. if (errno != EAGAIN && errno != EINTR)
  105. return false;
  106. }
  107. }
  108. bool JackMachFutex::TimedWait(long usec)
  109. {
  110. if (usec == LONG_MAX)
  111. return Wait();
  112. if (!fFutex) {
  113. jack_error("JackMachFutex::TimedWait name = %s already deallocated!!", fName);
  114. return false;
  115. }
  116. if (fFutex->needsChange)
  117. {
  118. fFutex->needsChange = false;
  119. fFutex->internal = !fFutex->internal;
  120. }
  121. const uint32_t operation = fFutex->internal ? UL_COMPARE_AND_WAIT : UL_COMPARE_AND_WAIT_SHARED;
  122. for (;;)
  123. {
  124. if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
  125. return true;
  126. if (__ulock_wait(operation, fFutex, 0, usec) != 0)
  127. if (errno != EAGAIN && errno != EINTR)
  128. return false;
  129. }
  130. }
  131. // Server side : publish the futex in the global namespace
  132. bool JackMachFutex::Allocate(const char* name, const char* server_name, int value, bool internal)
  133. {
  134. BuildName(name, server_name, fName, sizeof(fName));
  135. jack_log("JackMachFutex::Allocate name = %s val = %ld", fName, value);
  136. // FIXME
  137. shm_unlink(fName);
  138. if ((fSharedMem = shm_open(fName, O_CREAT | O_RDWR, 0777)) < 0) {
  139. jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
  140. return false;
  141. }
  142. if (ftruncate(fSharedMem, sizeof(FutexData)) != 0) {
  143. jack_error("Allocate: can't set shared memory size in named futex name = %s err = %s", fName, strerror(errno));
  144. return false;
  145. }
  146. if (fPromiscuous && (jack_promiscuous_perms(fSharedMem, fName, fPromiscuousGid) < 0)) {
  147. close(fSharedMem);
  148. fSharedMem = -1;
  149. shm_unlink(fName);
  150. return false;
  151. }
  152. FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED, fSharedMem, 0);
  153. if (futex == NULL || futex == MAP_FAILED) {
  154. jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
  155. close(fSharedMem);
  156. fSharedMem = -1;
  157. shm_unlink(fName);
  158. return false;
  159. }
  160. mlock(futex, sizeof(FutexData));
  161. fPrivate = internal;
  162. futex->futex = value;
  163. futex->internal = internal;
  164. futex->wasInternal = internal;
  165. futex->needsChange = false;
  166. futex->externalCount = 0;
  167. fFutex = futex;
  168. return true;
  169. }
  170. // Client side : get the published futex from server
  171. bool JackMachFutex::Connect(const char* name, const char* server_name)
  172. {
  173. BuildName(name, server_name, fName, sizeof(fName));
  174. jack_log("JackMachFutex::Connect name = %s", fName);
  175. // Temporary...
  176. if (fFutex) {
  177. jack_log("Already connected name = %s", name);
  178. return true;
  179. }
  180. if ((fSharedMem = shm_open(fName, O_RDWR, 0)) < 0) {
  181. jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
  182. return false;
  183. }
  184. FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED, fSharedMem, 0);
  185. if (futex == NULL || futex == MAP_FAILED) {
  186. jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
  187. close(fSharedMem);
  188. fSharedMem = -1;
  189. return false;
  190. }
  191. mlock(futex, sizeof(FutexData));
  192. if (! fPrivate && futex->wasInternal)
  193. {
  194. const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
  195. if (externalSync != NULL && strstr(fName, externalSync) != NULL && ++futex->externalCount == 1)
  196. {
  197. jack_error("Note: client %s running as external client temporarily", fName);
  198. futex->needsChange = true;
  199. }
  200. }
  201. fFutex = futex;
  202. return true;
  203. }
  204. bool JackMachFutex::ConnectInput(const char* name, const char* server_name)
  205. {
  206. return Connect(name, server_name);
  207. }
  208. bool JackMachFutex::ConnectOutput(const char* name, const char* server_name)
  209. {
  210. return Connect(name, server_name);
  211. }
  212. bool JackMachFutex::Disconnect()
  213. {
  214. if (!fFutex) {
  215. return true;
  216. }
  217. if (! fPrivate && fFutex->wasInternal)
  218. {
  219. const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
  220. if (externalSync != NULL && strstr(fName, externalSync) != NULL && --fFutex->externalCount == 0)
  221. {
  222. jack_error("Note: client %s now running as internal client again", fName);
  223. fFutex->needsChange = true;
  224. }
  225. }
  226. munmap(fFutex, sizeof(FutexData));
  227. fFutex = NULL;
  228. close(fSharedMem);
  229. fSharedMem = -1;
  230. return true;
  231. }
  232. // Server side : destroy the futex
  233. void JackMachFutex::Destroy()
  234. {
  235. if (!fFutex) {
  236. return;
  237. }
  238. munmap(fFutex, sizeof(FutexData));
  239. fFutex = NULL;
  240. close(fSharedMem);
  241. fSharedMem = -1;
  242. shm_unlink(fName);
  243. }
  244. } // end of namespace