jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
6.8KB

  1. /*
  2. Copyright (C) 2004-2006 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifdef WIN32
  16. #else
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <sys/mman.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <semaphore.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. #endif
  28. #ifdef __APPLE__
  29. #include "JackMachSemaphore.h"
  30. #include "JackMachThread.h"
  31. #endif
  32. #ifdef WIN32
  33. #include "JackWinThread.h"
  34. #include "JackWinEvent.h"
  35. #endif
  36. #ifdef linux
  37. #include "JackPosixThread.h"
  38. #include "JackPosixSemaphore.h"
  39. #include "JackPthreadCond.h"
  40. #include "JackFifo.h"
  41. #endif
  42. #define ITER 100000
  43. #define SERVER "serveur1"
  44. #define CLIENT "client1"
  45. int verbose = 0;
  46. using namespace Jack;
  47. #ifdef WIN32
  48. LARGE_INTEGER gFreq;
  49. long gQueryOverhead;
  50. static long elapsed (LARGE_INTEGER * t1, LARGE_INTEGER *t2)
  51. {
  52. long high = t1->HighPart - t2->HighPart;
  53. double low = t1->LowPart - t2->LowPart;
  54. // ignore values when high part changes
  55. return high ? 0 : (long)((low * 1000000) / gFreq.LowPart);
  56. }
  57. static BOOL overhead (long * overhead)
  58. {
  59. LARGE_INTEGER t1, t2;
  60. BOOL r1, r2;
  61. int i = 50;
  62. r1 = QueryPerformanceCounter (&t1);
  63. while (i--)
  64. QueryPerformanceCounter (&t2);
  65. r2 = QueryPerformanceCounter (&t2);
  66. if (!r1 || !r2)
  67. return FALSE;
  68. *overhead = elapsed(&t2, &t1) / 50;
  69. return TRUE;
  70. }
  71. #endif
  72. class Test1 : public JackRunnableInterface
  73. {
  74. private:
  75. JackSynchro* fSynchro1;
  76. JackSynchro* fSynchro2;
  77. public:
  78. Test1(JackSynchro* synchro1, JackSynchro* synchro2)
  79. : fSynchro1(synchro1), fSynchro2(synchro2)
  80. {}
  81. bool Execute()
  82. {
  83. #ifdef WIN32
  84. LARGE_INTEGER t1, t2;
  85. BOOL r1, r2;
  86. r1 = QueryPerformanceCounter (&t1);
  87. #else
  88. struct timeval T0, T1;
  89. clock_t time1, time2;
  90. // Get total time for 2 * ITER process swaps
  91. time1 = clock();
  92. gettimeofday(&T0, 0);
  93. #endif
  94. printf("Execute loop Test1\n");
  95. for (int i = 0; i < ITER; i++) {
  96. fSynchro2->Signal();
  97. fSynchro1->Wait();
  98. }
  99. #ifdef WIN32
  100. r2 = QueryPerformanceCounter (&t2);
  101. elapsed(&t2, &t1);
  102. printf ("%5.1lf usec for inter process swap\n", elapsed(&t2, &t1) / (2.0 * ITER));
  103. #else
  104. time2 = clock();
  105. gettimeofday(&T1, 0);
  106. 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));
  107. printf ("%f usec for inter process swap \n", (1e6 * ((time2 - time1) / (double(CLOCKS_PER_SEC)))) / (2.0 * ITER));
  108. #endif
  109. return true;
  110. }
  111. };
  112. class Test2 : public JackRunnableInterface
  113. {
  114. private:
  115. JackSynchro* fSynchro1;
  116. JackSynchro* fSynchro2;
  117. public:
  118. Test2(JackSynchro* synchro1, JackSynchro* synchro2)
  119. : fSynchro1(synchro1), fSynchro2(synchro2)
  120. {}
  121. bool Execute()
  122. {
  123. printf("Execute loop Test2\n");
  124. for (int i = 0; i < ITER; i++) {
  125. fSynchro2->Wait();
  126. fSynchro1->Signal();
  127. }
  128. return true;
  129. }
  130. };
  131. int main(int ac, char *av [])
  132. {
  133. Test1* obj1;
  134. Test2* obj2;
  135. JackSynchro* sem1 = NULL;
  136. JackSynchro* sem2 = NULL;
  137. JackSynchro* sem3 = NULL;
  138. JackSynchro* sem4 = NULL;
  139. JackThread* thread1;
  140. JackThread* thread2;
  141. #ifdef WIN32
  142. if (!QueryPerformanceFrequency (&gFreq) ||
  143. !overhead (&gQueryOverhead)) {
  144. printf ("cannot query performance counter\n");
  145. }
  146. #endif
  147. printf("Test of synchronization primitives : inside a process\n");
  148. printf("type -s to test Posix semaphore\n");
  149. printf("type -f to test Fifo\n");
  150. printf("type -m to test Mach semaphore\n");
  151. printf("type -e to test Windows event\n");
  152. #ifdef __APPLE__
  153. if (strcmp(av[1], "-m") == 0) {
  154. printf("Mach semaphore\n");
  155. sem1 = new JackMachSemaphore();
  156. sem2 = new JackMachSemaphore();
  157. sem3 = new JackMachSemaphore();
  158. sem4 = new JackMachSemaphore();
  159. }
  160. #endif
  161. #ifdef WIN32
  162. if (strcmp(av[1], "-e") == 0) {
  163. printf("Win event\n");
  164. sem1 = new JackWinEvent();
  165. sem2 = new JackWinEvent();
  166. sem3 = new JackWinEvent();
  167. sem4 = new JackWinEvent();
  168. }
  169. #endif
  170. #ifdef linux
  171. if (strcmp(av[1], "-s") == 0) {
  172. printf("Posix semaphore\n");
  173. sem1 = new JackPosixSemaphore();
  174. sem2 = new JackPosixSemaphore();
  175. sem3 = new JackPosixSemaphore();
  176. sem4 = new JackPosixSemaphore();
  177. }
  178. if (strcmp(av[1], "-f") == 0) {
  179. printf("Fifo\n");
  180. sem1 = new JackFifo();
  181. sem2 = new JackFifo();
  182. sem3 = new JackFifo();
  183. sem4 = new JackFifo();
  184. }
  185. if (strcmp(av[1], "-p") == 0) {
  186. printf("Fifo\n");
  187. sem1 = new JackPthreadCondServer();
  188. sem2 = new JackPthreadCondServer();
  189. sem3 = new JackPthreadCondClient(0);
  190. sem4 = new JackPthreadCondClient(0);
  191. }
  192. #endif
  193. sem1->Allocate(SERVER, 0);
  194. sem2->Allocate(CLIENT, 0);
  195. sem3->ConnectOutput(SERVER);
  196. sem4->ConnectInput(CLIENT);
  197. // run test in RT thread
  198. obj1 = new Test1(sem1, sem2);
  199. obj2 = new Test2(sem3, sem4);
  200. #ifdef __APPLE__
  201. thread1 = new JackMachThread(obj1, 10000 * 1000, 500 * 1000, 10000 * 1000);
  202. thread2 = new JackMachThread(obj2, 10000 * 1000, 500 * 1000, 10000 * 1000);
  203. #endif
  204. #ifdef WIN32
  205. thread1 = new JackWinThread(obj1);
  206. thread2 = new JackWinThread(obj2);
  207. #endif
  208. #ifdef linux
  209. thread1 = new JackPosixThread(obj1, false, 50, PTHREAD_CANCEL_DEFERRED);
  210. thread2 = new JackPosixThread(obj2, false, 50, PTHREAD_CANCEL_DEFERRED);
  211. #endif
  212. thread1->Start();
  213. thread2->Start();
  214. //thread1->AcquireRealTime();
  215. //thread2->AcquireRealTime();
  216. #ifdef WIN32
  217. Sleep(30 * 1000);
  218. #else
  219. sleep (30);
  220. #endif
  221. thread1->Stop();
  222. thread2->Stop();
  223. sem3->Disconnect();
  224. sem4->Disconnect();
  225. sem1->Destroy();
  226. sem2->Destroy();
  227. delete obj1;
  228. delete obj2;
  229. delete thread1;
  230. delete thread2;
  231. delete sem1;
  232. delete sem2;
  233. delete sem3;
  234. delete sem4;
  235. return 0;
  236. }