Merge pull request #23 from jackaudio/device_reservation_fixes
[jack2.git] / tests / testSynchroClient.cpp
blob6da201c5d423154de275083bbc39dcdb90577bca
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 class Test2 : public JackRunnableInterface
62 private:
64 detail::JackSynchro* fSynchro1;
65 detail::JackSynchro* fSynchro2;
67 public:
69 Test2(detail::JackSynchro* synchro1, detail::JackSynchro* synchro2)
70 : fSynchro1(synchro1), fSynchro2(synchro2)
73 bool Execute()
75 int a = 1;
76 for (int i = 0; i < ITER; i++) {
77 fSynchro2->Wait();
78 for (int j = 0; j < 2000000; j++) {
79 a += j;
81 fSynchro1->Signal();
83 return true;
88 int main(int ac, char *av [])
90 Test2* obj;
91 detail::JackSynchro* sem1 = NULL;
92 detail::JackSynchro* sem2 = NULL;
93 JackThread* thread;
95 printf("Test of synchronization primitives : client side\n");
96 printf("type -s to test Posix semaphore\n");
97 printf("type -f to test Fifo\n");
98 printf("type -m to test Mach semaphore\n");
99 printf("type -e to test Windows event\n");
101 #ifdef __APPLE__
102 if (strcmp(av[1], "-m") == 0) {
103 printf("Mach semaphore\n");
104 sem1 = new JackMachSemaphore();
105 sem2 = new JackMachSemaphore();
107 #endif
109 #ifdef WIN32
110 if (strcmp(av[1], "-e") == 0) {
111 printf("Win event\n");
112 sem1 = new JackWinEvent();
113 sem2 = new JackWinEvent();
115 #endif
117 #ifdef linux
118 if (strcmp(av[1], "-s") == 0) {
119 printf("Posix semaphore\n");
120 sem1 = new JackPosixSemaphore();
121 sem2 = new JackPosixSemaphore();
124 if (strcmp(av[1], "-f") == 0) {
125 printf("Fifo\n");
126 sem1 = new JackFifo();
127 sem2 = new JackFifo();
129 #endif
131 if (!sem1->ConnectOutput(SERVER, "default"))
132 return -1;
133 if (!sem2->ConnectInput(CLIENT, "default"))
134 return -1;
136 obj = new Test2(sem1, sem2);
138 #ifdef __APPLE__
139 thread = new JackMachThread(obj, 10000 * 1000, 500 * 1000, 10000 * 1000);
140 #endif
142 #ifdef WIN32
143 thread = new JackWinThread(obj);
144 #endif
146 #ifdef linux
147 thread = new JackPosixThread(obj, false, 50, PTHREAD_CANCEL_DEFERRED);
148 #endif
150 thread->Start();
151 thread->AcquireRealTime();
152 #ifdef WIN32
153 Sleep(30 * 1000);
154 #else
155 sleep(30);
156 #endif
157 //thread->Stop();
158 thread->Kill();
159 sem1->Disconnect();
160 sem2->Disconnect();
161 delete obj;
162 delete thread;
163 delete sem1;
164 delete sem2;
165 return 0;