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.

224 lines
5.1KB

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