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.

testSynchroServerClient.cpp 5.9KB

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