-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
773 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2016-2018 Vegard IT GmbH, https://vegardit.com | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package hx.concurrent; | ||
|
||
import hx.concurrent.atomic.AtomicInt; | ||
import hx.concurrent.lock.RLock; | ||
import hx.concurrent.thread.Threads; | ||
|
||
/** | ||
* @author Sebastian Thomschke, Vegard IT GmbH | ||
*/ | ||
#if java | ||
abstract CountDownLatch(java.util.concurrent.CountDownLatch) { | ||
|
||
public var count(get, never):Int; | ||
inline function get_count():Int return haxe.Int64.toInt(this.getCount()); | ||
|
||
inline public function new(count:Int) this = new java.util.concurrent.CountDownLatch(count); | ||
inline public function countDown():Void this.countDown(); | ||
inline public function await():Void this.await(); | ||
inline public function tryAwait(timeoutMS:Int):Bool return this.await(timeoutMS, java.util.concurrent.TimeUnit.MILLISECONDS); | ||
} | ||
|
||
#elseif threads | ||
class CountDownLatch { | ||
|
||
public var count(get, null):Int; | ||
inline function get_count():Int return _count; | ||
|
||
var _count:AtomicInt; | ||
|
||
|
||
inline | ||
public function new(count:Int) { | ||
_count = new AtomicInt(count); | ||
} | ||
|
||
|
||
inline | ||
public function countDown():Void { | ||
_count--; | ||
} | ||
|
||
|
||
public function await():Void { | ||
while(_count > 0) | ||
Threads.sleep(10); | ||
} | ||
|
||
|
||
public function tryAwait(timeoutMS:Int):Bool { | ||
return Threads.await(function() return count < 1, timeoutMS); | ||
} | ||
} | ||
#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2016-2018 Vegard IT GmbH, https://vegardit.com | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package hx.concurrent.internal; | ||
|
||
/** | ||
* @author Sebastian Thomschke, Vegard IT GmbH | ||
*/ | ||
class Ints { | ||
|
||
/** | ||
* Maximum value Int type can hold depending on target platform | ||
*/ | ||
public static var MAX_VALUE(default, never):Int = { | ||
#if cs | ||
untyped __cs__("int.MaxValue"); | ||
#elseif flash | ||
untyped __global__["int"].MAX_VALUE; | ||
#elseif java | ||
untyped __java__("Integer.MAX_VALUE"); | ||
#elseif js | ||
untyped __js__("Number.MAX_SAFE_INTEGER"); | ||
#elseif php | ||
untyped __php__("PHP_INT_MAX"); | ||
#elseif python | ||
python.Syntax.pythonCode("import sys"); | ||
python.Syntax.pythonCode("sys.maxsize"); | ||
#else // neko, cpp, lua, etc. | ||
Std.int(Math.pow(2,31)-1); | ||
#end | ||
} | ||
|
||
|
||
/** | ||
* Maximum negative value Int type can hold depending on target platform | ||
*/ | ||
public static var MIN_VALUE(default, never):Int = { | ||
#if cs | ||
untyped __cs__("int.MinValue"); | ||
#elseif flash | ||
untyped __global__["int"].MIN_VALUE; | ||
#elseif java | ||
untyped __java__("Integer.MIN_VALUE"); | ||
#elseif js | ||
untyped __js__("Number.MIN_SAFE_INTEGER"); | ||
#elseif php | ||
untyped __php__("PHP_INT_MIN"); | ||
#elseif python | ||
python.Syntax.pythonCode("import sys"); | ||
-python.Syntax.pythonCode("sys.maxsize") -1; | ||
#else // neko, cpp, lua, etc. | ||
-Std.int(Math.pow(2,31)); | ||
#end | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2016-2018 Vegard IT GmbH, https://vegardit.com | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package hx.concurrent.lock; | ||
|
||
/** | ||
* @author Sebastian Thomschke, Vegard IT GmbH | ||
*/ | ||
interface Acquirable { | ||
|
||
public var availablePermits(get, never):Int; | ||
|
||
/** | ||
* Blocks until a permit can be acquired. | ||
*/ | ||
public function acquire():Void; | ||
|
||
/** | ||
* By default this call is non-blocking, meaning if the object cannot be aqcuired currently `false` is returned immediately. | ||
* | ||
* If <code>timeoutMS</code> is set to value > 0, results in blocking for the given time to aqcuire the object. | ||
* If <code>timeoutMS</code> is set to value lower than 0, results in an exception. | ||
* | ||
* @return `false` if lock could not be acquired | ||
*/ | ||
public function tryAcquire(timeoutMS:Int = 0):Bool; | ||
|
||
/** | ||
* Releases one permit. | ||
* | ||
* Depending on the implementation this method may throw an exception if the current thread doesn't hold the permit. | ||
*/ | ||
public function release():Void; | ||
|
||
} |
Oops, something went wrong.