Jack2  1.9.10
JackPosixSemaphore.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3 
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 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 */
19 
20 #include "JackPosixSemaphore.h"
21 #include "JackTools.h"
22 #include "JackConstants.h"
23 #include "JackError.h"
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <sys/time.h>
27 
28 namespace Jack
29 {
30 
31 void JackPosixSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
32 {
33  char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
34  JackTools::RewriteName(client_name, ext_client_name);
35  if (getenv("JACK_PROMISCUOUS_SERVER")) {
36  snprintf(res, size, "jack_sem.%s_%s", server_name, ext_client_name);
37  } else {
38  snprintf(res, size, "jack_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
39  }
40 }
41 
42 bool JackPosixSemaphore::Signal()
43 {
44  int res;
45 
46  if (!fSemaphore) {
47  jack_error("JackPosixSemaphore::Signal name = %s already deallocated!!", fName);
48  return false;
49  }
50 
51  if (fFlush)
52  return true;
53 
54  if ((res = sem_post(fSemaphore)) != 0) {
55  jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName, strerror(errno));
56  }
57  return (res == 0);
58 }
59 
60 bool JackPosixSemaphore::SignalAll()
61 {
62  int res;
63 
64  if (!fSemaphore) {
65  jack_error("JackPosixSemaphore::SignalAll name = %s already deallocated!!", fName);
66  return false;
67  }
68 
69  if (fFlush)
70  return true;
71 
72  if ((res = sem_post(fSemaphore)) != 0) {
73  jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName, strerror(errno));
74  }
75  return (res == 0);
76 }
77 
78 /*
79 bool JackPosixSemaphore::Wait()
80 {
81  int res;
82 
83  if (!fSemaphore) {
84  jack_error("JackPosixSemaphore::Wait name = %s already deallocated!!", fName);
85  return false;
86  }
87 
88  if ((res = sem_wait(fSemaphore)) != 0) {
89  jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
90  }
91  return (res == 0);
92 }
93 */
94 
95 bool JackPosixSemaphore::Wait()
96 {
97  int res;
98 
99  while ((res = sem_wait(fSemaphore) < 0)) {
100  jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
101  if (errno != EINTR) {
102  break;
103  }
104  }
105  return (res == 0);
106 }
107 
108 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) // glibc feature test
109 
110 bool JackPosixSemaphore::TimedWait(long usec)
111 {
112  int res;
113  struct timeval now;
114  timespec time;
115 
116  if (!fSemaphore) {
117  jack_error("JackPosixSemaphore::TimedWait name = %s already deallocated!!", fName);
118  return false;
119  }
120  gettimeofday(&now, 0);
121  time.tv_sec = now.tv_sec + usec / 1000000;
122  long tv_usec = (now.tv_usec + (usec % 1000000));
123  time.tv_sec += tv_usec / 1000000;
124  time.tv_nsec = (tv_usec % 1000000) * 1000;
125 
126  while ((res = sem_timedwait(fSemaphore, &time)) < 0) {
127  jack_error("JackPosixSemaphore::TimedWait err = %s", strerror(errno));
128  jack_log("JackPosixSemaphore::TimedWait now : %ld %ld ", now.tv_sec, now.tv_usec);
129  jack_log("JackPosixSemaphore::TimedWait next : %ld %ld ", time.tv_sec, time.tv_nsec/1000);
130  if (errno != EINTR) {
131  break;
132  }
133  }
134  return (res == 0);
135 }
136 
137 #else
138 #warning "JackPosixSemaphore::TimedWait is not supported: Jack in SYNC mode with JackPosixSemaphore will not run properly !!"
139 
140 bool JackPosixSemaphore::TimedWait(long usec)
141 {
142  return Wait();
143 }
144 #endif
145 
146 // Server side : publish the semaphore in the global namespace
147 bool JackPosixSemaphore::Allocate(const char* name, const char* server_name, int value)
148 {
149  BuildName(name, server_name, fName, sizeof(fName));
150  jack_log("JackPosixSemaphore::Allocate name = %s val = %ld", fName, value);
151 
152  if ((fSemaphore = sem_open(fName, O_CREAT | O_RDWR, 0777, value)) == (sem_t*)SEM_FAILED) {
153  jack_error("Allocate: can't check in named semaphore name = %s err = %s", fName, strerror(errno));
154  return false;
155  } else {
156  return true;
157  }
158 }
159 
160 // Client side : get the published semaphore from server
161 bool JackPosixSemaphore::ConnectInput(const char* name, const char* server_name)
162 {
163  BuildName(name, server_name, fName, sizeof(fName));
164  jack_log("JackPosixSemaphore::Connect name = %s", fName);
165 
166  // Temporary...
167  if (fSemaphore) {
168  jack_log("Already connected name = %s", name);
169  return true;
170  }
171 
172  if ((fSemaphore = sem_open(fName, O_RDWR)) == (sem_t*)SEM_FAILED) {
173  jack_error("Connect: can't connect named semaphore name = %s err = %s", fName, strerror(errno));
174  return false;
175  } else {
176  int val = 0;
177  sem_getvalue(fSemaphore, &val);
178  jack_log("JackPosixSemaphore::Connect sem_getvalue %ld", val);
179  return true;
180  }
181 }
182 
183 bool JackPosixSemaphore::Connect(const char* name, const char* server_name)
184 {
185  return ConnectInput(name, server_name);
186 }
187 
188 bool JackPosixSemaphore::ConnectOutput(const char* name, const char* server_name)
189 {
190  return ConnectInput(name, server_name);
191 }
192 
193 bool JackPosixSemaphore::Disconnect()
194 {
195  if (fSemaphore) {
196  jack_log("JackPosixSemaphore::Disconnect name = %s", fName);
197  if (sem_close(fSemaphore) != 0) {
198  jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName, strerror(errno));
199  return false;
200  } else {
201  fSemaphore = NULL;
202  return true;
203  }
204  } else {
205  return true;
206  }
207 }
208 
209 // Server side : destroy the semaphore
210 void JackPosixSemaphore::Destroy()
211 {
212  if (fSemaphore != NULL) {
213  jack_log("JackPosixSemaphore::Destroy name = %s", fName);
214  sem_unlink(fName);
215  if (sem_close(fSemaphore) != 0) {
216  jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName, strerror(errno));
217  }
218  fSemaphore = NULL;
219  } else {
220  jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
221  }
222 }
223 
224 } // end of namespace
225