Add Pieter Palmers FreeBob driver.
[jack2.git] / common / JackProcessSync.h
blobc8ddd99ee59c87171526a308124118e9ba8546ad
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 #ifndef __JackProcessSync__
21 #define __JackProcessSync__
23 #include "JackSyncInterface.h"
24 #include "JackSynchro.h"
25 #include <pthread.h>
26 #include <sys/time.h>
27 #include <unistd.h>
29 namespace Jack
32 /*!
33 \brief A synchronization primitive built using a condition variable.
36 class JackProcessSync : public JackSyncInterface
39 private:
41 pthread_mutex_t fLock; // Mutex
42 pthread_cond_t fCond; // Condition variable
44 public:
46 JackProcessSync(): JackSyncInterface()
48 pthread_mutex_init(&fLock, NULL);
49 pthread_cond_init(&fCond, NULL);
51 virtual ~JackProcessSync()
53 pthread_mutex_destroy(&fLock);
54 pthread_cond_destroy(&fCond);
57 bool Allocate(const char* name)
59 return true;
62 bool Connect(const char* name)
64 return true;
67 void Destroy()
70 bool TimedWait(long usec)
72 struct timeval T0, T1;
73 timespec time;
74 struct timeval now;
75 int res;
77 pthread_mutex_lock(&fLock);
78 JackLog("JackProcessSync::Wait time out = %ld\n", usec);
79 gettimeofday(&T0, 0);
81 static const UInt64 kNanosPerSec = 1000000000ULL;
82 static const UInt64 kNanosPerUsec = 1000ULL;
83 gettimeofday(&now, 0);
84 UInt64 nextDateNanos = now.tv_sec * kNanosPerSec + (now.tv_usec + usec) * kNanosPerUsec;
85 time.tv_sec = nextDateNanos / kNanosPerSec;
86 time.tv_nsec = nextDateNanos % kNanosPerSec;
87 res = pthread_cond_timedwait(&fCond, &fLock, &time);
88 if (res != 0)
89 jack_error("pthread_cond_timedwait error usec = %ld err = %s", usec, strerror(res));
91 gettimeofday(&T1, 0);
92 pthread_mutex_unlock(&fLock);
93 JackLog("JackProcessSync::Wait finished delta = %5.1lf\n",
94 (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
95 return (res == 0);
98 void Wait()
100 int res;
101 pthread_mutex_lock(&fLock);
102 JackLog("JackProcessSync::Wait...\n");
103 if ((res = pthread_cond_wait(&fCond, &fLock)) != 0)
104 jack_error("pthread_cond_wait error err = %s", strerror(errno));
105 pthread_mutex_unlock(&fLock);
106 JackLog("JackProcessSync::Wait finished\n");
109 void SignalAll()
111 //pthread_mutex_lock(&fLock);
112 pthread_cond_broadcast(&fCond);
113 //pthread_mutex_unlock(&fLock);
119 \brief A synchronization primitive built using an inter-process synchronization object.
122 class JackInterProcessSync : public JackSyncInterface
125 private:
127 JackSynchro* fSynchro;
129 public:
131 JackInterProcessSync(JackSynchro* synchro): fSynchro(synchro)
133 virtual ~JackInterProcessSync()
135 delete fSynchro;
138 bool Allocate(const char* name)
140 return fSynchro->Allocate(name, 0);
143 void Destroy()
145 fSynchro->Destroy();
148 bool Connect(const char* name)
150 return fSynchro->Connect(name);
153 bool TimedWait(long usec)
155 struct timeval T0, T1;
156 JackLog("JackInterProcessSync::Wait...\n");
157 gettimeofday(&T0, 0);
158 bool res = fSynchro->TimedWait(usec);
159 gettimeofday(&T1, 0);
160 JackLog("JackInterProcessSync::Wait finished delta = %5.1lf\n",
161 (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
162 return res;
165 void Wait()
167 fSynchro->Wait();
170 void SignalAll()
172 fSynchro->SignalAll();
177 } // end of namespace
179 #endif