summary refs log tree commit diff stats
path: root/lib/genode_cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/genode_cpp')
-rw-r--r--lib/genode_cpp/syslocks.h78
-rw-r--r--lib/genode_cpp/threads.h69
2 files changed, 147 insertions, 0 deletions
diff --git a/lib/genode_cpp/syslocks.h b/lib/genode_cpp/syslocks.h
new file mode 100644
index 000000000..8ba39abc2
--- /dev/null
+++ b/lib/genode_cpp/syslocks.h
@@ -0,0 +1,78 @@
+/*
+ *
+ *           Nim's Runtime Library
+ *       (c) Copyright 2017 Emery Hemingway
+ *
+ *   See the file "copying.txt", included in this
+ *   distribution, for details about the copyright.
+ *
+ */
+
+#ifndef _GENODE_CPP__SYSLOCKS_H_
+#define _GENODE_CPP__SYSLOCKS_H_
+
+/* Genode includes */
+#include <base/semaphore.h>
+#include <base/lock.h>
+
+namespace Nim {
+	struct SysLock;
+	struct SysCond;
+}
+
+struct Nim::SysLock
+{
+	Genode::Lock _lock_a, _lock_b;
+	bool         _locked;
+
+	void acquireSys()
+	{
+		_lock_a.lock();
+		_locked = true;
+		_lock_a.unlock();
+		_lock_b.lock();
+	}
+
+	bool tryAcquireSys()
+	{
+		if (_locked)
+			return false;
+
+		_lock_a.lock();
+		if (_locked) {
+			_lock_a.unlock();
+			return false;
+		} else {
+			_locked = true;
+			_lock_b.lock();
+			_lock_a.unlock();
+			return true;
+		}
+	}
+
+	void releaseSys()
+	{
+		_locked = false;
+		_lock_a.unlock();
+		_lock_b.unlock();
+	}
+};
+
+struct Nim::SysCond
+{
+	Genode::Semaphore _semaphore;
+
+	void waitSysCond(SysLock &syslock)
+	{
+		syslock.releaseSys();
+		_semaphore.down();
+		syslock.acquireSys();
+	}
+
+	void signalSysCond()
+	{
+		_semaphore.up();
+	}
+};
+
+#endif
diff --git a/lib/genode_cpp/threads.h b/lib/genode_cpp/threads.h
new file mode 100644
index 000000000..a1cd61cd2
--- /dev/null
+++ b/lib/genode_cpp/threads.h
@@ -0,0 +1,69 @@
+/*
+ *
+ *           Nim's Runtime Library
+ *       (c) Copyright 2017 Emery Hemingway
+ *
+ *   See the file "copying.txt", included in this
+ *   distribution, for details about the copyright.
+ *
+ */
+
+
+#ifndef _GENODE_CPP__THREAD_H_
+#define _GENODE_CPP__THREAD_H_
+
+#include <base/thread.h>
+#include <util/avl_tree.h>
+#include <util/reconstructible.h>
+
+namespace Nim { struct SysThread; }
+
+struct Nim::SysThread
+{
+	typedef void (Entry)(void*);
+
+	struct Thread : Genode::Thread
+	{
+		void *_tls;
+
+		Entry *_func;
+		void  *_arg;
+
+		void entry() override {
+			(_func)(_arg); }
+
+		Thread(Genode::Env &env, Genode::size_t stack_size, Entry func, void *arg)
+		: Genode::Thread(env, "nim-thread", stack_size), _func(func), _arg(arg)
+		{
+			Genode::Thread::start();
+		}
+	};
+
+	Genode::Constructible<Thread> _thread;
+
+	void initThread(Genode::Env *env, Genode::size_t stack_size, Entry func, void *arg) {
+		_thread.construct(*env, stack_size, func, arg); }
+
+	void joinThread() {
+		_thread->join(); }
+
+	static bool offMainThread() {
+		return dynamic_cast<SysThread::Thread*>(Genode::Thread::myself()); }
+
+	static void *threadVarGetValue()
+	{
+		SysThread::Thread *thr =
+			static_cast<SysThread::Thread*>(Genode::Thread::myself());
+		return thr->_tls;
+	}
+
+	static void threadVarSetValue(void *value)
+	{
+		SysThread::Thread *thr =
+			static_cast<SysThread::Thread*>(Genode::Thread::myself());
+		thr->_tls = value;
+	}
+
+};
+
+#endif
'n300' href='#n300'>300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359