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"
33 \brief A synchronization primitive built using a condition variable.
36 class JackProcessSync
: public JackSyncInterface
41 pthread_mutex_t fLock
; // Mutex
42 pthread_cond_t fCond
; // Condition variable
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
)
62 bool Connect(const char* name
)
70 bool TimedWait(long usec
)
72 struct timeval T0
, T1
;
77 pthread_mutex_lock(&fLock
);
78 JackLog("JackProcessSync::Wait time out = %ld\n", usec
);
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
);
89 jack_error("pthread_cond_timedwait error usec = %ld err = %s", usec
, strerror(res
));
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
));
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");
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
127 JackSynchro
* fSynchro
;
131 JackInterProcessSync(JackSynchro
* synchro
): fSynchro(synchro
)
133 virtual ~JackInterProcessSync()
138 bool Allocate(const char* name
)
140 return fSynchro
->Allocate(name
, 0);
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
));
172 fSynchro
->SignalAll();
177 } // end of namespace