daily pastebin goal
14%
SHARE
TWEET

libnx simple thread class

rajkosto Sep 17th, 2018 168 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include "thread.hpp"
  2. extern "C" {
  3. #include <switch/kernel/svc.h>
  4. #include <switch/result.h>
  5. }
  6. static inline int getNextCpuId()
  7. {
  8.     const auto currProcNum = svcGetCurrentProcessorNumber();
  9.     const auto nextProcNum = (currProcNum + 1) % 4;
  10.     if (nextProcNum == 3) //ew, kernel cpu
  11.         return 0;
  12.     else
  13.         return (int)nextProcNum;
  14. }
  15.  
  16. thread::thread(thread::FuncType&& func, size_t stackSize, int priority) : _func(std::move(func))
  17. {
  18.     _status = threadCreate(&_thr, [](void* arg) { static_cast<thread*>(arg)->run(); }, this, stackSize, priority, getNextCpuId());
  19.     if (R_FAILED(_status))
  20.         return;
  21.    
  22.     _status = threadStart(&_thr);
  23.     if (R_FAILED(_status))
  24.     {
  25.         threadClose(&_thr);
  26.         return;
  27.     }
  28. }
  29.  
  30. Result thread::join()
  31. {
  32.     if (R_FAILED(_status))
  33.         return _status;
  34.  
  35.     return threadWaitForExit(&_thr);
  36. }
  37.  
  38. thread::~thread()
  39. {
  40.     if (R_SUCCEEDED(_status))
  41.     {
  42.         join();
  43.         threadClose(&_thr);
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top