| 1 | #include "Library/Thread/AsyncFunctorThread.h" |
| 2 | |
| 3 | #include <prim/seadDelegate.h> |
| 4 | #include <thread/seadDelegateThread.h> |
| 5 | #include <thread/seadMessageQueue.h> |
| 6 | #include <thread/seadThread.h> |
| 7 | |
| 8 | namespace al { |
| 9 | |
| 10 | AsyncFunctorThread::AsyncFunctorThread(const sead::SafeString& functor_name, |
| 11 | const FunctorBase& functor, s32 priority, s32 stack_size, |
| 12 | sead::CoreId id) { |
| 13 | s32 size = stack_size < 0 ? 4096 : stack_size; |
| 14 | mDelegateThread = new sead::DelegateThread( |
| 15 | functor_name, |
| 16 | new sead::Delegate2<AsyncFunctorThread, sead::Thread*, sead::MessageQueue::Element>( |
| 17 | this, &AsyncFunctorThread::threadFunction), |
| 18 | nullptr, priority, sead::MessageQueue::BlockType::Blocking, 0x7FFFFFFF, size, 4); |
| 19 | |
| 20 | if (id) |
| 21 | mDelegateThread->setAffinity(sead::CoreIdMask(id)); |
| 22 | |
| 23 | mFunctor = functor.clone(); |
| 24 | mDelegateThread->start(); |
| 25 | } |
| 26 | |
| 27 | AsyncFunctorThread::~AsyncFunctorThread() { |
| 28 | mDelegateThread->quitAndWaitDoneSingleThread(is_jam: false); |
| 29 | } |
| 30 | |
| 31 | void AsyncFunctorThread::threadFunction(sead::Thread* unused_1, s64 unused_2) { |
| 32 | (*mFunctor)(); |
| 33 | mIsDone = true; |
| 34 | } |
| 35 | |
| 36 | void AsyncFunctorThread::start() { |
| 37 | mDelegateThread->sendMessage(msg: 1, block_type: sead::MessageQueue::BlockType::NonBlocking); |
| 38 | mIsDone = false; |
| 39 | } |
| 40 | |
| 41 | bool AsyncFunctorThread::isDone() const { |
| 42 | return mIsDone; |
| 43 | } |
| 44 | |
| 45 | } // namespace al |
| 46 | |