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.

209 lines
4.2KB

  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. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #ifdef __APPLE__
  20. #include "JackMachSemaphore.h"
  21. #endif
  22. #include "JackPosixSemaphore.h"
  23. #include "JackFifo.h"
  24. #include "JackPlatformPlug.h"
  25. #define ITER 500000
  26. using namespace Jack;
  27. struct ServerThread : public JackRunnableInterface {
  28. JackThread* fThread;
  29. detail::JackSynchro* fServerSem;
  30. detail::JackSynchro* fClientSem;
  31. ServerThread()
  32. {
  33. fServerSem->Allocate("JackSemServer", "default", 0);
  34. fClientSem->Allocate("JackSemClient", "default", 0);
  35. //fThread = new JackMachThread(this);
  36. fThread->SetParams(0, 500*1000, 500*1000);
  37. fThread->Start();
  38. //fThread->AcquireRealTime();
  39. }
  40. virtual ~ServerThread()
  41. {
  42. fThread->Kill();
  43. delete fThread;
  44. }
  45. bool Execute()
  46. {
  47. printf("Execute Server\n");
  48. for (int i = 0; i < ITER; i++) {
  49. fClientSem->Signal();
  50. fServerSem->Wait();
  51. }
  52. return true;
  53. }
  54. };
  55. struct ClientThread : public JackRunnableInterface {
  56. JackThread* fThread;
  57. detail::JackSynchro* fServerSem;
  58. detail::JackSynchro* fClientSem;
  59. ClientThread()
  60. {
  61. fServerSem->Connect("JackSemServer", "default");
  62. fClientSem->Connect("JackSemClient", "default");
  63. //fThread = new JackMachThread(this);
  64. fThread->SetParams(0, 500*1000, 500*1000);
  65. fThread->Start();
  66. //fThread->AcquireRealTime();
  67. }
  68. virtual ~ClientThread()
  69. {
  70. fThread->Kill();
  71. delete fThread;
  72. }
  73. bool Execute()
  74. {
  75. struct timeval T0, T1;
  76. printf("Execute Client\n");
  77. fClientSem->Wait();
  78. gettimeofday(&T0, 0);
  79. for (int i = 0; i < ITER; i++) {
  80. fServerSem->Signal();
  81. fClientSem->Wait();
  82. }
  83. gettimeofday(&T1, 0);
  84. printf("%5.1lf usec\n", (1e6 * T1.tv_sec - 1e6 * T0.tv_sec + T1.tv_usec - T0.tv_usec) / (2.0 * ITER));
  85. return true;
  86. }
  87. };
  88. void server(detail::JackSynchro* sem)
  89. {
  90. char c;
  91. printf("server\n");
  92. sem->Allocate("JackSem", "default", 0);
  93. while (((c = getchar()) != 'q')) {
  94. switch(c) {
  95. case 's':
  96. printf("SynchroSignal....\n");
  97. //sem->Signal();
  98. sem->SignalAll();
  99. printf("SynchroSignal OK\n");
  100. break;
  101. case 'w':
  102. printf("SemaphoreWait....\n");
  103. sem->Wait();
  104. printf("SemaphoreWait OK\n");
  105. break;
  106. }
  107. }
  108. }
  109. void client(detail::JackSynchro* sem)
  110. {
  111. char c;
  112. printf("client\n");
  113. sem->Connect("JackSem", "default");
  114. while (((c = getchar()) != 'q')) {
  115. switch(c) {
  116. case 's':
  117. printf("SemaphoreSignal....\n");
  118. sem->Signal();
  119. printf("SemaphoreSignal OK\n");
  120. break;
  121. case 'w':
  122. printf("SemaphoreWait....\n");
  123. sem->Wait();
  124. printf("SemaphoreWait OK\n");
  125. break;
  126. }
  127. }
  128. }
  129. int main (int argc, char * const argv[])
  130. {
  131. char c;
  132. ServerThread* serverthread = NULL;
  133. ClientThread* clientthread = NULL;
  134. detail::JackSynchro* sem1 = NULL;
  135. if (strcmp(argv[1],"-s") == 0) {
  136. printf("Posix semaphore\n");
  137. sem1 = new JackPosixSemaphore();
  138. }
  139. if (strcmp(argv[1],"-f") == 0) {
  140. printf("Fifo\n");
  141. sem1 = new JackFifo();
  142. }
  143. #ifdef __APPLE__
  144. if (strcmp(argv[1],"-m") == 0) {
  145. printf("Mach semaphore\n");
  146. sem1 = new JackMachSemaphore();
  147. }
  148. #endif
  149. /*
  150. if (strcmp(argv[2], "server") == 0) {
  151. serverthread = new ServerThread();
  152. } else {
  153. clientthread = new ClientThread();
  154. }
  155. */
  156. if (strcmp(argv[2], "server") == 0) {
  157. server(sem1);
  158. } else {
  159. client(sem1);
  160. }
  161. while (((c = getchar()) != 'q')) {}
  162. delete serverthread;
  163. delete clientthread;
  164. }