Lock.hx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. import java.Lib;
  24. import java.lang.System;
  25. using haxe.Int64;
  26. @:coreApi
  27. @:native('haxe.java.vm.Lock') class Lock
  28. {
  29. @:private @:volatile var releasedCount = 0;
  30. public function new()
  31. {
  32. }
  33. public function wait(?timeout : Float) : Bool
  34. {
  35. var ret = false;
  36. java.Lib.lock(this,
  37. {
  38. if (--releasedCount < 0)
  39. {
  40. if (timeout == null)
  41. {
  42. // since .notify() is asynchronous, this `while` is needed
  43. // because there is a very remote possibility of release() awaking a thread,
  44. // but before it releases, another thread calls wait - and since the release count
  45. // is still positive, it will get the lock.
  46. while( releasedCount < 0 )
  47. {
  48. try
  49. {
  50. untyped __java__("this.wait()");
  51. }
  52. catch(e:java.lang.InterruptedException)
  53. {
  54. }
  55. }
  56. } else {
  57. var timeout:haxe.Int64 = cast timeout * 1000;
  58. var cur = System.currentTimeMillis(),
  59. max = cur.add(timeout);
  60. // see above comment about this while loop
  61. while ( releasedCount < 0 && cur.compare(max) < 0 )
  62. {
  63. try
  64. {
  65. var t = max.sub(cur);
  66. untyped __java__("this.wait({0})",t);
  67. cur = System.currentTimeMillis();
  68. }
  69. catch(e:java.lang.InterruptedException)
  70. {
  71. }
  72. }
  73. }
  74. }
  75. ret = this.releasedCount >= 0;
  76. if (!ret)
  77. this.releasedCount++; //timed out
  78. });
  79. return ret;
  80. }
  81. public function release():Void
  82. {
  83. untyped __lock__(this,
  84. {
  85. if (++releasedCount >= 0)
  86. {
  87. untyped this.notify();
  88. }
  89. });
  90. }
  91. }