00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __JackProcessSync__
00021 #define __JackProcessSync__
00022
00023 #include "JackSyncInterface.h"
00024 #include "JackSynchro.h"
00025 #include <pthread.h>
00026 #include <sys/time.h>
00027 #include <unistd.h>
00028
00029 namespace Jack
00030 {
00031
00036 class JackProcessSync : public JackSyncInterface
00037 {
00038
00039 private:
00040
00041 pthread_mutex_t fLock;
00042 pthread_cond_t fCond;
00043
00044 public:
00045
00046 JackProcessSync(): JackSyncInterface()
00047 {
00048 pthread_mutex_init(&fLock, NULL);
00049 pthread_cond_init(&fCond, NULL);
00050 }
00051 virtual ~JackProcessSync()
00052 {
00053 pthread_mutex_destroy(&fLock);
00054 pthread_cond_destroy(&fCond);
00055 }
00056
00057 bool Allocate(const char* name)
00058 {
00059 return true;
00060 }
00061
00062 bool Connect(const char* name)
00063 {
00064 return true;
00065 }
00066
00067 void Destroy()
00068 {}
00069
00070 bool TimedWait(long usec)
00071 {
00072 struct timeval T0, T1;
00073 timespec time;
00074 struct timeval now;
00075 int res;
00076
00077 pthread_mutex_lock(&fLock);
00078 JackLog("JackProcessSync::Wait time out = %ld\n", usec);
00079 gettimeofday(&T0, 0);
00080
00081 static const UInt64 kNanosPerSec = 1000000000ULL;
00082 static const UInt64 kNanosPerUsec = 1000ULL;
00083 gettimeofday(&now, 0);
00084 UInt64 nextDateNanos = now.tv_sec * kNanosPerSec + (now.tv_usec + usec) * kNanosPerUsec;
00085 time.tv_sec = nextDateNanos / kNanosPerSec;
00086 time.tv_nsec = nextDateNanos % kNanosPerSec;
00087 res = pthread_cond_timedwait(&fCond, &fLock, &time);
00088 if (res != 0)
00089 jack_error("pthread_cond_timedwait error usec = %ld err = %s", usec, strerror(res));
00090
00091 gettimeofday(&T1, 0);
00092 pthread_mutex_unlock(&fLock);
00093 JackLog("JackProcessSync::Wait finished delta = %5.1lf\n",
00094 (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
00095 return (res == 0);
00096 }
00097
00098 void Wait()
00099 {
00100 int res;
00101 pthread_mutex_lock(&fLock);
00102 JackLog("JackProcessSync::Wait...\n");
00103 if ((res = pthread_cond_wait(&fCond, &fLock)) != 0)
00104 jack_error("pthread_cond_wait error err = %s", strerror(errno));
00105 pthread_mutex_unlock(&fLock);
00106 JackLog("JackProcessSync::Wait finished\n");
00107 }
00108
00109 void SignalAll()
00110 {
00111
00112 pthread_cond_broadcast(&fCond);
00113
00114 }
00115
00116 };
00117
00122 class JackInterProcessSync : public JackSyncInterface
00123 {
00124
00125 private:
00126
00127 JackSynchro* fSynchro;
00128
00129 public:
00130
00131 JackInterProcessSync(JackSynchro* synchro): fSynchro(synchro)
00132 {}
00133 virtual ~JackInterProcessSync()
00134 {
00135 delete fSynchro;
00136 }
00137
00138 bool Allocate(const char* name)
00139 {
00140 return fSynchro->Allocate(name, "", 0);
00141 }
00142
00143 void Destroy()
00144 {
00145 fSynchro->Destroy();
00146 }
00147
00148 bool Connect(const char* name)
00149 {
00150 return fSynchro->Connect(name, "");
00151 }
00152
00153 bool TimedWait(long usec)
00154 {
00155 struct timeval T0, T1;
00156 JackLog("JackInterProcessSync::Wait...\n");
00157 gettimeofday(&T0, 0);
00158 bool res = fSynchro->TimedWait(usec);
00159 gettimeofday(&T1, 0);
00160 JackLog("JackInterProcessSync::Wait finished delta = %5.1lf\n",
00161 (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec));
00162 return res;
00163 }
00164
00165 void Wait()
00166 {
00167 fSynchro->Wait();
00168 }
00169
00170 void SignalAll()
00171 {
00172 fSynchro->SignalAll();
00173 }
00174 };
00175
00176
00177 }
00178
00179 #endif
00180