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.

243 lines
6.9KB

  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. // #define _POSIX_C_SOURCE 200112L
  16. #include "JackPosixSemaphore.h"
  17. #include "JackTools.h"
  18. #include "JackConstants.h"
  19. #include "JackError.h"
  20. #include <fcntl.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <sys/time.h>
  24. #ifdef __linux__
  25. #include "promiscuous.h"
  26. #endif
  27. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  28. #define JACK_SEM_PREFIX "/jack_sem"
  29. #define SEM_DEFAULT_O 0
  30. #else
  31. #define JACK_SEM_PREFIX "jack_sem"
  32. #define SEM_DEFAULT_O O_RDWR
  33. #endif
  34. namespace Jack
  35. {
  36. JackPosixSemaphore::JackPosixSemaphore() : JackSynchro(), fSemaphore(NULL)
  37. {
  38. const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
  39. fPromiscuous = (promiscuous != NULL);
  40. #ifdef __linux__
  41. fPromiscuousGid = jack_group2gid(promiscuous);
  42. #endif
  43. }
  44. void JackPosixSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
  45. {
  46. char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
  47. JackTools::RewriteName(client_name, ext_client_name);
  48. #if __APPLE__ // POSIX semaphore names are limited to 32 characters...
  49. snprintf(res, 32, "js_%s", ext_client_name);
  50. #else
  51. if (fPromiscuous) {
  52. snprintf(res, size, JACK_SEM_PREFIX ".%s_%s", server_name, ext_client_name);
  53. } else {
  54. snprintf(res, size, JACK_SEM_PREFIX ".%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
  55. }
  56. #endif
  57. }
  58. bool JackPosixSemaphore::Signal()
  59. {
  60. int res;
  61. if (!fSemaphore) {
  62. jack_error("JackPosixSemaphore::Signal name = %s already deallocated!!", fName);
  63. return false;
  64. }
  65. if (fFlush) {
  66. return true;
  67. }
  68. if ((res = sem_post(fSemaphore)) != 0) {
  69. jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName, strerror(errno));
  70. }
  71. return (res == 0);
  72. }
  73. bool JackPosixSemaphore::SignalAll()
  74. {
  75. int res;
  76. if (!fSemaphore) {
  77. jack_error("JackPosixSemaphore::SignalAll name = %s already deallocated!!", fName);
  78. return false;
  79. }
  80. if (fFlush) {
  81. return true;
  82. }
  83. if ((res = sem_post(fSemaphore)) != 0) {
  84. jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName, strerror(errno));
  85. }
  86. return (res == 0);
  87. }
  88. bool JackPosixSemaphore::Wait()
  89. {
  90. int res;
  91. if (!fSemaphore) {
  92. jack_error("JackPosixSemaphore::Wait name = %s already deallocated!!", fName);
  93. return false;
  94. }
  95. while ((res = sem_wait(fSemaphore) < 0)) {
  96. jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
  97. if (errno != EINTR) {
  98. break;
  99. }
  100. }
  101. return (res == 0);
  102. }
  103. bool JackPosixSemaphore::TimedWait(long usec)
  104. {
  105. int res;
  106. struct timeval now;
  107. timespec time;
  108. if (!fSemaphore) {
  109. jack_error("JackPosixSemaphore::TimedWait name = %s already deallocated!!", fName);
  110. return false;
  111. }
  112. gettimeofday(&now, 0);
  113. time.tv_sec = now.tv_sec + usec / 1000000;
  114. long tv_usec = (now.tv_usec + (usec % 1000000));
  115. time.tv_sec += tv_usec / 1000000;
  116. time.tv_nsec = (tv_usec % 1000000) * 1000;
  117. while ((res = sem_timedwait(fSemaphore, &time)) < 0) {
  118. jack_error("JackPosixSemaphore::TimedWait err = %s", strerror(errno));
  119. jack_log("JackPosixSemaphore::TimedWait now : %ld %ld ", now.tv_sec, now.tv_usec);
  120. jack_log("JackPosixSemaphore::TimedWait next : %ld %ld ", time.tv_sec, time.tv_nsec/1000);
  121. if (errno != EINTR) {
  122. break;
  123. }
  124. }
  125. return (res == 0);
  126. }
  127. // Server side : publish the semaphore in the global namespace
  128. bool JackPosixSemaphore::Allocate(const char* name, const char* server_name, int value)
  129. {
  130. BuildName(name, server_name, fName, sizeof(fName));
  131. jack_log("JackPosixSemaphore::Allocate name = %s val = %ld", fName, value);
  132. if ((fSemaphore = sem_open(fName, O_CREAT | SEM_DEFAULT_O, 0777, value)) == (sem_t*)SEM_FAILED) {
  133. jack_error("Allocate: can't check in named semaphore name = %s err = %s", fName, strerror(errno));
  134. return false;
  135. } else {
  136. #ifdef __linux__
  137. if (fPromiscuous) {
  138. char sempath[SYNC_MAX_NAME_SIZE+13];
  139. snprintf(sempath, sizeof(sempath), "/dev/shm/sem.%s", fName);
  140. if (jack_promiscuous_perms(-1, sempath, fPromiscuousGid) < 0)
  141. return false;
  142. }
  143. #endif
  144. return true;
  145. }
  146. }
  147. // Client side : get the published semaphore from server
  148. bool JackPosixSemaphore::ConnectInput(const char* name, const char* server_name)
  149. {
  150. BuildName(name, server_name, fName, sizeof(fName));
  151. jack_log("JackPosixSemaphore::Connect name = %s", fName);
  152. // Temporary...
  153. if (fSemaphore) {
  154. jack_log("Already connected name = %s", name);
  155. return true;
  156. }
  157. if ((fSemaphore = sem_open(fName, SEM_DEFAULT_O)) == (sem_t*)SEM_FAILED) {
  158. jack_error("Connect: can't connect named semaphore name = %s err = %s", fName, strerror(errno));
  159. return false;
  160. } else if (fSemaphore) {
  161. int val = 0;
  162. sem_getvalue(fSemaphore, &val);
  163. jack_log("JackPosixSemaphore::Connect sem_getvalue %ld", val);
  164. return true;
  165. } else {
  166. jack_error("Connect: fSemaphore not initialized!");
  167. return false;
  168. }
  169. }
  170. bool JackPosixSemaphore::Connect(const char* name, const char* server_name)
  171. {
  172. return ConnectInput(name, server_name);
  173. }
  174. bool JackPosixSemaphore::ConnectOutput(const char* name, const char* server_name)
  175. {
  176. return ConnectInput(name, server_name);
  177. }
  178. bool JackPosixSemaphore::Disconnect()
  179. {
  180. if (fSemaphore) {
  181. jack_log("JackPosixSemaphore::Disconnect name = %s", fName);
  182. if (sem_close(fSemaphore) != 0) {
  183. jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName, strerror(errno));
  184. return false;
  185. } else {
  186. fSemaphore = NULL;
  187. return true;
  188. }
  189. } else {
  190. return true;
  191. }
  192. }
  193. // Server side : destroy the semaphore
  194. void JackPosixSemaphore::Destroy()
  195. {
  196. if (fSemaphore != NULL) {
  197. jack_log("JackPosixSemaphore::Destroy name = %s", fName);
  198. sem_unlink(fName);
  199. if (sem_close(fSemaphore) != 0) {
  200. jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName, strerror(errno));
  201. }
  202. fSemaphore = NULL;
  203. } else {
  204. jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
  205. }
  206. }
  207. } // end of namespace