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.

281 lines
7.6KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. Copyright (C) 2016 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 "JackLinuxFutex.h"
  17. #include "JackTools.h"
  18. #include "JackConstants.h"
  19. #include "JackError.h"
  20. #include "promiscuous.h"
  21. #include <climits>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <sys/mman.h>
  25. #include <syscall.h>
  26. #include <linux/futex.h>
  27. #if !defined(SYS_futex) && defined(SYS_futex_time64)
  28. #define SYS_futex SYS_futex_time64
  29. #endif
  30. namespace Jack
  31. {
  32. JackLinuxFutex::JackLinuxFutex() : JackSynchro(), fSharedMem(-1), fFutex(NULL), fPrivate(false)
  33. {
  34. const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
  35. fPromiscuous = (promiscuous != NULL);
  36. fPromiscuousGid = jack_group2gid(promiscuous);
  37. }
  38. void JackLinuxFutex::BuildName(const char* client_name, const char* server_name, char* res, int size)
  39. {
  40. char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
  41. JackTools::RewriteName(client_name, ext_client_name);
  42. if (fPromiscuous) {
  43. snprintf(res, size, "jack_sem.%s_%s", server_name, ext_client_name);
  44. } else {
  45. snprintf(res, size, "jack_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
  46. }
  47. }
  48. bool JackLinuxFutex::Signal()
  49. {
  50. if (!fFutex) {
  51. jack_error("JackLinuxFutex::Signal name = %s already deallocated!!", fName);
  52. return false;
  53. }
  54. if (fFlush) {
  55. return true;
  56. }
  57. if (! __sync_bool_compare_and_swap(&fFutex->futex, 0, 1))
  58. {
  59. // already unlocked, do not wake futex
  60. if (! fFutex->internal) return true;
  61. }
  62. ::syscall(SYS_futex, fFutex, fFutex->internal ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1, NULL, NULL, 0);
  63. return true;
  64. }
  65. bool JackLinuxFutex::SignalAll()
  66. {
  67. return Signal();
  68. }
  69. bool JackLinuxFutex::Wait()
  70. {
  71. if (!fFutex) {
  72. jack_error("JackLinuxFutex::Wait name = %s already deallocated!!", fName);
  73. return false;
  74. }
  75. if (fFutex->needsChange)
  76. {
  77. fFutex->needsChange = false;
  78. fFutex->internal = !fFutex->internal;
  79. }
  80. const int wait_mode = fFutex->internal ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT;
  81. for (;;)
  82. {
  83. if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
  84. return true;
  85. if (::syscall(SYS_futex, fFutex, wait_mode, 0, NULL, NULL, 0) != 0)
  86. if (errno != EAGAIN && errno != EINTR)
  87. return false;
  88. }
  89. }
  90. bool JackLinuxFutex::TimedWait(long usec)
  91. {
  92. if (usec == LONG_MAX)
  93. return Wait();
  94. if (!fFutex) {
  95. jack_error("JackLinuxFutex::TimedWait name = %s already deallocated!!", fName);
  96. return false;
  97. }
  98. if (fFutex->needsChange)
  99. {
  100. fFutex->needsChange = false;
  101. fFutex->internal = !fFutex->internal;
  102. }
  103. const uint secs = usec / 1000000;
  104. const int nsecs = (usec % 1000000) * 1000;
  105. const timespec timeout = { static_cast<time_t>(secs), nsecs };
  106. const int wait_mode = fFutex->internal ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT;
  107. for (;;)
  108. {
  109. if (__sync_bool_compare_and_swap(&fFutex->futex, 1, 0))
  110. return true;
  111. if (::syscall(SYS_futex, fFutex, wait_mode, 0, &timeout, NULL, 0) != 0)
  112. if (errno != EAGAIN && errno != EINTR)
  113. return false;
  114. }
  115. }
  116. // Server side : publish the futex in the global namespace
  117. bool JackLinuxFutex::Allocate(const char* name, const char* server_name, int value, bool internal)
  118. {
  119. BuildName(name, server_name, fName, sizeof(fName));
  120. jack_log("JackLinuxFutex::Allocate name = %s val = %ld", fName, value);
  121. if ((fSharedMem = shm_open(fName, O_CREAT | O_RDWR, 0777)) < 0) {
  122. jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
  123. return false;
  124. }
  125. if (ftruncate(fSharedMem, sizeof(FutexData)) != 0) {
  126. jack_error("Allocate: can't set shared memory size in named futex name = %s err = %s", fName, strerror(errno));
  127. return false;
  128. }
  129. if (fPromiscuous && (jack_promiscuous_perms(fSharedMem, fName, fPromiscuousGid) < 0)) {
  130. close(fSharedMem);
  131. fSharedMem = -1;
  132. shm_unlink(fName);
  133. return false;
  134. }
  135. FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0);
  136. if (futex == NULL || futex == MAP_FAILED) {
  137. jack_error("Allocate: can't check in named futex name = %s err = %s", fName, strerror(errno));
  138. close(fSharedMem);
  139. fSharedMem = -1;
  140. shm_unlink(fName);
  141. return false;
  142. }
  143. fPrivate = internal;
  144. futex->futex = value;
  145. futex->internal = internal;
  146. futex->wasInternal = internal;
  147. futex->needsChange = false;
  148. futex->externalCount = 0;
  149. fFutex = futex;
  150. return true;
  151. }
  152. // Client side : get the published futex from server
  153. bool JackLinuxFutex::Connect(const char* name, const char* server_name)
  154. {
  155. BuildName(name, server_name, fName, sizeof(fName));
  156. jack_log("JackLinuxFutex::Connect name = %s", fName);
  157. // Temporary...
  158. if (fFutex) {
  159. jack_log("Already connected name = %s", name);
  160. return true;
  161. }
  162. if ((fSharedMem = shm_open(fName, O_RDWR, 0)) < 0) {
  163. jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
  164. return false;
  165. }
  166. FutexData* futex = (FutexData*)mmap(NULL, sizeof(FutexData), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fSharedMem, 0);
  167. if (futex == NULL || futex == MAP_FAILED) {
  168. jack_error("Connect: can't connect named futex name = %s err = %s", fName, strerror(errno));
  169. close(fSharedMem);
  170. fSharedMem = -1;
  171. return false;
  172. }
  173. if (! fPrivate && futex->wasInternal)
  174. {
  175. const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
  176. if (externalSync != NULL && strstr(fName, externalSync) != NULL && ++futex->externalCount == 1)
  177. {
  178. jack_error("Note: client %s running as external client temporarily", fName);
  179. futex->needsChange = true;
  180. }
  181. }
  182. fFutex = futex;
  183. return true;
  184. }
  185. bool JackLinuxFutex::ConnectInput(const char* name, const char* server_name)
  186. {
  187. return Connect(name, server_name);
  188. }
  189. bool JackLinuxFutex::ConnectOutput(const char* name, const char* server_name)
  190. {
  191. return Connect(name, server_name);
  192. }
  193. bool JackLinuxFutex::Disconnect()
  194. {
  195. if (!fFutex) {
  196. return true;
  197. }
  198. if (! fPrivate && fFutex->wasInternal)
  199. {
  200. const char* externalSync = getenv("JACK_INTERNAL_CLIENT_SYNC");
  201. if (externalSync != NULL && strstr(fName, externalSync) != NULL && --fFutex->externalCount == 0)
  202. {
  203. jack_error("Note: client %s now running as internal client again", fName);
  204. fFutex->needsChange = true;
  205. }
  206. }
  207. munmap(fFutex, sizeof(FutexData));
  208. fFutex = NULL;
  209. close(fSharedMem);
  210. fSharedMem = -1;
  211. return true;
  212. }
  213. // Server side : destroy the futex
  214. void JackLinuxFutex::Destroy()
  215. {
  216. if (!fFutex) {
  217. return;
  218. }
  219. munmap(fFutex, sizeof(FutexData));
  220. fFutex = NULL;
  221. close(fSharedMem);
  222. fSharedMem = -1;
  223. shm_unlink(fName);
  224. }
  225. } // end of namespace