Files
nt-lite/code/user/personalities/posix/posix_thread.c
T
maksw 73abd6f90d isolate POSIX builds from the host UCRT, wire lwext4 and ntfs-3g as ext4.sys and ntfs.sys, split the FAT driver into directory/file/async/driver/mount units, add posixsrv and dwm plus input/console/desktop syscall clients and a pile of new smoke targets, rename serial.sys to cereal.sys, name HDA/USB/lwIP/AHCI workers, harden ATA probing/reset/removal around actual hardware deadlines, absent-channel detection, diagnostics and explicit worker lifetime, redo ACPI S5 shutdown around GAS/MMIO and HW-reduced firmware, scan SSDTs for _S5 and deal with Intel SMI sleep trapping, make serial and SMP timeouts use real monotonic deadlines, add thread names and fix terminal thread teardown, scheduler and APC prepare/commit races, make advisory file locks queryable and preserve split ranges and add mutable file append state, add shared-memory pipes with kernel-assisted wait/wake, introduce capability-scoped input and display managers, split surface map rights into read/write, add explicit unmap and replace present with commit/generation/damage tracking while keeping physical presentation under the display manager, decouple native exception classes from Win32 exception numbers and translate them in kernel32, add opaque process startup data, bump the resource protocol to v3 with namespace directories, symlinks and enumeration and service control to v2 with generic READY handle exports, add the new POSIX errno/status surface and rebuild fds around posixsrv-owned shared open descriptions, descriptor-local flags, exact inheritance snapshots, dup/fcntl/record locks and shared-ring pipes, add waitpid, POSIX credentials/groups/sessions/passwd/group lookups and move sockets onto stable open descriptions, port the shells and apps to the new console/input services and surface commit API, teach init to load services.ini and start/supervise RSSRV, DWM and generic services under restricted capabilities while publishing their endpoints through RSSRV, split RSSRV into namespace, volume, display, session, input, console and resource components and make /SystemRoot a real symlink to the independently published system volume, shift the syscall table again, expand lock/resource/surface/POSIX/service/thread smoke coverage, make docs scream ASK QUESTIONS and DISCUSS at LLMs, replace MIT No Attribution with WTFPL+.
2026-09-16 10:33:20 +02:00

162 lines
4.7 KiB
C

// POSIX compatibility support for posix thread.
#include <stddef.h>
#include <stdlib.h>
#include <ntlite/syscall.h>
#include "posix_internal.h"
// Allocates a join-result slot in process-local POSIX state.
static POSIX_PTHREAD_SLOT* PthreadSlotAlloc(uint32_t tid) {
POSIX_PROCESS_STATE* process = PosixRuntimeProcessState();
if (!process) return (POSIX_PTHREAD_SLOT*)0;
for (uint32_t i = 0; i < POSIX_PTHREAD_TABLE_SIZE; i++) {
if (!process->PthreadTable[i].Used) {
process->PthreadTable[i] = (POSIX_PTHREAD_SLOT){ tid, (void*)0, true };
return &process->PthreadTable[i];
}
}
return (POSIX_PTHREAD_SLOT*)0;
}
// Finds a join-result slot in process-local POSIX state.
static POSIX_PTHREAD_SLOT* PthreadSlotFind(uint32_t tid) {
POSIX_PROCESS_STATE* process = PosixRuntimeProcessState();
if (!process) return (POSIX_PTHREAD_SLOT*)0;
for (uint32_t i = 0; i < POSIX_PTHREAD_TABLE_SIZE; i++) {
if (process->PthreadTable[i].Used && process->PthreadTable[i].Tid == tid)
return &process->PthreadTable[i];
}
return (POSIX_PTHREAD_SLOT*)0;
}
// Trampoline - this is what actually runs as the new thread.
typedef struct {
void* (*Fn)(void*);
void* Arg;
} PTHREAD_TRAMP;
// Implements the pthread Trampoline operation.
static void PthreadTrampoline(uintptr_t raw) {
PTHREAD_TRAMP* tr = (PTHREAD_TRAMP*)raw;
void* (*fn)(void*) = tr->Fn;
void* arg = tr->Arg;
uint32_t tid = SysGetCurrentTid();
if (PosixRuntimeAttachThread() != NTLSTATUS_SUCCESS) {
free(tr);
SysExitThread(ENOMEM);
}
// Create the join slot from inside the new thread. KeCreateThread makes the
// thread runnable before the creator regains control, so the old design
// raced on tr->Tid and could observe zero/stale memory.
POSIX_PTHREAD_SLOT* slot = PthreadSlotFind(tid);
if (!slot) slot = PthreadSlotAlloc(tid);
free(tr);
void* retval = fn(arg);
// Store retval for pthread_join.
if (slot) slot->RetVal = retval;
PosixRuntimeDetachThread();
SysExitThread(0);
}
// pthread API.
__declspec(dllexport) int pthread_create(pthread_t* t, const pthread_attr_t* attr,
void* (*fn)(void*), void* arg) {
(void)attr;
PTHREAD_TRAMP* tr = (PTHREAD_TRAMP*)malloc(sizeof(PTHREAD_TRAMP));
if (!tr) return ENOMEM;
tr->Fn = fn;
tr->Arg = arg;
uint32_t tid = 0;
uint32_t handle = 0;
SYS_CREATE_THREAD_REQUEST req = {
sizeof(SYS_CREATE_THREAD_REQUEST), (uintptr_t)PthreadTrampoline,
(uintptr_t)tr,
1,
0,
&handle,
&tid
};
uint32_t s = SysCreateThread(&req);
if (s != 0) {
free(tr);
return EAGAIN;
}
// The trampoline allocates the retval slot itself after it knows its real
// TID, avoiding the creator/new-thread race.
if (t) {
t->Handle = handle;
t->Tid = tid;
} else {
SysCloseHandle(handle);
}
return 0;
}
// Implements the pthread join operation.
__declspec(dllexport) int pthread_join(pthread_t t, void** retval) {
uint32_t s = SysWait(t.Handle, SYS_WAIT_FLAG_NONE);
if (s != 0) return ESRCH;
POSIX_PTHREAD_SLOT* slot = PthreadSlotFind(t.Tid);
if (retval) *retval = slot ? slot->RetVal : (void*)0;
if (slot) slot->Used = false;
SysCloseHandle(t.Handle);
return 0;
}
// Implements the pthread exit operation.
__declspec(dllexport) void pthread_exit(void* retval) {
uint32_t tid = SysGetCurrentTid();
POSIX_PTHREAD_SLOT* slot = PthreadSlotFind(tid);
if (slot) slot->RetVal = retval;
PosixRuntimeDetachThread();
SysExitThread(0);
while (1); // unreachable
}
// Implements the pthread mutex init operation.
__declspec(dllexport) int pthread_mutex_init(pthread_mutex_t* m, const pthread_mutexattr_t* attr) {
(void)attr;
uint32_t h;
uint32_t s = SysCreateMutex(&h);
if (s != 0) return ENOMEM;
m->Handle = h;
return 0;
}
// Implements the pthread mutex destroy operation.
__declspec(dllexport) int pthread_mutex_destroy(pthread_mutex_t* m) {
SysCloseHandle(m->Handle);
m->Handle = 0;
return 0;
}
// Implements the pthread mutex lock operation.
__declspec(dllexport) int pthread_mutex_lock(pthread_mutex_t* m) {
uint32_t s = SysWait(m->Handle, SYS_WAIT_FLAG_NONE);
return s == 0 ? 0 : EINVAL;
}
// Implements the pthread mutex trylock operation.
__declspec(dllexport) int pthread_mutex_trylock(pthread_mutex_t* m) {
uint32_t s = SysTryAcquireMutex(m->Handle);
return s == 0 ? 0 : EAGAIN;
}
// Implements the pthread mutex unlock operation.
__declspec(dllexport) int pthread_mutex_unlock(pthread_mutex_t* m) {
uint32_t s = SysReleaseMutex(m->Handle);
return s == 0 ? 0 : EINVAL;
}