FixedThreadPool.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.thread;
  23. #if (!target.threaded)
  24. #error "This class is not available on this target"
  25. #end
  26. import haxe.Exception;
  27. /**
  28. Thread pool with a constant amount of threads.
  29. Threads in the pool will exist until the pool is explicitly shut down.
  30. **/
  31. @:coreApi
  32. class FixedThreadPool implements IThreadPool {
  33. /* Amount of threads in this pool. */
  34. public var threadsCount(get,null):Int;
  35. function get_threadsCount():Int return threadsCount;
  36. /** Indicates if `shutdown` method of this pool has been called. */
  37. public var isShutdown(get,never):Bool;
  38. var _isShutdown = false;
  39. function get_isShutdown():Bool return _isShutdown;
  40. final pool:Array<Worker>;
  41. final poolMutex = new Mutex();
  42. final queue = new Deque<()->Void>();
  43. /**
  44. Create a new thread pool with `threadsCount` threads.
  45. **/
  46. public function new(threadsCount:Int):Void {
  47. if(threadsCount < 1)
  48. throw new ThreadPoolException('FixedThreadPool needs threadsCount to be at least 1.');
  49. this.threadsCount = threadsCount;
  50. pool = [for(i in 0...threadsCount) new Worker(queue)];
  51. }
  52. /**
  53. Submit a task to run in a thread.
  54. Throws an exception if the pool is shut down.
  55. **/
  56. public function run(task:()->Void):Void {
  57. if(_isShutdown)
  58. throw new ThreadPoolException('Task is rejected. Thread pool is shut down.');
  59. if(task == null)
  60. throw new ThreadPoolException('Task to run must not be null.');
  61. queue.add(task);
  62. }
  63. /**
  64. Initiates a shutdown.
  65. All previousely submitted tasks will be executed, but no new tasks will
  66. be accepted.
  67. Multiple calls to this method have no effect.
  68. **/
  69. public function shutdown():Void {
  70. if(_isShutdown) return;
  71. _isShutdown = true;
  72. for(_ in pool) {
  73. queue.add(shutdownTask);
  74. }
  75. }
  76. static function shutdownTask():Void {
  77. throw new ShutdownException('');
  78. }
  79. }
  80. private class ShutdownException extends Exception {}
  81. private class Worker {
  82. var thread:Thread;
  83. final queue:Deque<Null<()->Void>>;
  84. public function new(queue:Deque<Null<()->Void>>) {
  85. this.queue = queue;
  86. thread = Thread.create(loop);
  87. }
  88. function loop() {
  89. try {
  90. while(true) {
  91. var task = queue.pop(true);
  92. task();
  93. }
  94. } catch(_:ShutdownException) {
  95. } catch(e) {
  96. thread = Thread.create(loop);
  97. throw e;
  98. }
  99. }
  100. }