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.

130 lines
2.7KB

  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. #include <unistd.h>
  20. #ifdef __APPLE__
  21. #include "JackMachSemaphore.h"
  22. #include "JackMachThread.h"
  23. #endif
  24. #include "JackPosixThread.h"
  25. #include "JackMutex.h"
  26. using namespace Jack;
  27. struct LockedObject : public JackLockAble {
  28. JackThread* fThread;
  29. int fCount;
  30. LockedObject():fCount(0)
  31. {}
  32. virtual ~LockedObject()
  33. {
  34. fThread->Kill();
  35. delete fThread;
  36. }
  37. void LockedMethod1()
  38. {
  39. JackLock lock(this);
  40. fCount++;
  41. //printf("LockedMethod1 self %x fCount %ld\n", pthread_self(), fCount);
  42. }
  43. void LockedMethod2()
  44. {
  45. JackLock lock(this);
  46. fCount--;
  47. //printf("LockedMethod2 self %x fCount %ld\n", pthread_self(), fCount);
  48. }
  49. void LockedMethod3()
  50. {
  51. JackLock lock(this);
  52. fCount--;
  53. //printf("LockedMethod3 self %x fCount %ld\n", pthread_self(), fCount);
  54. }
  55. };
  56. struct TestThread : public JackRunnableInterface {
  57. JackThread* fThread;
  58. LockedObject* fObject;
  59. int fNum;
  60. TestThread(LockedObject* obj, int num)
  61. {
  62. fThread = new JackMachThread(this);
  63. fObject = obj;
  64. fNum = num;
  65. fThread->StartSync();
  66. }
  67. virtual ~TestThread()
  68. {
  69. fThread->Kill();
  70. delete fThread;
  71. }
  72. bool Execute()
  73. {
  74. //printf("TestThread Execute\n");
  75. switch (fNum) {
  76. case 1:
  77. fObject->LockedMethod1();
  78. break;
  79. case 2:
  80. fObject->LockedMethod2();
  81. break;
  82. case 3:
  83. fObject->LockedMethod3();
  84. break;
  85. };
  86. //usleep(fNum * 1000);
  87. return true;
  88. }
  89. };
  90. int main (int argc, char * const argv[])
  91. {
  92. char c;
  93. LockedObject* obj = new LockedObject();
  94. TestThread* th1 = new TestThread(obj, 1);
  95. TestThread* th2 = new TestThread(obj,3);
  96. TestThread* th3 = new TestThread(obj, 2);
  97. while ((c = getchar()) != 'q')) {}
  98. }