Correct JackPortAudioDriver::Open : special case for ASIO drivers.
[jack2.git] / tests / testSynchroServerClient.cpp
blob1e1f46097a404719f65f3ec345145998c89fbb80
1 /*
2 Copyright (C) 2004-2008 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 #ifdef WIN32
22 #else
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <semaphore.h>
32 #include <sys/time.h>
33 #include <time.h>
35 #endif
37 #ifdef __APPLE__
38 #include "JackMachSemaphore.h"
39 #endif
41 #ifdef WIN32
42 #include "JackWinEvent.h"
43 #endif
45 #ifdef linux
46 #include "JackPosixSemaphore.h"
47 #include "JackFifo.h"
48 #endif
50 #include "JackPlatformPlug.h"
52 #define ITER 100000
54 #define SERVER "serveur1"
55 #define CLIENT "client1"
57 using namespace Jack;
59 #ifdef WIN32
60 LARGE_INTEGER gFreq;
61 long gQueryOverhead;
63 static long elapsed (LARGE_INTEGER * t1, LARGE_INTEGER *t2)
65 long high = t1->HighPart - t2->HighPart;
66 double low = t1->LowPart - t2->LowPart;
67 // ignore values when high part changes
68 return high ? 0 : (long)((low * 1000000) / gFreq.LowPart);
71 static BOOL overhead (long * overhead)
73 LARGE_INTEGER t1, t2;
74 BOOL r1, r2;
75 int i = 50;
76 r1 = QueryPerformanceCounter (&t1);
77 while (i--)
78 QueryPerformanceCounter (&t2);
79 r2 = QueryPerformanceCounter (&t2);
80 if (!r1 || !r2)
81 return FALSE;
82 *overhead = elapsed(&t2, &t1) / 50;
83 return TRUE;
86 #endif
88 template <typename sync_type>
89 class Test1 : public JackRunnableInterface
92 private:
94 sync_type* fSynchro1;
95 sync_type* fSynchro2;
97 public:
99 Test1(sync_type* synchro1, sync_type* synchro2)
100 : fSynchro1(synchro1), fSynchro2(synchro2)
103 bool Execute()
106 #ifdef WIN32
107 LARGE_INTEGER t1, t2;
108 BOOL r1, r2;
109 r1 = QueryPerformanceCounter (&t1);
110 #else
111 struct timeval T0, T1;
112 clock_t time1, time2;
113 // Get total time for 2 * ITER process swaps
114 time1 = clock();
115 gettimeofday(&T0, 0);
116 #endif
118 printf("Execute loop Test1\n");
119 for (int i = 0; i < ITER; i++) {
120 fSynchro2->Signal();
121 fSynchro1->Wait();
124 #ifdef WIN32
125 r2 = QueryPerformanceCounter (&t2);
126 elapsed(&t2, &t1);
127 printf ("%5.1lf usec for inter process swap\n", elapsed(&t2, &t1) / (2.0 * ITER));
128 #else
129 time2 = clock();
130 gettimeofday(&T1, 0);
131 printf ("%5.1lf usec for inter process swap\n", (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec) / (2.0 * ITER));
132 printf ("%f usec for inter process swap \n", (1e6 * ((time2 - time1) / (double(CLOCKS_PER_SEC)))) / (2.0 * ITER));
133 #endif
134 return true;
139 template <typename sync_type>
140 class Test2 : public JackRunnableInterface
143 private:
145 sync_type* fSynchro1;
146 sync_type* fSynchro2;
148 public:
150 Test2(sync_type* synchro1, sync_type* synchro2)
151 : fSynchro1(synchro1), fSynchro2(synchro2)
154 bool Execute()
156 printf("Execute loop Test2\n");
157 for (int i = 0; i < ITER; i++) {
158 fSynchro2->Wait();
159 fSynchro1->Signal();
161 return true;
166 template <typename sync_type>
167 void run_tests(void)
169 sync_type sem1, sem2, sem3, sem4;
171 sem1.Allocate(SERVER, "default", 0);
172 sem2.Allocate(CLIENT, "default", 0);
173 sem3.ConnectOutput(SERVER, "default");
174 sem4.ConnectInput(CLIENT, "default");
176 Test1<sync_type> obj1(&sem1, &sem2);
177 Test2<sync_type> obj2(&sem3, &sem4);
179 JackThread* thread1;
180 JackThread* thread2;
182 #ifdef __APPLE__
183 thread1 = new JackMachThread(&obj1, 10000 * 1000, 500 * 1000, 10000 * 1000);
184 thread2 = new JackMachThread(&obj2, 10000 * 1000, 500 * 1000, 10000 * 1000);
185 #endif
187 #ifdef WIN32
188 thread1 = new JackWinThread(&obj1);
189 thread2 = new JackWinThread(&obj2);
190 #endif
192 #ifdef linux
193 thread1 = new JackPosixThread(&obj1, false, 50, PTHREAD_CANCEL_DEFERRED);
194 thread2 = new JackPosixThread(&obj2, false, 50, PTHREAD_CANCEL_DEFERRED);
195 #endif
196 thread1->Start();
197 thread2->Start();
198 //thread1->AcquireRealTime();
199 //thread2->AcquireRealTime();
200 #ifdef WIN32
201 Sleep(30 * 1000);
202 #else
203 sleep (30);
204 #endif
206 thread1->Stop();
207 thread2->Stop();
208 sem3.Disconnect();
209 sem4.Disconnect();
210 sem1.Destroy();
211 sem2.Destroy();
213 delete thread1;
214 delete thread2;
217 int main(int ac, char *av [])
219 #ifdef WIN32
220 if (!QueryPerformanceFrequency (&gFreq) ||
221 !overhead (&gQueryOverhead)) {
222 printf ("cannot query performance counter\n");
224 #endif
226 printf("Test of synchronization primitives : inside a process\n");
227 printf("type -s to test Posix semaphore\n");
228 printf("type -f to test Fifo\n");
229 printf("type -m to test Mach semaphore\n");
230 printf("type -e to test Windows event\n");
232 #ifdef __APPLE__
233 if (strcmp(av[1], "-m") == 0) {
234 printf("Mach semaphore\n");
235 run_tests<JackMachSemaphore>();
237 #endif
239 #ifdef WIN32
240 if (strcmp(av[1], "-e") == 0) {
241 printf("Win event\n");
242 run_tests<JackWinEvent>();
244 #endif
246 #ifdef linux
247 if (strcmp(av[1], "-s") == 0) {
248 printf("Posix semaphore\n");
249 run_tests<JackPosixSemaphore>();
252 if (strcmp(av[1], "-f") == 0) {
253 printf("Fifo\n");
254 run_tests<JackFifo>();
257 #endif
258 return 0;