Thread.hx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. private typedef ThreadImpl = {};
  27. extern abstract Thread(ThreadImpl) from ThreadImpl {
  28. /**
  29. Event loop of this thread (if available).
  30. Note that by default event loop is only available in the main thread.
  31. To setup an event loop in other threads use `sys.thread.Thread.runWithEventLoop`
  32. or create new threads with built-in event loops using `sys.thread.Thread.createWithEventLoop`
  33. **/
  34. public var events(get,never):EventLoop;
  35. /**
  36. Send a message to the thread queue. This message can be read by using `readMessage`.
  37. **/
  38. public function sendMessage(msg:Dynamic):Void;
  39. /**
  40. Returns the current thread.
  41. **/
  42. public static function current():Thread;
  43. /**
  44. Creates a new thread that will execute the `job` function, then exit.
  45. This function does not setup an event loop for a new thread.
  46. **/
  47. public static function create(job:()->Void):Thread;
  48. /**
  49. Simply execute `job` if current thread already has an event loop.
  50. But if current thread does not have an event loop: setup event loop,
  51. run `job` and then destroy event loop. And in this case this function
  52. does not return until no more events left to run.
  53. **/
  54. public static function runWithEventLoop(job:()->Void):Void;
  55. /**
  56. This is logically equal to `Thread.create(() -> Thread.runWithEventLoop(job));`
  57. **/
  58. public static function createWithEventLoop(job:()->Void):Thread;
  59. /**
  60. Reads a message from the thread queue. If `block` is true, the function
  61. blocks until a message is available. If `block` is false, the function
  62. returns `null` if no message is available.
  63. **/
  64. public static function readMessage(block:Bool):Dynamic;
  65. /**
  66. Run event loop of the current thread
  67. **/
  68. private static function processEvents():Void;
  69. }