summaryrefslogtreecommitdiff
path: root/src/egg/core/eggThread.cpp
blob: 56f130c74e1d1f8319c44bdc1c8a445edb08e8db (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
118
119
120
121
122
123
124
#include "egg/core/eggThread.h"

namespace EGG {

nw4r::ut::List Thread::sThreadList;
void (*Thread::sOldSwitchThreadCallback)(OSThread *, OSThread *);

Thread::Thread(u32 stackSize, int msgCount, int priority, Heap *heap) {
    if (heap == nullptr) {
        heap = Heap::sCurrentHeap;
    }

    mContainingHeap = heap;
    mStackSize = ROUND_DOWN(stackSize, 0x20);
    mStackMemory = Heap::alloc(ROUND_DOWN(stackSize, 0x20), 0x20, heap);
    mOSThread = Heap::alloc<OSThread>(mContainingHeap, 0x20);
    OSCreateThread(mOSThread, start, this, (char *)mStackMemory + mStackSize, mStackSize, priority, 1);
    mAllocatableHeap = nullptr;
    mCurrentHeap = nullptr;
    setCommonMesgQueue(msgCount, mContainingHeap);
}

Thread::Thread(OSThread *osThread, int msgCount) {
    mContainingHeap = nullptr;
    mOSThread = osThread;
    mStackSize = (u8 *)osThread->stackBegin - (u8 *)osThread->stackEnd;
    mStackMemory = osThread->stackEnd;
    mAllocatableHeap = nullptr;
    mCurrentHeap = nullptr;
    setCommonMesgQueue(msgCount, Heap::sCurrentHeap);
}

Thread::~Thread() {
    nw4r::ut::List_Remove(&sThreadList, this);
    if (mContainingHeap != nullptr) {
        if (!OSIsThreadTerminated(mOSThread)) {
            OSDetachThread(mOSThread);
            OSCancelThread(mOSThread);
        }
        Heap::free(mStackMemory, mContainingHeap);
        Heap::free(mOSThread, mContainingHeap);
    }
    Heap::free(mMesgBuffer, nullptr);
}

Thread *Thread::findThread(OSThread *thread) {
    Thread *ptr = nullptr;
    while ((ptr = (Thread *)nw4r::ut::List_GetNext(&sThreadList, ptr)) != nullptr) {
        if (ptr->mOSThread == thread) {
            return ptr;
        }
    }
    return nullptr;
}

void Thread::initialize() {
    // TODO offsetof
    nw4r::ut::List_Init(&sThreadList, 0x44);
    sOldSwitchThreadCallback = OSSetSwitchThreadCallback(switchThreadCallback);
}

void Thread::setThreadCurrentHeap(Heap *heap) {
    OSDisableScheduler();
    OSThread *myThread = mOSThread;
    OSThread *currentThread = OSGetCurrentThread();
    if (currentThread != myThread) {
        mCurrentHeap = heap;
    } else {
        if (heap != nullptr) {
            if (mCurrentHeap == nullptr) {
                mCurrentHeap = Heap::sCurrentHeap;
            }
            heap->_becomeCurrentHeapWithoutLock();
        } else {
            if (mCurrentHeap != nullptr) {
                mCurrentHeap->_becomeCurrentHeapWithoutLock();
                mCurrentHeap = nullptr;
            }
        }
    }

    OSEnableScheduler();
}

void Thread::switchThreadCallback(OSThread *from, OSThread *to) {
    Thread *fromThread = from != nullptr ? findThread(from) : nullptr;
    Thread *toThread = to != nullptr ? findThread(to) : nullptr;

    if (fromThread != nullptr) {
        fromThread->onExit();
        if (fromThread->mCurrentHeap != nullptr) {
            Heap *curr = Heap::sCurrentHeap;
            fromThread->mCurrentHeap->_becomeCurrentHeapWithoutLock();
            fromThread->mCurrentHeap = curr;
        }
    }

    if (toThread != nullptr) {
        if (toThread->mCurrentHeap != nullptr) {
            Heap *curr = Heap::sCurrentHeap;
            toThread->mCurrentHeap->_becomeCurrentHeapWithoutLock();
            toThread->mCurrentHeap = curr;
        }
        toThread->onEnter();
    }

    if (sOldSwitchThreadCallback != nullptr) {
        (sOldSwitchThreadCallback)(from, to);
    }
}

void Thread::setCommonMesgQueue(int mesgCount, Heap *heap) {
    mMesgCount = mesgCount;
    mMesgBuffer = Heap::alloc<OSMessage>(mesgCount, heap);
    OSInitMessageQueue(&mMesgQueue, mMesgBuffer, mMesgCount);
    nw4r::ut::List_Append(&sThreadList, this);
}

void *Thread::start(void *arg) {
    Thread *thread = static_cast<Thread *>(arg);
    return thread->run();
}

} // namespace EGG