TestThreads.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package unit;
  2. #if neko
  3. import neko.vm.Thread;
  4. import neko.vm.Deque;
  5. import neko.vm.Lock;
  6. import neko.vm.Tls;
  7. import neko.vm.Mutex;
  8. #elseif cpp
  9. import cpp.vm.Thread;
  10. import cpp.vm.Deque;
  11. import cpp.vm.Lock;
  12. import cpp.vm.Tls;
  13. import cpp.vm.Mutex;
  14. #elseif java
  15. import java.vm.Thread;
  16. import java.vm.Deque;
  17. import java.vm.Lock;
  18. import java.vm.Tls;
  19. import java.vm.Mutex;
  20. #end
  21. class TestThreads extends Test
  22. {
  23. private function testSort()
  24. {
  25. var ts = new ThreadSort();
  26. #if java
  27. ts.maxVal *= 10;
  28. #end
  29. for (creatorWait in [.02, .05, 0])
  30. for (creatorLoad in [false, true])
  31. for (consumerWait in [.02,.05,0])
  32. for (useTls in [false,true])
  33. for (q in [new QDeque() #if java, new QLockFree()#end])
  34. for (lock in [ [new DequeSemaphore(), new LockSemaphore()], [new LockSemaphore(), new DequeSemaphore() ] ])
  35. {
  36. ts.creatorWait = creatorWait;
  37. ts.creatorLoad = creatorLoad;
  38. ts.consumerWait = consumerWait;
  39. ts.useTls = useTls;
  40. ts.queue = q;
  41. var lock1 = lock[0], lock2 = lock[1];
  42. ts.lock1 = lock1;
  43. ts.lock2 = lock2;
  44. try
  45. {
  46. ts.run();
  47. t(true);
  48. }
  49. catch(e:Dynamic)
  50. {
  51. Test.report('Error $e for parameters: $creatorWait, $creatorLoad, $consumerWait, $useTls, $q, $lock1, $lock2');
  52. }
  53. }
  54. }
  55. }
  56. class ThreadSort
  57. {
  58. //params
  59. public var creatorWait:Seconds = .2;
  60. //should an extra load be performed on the creator?
  61. public var creatorLoad:Bool = false;
  62. public var consumerWait:Seconds = 0;
  63. //should an extra load be performed on the consumer?
  64. public var consumerLoad:Bool = false;
  65. public var useTls:Bool = false;
  66. public var nThreads:Int = 10;
  67. public var maxVal:Int = 1000;
  68. public var queue:QueueStrategy<Int>;
  69. public var lock1:SemaphoreStrategy;
  70. public var lock2:SemaphoreStrategy;
  71. public function new()
  72. {
  73. }
  74. public function run()
  75. {
  76. //spawning creators
  77. lock1.reset(); lock2.reset(); queue.reset();
  78. lock1.setReleaseCount(nThreads);
  79. lock2.setReleaseCount(nThreads);
  80. var finishedMutex = new Mutex();
  81. finishedMutex.acquire();
  82. var tls = new Tls();
  83. for (i in 0...nThreads)
  84. {
  85. Thread.create(function() {
  86. tls.value = i;
  87. Sys.sleep(creatorWait.secs());
  88. var i = useTls ? tls.value : i;
  89. for (j in 1...maxVal)
  90. {
  91. if (j % nThreads == i)
  92. {
  93. queue.add(j);
  94. }
  95. if (j % Std.int(maxVal / 10) == 0 && creatorLoad)
  96. Sys.sleep(.01);
  97. }
  98. //creator thread finished
  99. lock1.release();
  100. });
  101. }
  102. //spawning consumers
  103. if (consumerWait != 0)
  104. Sys.sleep(consumerWait.secs());
  105. var arr = [];
  106. for (i in 0...nThreads)
  107. {
  108. var myarr = [];
  109. arr.push(myarr);
  110. Thread.create(function() {
  111. var i = 0;
  112. while(true)
  113. {
  114. var val = queue.pop();
  115. if (val != null) {
  116. myarr.push(val);
  117. } else if (finishedMutex.tryAcquire()) {
  118. finishedMutex.release();
  119. break;
  120. }
  121. if (++i % Std.int(maxVal / 10) == 0 && consumerLoad)
  122. Sys.sleep(.01);
  123. }
  124. lock2.release();
  125. });
  126. }
  127. //wait creators to finish
  128. lock1.block();
  129. //release finishedMutex
  130. finishedMutex.release();
  131. //wait consumers to finish
  132. lock2.block();
  133. //start checking
  134. var mainarr = [];
  135. mainarr[maxVal] = maxVal; //prealloc
  136. for(a in arr)
  137. {
  138. for (a in a)
  139. {
  140. var val = mainarr[a];
  141. if (val == a)
  142. throw 'Same value detected for $val and $a';
  143. mainarr[a] = a;
  144. }
  145. }
  146. //no repeats, ok!
  147. for (i in 1...mainarr.length)
  148. {
  149. if (i != mainarr[i])
  150. throw 'No value found for $i and ${mainarr[i]}';
  151. }
  152. }
  153. }
  154. private interface SemaphoreStrategy //may be released by another thread
  155. {
  156. function block():Void; //block until all semaphores are released
  157. function release():Void;
  158. function setReleaseCount(i:Int):Void;
  159. function reset():Void;
  160. }
  161. private class DequeSemaphore implements SemaphoreStrategy
  162. {
  163. var d:Deque<Bool>;
  164. var c:Int = 0;
  165. public function new()
  166. {
  167. this.d = new Deque();
  168. this.c = 0;
  169. }
  170. public function block()
  171. {
  172. for (i in 0...c)
  173. {
  174. d.pop(true);
  175. }
  176. this.c = 0;
  177. }
  178. public function release()
  179. {
  180. d.push(true);
  181. }
  182. public function setReleaseCount(c:Int)
  183. {
  184. this.c = c;
  185. }
  186. public function reset()
  187. {
  188. this.d = new Deque();
  189. this.c = 0;
  190. }
  191. @:keep public function toString()
  192. {
  193. return "DequeSemaphore";
  194. }
  195. }
  196. private class LockSemaphore implements SemaphoreStrategy
  197. {
  198. var l:Lock;
  199. var c:Int = 0;
  200. public function new()
  201. {
  202. this.l = new Lock();
  203. this.l.release();
  204. }
  205. public function block()
  206. {
  207. for(i in 0...c)
  208. {
  209. l.wait();
  210. }
  211. this.c = 0;
  212. }
  213. public function release()
  214. {
  215. this.l.release();
  216. }
  217. public function setReleaseCount(c:Int)
  218. {
  219. this.c = c;
  220. }
  221. public function reset()
  222. {
  223. this.l = new Lock();
  224. this.c = 0;
  225. }
  226. @:keep public function toString()
  227. {
  228. return "LockSemaphore";
  229. }
  230. }
  231. private interface QueueStrategy<T>
  232. {
  233. function add(t:T):Void;
  234. function pop():Null<T>; //not blocking
  235. function reset():Void;
  236. }
  237. private class QDeque<T> implements QueueStrategy<T>
  238. {
  239. var q:Deque<T>;
  240. public function new()
  241. {
  242. this.q = new Deque();
  243. }
  244. public function add(t:T)
  245. {
  246. this.q.add(t);
  247. }
  248. public function pop():Null<T>
  249. {
  250. return this.q.pop(false);
  251. }
  252. public function reset()
  253. {
  254. this.q = new Deque();
  255. }
  256. @:keep public function toString()
  257. {
  258. return "QDeque";
  259. }
  260. }
  261. #if java
  262. private class QLockFree<T> implements QueueStrategy<T>
  263. {
  264. var q:java.vm.AtomicList<T>;
  265. public function new()
  266. {
  267. this.q = new java.vm.AtomicList();
  268. }
  269. public function add(t:T)
  270. {
  271. this.q.add(t);
  272. }
  273. public function pop():Null<T>
  274. {
  275. return this.q.pop();
  276. }
  277. public function reset()
  278. {
  279. this.q = new java.vm.AtomicList();
  280. }
  281. @:keep public function toString()
  282. {
  283. return "QLockFree";
  284. }
  285. }
  286. #end
  287. private abstract Seconds(Float) from Float
  288. {
  289. public inline function secs():Float
  290. {
  291. return this;
  292. }
  293. public inline function ms():Float
  294. {
  295. return this * 1000;
  296. }
  297. }