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"
31 void JackPosixSemaphore::BuildName(const char* client_name
, const char* server_name
, char* res
)
33 char ext_client_name
[JACK_CLIENT_NAME_SIZE
+ 1];
34 JackTools::RewriteName(client_name
, ext_client_name
);
35 sprintf(res
, "jack_sem.%d_%s_%s", JackTools::GetUID(), server_name
, ext_client_name
);
38 bool JackPosixSemaphore::Signal()
43 jack_error("JackPosixSemaphore::Signal name = %s already desallocated!!", fName
);
50 if ((res
= sem_post(fSemaphore
)) != 0) {
51 jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName
, strerror(errno
));
56 bool JackPosixSemaphore::SignalAll()
61 jack_error("JackPosixSemaphore::SignalAll name = %s already desallocated!!", fName
);
68 if ((res
= sem_post(fSemaphore
)) != 0) {
69 jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName
, strerror(errno
));
75 bool JackPosixSemaphore::Wait()
80 jack_error("JackPosixSemaphore::Wait name = %s already desallocated!!", fName);
84 if ((res = sem_wait(fSemaphore)) != 0) {
85 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
91 bool JackPosixSemaphore::Wait()
95 while ((res
= sem_wait(fSemaphore
) < 0)) {
96 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName
, strerror(errno
));
103 #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) // glibc feature test
105 bool JackPosixSemaphore::TimedWait(long usec
)
112 jack_error("JackPosixSemaphore::TimedWait name = %s already desallocated!!", fName
);
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;
121 if ((res
= sem_timedwait(fSemaphore
, &time
)) != 0) {
122 jack_error("JackPosixSemaphore::TimedWait err = %s", strerror(errno
));
123 jack_log("now %ld %ld ", now
.tv_sec
, now
.tv_usec
);
124 jack_log("next %ld %ld ", time
.tv_sec
, time
.tv_nsec
/1000);
130 #warning "JackPosixSemaphore::TimedWait is not supported: Jack in SYNC mode with JackPosixSemaphore will not run properly !!"
132 bool JackPosixSemaphore::TimedWait(long usec
)
138 // Server side : publish the semaphore in the global namespace
139 bool JackPosixSemaphore::Allocate(const char* name
, const char* server_name
, int value
)
141 BuildName(name
, server_name
, fName
);
142 jack_log("JackPosixSemaphore::Allocate name = %s val = %ld", fName
, value
);
144 if ((fSemaphore
= sem_open(fName
, O_CREAT
, 0777, value
)) == (sem_t
*)SEM_FAILED
) {
145 jack_error("Allocate: can't check in named semaphore name = %s err = %s", fName
, strerror(errno
));
152 // Client side : get the published semaphore from server
153 bool JackPosixSemaphore::ConnectInput(const char* name
, const char* server_name
)
155 BuildName(name
, server_name
, fName
);
156 jack_log("JackPosixSemaphore::Connect %s", fName
);
160 jack_log("Already connected name = %s", name
);
164 if ((fSemaphore
= sem_open(fName
, O_CREAT
)) == (sem_t
*)SEM_FAILED
) {
165 jack_error("Connect: can't connect named semaphore name = %s err = %s", fName
, strerror(errno
));
169 sem_getvalue(fSemaphore
, &val
);
170 jack_log("JackPosixSemaphore::Connect sem_getvalue %ld", val
);
175 bool JackPosixSemaphore::Connect(const char* name
, const char* server_name
)
177 return ConnectInput(name
, server_name
);
180 bool JackPosixSemaphore::ConnectOutput(const char* name
, const char* server_name
)
182 return ConnectInput(name
, server_name
);
185 bool JackPosixSemaphore::Disconnect()
187 jack_log("JackPosixSemaphore::Disconnect %s", fName
);
190 if (sem_close(fSemaphore
) != 0) {
191 jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName
, strerror(errno
));
202 // Server side : destroy the semaphore
203 void JackPosixSemaphore::Destroy()
205 if (fSemaphore
!= NULL
) {
206 jack_log("JackPosixSemaphore::Destroy");
208 if (sem_close(fSemaphore
) != 0) {
209 jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName
, strerror(errno
));
213 jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
217 } // end of namespace