Atlas - SDL_dos_scheduler.h
Home / ext / SDL / src / core / dos Lines: 1 | Size: 3615 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22#ifndef SDL_dos_scheduler_h_ 23#define SDL_dos_scheduler_h_ 24 25#include "SDL_internal.h" 26#include <setjmp.h> 27 28#ifdef __cplusplus 29extern "C" { 30#endif 31 32// Maximum number of cooperative threads. DOS doesn't need many — 33// typically just main thread + audio thread + maybe a loading thread. 34#define DOS_MAX_THREADS 16 35 36// Default stack size for new threads (64 KB) 37#define DOS_DEFAULT_STACK_SIZE (64 * 1024) 38 39// Thread states 40typedef enum 41{ 42 DOS_THREAD_FREE = 0, // Slot is available 43 DOS_THREAD_READY, // Runnable 44 DOS_THREAD_RUNNING, // Currently executing 45 DOS_THREAD_BLOCKED, // Waiting on a semaphore/mutex 46 DOS_THREAD_FINISHED // Thread function returned 47} DOS_ThreadState; 48 49// Per-thread context 50typedef struct DOS_ThreadContext 51{ 52 jmp_buf env; // Saved CPU state for context switch 53 DOS_ThreadState state; 54 int id; // Thread ID (index into thread table) 55 void *stack_base; // malloc'd stack memory 56 size_t stack_size; 57 int exit_status; // Return value from thread function 58 59 // Entry point 60 int (*entry_fn)(void *); 61 void *entry_arg; 62 63 // Join support 64 volatile bool finished; // Set when thread function returns 65 int join_waiter; // ID of thread waiting in WaitThread, or -1 66} DOS_ThreadContext; 67 68// Initialize the scheduler. Must be called before any thread operations. 69// Registers the calling context as thread 0 (the main thread). 70void DOS_SchedulerInit(void); 71 72// Shut down the scheduler. 73void DOS_SchedulerQuit(void); 74 75// Create a new thread. Returns thread ID (>0) on success, -1 on failure. 76// The thread starts in READY state and will run when yielded to. 77int DOS_CreateThread(int (*fn)(void *), void *arg, size_t stack_size); 78 79// Yield the current thread's timeslice. Switches to the next runnable thread 80// using round-robin scheduling. If no other thread is runnable, returns 81// immediately (no-op). This is the core cooperative scheduling primitive. 82void DOS_Yield(void); 83 84// Mark a thread as finished and yield. Called when a thread's entry 85// function returns. 86void DOS_ExitThread(int status); 87 88// Block until the specified thread finishes. Returns the thread's exit status. 89int DOS_JoinThread(int thread_id); 90 91// Get the current thread's ID. 92int DOS_GetCurrentThreadID(void); 93 94// Mark a thread as READY (used by semaphore signal to wake a blocked thread). 95void DOS_WakeThread(int thread_id); 96 97// Mark the current thread as BLOCKED and yield (used by semaphore wait). 98void DOS_BlockCurrentThread(void); 99 100// Destroy a thread's resources (stack, etc). Thread must be FINISHED or FREE. 101void DOS_DestroyThread(int thread_id); 102 103#ifdef __cplusplus 104} 105#endif 106 107#endif // SDL_dos_scheduler_h_ 108[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.