libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
Thread.h
1/****************************************************************************
2 * Platform Abstraction Layer
3 * Daryl Borth 2026
4 * Thread.h
5 *
6 * Generic - Everything platform-specific lives behind platform->getThread().
7 ***************************************************************************/
8#pragma once
9
10#include <stdint.h>
11
12#include "Mutex.h"
13#include "Cond.h"
14
18{
19 Idle,
20 Low,
21 Normal,
22 High,
23 TimeCritical
24};
25
39class Thread
40{
41 public:
42 Thread() : handle(nullptr) {}
44 ~Thread();
45
46 Thread(const Thread &) = delete;
47 Thread & operator=(const Thread &) = delete;
48
61 bool start(ThreadEntry entry, void * arg = nullptr, uint32_t stackSize = 8192, ThreadPriority priority = ThreadPriority::Normal, void (*wake)(void) = nullptr);
63 void join();
69 void cancel();
72 void suspend();
74 void resume();
76 bool isSuspended() const;
79 bool isRunning() const { return handle != nullptr; }
80
85 void requestStop();
90 bool stopRequested() const { return stopFlag; }
91
97 static void JoinAll();
98
99 protected:
100 void * handle = nullptr;
101 volatile bool stopFlag = false;
102 void (*wakeFn)(void) = nullptr;
103 Thread * registryNext = nullptr;
104};
105
112class ThreadId
113{
114 public:
115 ThreadId() : id(0) {}
116
118 static ThreadId current();
119
121 bool operator==(const ThreadId & other) const { return id == other.id; }
123 bool operator!=(const ThreadId & other) const { return !(*this == other); }
124
125 protected:
127 explicit ThreadId(uintptr_t v) : id(v) {}
128 uintptr_t id;
129};
130
A condition variable.
Definition Cond.h:17
A simple mutual-exclusion lock.
Definition Mutex.h:14
uintptr_t id
Backend-defined thread identifier (0 = none)
Definition Thread.h:128
ThreadId(uintptr_t v)
Wraps a backend-defined thread identifier; use current() instead.
Definition Thread.h:127
static ThreadId current()
Definition Thread.cpp:129
bool operator==(const ThreadId &other) const
Definition Thread.h:121
bool operator!=(const ThreadId &other) const
Definition Thread.h:123
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
bool isRunning() const
Definition Thread.h:79
bool stopRequested() const
Definition Thread.h:90
ThreadPriority
Abstract thread priority, mapped to each platform's native priority scheme.
Definition Thread.h:18
Bundles the mutex and pair of condition variables used by the common producer/consumer handshake betw...
Definition Thread.h:141
Cond idleCond
signalled to wake a waiter once the other side is idle/done
Definition Thread.h:144
Mutex mutex
Protects the flag(s) the caller shares between workCond and idleCond.
Definition Thread.h:142
Cond workCond
signalled to wake a waiter when new work/state is available
Definition Thread.h:143