Merge branch 'master' into ladi-experimental
[jackdbus.git] / tests / testSem.cpp
bloba3e9c4c0f473a5aff38d16563c532f87d0a36b2e
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 #include <stdio.h>
21 #include <sys/time.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #ifdef __APPLE__
26 #include "JackMachSemaphore.h"
27 #endif
29 #include "JackPosixSemaphore.h"
30 #include "JackFifo.h"
32 #include "JackPlatformPlug.h"
34 #define ITER 500000
36 using namespace Jack;
38 struct ServerThread : public JackRunnableInterface {
40 JackThread* fThread;
41 detail::JackSynchro* fServerSem;
42 detail::JackSynchro* fClientSem;
44 ServerThread()
46 fServerSem->Allocate("JackSemServer", "default", 0);
47 fClientSem->Allocate("JackSemClient", "default", 0);
48 //fThread = new JackMachThread(this);
49 fThread->SetParams(0, 500*1000, 500*1000);
50 fThread->Start();
51 //fThread->AcquireRealTime();
54 virtual ~ServerThread()
56 fThread->Kill();
57 delete fThread;
60 bool Execute()
62 printf("Execute Server\n");
63 for (int i = 0; i < ITER; i++) {
64 fClientSem->Signal();
65 fServerSem->Wait();
67 return true;
72 struct ClientThread : public JackRunnableInterface {
74 JackThread* fThread;
75 detail::JackSynchro* fServerSem;
76 detail::JackSynchro* fClientSem;
78 ClientThread()
80 fServerSem->Connect("JackSemServer", "default");
81 fClientSem->Connect("JackSemClient", "default");
82 //fThread = new JackMachThread(this);
83 fThread->SetParams(0, 500*1000, 500*1000);
84 fThread->Start();
85 //fThread->AcquireRealTime();
88 virtual ~ClientThread()
90 fThread->Kill();
91 delete fThread;
94 bool Execute()
96 struct timeval T0, T1;
97 printf("Execute Client\n");
98 fClientSem->Wait();
99 gettimeofday(&T0, 0);
101 for (int i = 0; i < ITER; i++) {
102 fServerSem->Signal();
103 fClientSem->Wait();
106 gettimeofday(&T1, 0);
107 printf("%5.1lf usec\n", (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec) / (2.0 * ITER));
108 return true;
113 void server(detail::JackSynchro* sem)
115 char c;
116 printf("server\n");
118 sem->Allocate("JackSem", "default", 0);
120 while (((c = getchar()) != 'q')) {
122 switch(c) {
124 case 's':
125 printf("SynchroSignal....\n");
126 //sem->Signal();
127 sem->SignalAll();
128 printf("SynchroSignal OK\n");
129 break;
131 case 'w':
132 printf("SemaphoreWait....\n");
133 sem->Wait();
134 printf("SemaphoreWait OK\n");
135 break;
140 void client(detail::JackSynchro* sem)
142 char c;
143 printf("client\n");
145 sem->Connect("JackSem", "default");
147 while (((c = getchar()) != 'q')) {
149 switch(c) {
151 case 's':
152 printf("SemaphoreSignal....\n");
153 sem->Signal();
154 printf("SemaphoreSignal OK\n");
155 break;
157 case 'w':
158 printf("SemaphoreWait....\n");
159 sem->Wait();
160 printf("SemaphoreWait OK\n");
161 break;
166 int main (int argc, char * const argv[])
168 char c;
169 ServerThread* serverthread = NULL;
170 ClientThread* clientthread = NULL;
171 detail::JackSynchro* sem1 = NULL;
173 if (strcmp(argv[1],"-s") == 0) {
174 printf("Posix semaphore\n");
175 sem1 = new JackPosixSemaphore();
178 if (strcmp(argv[1],"-f") == 0) {
179 printf("Fifo\n");
180 sem1 = new JackFifo();
183 #ifdef __APPLE__
184 if (strcmp(argv[1],"-m") == 0) {
185 printf("Mach semaphore\n");
186 sem1 = new JackMachSemaphore();
188 #endif
191 if (strcmp(argv[2], "server") == 0) {
192 serverthread = new ServerThread();
193 } else {
194 clientthread = new ClientThread();
198 if (strcmp(argv[2], "server") == 0) {
199 server(sem1);
200 } else {
201 client(sem1);
204 while (((c = getchar()) != 'q')) {}
206 delete serverthread;
207 delete clientthread;