Browse Source

added threads api

Nicolas Cannasse 7 years ago
parent
commit
a276692957
4 changed files with 253 additions and 0 deletions
  1. 58 0
      std/hl/vm/Deque.hx
  2. 66 0
      std/hl/vm/Mutex.hx
  3. 68 0
      std/hl/vm/Thread.hx
  4. 61 0
      std/hl/vm/Tls.hx

+ 58 - 0
std/hl/vm/Deque.hx

@@ -0,0 +1,58 @@
+/*
+ * Copyright (C)2005-2018 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 hl.vm;
+
+/**
+	A message queue for multithread access.
+*/
+@:hlNative("std","deque_")
+abstract Deque<T>(hl.Abstract<"hl_deque">) {
+
+	/**
+		Create a message queue for multithread access.
+	*/
+	public function new() {
+		this = alloc();
+	}
+
+	/**
+		Add a message at the end of the queue.
+	*/
+	public function add( i : T ) {
+	}
+
+	/**
+		Add a message at the head of the queue.
+	*/
+	public function push( i : T ) {
+	}
+
+	/**
+		Pop a message from the queue head. Either block until a message
+		is available or return immediately with `null`.
+	*/
+	public function pop( block : Bool ) : Null<T> {
+		return null;
+	}
+
+	static function alloc() { return null; }
+}

+ 66 - 0
std/hl/vm/Mutex.hx

@@ -0,0 +1,66 @@
+/*
+ * Copyright (C)2005-2018 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 hl.vm;
+
+/**
+	Creates a mutex, which can be used to acquire a temporary lock 
+	to access some ressource. The main difference with a lock is 
+	that a mutex must always be released by the owner thread.
+*/
+abstract Mutex(hl.Abstract<"hl_mutex">) {
+
+	/**
+		Creates a mutex.
+	*/
+	public function new() {
+		this = alloc(true);
+	}
+
+	/**
+		The current thread acquire the mutex or wait if not available.
+		The same thread can acquire several times the same mutex but 
+		must release it as many times it has been acquired.
+	*/
+	@:hlNative("std","mutex_acquire") public function acquire() {
+	}
+
+	/**
+		Try to acquire the mutex, returns true if acquire or false 
+		if it's already locked by another thread.
+	*/
+	@:hlNative("std","mutex_try_acquire") public function tryAcquire() : Bool {
+		return false;
+	}
+
+	/**
+		Release a mutex that has been acquired by the current thread. 
+		The behavior is undefined if the current thread does not own
+		the mutex.
+	*/
+	@:hlNative("std","mutex_release") public function release() {
+	}
+
+	@:hlNative("std","mutex_alloc") public static function alloc( b : Bool ) {
+		return null;
+	}
+	
+}

+ 68 - 0
std/hl/vm/Thread.hx

@@ -0,0 +1,68 @@
+/*
+ * Copyright (C)2005-2018 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 hl.vm;
+
+typedef ThreadHandle = hl.Abstract<"hl_thread">;
+
+abstract Thread(ThreadHandle) {
+
+	public function sendMessage( msg : Dynamic ) {
+		getQueue(this).add(msg);
+	}
+
+	public static function readMessage( block = true ) : Dynamic {
+		return getQueue(cast current()).pop(block);
+	}
+
+	static var queue_mutex : Mutex = null;
+	static var threads_queues : Array<{ t : ThreadHandle, q : Deque<Dynamic> }> = null;
+	static function getQueue( t : ThreadHandle ) {
+		if( queue_mutex == null ) {
+			queue_mutex = new Mutex();
+			threads_queues = [];
+		}
+		queue_mutex.acquire();
+		var q = null;
+		for( tq in threads_queues )
+			if( tq.t == t ) {
+				q = tq.q;
+				break;
+			}
+		if( q == null ) {
+			q = new Deque<Dynamic>();
+			threads_queues.push({ t : t, q : q });
+		}
+		queue_mutex.release();
+		return q;
+	}
+
+	@:hlNative("std","thread_create")
+	public static function create( callb : Void -> Void ) : Thread {
+		return null;
+	}
+
+	@:hlNative("std","thread_current")
+	public static function current() : Thread {
+		return null;
+	}
+
+}

+ 61 - 0
std/hl/vm/Tls.hx

@@ -0,0 +1,61 @@
+/*
+ * Copyright (C)2005-2018 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 hl.vm;
+
+/**
+	Creates thread local storage.
+	Warning : ATM Tls does not protect the value from being GC'ed. Keep the value reachable to avoid crashes.
+*/
+@:hlNative("std")
+abstract Tls<T>(hl.Abstract<"hl_tls">) {
+
+	public var value(get,set) : T;
+
+	/**
+		Creates thread local storage. This is placeholder that can store
+		a value that will be different depending on the local thread. 
+		Set the tls value to `null` before exiting the thread 
+		or the memory will never be collected.
+	*/
+	public function new() {
+		this = tls_alloc(true);
+	}
+
+	/**
+		Returns the value set by tls_set for the local thread.
+	*/
+	function get_value() : T {
+		return tls_get(this);
+	}
+
+	/**
+		Set the value of the TLS for the local thread.
+	*/
+	function set_value( v : T ) {
+		tls_set(this, v);
+		return v;
+	}
+
+	static function tls_alloc( gcValue : Bool ) return null;
+	static function tls_get(t) : Dynamic return null;
+	static function tls_set(t,v:Dynamic) {}
+}