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.

262 lines
5.9KB

  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. #endif
  31. #ifdef WIN32
  32. #include "JackWinEvent.h"
  33. #endif
  34. #ifdef linux
  35. #include "JackPosixSemaphore.h"
  36. #include "JackFifo.h"
  37. #endif
  38. #include "JackPlatformThread.h"
  39. #define ITER 100000
  40. #define SERVER "serveur1"
  41. #define CLIENT "client1"
  42. #include "JackPlatformSynchro.h"
  43. using namespace Jack;
  44. #ifdef WIN32
  45. LARGE_INTEGER gFreq;
  46. long gQueryOverhead;
  47. static long elapsed (LARGE_INTEGER * t1, LARGE_INTEGER *t2)
  48. {
  49. long high = t1->HighPart - t2->HighPart;
  50. double low = t1->LowPart - t2->LowPart;
  51. // ignore values when high part changes
  52. return high ? 0 : (long)((low * 1000000) / gFreq.LowPart);
  53. }
  54. static BOOL overhead (long * overhead)
  55. {
  56. LARGE_INTEGER t1, t2;
  57. BOOL r1, r2;
  58. int i = 50;
  59. r1 = QueryPerformanceCounter (&t1);
  60. while (i--)
  61. QueryPerformanceCounter (&t2);
  62. r2 = QueryPerformanceCounter (&t2);
  63. if (!r1 || !r2)
  64. return FALSE;
  65. *overhead = elapsed(&t2, &t1) / 50;
  66. return TRUE;
  67. }
  68. #endif
  69. template <typename sync_type>
  70. class Test1 : public JackRunnableInterface
  71. {
  72. private:
  73. sync_type* fSynchro1;
  74. sync_type* fSynchro2;
  75. public:
  76. Test1(sync_type* synchro1, sync_type* 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. template <typename sync_type>
  111. class Test2 : public JackRunnableInterface
  112. {
  113. private:
  114. sync_type* fSynchro1;
  115. sync_type* fSynchro2;
  116. public:
  117. Test2(sync_type* synchro1, sync_type* synchro2)
  118. : fSynchro1(synchro1), fSynchro2(synchro2)
  119. {}
  120. bool Execute()
  121. {
  122. printf("Execute loop Test2\n");
  123. for (int i = 0; i < ITER; i++) {
  124. fSynchro2->Wait();
  125. fSynchro1->Signal();
  126. }
  127. return true;
  128. }
  129. };
  130. template <typename sync_type>
  131. void run_tests(void)
  132. {
  133. sync_type sem1, sem2, sem3, sem4;
  134. sem1.Allocate(SERVER, "default", 0);
  135. sem2.Allocate(CLIENT, "default", 0);
  136. sem3.ConnectOutput(SERVER, "default");
  137. sem4.ConnectInput(CLIENT, "default");
  138. Test1<sync_type> obj1(&sem1, &sem2);
  139. Test2<sync_type> obj2(&sem3, &sem4);
  140. JackThread* thread1;
  141. JackThread* thread2;
  142. #ifdef __APPLE__
  143. thread1 = new JackMachThread(&obj1, 10000 * 1000, 500 * 1000, 10000 * 1000);
  144. thread2 = new JackMachThread(&obj2, 10000 * 1000, 500 * 1000, 10000 * 1000);
  145. #endif
  146. #ifdef WIN32
  147. thread1 = new JackWinThread(&obj1);
  148. thread2 = new JackWinThread(&obj2);
  149. #endif
  150. #ifdef linux
  151. thread1 = new JackPosixThread(&obj1, false, 50, PTHREAD_CANCEL_DEFERRED);
  152. thread2 = new JackPosixThread(&obj2, false, 50, PTHREAD_CANCEL_DEFERRED);
  153. #endif
  154. thread1->Start();
  155. thread2->Start();
  156. //thread1->AcquireRealTime();
  157. //thread2->AcquireRealTime();
  158. #ifdef WIN32
  159. Sleep(30 * 1000);
  160. #else
  161. sleep (30);
  162. #endif
  163. thread1->Stop();
  164. thread2->Stop();
  165. sem3.Disconnect();
  166. sem4.Disconnect();
  167. sem1.Destroy();
  168. sem2.Destroy();
  169. delete thread1;
  170. delete thread2;
  171. }
  172. int main(int ac, char *av [])
  173. {
  174. #ifdef WIN32
  175. if (!QueryPerformanceFrequency (&gFreq) ||
  176. !overhead (&gQueryOverhead)) {
  177. printf ("cannot query performance counter\n");
  178. }
  179. #endif
  180. printf("Test of synchronization primitives : inside a process\n");
  181. printf("type -s to test Posix semaphore\n");
  182. printf("type -f to test Fifo\n");
  183. printf("type -m to test Mach semaphore\n");
  184. printf("type -e to test Windows event\n");
  185. #ifdef __APPLE__
  186. if (strcmp(av[1], "-m") == 0) {
  187. printf("Mach semaphore\n");
  188. run_tests<JackMachSemaphore>();
  189. }
  190. #endif
  191. #ifdef WIN32
  192. if (strcmp(av[1], "-e") == 0) {
  193. printf("Win event\n");
  194. run_tests<JackWinEvent>();
  195. }
  196. #endif
  197. #ifdef linux
  198. if (strcmp(av[1], "-s") == 0) {
  199. printf("Posix semaphore\n");
  200. run_tests<JackPosixSemaphore>();
  201. }
  202. if (strcmp(av[1], "-f") == 0) {
  203. printf("Fifo\n");
  204. run_tests<JackFifo>();
  205. }
  206. #endif
  207. return 0;
  208. }