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.

150 lines
2.8KB

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