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.

225 lines
5.1KB

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