Atlas - SDL_dos_scheduler.c

Home / ext / SDL / src / core / dos Lines: 1 | Size: 9513 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#include "SDL_internal.h" 23 24#ifdef SDL_PLATFORM_DOS 25 26#include "SDL_dos.h" 27#include "SDL_dos_scheduler.h" 28#include <setjmp.h> 29 30/* DJGPP's jmp_buf is defined as: 31 typedef struct __jmp_buf { 32 unsigned long __eax, __ebx, __ecx, __edx, __esi; 33 unsigned long __edi, __ebp, __esp, __eip, __eflags; 34 unsigned short __cs, __ds, __es, __fs, __gs, __ss; 35 unsigned long __sigmask, __signum, __exception_ptr; 36 unsigned char __fpu_state[108]; 37 } jmp_buf[1]; 38 We patch __esp, __ebp, and __eip to bootstrap new thread contexts. */ 39 40// Thread table — static array, no dynamic allocation needed 41static DOS_ThreadContext threads[DOS_MAX_THREADS]; 42static int current_thread = 0; // Index of currently running thread 43static bool scheduler_initialized = false; 44 45// Find the next runnable thread using round-robin scheduling. 46// Returns thread ID, or -1 if no other thread is runnable. 47static int FindNextRunnable(int start) 48{ 49 for (int i = 1; i <= DOS_MAX_THREADS; i++) { 50 int idx = (start + i) % DOS_MAX_THREADS; 51 if (threads[idx].state == DOS_THREAD_READY) { 52 return idx; 53 } 54 } 55 return -1; // No other thread is runnable 56} 57 58// Trampoline function that runs on a new thread's stack. 59// This is jumped to from DOS_Yield when the new thread runs for the first time. 60// We use a global to pass the thread ID since we just switched stacks. 61static volatile int trampoline_thread_id; 62 63static void ThreadTrampoline(void) 64{ 65 int tid = trampoline_thread_id; 66 DOS_ThreadContext *ctx = &threads[tid]; 67 68 // Run the user's thread function 69 int result = ctx->entry_fn(ctx->entry_arg); 70 71 // Thread is done 72 DOS_ExitThread(result); 73 74 // Should never reach here 75 for (;;) { 76 } 77} 78 79void DOS_SchedulerInit(void) 80{ 81 if (scheduler_initialized) { 82 return; 83 } 84 85 SDL_memset(threads, 0, sizeof(threads)); 86 87 // Thread 0 is the main thread (already running) 88 threads[0].state = DOS_THREAD_RUNNING; 89 threads[0].id = 0; 90 threads[0].join_waiter = -1; 91 current_thread = 0; 92 93 scheduler_initialized = true; 94 95 _go32_dpmi_lock_data((void *)threads, sizeof(threads)); 96 _go32_dpmi_lock_data((void *)&current_thread, sizeof(current_thread)); 97 _go32_dpmi_lock_data((void *)&scheduler_initialized, sizeof(scheduler_initialized)); 98 _go32_dpmi_lock_data((void *)&trampoline_thread_id, sizeof(trampoline_thread_id)); 99} 100 101void DOS_SchedulerQuit(void) 102{ 103 // Clean up any remaining threads 104 for (int i = 1; i < DOS_MAX_THREADS; i++) { 105 if (threads[i].state != DOS_THREAD_FREE) { 106 SDL_assert(threads[i].state == DOS_THREAD_FINISHED); 107 DOS_DestroyThread(i); 108 } 109 } 110 111 scheduler_initialized = false; 112} 113 114int DOS_CreateThread(int (*fn)(void *), void *arg, size_t stack_size) 115{ 116 if (!scheduler_initialized) { 117 return -1; 118 } 119 120 // Find a free slot 121 int tid = -1; 122 for (int i = 1; i < DOS_MAX_THREADS; i++) { 123 if (threads[i].state == DOS_THREAD_FREE) { 124 tid = i; 125 break; 126 } 127 } 128 129 if (tid < 0) { 130 return -1; // No free slots 131 } 132 133 if (stack_size == 0) { 134 stack_size = DOS_DEFAULT_STACK_SIZE; 135 } 136 137 // Allocate stack 138 void *stack = SDL_malloc(stack_size); 139 if (!stack) { 140 return -1; 141 } 142 143 // Lock the stack memory so it won't be paged out. 144 // This is important for context switches — we can't take a page fault 145 // while switching stacks. 146 _go32_dpmi_lock_data(stack, stack_size); 147 148 DOS_ThreadContext *ctx = &threads[tid]; 149 SDL_memset(ctx, 0, sizeof(*ctx)); 150 ctx->id = tid; 151 ctx->state = DOS_THREAD_READY; 152 ctx->stack_base = stack; 153 ctx->stack_size = stack_size; 154 ctx->entry_fn = fn; 155 ctx->entry_arg = arg; 156 ctx->finished = false; 157 ctx->join_waiter = -1; 158 159 // Set up the initial context. We use setjmp to save a template context, 160 // then modify the stack pointer to point to our new stack. 161 // 162 // The trick: we setjmp here to capture register state, then manually 163 // patch the saved __esp and __eip in the jmp_buf struct to point to 164 // our new stack and trampoline function. 165 if (setjmp(ctx->env) == 0) { 166 // Patch the saved context to use our new stack and trampoline. 167 // Stack grows downward, so SP starts at the top. 168 // Align to 16 bytes for ABI compliance. 169 Uint8 *stack_top = (Uint8 *)stack + stack_size; 170 stack_top = (Uint8 *)((uintptr_t)stack_top & ~0xFUL); // 16-byte align 171 172 // Leave room for a fake return address (the trampoline never returns, 173 // but the ABI expects one on the stack at function entry) 174 stack_top -= sizeof(void *); 175 *(void **)stack_top = NULL; // Fake return address 176 177 ctx->env[0].__esp = (unsigned long)(uintptr_t)stack_top; 178 ctx->env[0].__ebp = (unsigned long)(uintptr_t)stack_top; 179 ctx->env[0].__eip = (unsigned long)(uintptr_t)ThreadTrampoline; 180 } else { 181 SDL_assert(!"Unreachable"); 182 } 183 184 return tid; 185} 186 187void DOS_Yield(void) 188{ 189 if (!scheduler_initialized) { 190 return; 191 } 192 193 int next = FindNextRunnable(current_thread); 194 if (next < 0) { 195 return; // No other runnable thread, continue current 196 } 197 198 int prev = current_thread; 199 200 // Save current context and switch 201 if (setjmp(threads[prev].env) == 0) { 202 // Mark previous thread as READY (unless it's BLOCKED or FINISHED) 203 if (threads[prev].state == DOS_THREAD_RUNNING) { 204 threads[prev].state = DOS_THREAD_READY; 205 } 206 207 // Switch to next thread 208 current_thread = next; 209 threads[next].state = DOS_THREAD_RUNNING; 210 211 // For new threads that haven't run yet, set the trampoline ID 212 trampoline_thread_id = next; 213 214 longjmp(threads[next].env, 1); 215 } 216 // else: we've been switched back to — just return 217} 218 219void DOS_ExitThread(int status) 220{ 221 DOS_ThreadContext *ctx = &threads[current_thread]; 222 ctx->exit_status = status; 223 ctx->finished = true; 224 ctx->state = DOS_THREAD_FINISHED; 225 226 // Wake up anyone waiting to join this thread 227 if (ctx->join_waiter >= 0 && ctx->join_waiter < DOS_MAX_THREADS) { 228 DOS_WakeThread(ctx->join_waiter); 229 } 230 231 // Find another thread to run. We can't return — our stack frame 232 // belongs to the thread that just exited. 233 int next = FindNextRunnable(current_thread); 234 if (next >= 0) { 235 current_thread = next; 236 threads[next].state = DOS_THREAD_RUNNING; 237 trampoline_thread_id = next; 238 longjmp(threads[next].env, 1); 239 } 240 241 // If no other thread is runnable and we're not the main thread, 242 // this is a problem. Spin-wait for someone to become runnable. 243 // (This shouldn't happen in practice — the main thread should 244 // always be runnable or waiting.) 245 for (;;) { 246 __asm__ __volatile__("hlt"); // Wait for interrupt before retrying 247 next = FindNextRunnable(current_thread); 248 if (next >= 0) { 249 current_thread = next; 250 threads[next].state = DOS_THREAD_RUNNING; 251 trampoline_thread_id = next; 252 longjmp(threads[next].env, 1); 253 } 254 } 255} 256 257int DOS_JoinThread(int thread_id) 258{ 259 if (thread_id < 0 || thread_id >= DOS_MAX_THREADS) { 260 return -1; 261 } 262 263 DOS_ThreadContext *target = &threads[thread_id]; 264 265 // If already finished, just return the status 266 if (target->finished) { 267 return target->exit_status; 268 } 269 270 // Register ourselves as the join waiter 271 target->join_waiter = current_thread; 272 273 // Block until the target thread finishes 274 while (!target->finished) { 275 DOS_BlockCurrentThread(); 276 } 277 278 return target->exit_status; 279} 280 281int DOS_GetCurrentThreadID(void) 282{ 283 if (!scheduler_initialized) { 284 return 1; 285 } 286 return current_thread + 1; 287} 288 289void DOS_WakeThread(int thread_id) 290{ 291 if (thread_id >= 0 && thread_id < DOS_MAX_THREADS) { 292 if (threads[thread_id].state == DOS_THREAD_BLOCKED) { 293 threads[thread_id].state = DOS_THREAD_READY; 294 } 295 } 296} 297 298void DOS_BlockCurrentThread(void) 299{ 300 threads[current_thread].state = DOS_THREAD_BLOCKED; 301 DOS_Yield(); 302} 303 304void DOS_DestroyThread(int thread_id) 305{ 306 if (thread_id <= 0 || thread_id >= DOS_MAX_THREADS) { 307 return; // Can't destroy main thread (0) or invalid IDs 308 } 309 310 DOS_ThreadContext *ctx = &threads[thread_id]; 311 if (ctx->stack_base) { 312 SDL_free(ctx->stack_base); 313 ctx->stack_base = NULL; 314 } 315 ctx->state = DOS_THREAD_FREE; 316} 317 318#endif // SDL_PLATFORM_DOS 319
[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.