diff options
Diffstat (limited to 'lib/genode_cpp')
-rw-r--r-- | lib/genode_cpp/signals.h | 39 | ||||
-rw-r--r-- | lib/genode_cpp/syslocks.h | 81 | ||||
-rw-r--r-- | lib/genode_cpp/threads.h | 73 |
3 files changed, 193 insertions, 0 deletions
diff --git a/lib/genode_cpp/signals.h b/lib/genode_cpp/signals.h new file mode 100644 index 000000000..fa3975d38 --- /dev/null +++ b/lib/genode_cpp/signals.h @@ -0,0 +1,39 @@ +/* + * + * Nim's Runtime Library + * (c) Copyright 2022 Emery Hemingway + * + * See the file "copying.txt", included in this + * distribution, for details about the copyright. + * + */ + +#ifndef _NIM_SIGNALS_H_ +#define _NIM_SIGNALS_H_ + +#include <libc/component.h> +#include <base/signal.h> +#include <util/reconstructible.h> + +// Symbol for calling back into Nim +extern "C" void nimHandleSignal(void *arg); + +namespace Nim { struct SignalHandler; } + +struct Nim::SignalHandler +{ + // Pointer to the Nim handler object. + void *arg; + + void handle_signal() { + Libc::with_libc([this] () { nimHandleSignal(arg); }); } + + Genode::Signal_handler<SignalHandler> handler; + + SignalHandler(Genode::Entrypoint *ep, void *arg) + : arg(arg), handler(*ep, *this, &SignalHandler::handle_signal) { } + + Genode::Signal_context_capability cap() { return handler; } +}; + +#endif diff --git a/lib/genode_cpp/syslocks.h b/lib/genode_cpp/syslocks.h new file mode 100644 index 000000000..b5d5ae694 --- /dev/null +++ b/lib/genode_cpp/syslocks.h @@ -0,0 +1,81 @@ +/* + * + * 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/mutex.h> + +namespace Nim { + struct SysLock; + struct SysCond; +} + +struct Nim::SysLock +{ + Genode::Mutex _mutex_a, _mutex_b; + bool _locked; + + void acquireSys() + { + Genode::Mutex::Guard guard(_mutex_a); + _locked = true; + _mutex_b.acquire(); + } + + bool tryAcquireSys() + { + if (_locked) + return false; + + Genode::Mutex::Guard guard(_mutex_a); + + if (_locked) { + return false; + } else { + _locked = true; + _mutex_b.acquire(); + return true; + } + } + + void releaseSys() + { + Genode::Mutex::Guard guard(_mutex_a); + _locked = false; + _mutex_b.release(); + } +}; + +struct Nim::SysCond +{ + Genode::Semaphore _semaphore; + + void waitSysCond(SysLock &syslock) + { + syslock.releaseSys(); + _semaphore.down(); + syslock.acquireSys(); + } + + void signalSysCond() + { + _semaphore.up(); + } + + void broadcastSysCond() + { + _semaphore.up(); + } +}; + +#endif diff --git a/lib/genode_cpp/threads.h b/lib/genode_cpp/threads.h new file mode 100644 index 000000000..c901efb45 --- /dev/null +++ b/lib/genode_cpp/threads.h @@ -0,0 +1,73 @@ +/* + * + * 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 <base/env.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, int affinity) + : Genode::Thread(env, "nim-thread", stack_size, + env.cpu().affinity_space().location_of_index(affinity), + Genode::Cpu_session::Weight(Genode::Cpu_session::Weight::DEFAULT_WEIGHT-1), + env.cpu()), + _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, int aff) { + _thread.construct(*env, stack_size, func, arg, aff); } + + 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 |