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.

265 lines
7.2KB

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