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.

169 lines
3.5KB

  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. using namespace Jack;
  45. int verbose = 0;
  46. class Test2 : public JackRunnableInterface
  47. {
  48. private:
  49. JackSynchro* fSynchro1;
  50. JackSynchro* fSynchro2;
  51. public:
  52. Test2(JackSynchro* synchro1, JackSynchro* synchro2)
  53. : fSynchro1(synchro1), fSynchro2(synchro2)
  54. {}
  55. bool Execute()
  56. {
  57. int a = 1;
  58. for (int i = 0; i < ITER; i++) {
  59. fSynchro2->Wait();
  60. for (int j = 0; j < 2000000; j++) {
  61. a += j;
  62. }
  63. fSynchro1->Signal();
  64. }
  65. return true;
  66. }
  67. };
  68. int main(int ac, char *av [])
  69. {
  70. Test2* obj;
  71. JackSynchro* sem1 = NULL;
  72. JackSynchro* sem2 = NULL;
  73. JackThread* thread;
  74. printf("Test of synchronization primitives : client side\n");
  75. printf("type -s to test Posix semaphore\n");
  76. printf("type -f to test Fifo\n");
  77. printf("type -m to test Mach semaphore\n");
  78. printf("type -e to test Windows event\n");
  79. #ifdef __APPLE__
  80. if (strcmp(av[1], "-m") == 0) {
  81. printf("Mach semaphore\n");
  82. sem1 = new JackMachSemaphore();
  83. sem2 = new JackMachSemaphore();
  84. }
  85. #endif
  86. #ifdef WIN32
  87. if (strcmp(av[1], "-e") == 0) {
  88. printf("Win event\n");
  89. sem1 = new JackWinEvent();
  90. sem2 = new JackWinEvent();
  91. }
  92. #endif
  93. #ifdef linux
  94. if (strcmp(av[1], "-s") == 0) {
  95. printf("Posix semaphore\n");
  96. sem1 = new JackPosixSemaphore();
  97. sem2 = new JackPosixSemaphore();
  98. }
  99. if (strcmp(av[1], "-f") == 0) {
  100. printf("Fifo\n");
  101. sem1 = new JackFifo();
  102. sem2 = new JackFifo();
  103. }
  104. #endif
  105. sem1->ConnectOutput(SERVER);
  106. sem2->ConnectInput(CLIENT);
  107. obj = new Test2(sem1, sem2);
  108. #ifdef __APPLE__
  109. thread = new JackMachThread(obj, 10000 * 1000, 500 * 1000, 10000 * 1000);
  110. #endif
  111. #ifdef WIN32
  112. thread = new JackWinThread(obj);
  113. #endif
  114. #ifdef linux
  115. thread = new JackPosixThread(obj, false, 50, PTHREAD_CANCEL_DEFERRED);
  116. #endif
  117. thread->Start();
  118. thread->AcquireRealTime();
  119. #ifdef WIN32
  120. Sleep(30 * 1000);
  121. #else
  122. sleep(30);
  123. #endif
  124. //thread->Stop();
  125. thread->Kill();
  126. sem1->Disconnect();
  127. sem2->Disconnect();
  128. delete obj;
  129. delete thread;
  130. delete sem1;
  131. delete sem2;
  132. return 0;
  133. }