threads.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. *
  3. * Nim's Runtime Library
  4. * (c) Copyright 2017 Emery Hemingway
  5. *
  6. * See the file "copying.txt", included in this
  7. * distribution, for details about the copyright.
  8. *
  9. */
  10. #ifndef _GENODE_CPP__THREAD_H_
  11. #define _GENODE_CPP__THREAD_H_
  12. #include <base/thread.h>
  13. #include <base/env.h>
  14. #include <util/reconstructible.h>
  15. namespace Nim { struct SysThread; }
  16. struct Nim::SysThread
  17. {
  18. typedef void (Entry)(void*);
  19. struct Thread : Genode::Thread
  20. {
  21. void *_tls;
  22. Entry *_func;
  23. void *_arg;
  24. void entry() override {
  25. (_func)(_arg); }
  26. Thread(Genode::Env &env, Genode::size_t stack_size, Entry func, void *arg, int affinity)
  27. : Genode::Thread(env, "nim-thread", stack_size,
  28. env.cpu().affinity_space().location_of_index(affinity),
  29. Genode::Cpu_session::Weight(Genode::Cpu_session::Weight::DEFAULT_WEIGHT-1),
  30. env.cpu()),
  31. _func(func), _arg(arg)
  32. {
  33. Genode::Thread::start();
  34. }
  35. };
  36. Genode::Constructible<Thread> _thread;
  37. void initThread(Genode::Env *env, Genode::size_t stack_size, Entry func, void *arg, int aff) {
  38. _thread.construct(*env, stack_size, func, arg, aff); }
  39. void joinThread() {
  40. _thread->join(); }
  41. static bool offMainThread() {
  42. return dynamic_cast<SysThread::Thread*>(Genode::Thread::myself()); }
  43. static void *threadVarGetValue()
  44. {
  45. SysThread::Thread *thr =
  46. static_cast<SysThread::Thread*>(Genode::Thread::myself());
  47. return thr->_tls;
  48. }
  49. static void threadVarSetValue(void *value)
  50. {
  51. SysThread::Thread *thr =
  52. static_cast<SysThread::Thread*>(Genode::Thread::myself());
  53. thr->_tls = value;
  54. }
  55. };
  56. #endif