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.
27 #include <sys/types.h>
31 #include <semaphore.h>
38 #include "JackMachSemaphore.h"
42 #include "JackWinEvent.h"
46 #include "JackPosixSemaphore.h"
50 #include "JackPlatformPlug.h"
54 #define SERVER "serveur1"
55 #define CLIENT "client1"
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
)
76 r1
= QueryPerformanceCounter (&t1
);
78 QueryPerformanceCounter (&t2
);
79 r2
= QueryPerformanceCounter (&t2
);
82 *overhead
= elapsed(&t2
, &t1
) / 50;
88 template <typename sync_type
>
89 class Test1
: public JackRunnableInterface
99 Test1(sync_type
* synchro1
, sync_type
* synchro2
)
100 : fSynchro1(synchro1
), fSynchro2(synchro2
)
107 LARGE_INTEGER t1
, t2
;
109 r1
= QueryPerformanceCounter (&t1
);
111 struct timeval T0
, T1
;
112 clock_t time1
, time2
;
113 // Get total time for 2 * ITER process swaps
115 gettimeofday(&T0
, 0);
118 printf("Execute loop Test1\n");
119 for (int i
= 0; i
< ITER
; i
++) {
125 r2
= QueryPerformanceCounter (&t2
);
127 printf ("%5.1lf usec for inter process swap\n", elapsed(&t2
, &t1
) / (2.0 * ITER
));
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
));
139 template <typename sync_type
>
140 class Test2
: public JackRunnableInterface
145 sync_type
* fSynchro1
;
146 sync_type
* fSynchro2
;
150 Test2(sync_type
* synchro1
, sync_type
* synchro2
)
151 : fSynchro1(synchro1
), fSynchro2(synchro2
)
156 printf("Execute loop Test2\n");
157 for (int i
= 0; i
< ITER
; i
++) {
166 template <typename sync_type
>
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
);
183 thread1
= new JackMachThread(&obj1
, 10000 * 1000, 500 * 1000, 10000 * 1000);
184 thread2
= new JackMachThread(&obj2
, 10000 * 1000, 500 * 1000, 10000 * 1000);
188 thread1
= new JackWinThread(&obj1
);
189 thread2
= new JackWinThread(&obj2
);
193 thread1
= new JackPosixThread(&obj1
, false, 50, PTHREAD_CANCEL_DEFERRED
);
194 thread2
= new JackPosixThread(&obj2
, false, 50, PTHREAD_CANCEL_DEFERRED
);
198 //thread1->AcquireRealTime();
199 //thread2->AcquireRealTime();
217 int main(int ac
, char *av
[])
220 if (!QueryPerformanceFrequency (&gFreq
) ||
221 !overhead (&gQueryOverhead
)) {
222 printf ("cannot query performance counter\n");
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");
233 if (strcmp(av
[1], "-m") == 0) {
234 printf("Mach semaphore\n");
235 run_tests
<JackMachSemaphore
>();
240 if (strcmp(av
[1], "-e") == 0) {
241 printf("Win event\n");
242 run_tests
<JackWinEvent
>();
247 if (strcmp(av
[1], "-s") == 0) {
248 printf("Posix semaphore\n");
249 run_tests
<JackPosixSemaphore
>();
252 if (strcmp(av
[1], "-f") == 0) {
254 run_tests
<JackFifo
>();