Switch build badge to github one
[jackdbus.git] / posix / JackPosixSemaphore.cpp
blob49012312b05baf32a3f5515319741a6011396992
1 /*
2 Copyright (C) 2004-2008 Grame
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.
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.
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.
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 <ctype.h>
27 #include <sys/time.h>
28 #ifdef __linux__
29 #include "promiscuous.h"
30 #endif
32 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
33 #define JACK_SEM_PREFIX "/jack_sem"
34 #define SEM_DEFAULT_O 0
35 #else
36 #define JACK_SEM_PREFIX "jack_sem"
37 #define SEM_DEFAULT_O O_RDWR
38 #endif
40 namespace Jack
43 JackPosixSemaphore::JackPosixSemaphore() : JackSynchro(), fSemaphore(NULL)
45 const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
46 fPromiscuous = (promiscuous != NULL);
47 #ifdef __linux__
48 fPromiscuousGid = jack_group2gid(promiscuous);
49 #endif
52 void JackPosixSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
54 char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
55 JackTools::RewriteName(client_name, ext_client_name);
56 #if __APPLE__ // POSIX semaphore names are limited to 32 characters...
57 snprintf(res, 32, "js_%s", ext_client_name);
58 #else
59 if (fPromiscuous) {
60 snprintf(res, size, JACK_SEM_PREFIX ".%s_%s", server_name, ext_client_name);
61 } else {
62 snprintf(res, size, JACK_SEM_PREFIX ".%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
64 #endif
67 bool JackPosixSemaphore::Signal()
69 int res;
71 if (!fSemaphore) {
72 jack_error("JackPosixSemaphore::Signal name = %s already deallocated!!", fName);
73 return false;
76 if (fFlush) {
77 return true;
80 if ((res = sem_post(fSemaphore)) != 0) {
81 jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName, strerror(errno));
83 return (res == 0);
86 bool JackPosixSemaphore::SignalAll()
88 int res;
90 if (!fSemaphore) {
91 jack_error("JackPosixSemaphore::SignalAll name = %s already deallocated!!", fName);
92 return false;
95 if (fFlush) {
96 return true;
99 if ((res = sem_post(fSemaphore)) != 0) {
100 jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName, strerror(errno));
102 return (res == 0);
105 bool JackPosixSemaphore::Wait()
107 int res;
109 if (!fSemaphore) {
110 jack_error("JackPosixSemaphore::Wait name = %s already deallocated!!", fName);
111 return false;
114 while ((res = sem_wait(fSemaphore) < 0)) {
115 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
116 if (errno != EINTR) {
117 break;
120 return (res == 0);
123 bool JackPosixSemaphore::TimedWait(long usec)
125 int res;
126 struct timeval now;
127 timespec time;
129 if (!fSemaphore) {
130 jack_error("JackPosixSemaphore::TimedWait name = %s already deallocated!!", fName);
131 return false;
133 gettimeofday(&now, 0);
134 time.tv_sec = now.tv_sec + usec / 1000000;
135 long tv_usec = (now.tv_usec + (usec % 1000000));
136 time.tv_sec += tv_usec / 1000000;
137 time.tv_nsec = (tv_usec % 1000000) * 1000;
139 while ((res = sem_timedwait(fSemaphore, &time)) < 0) {
140 jack_error("JackPosixSemaphore::TimedWait err = %s", strerror(errno));
141 jack_log("JackPosixSemaphore::TimedWait now : %ld %ld ", now.tv_sec, now.tv_usec);
142 jack_log("JackPosixSemaphore::TimedWait next : %ld %ld ", time.tv_sec, time.tv_nsec/1000);
143 if (errno != EINTR) {
144 break;
147 return (res == 0);
150 // Server side : publish the semaphore in the global namespace
151 bool JackPosixSemaphore::Allocate(const char* name, const char* server_name, int value)
153 BuildName(name, server_name, fName, sizeof(fName));
154 jack_log("JackPosixSemaphore::Allocate name = %s val = %ld", fName, value);
156 if ((fSemaphore = sem_open(fName, O_CREAT | SEM_DEFAULT_O, 0777, value)) == (sem_t*)SEM_FAILED) {
157 jack_error("Allocate: can't check in named semaphore name = %s err = %s", fName, strerror(errno));
158 return false;
159 } else {
160 #ifdef __linux__
161 if (fPromiscuous) {
162 char sempath[SYNC_MAX_NAME_SIZE+13];
163 snprintf(sempath, sizeof(sempath), "/dev/shm/sem.%s", fName);
164 if (jack_promiscuous_perms(-1, sempath, fPromiscuousGid) < 0)
165 return false;
167 #endif
168 return true;
172 // Client side : get the published semaphore from server
173 bool JackPosixSemaphore::ConnectInput(const char* name, const char* server_name)
175 BuildName(name, server_name, fName, sizeof(fName));
176 jack_log("JackPosixSemaphore::Connect name = %s", fName);
178 // Temporary...
179 if (fSemaphore) {
180 jack_log("Already connected name = %s", name);
181 return true;
184 if ((fSemaphore = sem_open(fName, SEM_DEFAULT_O)) == (sem_t*)SEM_FAILED) {
185 jack_error("Connect: can't connect named semaphore name = %s err = %s", fName, strerror(errno));
186 return false;
187 } else if (fSemaphore) {
188 int val = 0;
189 sem_getvalue(fSemaphore, &val);
190 jack_log("JackPosixSemaphore::Connect sem_getvalue %ld", val);
191 return true;
192 } else {
193 jack_error("Connect: fSemaphore not initialized!");
194 return false;
198 bool JackPosixSemaphore::Connect(const char* name, const char* server_name)
200 return ConnectInput(name, server_name);
203 bool JackPosixSemaphore::ConnectOutput(const char* name, const char* server_name)
205 return ConnectInput(name, server_name);
208 bool JackPosixSemaphore::Disconnect()
210 if (fSemaphore) {
211 jack_log("JackPosixSemaphore::Disconnect name = %s", fName);
212 if (sem_close(fSemaphore) != 0) {
213 jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName, strerror(errno));
214 return false;
215 } else {
216 fSemaphore = NULL;
217 return true;
219 } else {
220 return true;
224 // Server side : destroy the semaphore
225 void JackPosixSemaphore::Destroy()
227 if (fSemaphore != NULL) {
228 jack_log("JackPosixSemaphore::Destroy name = %s", fName);
229 sem_unlink(fName);
230 if (sem_close(fSemaphore) != 0) {
231 jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName, strerror(errno));
233 fSemaphore = NULL;
234 } else {
235 jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
239 } // end of namespace