Dmitry Baikov jackmp-time patch: add jack_get_time, jack_time_to_frames, jack_frames_...
[jack2.git] / common / JackPosixSemaphore.cpp
blob18e7a0f5febe65634275577ea0112dd0236e7be2
1 /*
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"
23 #include <fcntl.h>
24 #include <sys/time.h>
26 namespace Jack
29 void JackPosixSemaphore::BuildName(const char* name, char* res)
31 sprintf(res, "%s/jack_sem.%s", jack_client_dir, name);
34 bool JackPosixSemaphore::Signal()
36 int res;
37 assert(fSemaphore);
39 if (fFlush)
40 return true;
42 if ((res = sem_post(fSemaphore)) != 0) {
43 jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName, strerror(errno));
45 return (res == 0);
48 bool JackPosixSemaphore::SignalAll()
50 int res;
51 assert(fSemaphore);
53 if (fFlush)
54 return true;
56 if ((res = sem_post(fSemaphore)) != 0) {
57 jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName, strerror(errno));
59 return (res == 0);
63 bool JackPosixSemaphore::Wait()
65 int res;
66 assert(fSemaphore);
67 if ((res = sem_wait(fSemaphore)) != 0) {
68 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
70 return (res == 0);
74 bool JackPosixSemaphore::Wait()
76 int res;
78 while ((res = sem_wait(fSemaphore) < 0)) {
79 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
80 if (errno != EINTR)
81 break;
83 return (res == 0);
88 #ifdef __linux__
90 bool JackPosixSemaphore::TimedWait(long usec) // unusable semantic !!
92 int res;
93 struct timeval now;
94 timespec time;
95 assert(fSemaphore);
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);
105 return (res == 0);
108 #else
109 #warning "JackPosixSemaphore::TimedWait is not supported: Jack in SYNC mode with JackPosixSemaphore will not run properly !!"
111 bool JackPosixSemaphore::TimedWait(long usec)
113 return Wait();
115 #endif
118 #warning JackPosixSemaphore::TimedWait not available : synchronous mode may not work correctly if POSIX semaphore are used
120 bool JackPosixSemaphore::TimedWait(long usec)
122 return Wait();
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));
133 return false;
134 } else {
135 return true;
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);
145 // Temporary...
146 if (fSemaphore) {
147 JackLog("Already connected name = %s\n", name);
148 return true;
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));
153 return false;
154 } else {
155 int val = 0;
156 sem_getvalue(fSemaphore, &val);
157 JackLog("JackPosixSemaphore::Connect sem_getvalue %ld\n", val);
158 return true;
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);
176 if (fSemaphore) {
177 if (sem_close(fSemaphore) != 0) {
178 jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName, strerror(errno));
179 return false;
180 } else {
181 fSemaphore = NULL;
182 return true;
184 } else {
185 return true;
189 // Server side : destroy the semaphore
190 void JackPosixSemaphore::Destroy()
192 if (fSemaphore != NULL) {
193 JackLog("JackPosixSemaphore::Destroy\n");
194 sem_unlink(fName);
195 if (sem_close(fSemaphore) != 0) {
196 jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName, strerror(errno));
198 fSemaphore = NULL;
199 } else {
200 jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
204 } // end of namespace