sead
Loading...
Searching...
No Matches
seadThread.h
Go to the documentation of this file.
1#ifndef SEAD_THREAD_H_
2#define SEAD_THREAD_H_
3
4#include <container/seadTList.h>
5#include <heap/seadDisposer.h>
6#include <heap/seadHeap.h>
7#include <hostio/seadHostIODummy.h>
8#include <prim/seadNamable.h>
9#include <thread/seadMessageQueue.h>
10#include <thread/seadThreadLocalStorage.h>
11#include <time/seadTickSpan.h>
12
13#ifdef cafe
14#include <cafe/os/OSThread.h>
15#endif // cafe
16
17namespace sead {
18
19class CoreIdMask;
20class Thread;
21
24
25class ThreadMgr;
26
27class Thread : public IDisposer, public INamable
28{
29public:
30 static const s32 cDefaultSeadPriority = 16;
31 static const s32 cDefaultPriority;
32
34 static const MessageQueue::Element cDefaultQuitMsg = 0x7fffffff;
35 static const s32 cDefaultStackSize = 4 * 1024;
36 static const s32 cDefaultMsgQueueSize = 32;
37
38private:
39 Thread(const Thread&);
40 const Thread& operator=(const Thread&);
41
42protected:
44 Heap*
45#ifdef cafe
46 , OSThread*
47#endif // cafe
48 );
49 friend class ThreadMgr;
50
51public:
52 Thread(const SafeString& name, Heap* heap, s32 priority = cDefaultPriority, MessageQueue::BlockType block_type = cDefaultBlockType, MessageQueue::Element quit_msg = cDefaultQuitMsg, s32 stack_size = cDefaultStackSize, s32 msg_queue_size = cDefaultMsgQueueSize);
53 virtual ~Thread();
54
55 virtual void destroy() { waitDone(); }
58 virtual bool start();
59 virtual void quit(bool);
60 bool isDone() const;
61 virtual void waitDone();
62 virtual void quitAndDestroySingleThread(bool is_jam) { quitAndWaitDoneSingleThread(is_jam); }
63 virtual void quitAndWaitDoneSingleThread(bool is_jam);
64 static void sleep(TickSpan span);
65 static void yield();
66 u32 getID() const { return mID; }
67 Heap* getCurrentHeap() const { return mCurrentHeap; }
68 virtual void setPriority(s32);
69 virtual s32 getPriority() const;
70 virtual MessageQueue::BlockType getBlockType() const { return mBlockType; }
71 virtual s32 getStackSize() const { return mStackSize; }
72 virtual s32 calcStackUsedSizePeak() const;
74 const CoreIdMask& getAffinity() const;
75 void setAffinity(const CoreIdMask&);
76
77protected:
80 void run_();
81 virtual void calc_(MessageQueue::Element) = 0;
84#ifdef cafe
85 static void cafeThreadFunc_(void*);
86#endif // cafe
87
88public:
89 void* getStackTop() const { return mStackTop; }
90 void* getStackBottom() const { return PtrUtil::addOffset(mStackTop, mStackSize); }
91
92 // Temp
93 s32 getState() const { return mState[0]; }
95
96protected:
104 s32 mState[1]; // State
106#ifdef cafe
108#endif // cafe
111};
112#ifdef cafe
113static_assert(sizeof(Thread) == 0x90, "sead::Thread size mismatch");
114#endif // cafe
115
116class ThreadMgr : public hostio::Node
117{
119
120public:
122 virtual ~ThreadMgr();
123
125 void destroy();
126 //void initHostIO();
127
128 Thread* getCurrentThread() const;
129 bool isMainThread() const;
130 Thread* getMainThread() const { return mMainThread; }
131
132 ThreadList::constIterator constBegin() const { return mList.constBegin(); }
133 ThreadList::constIterator constEnd() const { return mList.constEnd(); }
134
136 static void waitDoneMultipleThread(Thread* const*, s32);
137
138protected:
141
142protected:
146};
147#ifdef cafe
148static_assert(sizeof(ThreadMgr) == 0x28, "sead::ThreadMgr size mismatch");
149#endif // cafe
150
151#ifdef cafe
152
153inline Thread*
155{
156 return static_cast<Thread*>(OSGetThreadSpecific(mThreadPtrTLS.getValue()));
157}
158
159#endif // cafe
160
161} // namespace sead
162
163#endif // SEAD_THREAD_H_
Definition seadCoreInfo.h:96
Definition seadHeap.h:23
Definition seadDisposer.h:12
Definition seadNamable.h:9
Definition seadMessageQueue.h:15
s32 Element
Definition seadMessageQueue.h:23
BlockType
Definition seadMessageQueue.h:18
@ cNoBlock
Definition seadMessageQueue.h:20
Definition seadTList.h:13
Definition seadTList.h:49
Definition seadThreadLocalStorage.h:9
Definition seadThread.h:117
Thread * mMainThread
Definition seadThread.h:144
virtual ~ThreadMgr()
void destroyMainThread_()
ThreadLocalStorage mThreadPtrTLS
Definition seadThread.h:145
void initialize(Heap *)
static void quitAndWaitDoneMultipleThread(Thread **, s32, bool)
Thread * getCurrentThread() const
Definition seadThread.h:154
Thread * getMainThread() const
Definition seadThread.h:130
ThreadList mList
Definition seadThread.h:143
ThreadList::constIterator constBegin() const
Definition seadThread.h:132
void initMainThread_(Heap *)
ThreadList::constIterator constEnd() const
Definition seadThread.h:133
bool isMainThread() const
static void waitDoneMultipleThread(Thread *const *, s32)
Definition seadThread.h:28
MessageQueue::Element getQuitMsg() const
Definition seadThread.h:94
virtual void quitAndWaitDoneSingleThread(bool is_jam)
virtual void quit(bool)
virtual void waitDone()
Thread(Heap *, OSThread *)
virtual s32 getStackSize() const
Definition seadThread.h:71
static const MessageQueue::BlockType cDefaultBlockType
Definition seadThread.h:33
void * getStackBottom() const
Definition seadThread.h:90
const Thread & operator=(const Thread &)
MessageQueue::Element mQuitMsg
Definition seadThread.h:102
virtual u32 * getStackCheckStartAddress_() const
static void sleep(TickSpan span)
static const MessageQueue::Element cDefaultQuitMsg
Definition seadThread.h:34
virtual bool sendMessage(MessageQueue::Element, MessageQueue::BlockType)
virtual void setPriority(s32)
void initStackCheck_()
virtual bool start()
const CoreIdMask & getAffinity() const
virtual MessageQueue::BlockType getBlockType() const
Definition seadThread.h:70
ThreadListNode * getListNode()
MessageQueue::BlockType mBlockType
Definition seadThread.h:101
static const s32 cDefaultPriority
Definition seadThread.h:31
Thread(const Thread &)
MessageQueue mMessageQueue
Definition seadThread.h:97
void checkStackOverFlow()
s32 mStackSize
Definition seadThread.h:98
virtual s32 getPriority() const
static const s32 cDefaultSeadPriority
Definition seadThread.h:30
Heap * getCurrentHeap() const
Definition seadThread.h:67
bool isDone() const
static const s32 cDefaultStackSize
Definition seadThread.h:35
void * getStackTop() const
Definition seadThread.h:89
u8 * mStackTop
Definition seadThread.h:109
u32 getID() const
Definition seadThread.h:66
virtual MessageQueue::Element recvMessage(MessageQueue::BlockType)
u32 mID
Definition seadThread.h:103
void setAffinity(const CoreIdMask &)
virtual void destroy()
Definition seadThread.h:55
s32 mPriority
Definition seadThread.h:110
Heap * mCurrentHeap
Definition seadThread.h:100
virtual ~Thread()
ThreadListNode mListNode
Definition seadThread.h:99
virtual void calc_(MessageQueue::Element)=0
Thread(const SafeString &name, Heap *heap, s32 priority=cDefaultPriority, MessageQueue::BlockType block_type=cDefaultBlockType, MessageQueue::Element quit_msg=cDefaultQuitMsg, s32 stack_size=cDefaultStackSize, s32 msg_queue_size=cDefaultMsgQueueSize)
virtual void quitAndDestroySingleThread(bool is_jam)
Definition seadThread.h:62
virtual s32 calcStackUsedSizePeak() const
s32 mState[1]
Definition seadThread.h:104
static const s32 cDefaultMsgQueueSize
Definition seadThread.h:36
static void yield()
void setCurrentHeap_(Heap *)
s32 getState() const
Definition seadThread.h:93
u32 mCoreIdMask
Definition seadThread.h:105
Definition seadTickSpan.h:12
Definition seadHostIODummy.h:25
Definition seadHeap.h:14
Definition seadAssert.h:44
TList< Thread * > ThreadList
Definition seadThread.h:22
SafeStringBase< char > SafeString
Definition seadSafeString.h:409
TListNode< Thread * > ThreadListNode
Definition seadThread.h:23
#define SEAD_SINGLETON_DISPOSER(CLASS)
Definition seadDisposer.h:29