2 Copyright (C) 2004-2006 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackPosixSemaphore.h"
21 #include "JackChannel.h"
22 #include "JackError.h"
29 void JackPosixSemaphore::BuildName(const char* name
, char* res
)
31 sprintf(res
, "%s/jack_sem.%s", jack_client_dir
, name
);
34 bool JackPosixSemaphore::Signal()
42 if ((res
= sem_post(fSemaphore
)) != 0) {
43 jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName
, strerror(errno
));
48 bool JackPosixSemaphore::SignalAll()
56 if ((res
= sem_post(fSemaphore
)) != 0) {
57 jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName
, strerror(errno
));
63 bool JackPosixSemaphore::Wait()
67 if ((res = sem_wait(fSemaphore)) != 0) {
68 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
74 bool JackPosixSemaphore::Wait()
78 while ((res
= sem_wait(fSemaphore
) < 0)) {
79 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName
, strerror(errno
));
90 bool JackPosixSemaphore::TimedWait(long usec) // unusable semantic !!
96 gettimeofday(&now, 0);
97 time.tv_sec = now.tv_sec + usec / 1000000;
98 time.tv_nsec = (now.tv_usec + (usec % 1000000)) * 1000;
100 if ((res = sem_timedwait(fSemaphore, &time)) != 0) {
101 jack_error("JackPosixSemaphore::TimedWait err = %s", strerror(errno));
102 JackLog("now %ld %ld \n", now.tv_sec, now.tv_usec);
103 JackLog("next %ld %ld \n", time.tv_sec, time.tv_nsec/1000);
109 #warning "JackPosixSemaphore::TimedWait is not supported: Jack in SYNC mode with JackPosixSemaphore will not run properly !!"
111 bool JackPosixSemaphore::TimedWait(long usec)
118 #warning JackPosixSemaphore::TimedWait not available : synchronous mode may not work correctly if POSIX semaphore are used
120 bool JackPosixSemaphore::TimedWait(long usec
)
125 // Server side : publish the semaphore in the global namespace
126 bool JackPosixSemaphore::Allocate(const char* name
, int value
)
128 BuildName(name
, fName
);
129 JackLog("JackPosixSemaphore::Allocate name = %s val = %ld\n", fName
, value
);
131 if ((fSemaphore
= sem_open(fName
, O_CREAT
, 0777, value
)) == (sem_t
*)SEM_FAILED
) {
132 jack_error("Allocate: can't check in named semaphore name = %s err = %s", fName
, strerror(errno
));
139 // Client side : get the published semaphore from server
140 bool JackPosixSemaphore::ConnectInput(const char* name
)
142 BuildName(name
, fName
);
143 JackLog("JackPosixSemaphore::Connect %s\n", fName
);
147 JackLog("Already connected name = %s\n", name
);
151 if ((fSemaphore
= sem_open(fName
, O_CREAT
)) == (sem_t
*)SEM_FAILED
) {
152 jack_error("Connect: can't connect named semaphore name = %s err = %s", fName
, strerror(errno
));
156 sem_getvalue(fSemaphore
, &val
);
157 JackLog("JackPosixSemaphore::Connect sem_getvalue %ld\n", val
);
162 bool JackPosixSemaphore::Connect(const char* name
)
164 return ConnectInput(name
);
167 bool JackPosixSemaphore::ConnectOutput(const char* name
)
169 return ConnectInput(name
);
172 bool JackPosixSemaphore::Disconnect()
174 JackLog("JackPosixSemaphore::Disconnect %s\n", fName
);
177 if (sem_close(fSemaphore
) != 0) {
178 jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName
, strerror(errno
));
189 // Server side : destroy the semaphore
190 void JackPosixSemaphore::Destroy()
192 if (fSemaphore
!= NULL
) {
193 JackLog("JackPosixSemaphore::Destroy\n");
195 if (sem_close(fSemaphore
) != 0) {
196 jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName
, strerror(errno
));
200 jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
204 } // end of namespace