| 1 | #pragma once |
| 2 | |
| 3 | #include "container/seadRingBuffer.h" |
| 4 | #include "mc/seadJobQueue.h" |
| 5 | #include "prim/seadEnum.h" |
| 6 | #include "prim/seadSafeString.h" |
| 7 | #include "thread/seadEvent.h" |
| 8 | #include "thread/seadThread.h" |
| 9 | #include "time/seadTickTime.h" |
| 10 | |
| 11 | namespace sead |
| 12 | { |
| 13 | class WorkerMgr; |
| 14 | |
| 15 | SEAD_ENUM(JobQueuePushType, cForward, cBackward) |
| 16 | |
| 17 | class Worker : public Thread |
| 18 | { |
| 19 | public: |
| 20 | static constexpr MessageQueue::Element cMsg_Process = 1; |
| 21 | |
| 22 | SEAD_ENUM(State, cSleep, cWakeup, cRunning, cRunning_WaitLock, cRunning_GetLock, cRunning_Run, |
| 23 | cRunning_AfterRun, cRunning_BeforeReturn, cRunning_AllJobDoneReturn, cFinished, |
| 24 | cWaitingAtWorker) |
| 25 | |
| 26 | Worker(WorkerMgr* mgr, u32 num_jobs, s32 stack_size, s32 priority, const SafeString& name); |
| 27 | |
| 28 | bool pushJobQueue(const char* name, JobQueue* queue, JobQueuePushType type); |
| 29 | void clearJobQQ(); |
| 30 | |
| 31 | void setState(Worker::State state) { mWorkerState = state; } |
| 32 | |
| 33 | protected: |
| 34 | friend class WorkerMgr; |
| 35 | |
| 36 | void calc_(MessageQueue::Element msg) override; |
| 37 | virtual void proc_(); |
| 38 | |
| 39 | JobQueue* getNextJQ_(); |
| 40 | void wakeup_(MessageQueue::Element msg); |
| 41 | |
| 42 | CoreId mCore = 0; |
| 43 | Atomic<Worker::State> mWorkerState{sead::AtomicDirectInitTag{}, Worker::State::cSleep}; |
| 44 | WorkerMgr* mMgr = nullptr; |
| 45 | RingBuffer<JobQueue*> mJobQueues; |
| 46 | JobQueueLock mLock; |
| 47 | JobQueue* mCurrentQueue = nullptr; |
| 48 | const char* mCurrentQueueDescription = nullptr; |
| 49 | u32 mNumRuns = 0; |
| 50 | TickTime mLastRun; |
| 51 | Event mEvent{true}; |
| 52 | }; |
| 53 | } // namespace sead |
| 54 | |