Lock.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C)2005-2017 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 java.vm;
  23. import java.Lib;
  24. import java.lang.System;
  25. using haxe.Int64;
  26. @:native('haxe.java.vm.Lock') class Lock
  27. {
  28. @:private @:volatile var releasedCount = 0;
  29. /**
  30. Creates a new lock, which is initially locked
  31. **/
  32. public function new()
  33. {
  34. }
  35. /**
  36. Waits for a lock to be released and acquire it.
  37. If `timeout` (in seconds) is not null and expires then the returned value is false
  38. **/
  39. public function wait(?timeout : Float) : Bool
  40. {
  41. var ret = false;
  42. java.Lib.lock(this,
  43. {
  44. if (--releasedCount < 0)
  45. {
  46. if (timeout == null)
  47. {
  48. // since .notify() is asynchronous, this `while` is needed
  49. // because there is a very remote possibility of release() awaking a thread,
  50. // but before it releases, another thread calls wait - and since the release count
  51. // is still positive, it will get the lock.
  52. while( releasedCount < 0 )
  53. {
  54. try
  55. {
  56. untyped __java__("this.wait()");
  57. }
  58. catch(e:java.lang.InterruptedException)
  59. {
  60. }
  61. }
  62. } else {
  63. var timeout:haxe.Int64 = cast timeout * 1000;
  64. var cur = System.currentTimeMillis(),
  65. max = cur.add(timeout);
  66. // see above comment about this while loop
  67. while ( releasedCount < 0 && cur.compare(max) < 0 )
  68. {
  69. try
  70. {
  71. var t = max.sub(cur);
  72. untyped __java__("this.wait({0})",t);
  73. cur = System.currentTimeMillis();
  74. }
  75. catch(e:java.lang.InterruptedException)
  76. {
  77. }
  78. }
  79. }
  80. }
  81. ret = this.releasedCount >= 0;
  82. if (!ret)
  83. this.releasedCount++; //timed out
  84. });
  85. return ret;
  86. }
  87. /**
  88. Release a lock. The thread does not need to own the lock to be able to release it.
  89. If a lock is released several times, it can be acquired as many times
  90. **/
  91. public function release()
  92. {
  93. untyped __lock__(this,
  94. {
  95. if (++releasedCount >= 0)
  96. {
  97. untyped this.notify();
  98. }
  99. });
  100. }
  101. }