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