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.

129 lines
2.6KB

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