Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Sep 20, 2018
1 parent 05b119a commit 0d497ed
Show file tree
Hide file tree
Showing 13 changed files with 773 additions and 154 deletions.
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]


## [2.0.0] - 2018-09-20

### Added
- interface hx.concurrent.lock.Acquirable
- class hx.concurrent.CountDownLatch
- class hx.concurrent.lock.RWLock (upgradeable Read Write Lock)

### Changed
- renamed methid hx.concurrent.thread.Threads#wait() to hx.concurrent.thread.Threads#await()
- changed hx.concurrent.lock.Semaphore from abstract to class

### Fixed
- define "threads" is now set correctly for targets supporting real threading


## [1.2.0] - 2018-08-22

### Added
Expand All @@ -25,8 +40,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [1.1.0] - 2018-04-15

### Added
- hx.concurrent.Service.ServiceState.STARTING
- hx.concurrent.Service.Service#start()
- field hx.concurrent.Service.ServiceState.STARTING
- method hx.concurrent.Service.Service#start()

### Changed
- replaced license header by "SPDX-License-Identifier: Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"vegardit"
],
"releasenote": "See https://github.com/vegardit/haxe-concurrent/blob/master/CHANGELOG.md",
"version": "1.2.0",
"version": "2.0.0",
"dependencies": { }
}
57 changes: 57 additions & 0 deletions src/hx/concurrent/CountDownLatch.hx
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
2 changes: 1 addition & 1 deletion src/hx/concurrent/collection/Queue.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Queue<T> {
_queueLock.release();
#end
} else {
Threads.wait(function() {
Threads.await(function() {
#if (cpp||hl||neko)
msg = _queue.pop(false);
#elseif java
Expand Down
6 changes: 3 additions & 3 deletions src/hx/concurrent/executor/Executor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Executor extends ServiceBase {
*/
public static function create(maxConcurrent:Int = 1, autostart = true):Executor {
#if threads
if (Threads.isSupported())
if (Threads.isSupported)
return new ThreadPoolExecutor(maxConcurrent, autostart);
#end
return new TimerExecutor(autostart);
Expand Down Expand Up @@ -132,12 +132,12 @@ class TaskFutureBase<T> extends FutureBase<T> implements TaskFuture<T> {

#if threads
public function waitAndGet(timeoutMS:Int):FutureResult<T> {
Threads.wait(function() {
Threads.await(function() {
return switch(this.result) {
case NONE(_): false;
default: true;
};
}, timeoutMS, ThreadPoolExecutor.SCHEDULER_RESOLUTION_SEC);
}, timeoutMS, ThreadPoolExecutor.SCHEDULER_RESOLUTION_MS);

return this.result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hx/concurrent/executor/ThreadPoolExecutor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import hx.concurrent.thread.Threads;
#if threads
class ThreadPoolExecutor extends Executor {

public inline static var SCHEDULER_RESOLUTION_MS = 5;
public inline static var SCHEDULER_RESOLUTION_MS = 10;
public inline static var SCHEDULER_RESOLUTION_SEC = SCHEDULER_RESOLUTION_MS / 1000;

var _threadPool:ThreadPool;
Expand Down Expand Up @@ -110,7 +110,7 @@ class ThreadPoolExecutor extends Executor {
t.cancel();
}

Threads.wait(function() {
Threads.await(function() {
return _threadPool.state == STOPPED;
}, -1);
state = STOPPED;
Expand Down
57 changes: 57 additions & 0 deletions src/hx/concurrent/internal/Ints.hx
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
}

}
36 changes: 36 additions & 0 deletions src/hx/concurrent/lock/Acquirable.hx
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;

}
Loading

0 comments on commit 0d497ed

Please sign in to comment.