File: testSynchroServerClient.cpp

package info (click to toggle)
jackd2 1.9.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,632 kB
  • sloc: cpp: 50,117; ansic: 29,194; python: 11,637; sh: 88; makefile: 68; objc: 39
file content (259 lines) | stat: -rw-r--r-- 6,044 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
	Copyright (C) 2004-2008 Grame

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful, 
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifdef WIN32

#else

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/time.h>
#include <time.h>

#endif

#ifdef __APPLE__
	#include "JackMachSemaphore.h"
#endif

#ifdef WIN32
	#include "JackWinEvent.h"
#endif

#ifdef linux
	#include "JackPosixSemaphore.h"
	#include "JackFifo.h"
#endif

#include "JackPlatformPlug.h"

#define ITER 100000

#define SERVER "serveur1"
#define CLIENT "client1"

using namespace Jack;

#ifdef WIN32
LARGE_INTEGER gFreq;
long gQueryOverhead;

static long elapsed (LARGE_INTEGER * t1, LARGE_INTEGER *t2)
{
    long high = t1->HighPart - t2->HighPart;
    double low = t1->LowPart - t2->LowPart;
    // ignore values when high part changes
    return high ? 0 : (long)((low * 1000000) / gFreq.LowPart);
}

static BOOL overhead (long * overhead)
{
    LARGE_INTEGER t1, t2;
    BOOL r1, r2;
    int i = 50;
    r1 = QueryPerformanceCounter (&t1);
    while (i--)
        QueryPerformanceCounter (&t2);
    r2 = QueryPerformanceCounter (&t2);
    if (!r1 || !r2)
        return FALSE;
    *overhead = elapsed(&t2, &t1) / 50;
    return TRUE;
}

#endif

template <typename sync_type>
class Test1 : public JackRunnableInterface
{

    private:

        sync_type* fSynchro1;
        sync_type* fSynchro2;

    public:

        Test1(sync_type* synchro1, sync_type* synchro2)
                : fSynchro1(synchro1), fSynchro2(synchro2)
        {}

        bool Execute()
        {

		#ifdef WIN32
            LARGE_INTEGER t1, t2;
            BOOL r1, r2;
            r1 = QueryPerformanceCounter (&t1);
		#else
			struct timeval T0, T1;
            clock_t time1, time2;
            // Get total time for 2 * ITER process swaps
            time1 = clock();
            gettimeofday(&T0, 0);
		#endif

            printf("Execute loop Test1\n");
            for (int i = 0; i < ITER; i++) {
                fSynchro2->Signal();
                fSynchro1->Wait();
            }

		#ifdef WIN32
            r2 = QueryPerformanceCounter (&t2);
            elapsed(&t2, &t1);
            printf ("%5.1lf usec for inter process swap\n", elapsed(&t2, &t1) / (2.0 * ITER));
		#else
			time2 = clock();
            gettimeofday(&T1, 0);
            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));
            printf ("%f usec for inter process swap \n", (1e6 * ((time2 - time1) / (double(CLOCKS_PER_SEC)))) / (2.0 * ITER));
		#endif
            return true;
        }

};

template <typename sync_type>
class Test2 : public JackRunnableInterface
{

    private:

        sync_type* fSynchro1;
        sync_type* fSynchro2;

    public:

        Test2(sync_type* synchro1, sync_type* synchro2)
                : fSynchro1(synchro1), fSynchro2(synchro2)
        {}

        bool Execute()
        {
            printf("Execute loop Test2\n");
            for (int i = 0; i < ITER; i++) {
                fSynchro2->Wait();
                fSynchro1->Signal();
            }
            return true;
        }

};

template <typename sync_type>
void run_tests(void)
{
    sync_type sem1, sem2, sem3, sem4;

    sem1.Allocate(SERVER, "default", 0);
    sem2.Allocate(CLIENT, "default", 0);
    sem3.ConnectOutput(SERVER, "default");
    sem4.ConnectInput(CLIENT, "default");

    Test1<sync_type> obj1(&sem1, &sem2);
    Test2<sync_type> obj2(&sem3, &sem4);

    JackThread* thread1;
    JackThread* thread2;

#ifdef __APPLE__
    thread1 = new JackMachThread(&obj1, 10000 * 1000, 500 * 1000, 10000 * 1000);
    thread2 = new JackMachThread(&obj2, 10000 * 1000, 500 * 1000, 10000 * 1000);
#endif

#ifdef  WIN32
    thread1 = new JackWinThread(&obj1);
    thread2 = new JackWinThread(&obj2);
#endif

#ifdef linux
    thread1 = new JackPosixThread(&obj1, false, 50, PTHREAD_CANCEL_DEFERRED);
    thread2 = new JackPosixThread(&obj2, false, 50, PTHREAD_CANCEL_DEFERRED);
#endif
    thread1->Start();
    thread2->Start();
    //thread1->AcquireRealTime();
    //thread2->AcquireRealTime();
#ifdef  WIN32
    Sleep(30 * 1000);
#else
    sleep (30);
#endif

    thread1->Stop();
    thread2->Stop();
    sem3.Disconnect();
    sem4.Disconnect();
    sem1.Destroy();
    sem2.Destroy();

    delete thread1;
    delete thread2;
}

int main(int ac, char *av [])
{
#ifdef WIN32
    if (!QueryPerformanceFrequency (&gFreq) ||
            !overhead (&gQueryOverhead)) {
        printf ("cannot query performance counter\n");
    }
#endif

    printf("Test of synchronization primitives : inside a process\n");
    printf("type -s to test Posix semaphore\n");
    printf("type -f to test Fifo\n");
    printf("type -m to test Mach semaphore\n");
    printf("type -e to test Windows event\n");

#ifdef __APPLE__
    if (strcmp(av[1], "-m") == 0) {
        printf("Mach semaphore\n");
        run_tests<JackMachSemaphore>();
    }
#endif

#ifdef WIN32
    if (strcmp(av[1], "-e") == 0) {
        printf("Win event\n");
        run_tests<JackWinEvent>();
    }
#endif

#ifdef linux
    if (strcmp(av[1], "-s") == 0) {
        printf("Posix semaphore\n");
        run_tests<JackPosixSemaphore>();
    }

    if (strcmp(av[1], "-f") == 0) {
        printf("Fifo\n");
        run_tests<JackFifo>();
    }

 #endif
    return 0;
}