merge new MTDM code from Fons' latest release.
[jack2.git] / tests / testSynchroServer.cpp
blob561988d34eea57851275176864069343dca9a6c1
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 1000
54 #define SERVER "serveur3"
55 #define CLIENT "client3"
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 class Test1 : public JackRunnableInterface
91 private:
93 detail::JackSynchro* fSynchro1;
94 detail::JackSynchro* fSynchro2;
96 public:
98 Test1(detail::JackSynchro* synchro1, detail::JackSynchro* synchro2)
99 : fSynchro1(synchro1), fSynchro2(synchro2)
102 bool Execute()
105 #ifdef WIN32
106 LARGE_INTEGER t1, t2;
107 BOOL r1, r2;
108 r1 = QueryPerformanceCounter(&t1);
109 #else
110 struct timeval T0, T1;
111 clock_t time1, time2;
112 // Get total time for 2 * ITER process swaps
113 time1 = clock();
114 gettimeofday(&T0, 0);
115 #endif
116 printf("Execute loop\n");
117 for (int i = 0; i < ITER; i++) {
118 fSynchro2->Signal();
119 fSynchro1->Wait();
122 #ifdef WIN32
123 r2 = QueryPerformanceCounter (&t2);
124 elapsed(&t2, &t1);
125 printf ("%5.1lf usec for inter process swap\n", elapsed(&t2, &t1) / (2.0 * ITER));
126 #else
127 time2 = clock();
128 gettimeofday(&T1, 0);
129 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));
130 printf ("%f usec for inter process swap \n", (1e6 * ((time2 - time1) / (double(CLOCKS_PER_SEC)))) / (2.0 * ITER));
131 #endif
132 return true;
137 int main(int ac, char *av [])
139 Test1* obj;
140 detail::JackSynchro* sem1 = NULL;
141 detail::JackSynchro* sem2 = NULL;
142 JackThread* thread;
144 #ifdef WIN32
145 if (!QueryPerformanceFrequency (&gFreq) ||
146 !overhead (&gQueryOverhead)) {
147 printf ("cannot query performance counter\n");
149 #endif
151 printf("Test of synchronization primitives : server side\n");
152 printf("type -s to test Posix semaphore\n");
153 printf("type -f to test Fifo\n");
154 printf("type -m to test Mach semaphore\n");
155 printf("type -e to test Windows event\n");
157 #ifdef __APPLE__
158 if (strcmp(av[1], "-m") == 0) {
159 printf("Mach semaphore\n");
160 sem1 = new JackMachSemaphore();
161 sem2 = new JackMachSemaphore();
163 #endif
165 #ifdef WIN32
166 if (strcmp(av[1], "-e") == 0) {
167 printf("Win event\n");
168 sem1 = new JackWinEvent();
169 sem2 = new JackWinEvent();
171 #endif
173 #ifdef linux
174 if (strcmp(av[1], "-s") == 0) {
175 printf("Posix semaphore\n");
176 sem1 = new JackPosixSemaphore();
177 sem2 = new JackPosixSemaphore();
180 if (strcmp(av[1], "-f") == 0) {
181 printf("Fifo\n");
182 sem1 = new JackFifo();
183 sem2 = new JackFifo();
185 #endif
187 if (!sem1->Allocate(SERVER, "default", 0))
188 return -1;
189 if (!sem2->Allocate(CLIENT, "default", 0))
190 return -1;
192 // run test in RT thread
193 obj = new Test1(sem1, sem2);
195 #ifdef __APPLE__
196 thread = new JackMachThread(obj, 10000 * 1000, 500 * 1000, 10000 * 1000);
197 #endif
199 #ifdef WIN32
200 thread = new JackWinThread(obj);
201 #endif
203 #ifdef linux
204 thread = new JackPosixThread(obj, false, 50, PTHREAD_CANCEL_DEFERRED);
205 #endif
207 thread->Start();
208 thread->AcquireRealTime();
209 #ifdef WIN32
210 Sleep(90 * 1000);
211 #else
212 sleep(30);
213 #endif
215 thread->Stop();
216 sem1->Destroy();
217 sem2->Destroy();
218 delete obj;
219 delete thread;
220 delete sem1;
221 delete sem2;
222 return 0;