libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
Thread.cpp
1/****************************************************************************
2 * Platform Abstraction Layer
3 * Daryl Borth 2026
4 * Thread.cpp
5 *
6 * Generic - Everything platform-specific lives behind platform->getThread().
7 ***************************************************************************/
8#include "Platform.h"
9
11static Mutex & RegistryLock() { static Mutex m; return m; }
12static Thread * registryHead = nullptr;
13
15{
16 join();
17}
18
19bool Thread::start(ThreadEntry entry, void * arg, uint32_t stackSize, ThreadPriority priority, void (*wake)(void))
20{
21 if(handle)
22 return false;
23
24 if(!platform || !platform->getThread())
25 return false;
26
27 // handle is passed by address so the driver can publish it before the
28 // new thread starts running - entry() may call back into this Thread
29 // (e.g. to suspend itself) as its first action. See ThreadDriver::createThread.
30 if(!platform->getThread()->createThread(entry, arg, stackSize, priority, &handle))
31 {
32 handle = nullptr;
33 return false;
34 }
35
36 stopFlag = false;
37 wakeFn = wake;
38
39 MutexLock guard(RegistryLock());
40 registryNext = registryHead;
41 registryHead = this;
42
43 return true;
44}
45
47{
48 {
49 MutexLock guard(RegistryLock());
50 Thread ** link = &registryHead;
51 while(*link)
52 {
53 if(*link == this)
54 {
55 *link = registryNext;
56 registryNext = nullptr;
57 break;
58 }
59 link = &(*link)->registryNext;
60 }
61 }
62
63 if(!handle)
64 return;
65
66 if(platform && platform->getThread())
67 platform->getThread()->joinThread(handle);
68 handle = nullptr;
69}
70
72{
73 stopFlag = true;
74 if(wakeFn)
75 wakeFn();
76}
77
79{
80 while(true)
81 {
82 Thread * t;
83 {
84 MutexLock guard(RegistryLock());
85 t = registryHead;
86 }
87
88 if(!t)
89 break;
90
91 // requestStop() then join() outside the lock - entry() may itself
92 // touch other Threads (eg. a wake callback locking a different
93 // mutex), and join() blocks until entry() actually returns.
94 t->requestStop();
95 t->join(); // removes t from the registry on return
96 }
97}
98
100{
101 if(!handle)
102 return;
103
104 if(platform && platform->getThread())
105 platform->getThread()->cancelThread(handle);
106 handle = nullptr;
107}
108
110{
111 if(handle && platform && platform->getThread())
112 platform->getThread()->suspendThread(handle);
113}
114
116{
117 if(handle && platform && platform->getThread())
118 platform->getThread()->resumeThread(handle);
119}
120
122{
123 if(!handle && platform && platform->getThread())
124 return false;
125
126 return platform->getThread()->isThreadSuspended(handle);
127}
128
130{
131 if(platform && platform->getThread())
132 return ThreadId(platform->getThread()->getCurrentThreadId());
133
134 return ThreadId();
135}
RAII lock guard - locks on construction, unlocks on destruction.
Definition Mutex.h:37
A simple mutual-exclusion lock.
Definition Mutex.h:14
static ThreadId current()
Definition Thread.cpp:129
A platform thread.
Definition Thread.h:40
bool start(ThreadEntry entry, void *arg=nullptr, uint32_t stackSize=8192, ThreadPriority priority=ThreadPriority::Normal, void(*wake)(void)=nullptr)
Starts entry(arg) running on a new thread.
Definition Thread.cpp:19
~Thread()
Joins the thread if it's still running, same as calling join().
Definition Thread.cpp:14
void join()
Blocks until the thread exits.
Definition Thread.cpp:46
Thread * registryNext
Intrusive next-pointer for the JoinAll() registry.
Definition Thread.h:103
volatile bool stopFlag
Set by requestStop(); polled by entry() via stopRequested()
Definition Thread.h:101
void requestStop()
Sets the flag entry() should check (via stopRequested()) to know it should return,...
Definition Thread.cpp:71
bool isSuspended() const
Definition Thread.cpp:121
void suspend()
Suspends thread execution.
Definition Thread.cpp:109
void resume()
Resumes a suspended thread.
Definition Thread.cpp:115
void cancel()
Requests the thread stop at its next safe point without blocking.
Definition Thread.cpp:99
static void JoinAll()
Requests every currently-registered, still-running Thread stop (per its own wake callback,...
Definition Thread.cpp:78
void * handle
Backend-assigned thread handle.
Definition Thread.h:100
void(* wakeFn)(void)
Optional callback to break entry() out of a wait; set by start()
Definition Thread.h:102
ThreadPriority
Abstract thread priority, mapped to each platform's native priority scheme.
Definition Thread.h:18