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.

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