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.

168 lines
3.5KB

  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. class Test2 : public JackRunnableInterface
  44. {
  45. private:
  46. detail::JackSynchro* fSynchro1;
  47. detail::JackSynchro* fSynchro2;
  48. public:
  49. Test2(detail::JackSynchro* synchro1, detail::JackSynchro* synchro2)
  50. : fSynchro1(synchro1), fSynchro2(synchro2)
  51. {}
  52. bool Execute()
  53. {
  54. int a = 1;
  55. for (int i = 0; i < ITER; i++) {
  56. fSynchro2->Wait();
  57. for (int j = 0; j < 2000000; j++) {
  58. a += j;
  59. }
  60. fSynchro1->Signal();
  61. }
  62. return true;
  63. }
  64. };
  65. int main(int ac, char *av [])
  66. {
  67. Test2* obj;
  68. detail::JackSynchro* sem1 = NULL;
  69. detail::JackSynchro* sem2 = NULL;
  70. JackThread* thread;
  71. printf("Test of synchronization primitives : client side\n");
  72. printf("type -s to test Posix semaphore\n");
  73. printf("type -f to test Fifo\n");
  74. printf("type -m to test Mach semaphore\n");
  75. printf("type -e to test Windows event\n");
  76. #ifdef __APPLE__
  77. if (strcmp(av[1], "-m") == 0) {
  78. printf("Mach semaphore\n");
  79. sem1 = new JackMachSemaphore();
  80. sem2 = new JackMachSemaphore();
  81. }
  82. #endif
  83. #ifdef WIN32
  84. if (strcmp(av[1], "-e") == 0) {
  85. printf("Win event\n");
  86. sem1 = new JackWinEvent();
  87. sem2 = new JackWinEvent();
  88. }
  89. #endif
  90. #ifdef linux
  91. if (strcmp(av[1], "-s") == 0) {
  92. printf("Posix semaphore\n");
  93. sem1 = new JackPosixSemaphore();
  94. sem2 = new JackPosixSemaphore();
  95. }
  96. if (strcmp(av[1], "-f") == 0) {
  97. printf("Fifo\n");
  98. sem1 = new JackFifo();
  99. sem2 = new JackFifo();
  100. }
  101. #endif
  102. if (!sem1->ConnectOutput(SERVER, "default"))
  103. return -1;
  104. if (!sem2->ConnectInput(CLIENT, "default"))
  105. return -1;
  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. }