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.

206 lines
4.4KB

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