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.

282 lines
6.6KB

  1. /*
  2. Copyright (C) 2004-2008 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 "JackFifo.h"
  40. #endif
  41. #define ITER 100000
  42. #define SERVER "serveur1"
  43. #define CLIENT "client1"
  44. using namespace Jack;
  45. #ifdef WIN32
  46. LARGE_INTEGER gFreq;
  47. long gQueryOverhead;
  48. static long elapsed (LARGE_INTEGER * t1, LARGE_INTEGER *t2)
  49. {
  50. long high = t1->HighPart - t2->HighPart;
  51. double low = t1->LowPart - t2->LowPart;
  52. // ignore values when high part changes
  53. return high ? 0 : (long)((low * 1000000) / gFreq.LowPart);
  54. }
  55. static BOOL overhead (long * overhead)
  56. {
  57. LARGE_INTEGER t1, t2;
  58. BOOL r1, r2;
  59. int i = 50;
  60. r1 = QueryPerformanceCounter (&t1);
  61. while (i--)
  62. QueryPerformanceCounter (&t2);
  63. r2 = QueryPerformanceCounter (&t2);
  64. if (!r1 || !r2)
  65. return FALSE;
  66. *overhead = elapsed(&t2, &t1) / 50;
  67. return TRUE;
  68. }
  69. #endif
  70. class Test1 : public JackRunnableInterface
  71. {
  72. private:
  73. JackSynchro* fSynchro1;
  74. JackSynchro* fSynchro2;
  75. public:
  76. Test1(JackSynchro* synchro1, JackSynchro* synchro2)
  77. : fSynchro1(synchro1), fSynchro2(synchro2)
  78. {}
  79. bool Execute()
  80. {
  81. #ifdef WIN32
  82. LARGE_INTEGER t1, t2;
  83. BOOL r1, r2;
  84. r1 = QueryPerformanceCounter (&t1);
  85. #else
  86. struct timeval T0, T1;
  87. clock_t time1, time2;
  88. // Get total time for 2 * ITER process swaps
  89. time1 = clock();
  90. gettimeofday(&T0, 0);
  91. #endif
  92. printf("Execute loop Test1\n");
  93. for (int i = 0; i < ITER; i++) {
  94. fSynchro2->Signal();
  95. fSynchro1->Wait();
  96. }
  97. #ifdef WIN32
  98. r2 = QueryPerformanceCounter (&t2);
  99. elapsed(&t2, &t1);
  100. printf ("%5.1lf usec for inter process swap\n", elapsed(&t2, &t1) / (2.0 * ITER));
  101. #else
  102. time2 = clock();
  103. gettimeofday(&T1, 0);
  104. 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));
  105. printf ("%f usec for inter process swap \n", (1e6 * ((time2 - time1) / (double(CLOCKS_PER_SEC)))) / (2.0 * ITER));
  106. #endif
  107. return true;
  108. }
  109. };
  110. class Test2 : public JackRunnableInterface
  111. {
  112. private:
  113. JackSynchro* fSynchro1;
  114. JackSynchro* fSynchro2;
  115. public:
  116. Test2(JackSynchro* synchro1, JackSynchro* synchro2)
  117. : fSynchro1(synchro1), fSynchro2(synchro2)
  118. {}
  119. bool Execute()
  120. {
  121. printf("Execute loop Test2\n");
  122. for (int i = 0; i < ITER; i++) {
  123. fSynchro2->Wait();
  124. fSynchro1->Signal();
  125. }
  126. return true;
  127. }
  128. };
  129. int main(int ac, char *av [])
  130. {
  131. Test1* obj1;
  132. Test2* obj2;
  133. JackSynchro* sem1 = NULL;
  134. JackSynchro* sem2 = NULL;
  135. JackSynchro* sem3 = NULL;
  136. JackSynchro* sem4 = NULL;
  137. JackThread* thread1;
  138. JackThread* thread2;
  139. #ifdef WIN32
  140. if (!QueryPerformanceFrequency (&gFreq) ||
  141. !overhead (&gQueryOverhead)) {
  142. printf ("cannot query performance counter\n");
  143. }
  144. #endif
  145. printf("Test of synchronization primitives : inside a process\n");
  146. printf("type -s to test Posix semaphore\n");
  147. printf("type -f to test Fifo\n");
  148. printf("type -m to test Mach semaphore\n");
  149. printf("type -e to test Windows event\n");
  150. #ifdef __APPLE__
  151. if (strcmp(av[1], "-m") == 0) {
  152. printf("Mach semaphore\n");
  153. sem1 = new JackMachSemaphore();
  154. sem2 = new JackMachSemaphore();
  155. sem3 = new JackMachSemaphore();
  156. sem4 = new JackMachSemaphore();
  157. }
  158. #endif
  159. #ifdef WIN32
  160. if (strcmp(av[1], "-e") == 0) {
  161. printf("Win event\n");
  162. sem1 = new JackWinEvent();
  163. sem2 = new JackWinEvent();
  164. sem3 = new JackWinEvent();
  165. sem4 = new JackWinEvent();
  166. }
  167. #endif
  168. #ifdef linux
  169. if (strcmp(av[1], "-s") == 0) {
  170. printf("Posix semaphore\n");
  171. sem1 = new JackPosixSemaphore();
  172. sem2 = new JackPosixSemaphore();
  173. sem3 = new JackPosixSemaphore();
  174. sem4 = new JackPosixSemaphore();
  175. }
  176. if (strcmp(av[1], "-f") == 0) {
  177. printf("Fifo\n");
  178. sem1 = new JackFifo();
  179. sem2 = new JackFifo();
  180. sem3 = new JackFifo();
  181. sem4 = new JackFifo();
  182. }
  183. #endif
  184. if (!sem1->Allocate(SERVER, "default", 0))
  185. return -1;
  186. if (!sem2->Allocate(CLIENT, "default", 0))
  187. return -1;
  188. if (sem3->ConnectOutput(SERVER, "default"))
  189. return -1;
  190. if (sem4->ConnectInput(CLIENT, "default"))
  191. return -1;
  192. // run test in RT thread
  193. obj1 = new Test1(sem1, sem2);
  194. obj2 = new Test2(sem3, sem4);
  195. #ifdef __APPLE__
  196. thread1 = new JackMachThread(obj1, 10000 * 1000, 500 * 1000, 10000 * 1000);
  197. thread2 = new JackMachThread(obj2, 10000 * 1000, 500 * 1000, 10000 * 1000);
  198. #endif
  199. #ifdef WIN32
  200. thread1 = new JackWinThread(obj1);
  201. thread2 = new JackWinThread(obj2);
  202. #endif
  203. #ifdef linux
  204. thread1 = new JackPosixThread(obj1, false, 50, PTHREAD_CANCEL_DEFERRED);
  205. thread2 = new JackPosixThread(obj2, false, 50, PTHREAD_CANCEL_DEFERRED);
  206. #endif
  207. thread1->Start();
  208. thread2->Start();
  209. //thread1->AcquireRealTime();
  210. //thread2->AcquireRealTime();
  211. #ifdef WIN32
  212. Sleep(30 * 1000);
  213. #else
  214. sleep (30);
  215. #endif
  216. thread1->Stop();
  217. thread2->Stop();
  218. sem3->Disconnect();
  219. sem4->Disconnect();
  220. sem1->Destroy();
  221. sem2->Destroy();
  222. delete obj1;
  223. delete obj2;
  224. delete thread1;
  225. delete thread2;
  226. delete sem1;
  227. delete sem2;
  228. delete sem3;
  229. delete sem4;
  230. return 0;
  231. }