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.

309 lines
6.9KB

  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. #include "thread.h"
  26. using namespace Jack;
  27. static void CleanupHandler(void * arg)
  28. {
  29. JackLockAble* locked = (JackLockAble*)arg;
  30. printf("CleanupHandler locked %px \n", locked);
  31. locked->Unlock();
  32. }
  33. struct LockedObject : public JackLockAble {
  34. int fCount;
  35. LockedObject():fCount(0)
  36. {}
  37. virtual ~LockedObject()
  38. {}
  39. /*
  40. void LockedMethod1()
  41. {
  42. JackLock lock(this);
  43. fCount++;
  44. printf("LockedMethod1 self %x fCount %d\n", pthread_self(), fCount);
  45. if (fCount >= 1000) {
  46. printf("Terminate self %x\n", pthread_self());
  47. pthread_exit(NULL);
  48. }
  49. }
  50. void LockedMethod2()
  51. {
  52. JackLock lock(this);
  53. fCount++;
  54. printf("LockedMethod2 self %x fCount %d\n", pthread_self(), fCount);
  55. if (fCount >= 1500) {
  56. printf("Terminate self %x\n", pthread_self());
  57. pthread_exit(NULL);
  58. }
  59. }
  60. void LockedMethod3()
  61. {
  62. JackLock lock(this);
  63. fCount++;
  64. printf("LockedMethod3 self %x fCount %d\n", pthread_self(), fCount);
  65. if (fCount >= 3000) {
  66. printf("Terminate self %x\n", pthread_self());
  67. pthread_exit(NULL);
  68. }
  69. }
  70. */
  71. void LockedMethod1()
  72. {
  73. pthread_cleanup_push(CleanupHandler, this);
  74. Lock();
  75. fCount++;
  76. //printf("LockedMethod1 self %x fCount %d\n", pthread_self(), fCount);
  77. if (fCount >= 1000) {
  78. printf("Terminate self = %px count = %d\n", pthread_self(), fCount);
  79. pthread_exit(NULL);
  80. }
  81. Unlock();
  82. pthread_cleanup_pop(0);
  83. }
  84. void LockedMethod2()
  85. {
  86. pthread_cleanup_push(CleanupHandler, this);
  87. Lock();
  88. fCount++;
  89. //printf("LockedMethod2 self %x fCount %d\n", pthread_self(), fCount);
  90. if (fCount >= 1500) {
  91. printf("Terminate self = %px count = %d\n", pthread_self(), fCount);
  92. pthread_exit(NULL);
  93. }
  94. Unlock();
  95. pthread_cleanup_pop(0);
  96. }
  97. void LockedMethod3()
  98. {
  99. pthread_cleanup_push(CleanupHandler, this);
  100. Lock();
  101. fCount++;
  102. //printf("LockedMethod3 self %x fCount %d\n", pthread_self(), fCount);
  103. if (fCount >= 3000) {
  104. printf("Terminate self = %px count = %d\n", pthread_self(), fCount);
  105. pthread_exit(NULL);
  106. }
  107. Unlock();
  108. pthread_cleanup_pop(0);
  109. }
  110. };
  111. struct TestThread : public JackRunnableInterface {
  112. JackMachThread* fThread;
  113. LockedObject* fObject;
  114. int fNum;
  115. TestThread(LockedObject* obj, int num)
  116. {
  117. printf("TestThread\n");
  118. fThread = new JackMachThread(this);
  119. fObject = obj;
  120. fNum = num;
  121. fThread->StartSync();
  122. }
  123. virtual ~TestThread()
  124. {
  125. printf("DELETE %px\n", fThread);
  126. fThread->Kill();
  127. delete fThread;
  128. }
  129. bool Execute()
  130. {
  131. //printf("TestThread Execute\n");
  132. switch (fNum) {
  133. case 1:
  134. fObject->LockedMethod1();
  135. /*
  136. if (fObject->fCount >= 500) {
  137. printf("Terminate self %x\n", pthread_self());
  138. fThread->Terminate();
  139. }
  140. */
  141. break;
  142. case 2:
  143. fObject->LockedMethod2();
  144. /*
  145. if (fObject->fCount >= 1500) {
  146. printf("Terminate self %x\n", pthread_self());
  147. fThread->Terminate();
  148. }
  149. */
  150. break;
  151. case 3:
  152. fObject->LockedMethod3();
  153. /*
  154. if (fObject->fCount >= 2000) {
  155. printf("Terminate self %x\n", pthread_self());
  156. fThread->Terminate();
  157. }
  158. */
  159. break;
  160. };
  161. //usleep(fNum * 1000);
  162. return true;
  163. }
  164. };
  165. static void* TestThread1_Execute(void* arg);
  166. struct TestThread1 : public JackRunnableInterface {
  167. pthread_t fThread;
  168. LockedObject* fObject;
  169. int fNum;
  170. TestThread1(LockedObject* obj, int num)
  171. {
  172. if (jack_client_create_thread(NULL, &fThread, 0, 0, TestThread1_Execute, this))
  173. jack_error( "Can't create the network manager control thread." );
  174. fObject = obj;
  175. fNum = num;
  176. }
  177. virtual ~TestThread1()
  178. {}
  179. bool Execute()
  180. {
  181. printf("TestThread Execute\n");
  182. switch (fNum) {
  183. case 1:
  184. fObject->LockedMethod1();
  185. break;
  186. case 2:
  187. fObject->LockedMethod2();
  188. break;
  189. case 3:
  190. fObject->LockedMethod3();
  191. break;
  192. };
  193. //usleep(fNum * 1000);
  194. return true;
  195. }
  196. };
  197. static void* TestThread1_Execute(void* arg)
  198. {
  199. TestThread1* obj = (TestThread1*)arg;
  200. while (true) {
  201. //printf("TestThread Execute\n");
  202. switch (obj->fNum) {
  203. case 1:
  204. obj->fObject->LockedMethod1();
  205. break;
  206. case 2:
  207. obj->fObject->LockedMethod2();
  208. break;
  209. case 3:
  210. obj->fObject->LockedMethod3();
  211. break;
  212. };
  213. //usleep(obj->fNum * 1000);
  214. }
  215. return 0;
  216. }
  217. int main (int argc, char * const argv[])
  218. {
  219. char c;
  220. LockedObject obj;
  221. TestThread th1(&obj, 1);
  222. TestThread th2(&obj, 2);
  223. TestThread th3(&obj, 3);
  224. /*
  225. LockedObject obj;
  226. TestThread1 th1(&obj, 1);
  227. TestThread th2(&obj, 2);
  228. TestThread th3(&obj, 3);
  229. */
  230. /*
  231. while ((c = getchar()) != 'q') {
  232. }
  233. */
  234. while (true) {
  235. usleep(1000);
  236. th1.fThread->Kill();
  237. }
  238. /*
  239. th1.fThread->Kill();
  240. th2.fThread->Kill();
  241. th3.fThread->Kill();
  242. while (true) {
  243. //usleep(100000);
  244. th1.fThread->Kill();
  245. }
  246. */
  247. }