Advertisement
rajkosto

libnx simple thread class

Sep 17th, 2018
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement