| 1 | #include "Project/Thread/InitializeThread.h" |
| 2 | |
| 3 | #include <thread/seadDelegateThread.h> |
| 4 | |
| 5 | #include "Library/Thread/FunctorV0M.h" |
| 6 | |
| 7 | namespace al { |
| 8 | InitializeThread::InitializeThread(const sead::SafeString& threadName, const FunctorBase& functor, |
| 9 | sead::Heap* heap, s32 priority, s32 stackSize) |
| 10 | : mHeap(heap) { |
| 11 | mThread = new sead::DelegateThread( |
| 12 | threadName, |
| 13 | new sead::Delegate2<InitializeThread, sead::Thread*, sead::MessageQueue::Element>( |
| 14 | this, &InitializeThread::threadFunction), |
| 15 | heap, priority, sead::MessageQueue::BlockType::NonBlocking, 0x7fffffff, stackSize, 0x20); |
| 16 | |
| 17 | mFunctor = functor.clone(); |
| 18 | } |
| 19 | |
| 20 | void InitializeThread::start() { |
| 21 | mThread->start(); |
| 22 | mIsDone = false; |
| 23 | } |
| 24 | |
| 25 | bool InitializeThread::tryWaitDoneAndDestroy() { |
| 26 | if (mIsDone) |
| 27 | return true; |
| 28 | |
| 29 | if (mThread->isDone()) { |
| 30 | mThread->destroy(); |
| 31 | mIsDone = true; |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | void InitializeThread::threadFunction(sead::Thread* thread, sead::MessageQueue::Element message) { |
| 39 | sead::ScopedCurrentHeapSetter heapSetter(mHeap); |
| 40 | |
| 41 | (*mFunctor)(); |
| 42 | mThread->quit(is_jam: false); |
| 43 | } |
| 44 | } // namespace al |
| 45 | |