summaryrefslogtreecommitdiff
path: root/src/KingSystem/Utils/Thread/TaskMgr.h
blob: 4c8fa131f0b50d35ecf59573ab5640b63630cb42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once

#include <basis/seadRawPrint.h>
#include <container/seadBuffer.h>
#include <container/seadOffsetList.h>
#include <container/seadSafeArray.h>
#include <heap/seadDisposer.h>
#include <prim/seadDelegate.h>
#include <prim/seadRuntimeTypeInfo.h>
#include <prim/seadScopedLock.h>
#include <prim/seadTypedBitFlag.h>
#include <thread/seadCriticalSection.h>
#include "KingSystem/Utils/HeapUtil.h"
#include "KingSystem/Utils/Thread/Event.h"
#include "KingSystem/Utils/Thread/ManagedTask.h"
#include "KingSystem/Utils/Types.h"

namespace ksys::util {

class ManagedTaskHandle;
class TaskRequest;
struct TaskMgrRequest;

using ManagedTaskFactory = sead::IDelegate1<ManagedTask**>;

struct TaskMgrRequest {
    /// Optional. If null, a task from the internal buffer will be used.
    ManagedTask* task = nullptr;
    /// Must not be null.
    TaskRequest* request = nullptr;
    /// Optional.
    ManagedTaskHandle* handle = nullptr;
};
KSYS_CHECK_SIZE_NX150(TaskMgrRequest, 0x18);

class TaskMgr {
    SEAD_RTTI_BASE(TaskMgr)
public:
    explicit TaskMgr(sead::Heap* heap);
    virtual ~TaskMgr();

    void init(s32 num_tasks, sead::Heap* heap, ManagedTaskFactory& factory);

    template <typename TaskType>
    void init(s32 num_tasks, sead::Heap* heap) {
        initImpl_<TaskType>(num_tasks, heap);
    }

    template <typename TaskType>
    void initAndCheckType(s32 num_tasks, sead::Heap* heap) {
        initImpl_<TaskType>(num_tasks, heap);
        if (hasTasks()) {
            Task* task = mFreeTaskLists[0].front();
            const bool is_derived_from_managed_task = sead::IsDerivedFrom<ManagedTask>(task);
            SEAD_ASSERT(is_derived_from_managed_task);
        }
    }

    void finalize();

    void submitRequest(TaskMgrRequest& request);
    bool trySubmitRequest(TaskMgrRequest& request);

    bool hasTasks() const;

    void freeTask(ManagedTask* task);

protected:
    enum class Flag {
        HeapIsFreeable = 0x1,
    };

    bool fetchIdleTaskForRequest_(TaskMgrRequest& request, bool retry_until_success);
    ManagedTask* fetchIdleTask_(bool retry_until_success);

    u8 getListIndex_() const { return mListIndex; }
    u8 getListIndex2_() const { return ~mListIndex & 1; }

    void swapLists_() {
        auto lock = sead::makeScopedLock(mCS2);
        mListIndex = getListIndex2_();
        mNewFreeTaskEvent.resetSignal();
    }

    bool tryFetchTaskForRequest_(TaskMgrRequest& request, bool b) {
        if (!mTasksCS.tryLock())
            return false;

        const bool ret = fetchIdleTaskForRequest_(request, b);
        mTasksCS.unlock();
        return ret;
    }

    template <typename TaskType>
    void makeTaskType_(ManagedTask** task) {
        *task = new TaskType(getCurrentHeap());
    }

    template <typename TaskType>
    void initImpl_(s32 num_tasks, sead::Heap* heap) {
        sead::Delegate1<TaskMgr, ManagedTask**> factory{this, &TaskMgr::makeTaskType_<TaskType>};
        init(num_tasks, heap, factory);
    }

    sead::TypedBitFlag<Flag, u8> mFlags;
    u8 mListIndex = 0;
    ManagedTask* mTask = nullptr;
    sead::CriticalSection mTasksCS;
    sead::CriticalSection mCS2;
    Event mNewFreeTaskEvent;
    Event mEvent2;
    sead::SafeArray<sead::OffsetList<ManagedTask>, 2> mFreeTaskLists;
    sead::Buffer<ManagedTask*> mTasks;
};
KSYS_CHECK_SIZE_NX150(TaskMgr, 0x158);

}  // namespace ksys::util