Update Files
This commit is contained in:
51
Kha/Tools/macos/std/java/_std/sys/thread/Deque.hx
Normal file
51
Kha/Tools/macos/std/java/_std/sys/thread/Deque.hx
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C)2005-2019 Haxe Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package sys.thread;
|
||||
|
||||
import java.Lib;
|
||||
|
||||
@:coreApi
|
||||
@:native('haxe.java.vm.Deque')
|
||||
@:nativeGen class Deque<T> {
|
||||
var lbd:java.util.concurrent.LinkedBlockingDeque<T>;
|
||||
|
||||
public function new() {
|
||||
lbd = new java.util.concurrent.LinkedBlockingDeque<T>();
|
||||
}
|
||||
|
||||
public function add(i:T):Void {
|
||||
lbd.add(i);
|
||||
}
|
||||
|
||||
public function push(i:T):Void {
|
||||
lbd.push(i);
|
||||
}
|
||||
|
||||
public inline function pop(block:Bool):Null<T> {
|
||||
return if (block) {
|
||||
lbd.take();
|
||||
} else {
|
||||
lbd.poll();
|
||||
}
|
||||
}
|
||||
}
|
78
Kha/Tools/macos/std/java/_std/sys/thread/Lock.hx
Normal file
78
Kha/Tools/macos/std/java/_std/sys/thread/Lock.hx
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C)2005-2019 Haxe Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package sys.thread;
|
||||
|
||||
import java.Lib;
|
||||
import java.lang.System;
|
||||
|
||||
using haxe.Int64;
|
||||
|
||||
@:coreApi
|
||||
@:native('haxe.java.vm.Lock') class Lock {
|
||||
@:private @:volatile var releasedCount = 0;
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function wait(?timeout:Float):Bool {
|
||||
var ret = false;
|
||||
java.Lib.lock(this, {
|
||||
if (--releasedCount < 0) {
|
||||
if (timeout == null) {
|
||||
// since .notify() is asynchronous, this `while` is needed
|
||||
// because there is a very remote possibility of release() awaking a thread,
|
||||
// but before it releases, another thread calls wait - and since the release count
|
||||
// is still positive, it will get the lock.
|
||||
while (releasedCount < 0) {
|
||||
try {
|
||||
(cast this : java.lang.Object).wait();
|
||||
} catch (e:java.lang.InterruptedException) {}
|
||||
}
|
||||
} else {
|
||||
var timeout:haxe.Int64 = cast timeout * 1000;
|
||||
var cur = System.currentTimeMillis(),
|
||||
max = cur.add(timeout);
|
||||
// see above comment about this while loop
|
||||
while (releasedCount < 0 && cur.compare(max) < 0) {
|
||||
try {
|
||||
var t = max.sub(cur);
|
||||
(cast this : java.lang.Object).wait(t);
|
||||
cur = System.currentTimeMillis();
|
||||
} catch (e:java.lang.InterruptedException) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
ret = this.releasedCount >= 0;
|
||||
if (!ret)
|
||||
this.releasedCount++; // timed out
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
public function release():Void {
|
||||
untyped __lock__(this, {
|
||||
if (++releasedCount >= 0) {
|
||||
untyped this.notify();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
46
Kha/Tools/macos/std/java/_std/sys/thread/Mutex.hx
Normal file
46
Kha/Tools/macos/std/java/_std/sys/thread/Mutex.hx
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C)2005-2019 Haxe Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package sys.thread;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@:coreApi
|
||||
@:native('haxe.java.vm.Mutex') class Mutex {
|
||||
@:private var lock:ReentrantLock;
|
||||
|
||||
public function new() {
|
||||
this.lock = new ReentrantLock();
|
||||
}
|
||||
|
||||
public function tryAcquire():Bool {
|
||||
return this.lock.tryLock();
|
||||
}
|
||||
|
||||
public function acquire():Void {
|
||||
this.lock.lock();
|
||||
}
|
||||
|
||||
public function release():Void {
|
||||
this.lock.unlock();
|
||||
}
|
||||
}
|
190
Kha/Tools/macos/std/java/_std/sys/thread/Thread.hx
Normal file
190
Kha/Tools/macos/std/java/_std/sys/thread/Thread.hx
Normal file
@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright (C)2005-2019 Haxe Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package sys.thread;
|
||||
|
||||
import java.Lib;
|
||||
import java.lang.Runnable;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.Collections;
|
||||
import java.lang.Thread as JavaThread;
|
||||
import java.lang.System;
|
||||
import java.StdTypes.Int64 as Long;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
private typedef ThreadImpl = HaxeThread;
|
||||
|
||||
abstract Thread(ThreadImpl) from ThreadImpl {
|
||||
public var events(get,never):EventLoop;
|
||||
|
||||
inline function new(t:HaxeThread) {
|
||||
this = t;
|
||||
}
|
||||
|
||||
public static inline function create(job:()->Void):Thread {
|
||||
return HaxeThread.create(job, false);
|
||||
}
|
||||
|
||||
public static inline function current():Thread {
|
||||
return HaxeThread.get(JavaThread.currentThread());
|
||||
}
|
||||
|
||||
public static inline function runWithEventLoop(job:()->Void):Void {
|
||||
HaxeThread.runWithEventLoop(job);
|
||||
}
|
||||
|
||||
public static inline function createWithEventLoop(job:()->Void):Thread {
|
||||
return HaxeThread.create(job, true);
|
||||
}
|
||||
|
||||
public static inline function readMessage(block:Bool):Dynamic {
|
||||
return current().getHandle().readMessage(block);
|
||||
}
|
||||
|
||||
public inline function sendMessage(msg:Dynamic):Void {
|
||||
this.sendMessage(msg);
|
||||
}
|
||||
|
||||
inline function getHandle():HaxeThread {
|
||||
return this;
|
||||
}
|
||||
|
||||
function get_events():EventLoop {
|
||||
if(this.events == null)
|
||||
throw new NoEventLoopException();
|
||||
return this.events;
|
||||
}
|
||||
|
||||
@:keep //TODO: keep only if events are actually used
|
||||
static function processEvents():Void {
|
||||
current().getHandle().events.loop();
|
||||
}
|
||||
}
|
||||
|
||||
private class HaxeThread {
|
||||
static var nativeThreads:java.util.Map<JavaThread,HaxeThread>;
|
||||
static var mainJavaThread:JavaThread;
|
||||
static var mainHaxeThread:HaxeThread;
|
||||
|
||||
static function __init__() {
|
||||
nativeThreads = Collections.synchronizedMap(new WeakHashMap<JavaThread,HaxeThread>());
|
||||
mainJavaThread = JavaThread.currentThread();
|
||||
mainHaxeThread = new HaxeThread();
|
||||
mainHaxeThread.events = new EventLoop();
|
||||
}
|
||||
|
||||
public final messages = new LinkedBlockingDeque<Dynamic>();
|
||||
|
||||
public var events(default,null):Null<EventLoop>;
|
||||
|
||||
public static function create(job:()->Void, withEventLoop:Bool):HaxeThread {
|
||||
var hx = new HaxeThread();
|
||||
if(withEventLoop)
|
||||
hx.events = new EventLoop();
|
||||
var thread = new NativeHaxeThread(hx, job, withEventLoop);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
return hx;
|
||||
}
|
||||
|
||||
public static function get(javaThread:JavaThread):HaxeThread {
|
||||
if(javaThread == mainJavaThread) {
|
||||
return mainHaxeThread;
|
||||
} else if(javaThread is NativeHaxeThread) {
|
||||
return (cast javaThread:NativeHaxeThread).haxeThread;
|
||||
} else {
|
||||
switch nativeThreads.get(javaThread) {
|
||||
case null:
|
||||
var hx = new HaxeThread();
|
||||
nativeThreads.put(javaThread, hx);
|
||||
return hx;
|
||||
case hx:
|
||||
return hx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function runWithEventLoop(job:()->Void):Void {
|
||||
var thread = get(JavaThread.currentThread());
|
||||
if(thread.events == null) {
|
||||
thread.events = new EventLoop();
|
||||
try {
|
||||
job();
|
||||
thread.events.loop();
|
||||
thread.events = null;
|
||||
} catch(e) {
|
||||
thread.events = null;
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
job();
|
||||
}
|
||||
}
|
||||
|
||||
function new() {}
|
||||
|
||||
public function sendMessage(msg:Dynamic):Void {
|
||||
messages.add(msg);
|
||||
}
|
||||
|
||||
public function readMessage(block:Bool):Dynamic {
|
||||
return block ? messages.take() : messages.poll();
|
||||
}
|
||||
}
|
||||
|
||||
private class NativeHaxeThread extends java.lang.Thread {
|
||||
public final haxeThread:HaxeThread;
|
||||
final withEventLoop:Bool;
|
||||
|
||||
public function new(haxeThread:HaxeThread, job:()->Void, withEventLoop:Bool) {
|
||||
super(new Job(job));
|
||||
this.haxeThread = haxeThread;
|
||||
this.withEventLoop = withEventLoop;
|
||||
}
|
||||
|
||||
override overload public function run() {
|
||||
super.run();
|
||||
if(withEventLoop)
|
||||
haxeThread.events.loop();
|
||||
}
|
||||
}
|
||||
|
||||
#if jvm
|
||||
private abstract Job(Runnable) from Runnable to Runnable {
|
||||
public inline function new(job:()->Void) {
|
||||
this = cast job;
|
||||
}
|
||||
}
|
||||
#else
|
||||
private class Job implements Runnable {
|
||||
final job:()->Void;
|
||||
|
||||
public function new(job:()->Void) {
|
||||
this.job = job;
|
||||
}
|
||||
|
||||
public function run() {
|
||||
job();
|
||||
}
|
||||
}
|
||||
#end
|
43
Kha/Tools/macos/std/java/_std/sys/thread/Tls.hx
Normal file
43
Kha/Tools/macos/std/java/_std/sys/thread/Tls.hx
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C)2005-2019 Haxe Foundation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package sys.thread;
|
||||
|
||||
// @:coreApi // causes some overload error...
|
||||
@:native('haxe.java.vm.Tls') class Tls<T> {
|
||||
var t:java.lang.ThreadLocal<T>;
|
||||
|
||||
public var value(get, set):T;
|
||||
|
||||
public function new() {
|
||||
this.t = new java.lang.ThreadLocal();
|
||||
}
|
||||
|
||||
inline private function get_value():T {
|
||||
return t.get();
|
||||
}
|
||||
|
||||
inline private function set_value(v:T):T {
|
||||
t.set(v);
|
||||
return v;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user