Jack2  1.9.9
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[JACK_CLIENT_NAME_SIZE + 1];
34  JackTools::RewriteName(client_name, ext_client_name);
35  snprintf(res, size, "jack_sem.%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
36 }
37 
38 bool JackPosixSemaphore::Signal()
39 {
40  int res;
41 
42  if (!fSemaphore) {
43  jack_error("JackPosixSemaphore::Signal name = %s already deallocated!!", fName);
44  return false;
45  }
46 
47  if (fFlush)
48  return true;
49 
50  if ((res = sem_post(fSemaphore)) != 0) {
51  jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName, strerror(errno));
52  }
53  return (res == 0);
54 }
55 
56 bool JackPosixSemaphore::SignalAll()
57 {
58  int res;
59 
60  if (!fSemaphore) {
61  jack_error("JackPosixSemaphore::SignalAll name = %s already deallocated!!", fName);
62  return false;
63  }
64 
65  if (fFlush)
66  return true;
67 
68  if ((res = sem_post(fSemaphore)) != 0) {
69  jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName, strerror(errno));
70  }
71  return (res == 0);
72 }
73 
74 /*
75 bool JackPosixSemaphore::Wait()
76 {
77  int res;
78 
79  if (!fSemaphore) {
80  jack_error("JackPosixSemaphore::Wait name = %s already deallocated!!", fName);
81  return false;
82  }
83 
84  if ((res = sem_wait(fSemaphore)) != 0) {
85  jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
86  }
87  return (res == 0);
88 }
89 */
90 
91 bool JackPosixSemaphore::Wait()
92 {
93  int res;
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  return (res == 0);
101 }
102 
103 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) // glibc feature test
104 
105 bool JackPosixSemaphore::TimedWait(long usec)
106 {
107  int res;
108  struct timeval now;
109  timespec time;
110 
111  if (!fSemaphore) {
112  jack_error("JackPosixSemaphore::TimedWait name = %s already deallocated!!", fName);
113  return false;
114  }
115  gettimeofday(&now, 0);
116  time.tv_sec = now.tv_sec + usec / 1000000;
117  long tv_usec = (now.tv_usec + (usec % 1000000));
118  time.tv_sec += tv_usec / 1000000;
119  time.tv_nsec = (tv_usec % 1000000) * 1000;
120 
121  while ((res = sem_timedwait(fSemaphore, &time)) < 0) {
122  jack_error("JackPosixSemaphore::TimedWait err = %s", strerror(errno));
123  jack_log("JackPosixSemaphore::TimedWait now : %ld %ld ", now.tv_sec, now.tv_usec);
124  jack_log("JackPosixSemaphore::TimedWait next : %ld %ld ", time.tv_sec, time.tv_nsec/1000);
125  if (errno != EINTR)
126  break;
127  }
128  return (res == 0);
129 }
130 
131 #else
132 #warning "JackPosixSemaphore::TimedWait is not supported: Jack in SYNC mode with JackPosixSemaphore will not run properly !!"
133 
134 bool JackPosixSemaphore::TimedWait(long usec)
135 {
136  return Wait();
137 }
138 #endif
139 
140 // Server side : publish the semaphore in the global namespace
141 bool JackPosixSemaphore::Allocate(const char* name, const char* server_name, int value)
142 {
143  BuildName(name, server_name, fName, sizeof(fName));
144  jack_log("JackPosixSemaphore::Allocate name = %s val = %ld", fName, value);
145 
146  if ((fSemaphore = sem_open(fName, O_CREAT, 0777, value)) == (sem_t*)SEM_FAILED) {
147  jack_error("Allocate: can't check in named semaphore name = %s err = %s", fName, strerror(errno));
148  return false;
149  } else {
150  return true;
151  }
152 }
153 
154 // Client side : get the published semaphore from server
155 bool JackPosixSemaphore::ConnectInput(const char* name, const char* server_name)
156 {
157  BuildName(name, server_name, fName, sizeof(fName));
158  jack_log("JackPosixSemaphore::Connect name = %s", fName);
159 
160  // Temporary...
161  if (fSemaphore) {
162  jack_log("Already connected name = %s", name);
163  return true;
164  }
165 
166  if ((fSemaphore = sem_open(fName, O_CREAT)) == (sem_t*)SEM_FAILED) {
167  jack_error("Connect: can't connect named semaphore name = %s err = %s", fName, strerror(errno));
168  return false;
169  } else {
170  int val = 0;
171  sem_getvalue(fSemaphore, &val);
172  jack_log("JackPosixSemaphore::Connect sem_getvalue %ld", val);
173  return true;
174  }
175 }
176 
177 bool JackPosixSemaphore::Connect(const char* name, const char* server_name)
178 {
179  return ConnectInput(name, server_name);
180 }
181 
182 bool JackPosixSemaphore::ConnectOutput(const char* name, const char* server_name)
183 {
184  return ConnectInput(name, server_name);
185 }
186 
187 bool JackPosixSemaphore::Disconnect()
188 {
189  if (fSemaphore) {
190  jack_log("JackPosixSemaphore::Disconnect name = %s", fName);
191  if (sem_close(fSemaphore) != 0) {
192  jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName, strerror(errno));
193  return false;
194  } else {
195  fSemaphore = NULL;
196  return true;
197  }
198  } else {
199  return true;
200  }
201 }
202 
203 // Server side : destroy the semaphore
204 void JackPosixSemaphore::Destroy()
205 {
206  if (fSemaphore != NULL) {
207  jack_log("JackPosixSemaphore::Destroy name = %s", fName);
208  sem_unlink(fName);
209  if (sem_close(fSemaphore) != 0) {
210  jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName, strerror(errno));
211  }
212  fSemaphore = NULL;
213  } else {
214  jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
215  }
216 }
217 
218 } // end of namespace
219 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107