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 "serveur3"
55 #define CLIENT "client3"
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 class Test1
: public JackRunnableInterface
93 detail::JackSynchro
* fSynchro1
;
94 detail::JackSynchro
* fSynchro2
;
98 Test1(detail::JackSynchro
* synchro1
, detail::JackSynchro
* synchro2
)
99 : fSynchro1(synchro1
), fSynchro2(synchro2
)
106 LARGE_INTEGER t1
, t2
;
108 r1
= QueryPerformanceCounter(&t1
);
110 struct timeval T0
, T1
;
111 clock_t time1
, time2
;
112 // Get total time for 2 * ITER process swaps
114 gettimeofday(&T0
, 0);
116 printf("Execute loop\n");
117 for (int i
= 0; i
< ITER
; i
++) {
123 r2
= QueryPerformanceCounter (&t2
);
125 printf ("%5.1lf usec for inter process swap\n", elapsed(&t2
, &t1
) / (2.0 * ITER
));
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
));
137 int main(int ac
, char *av
[])
140 detail::JackSynchro
* sem1
= NULL
;
141 detail::JackSynchro
* sem2
= NULL
;
145 if (!QueryPerformanceFrequency (&gFreq
) ||
146 !overhead (&gQueryOverhead
)) {
147 printf ("cannot query performance counter\n");
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");
158 if (strcmp(av
[1], "-m") == 0) {
159 printf("Mach semaphore\n");
160 sem1
= new JackMachSemaphore();
161 sem2
= new JackMachSemaphore();
166 if (strcmp(av
[1], "-e") == 0) {
167 printf("Win event\n");
168 sem1
= new JackWinEvent();
169 sem2
= new JackWinEvent();
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) {
182 sem1
= new JackFifo();
183 sem2
= new JackFifo();
187 if (!sem1
->Allocate(SERVER
, "default", 0))
189 if (!sem2
->Allocate(CLIENT
, "default", 0))
192 // run test in RT thread
193 obj
= new Test1(sem1
, sem2
);
196 thread
= new JackMachThread(obj
, 10000 * 1000, 500 * 1000, 10000 * 1000);
200 thread
= new JackWinThread(obj
);
204 thread
= new JackPosixThread(obj
, false, 50, PTHREAD_CANCEL_DEFERRED
);
208 thread
->AcquireRealTime();