3 * - Acess kernel emulation on another OS using SDL and UDP
6 * - Thread and process handling
8 #define _SIGNAL_H_ // Stop the acess signal.h being used
9 #define _HEAP_H_ // Stop heap.h being imported (collides with stdlib heap)
10 #define _VFS_EXT_H // Stop vfs_ext.h being imported (collides with fd_set)
11 #undef CLONE_VM // Such a hack
13 #undef NULL // Remove acess definition
16 #include <semaphore.h>
18 #include <sys/types.h>
20 #include "/usr/include/signal.h"
24 void VFS_CloneHandleList(int PID);
35 typedef struct sProcess
44 typedef struct sThread
46 struct sThread *GlobalNext;
54 struct sThread *Parent;
58 int State; // 0: Dead, 1: Active, 2: Paused, 3: Asleep
62 // Threads waiting for this thread to exit.
64 // - Wait for `WaitingThreads` to be non-null (maybe?)
65 // - Wake first in the queue, wait for it to be removed
67 // - Free thread and quit kernel thread
68 struct sThread *WaitingThreads;
69 struct sThread *WaitingThreadsEnd;
76 int Threads_Wake(tThread *Thread);
79 tProcess gProcessZero = {
85 tThread gThreadZero = {
87 .ThreadName="ThreadZero",
88 .Process = &gProcessZero
90 tThread *gpThreads = &gThreadZero;
91 __thread tThread *gpCurrentThread = &gThreadZero;
92 int giThreads_NextThreadID = 1;
95 tThread *Proc_GetCurThread(void)
97 return gpCurrentThread;
100 void Threads_Dump(void)
103 for( thread = gpThreads; thread; thread = thread->GlobalNext )
105 Log_Log("Threads", "TID %i (%s), PID %i",
106 thread->TID, thread->ThreadName, thread->PID);
107 Log_Log("Threads", "User: %i, Group: %i",
108 thread->UID, thread->GID);
109 Log_Log("Threads", "Kernel Thread ID: %i",
114 void Threads_SetThread(int TID)
117 for( thread = gpThreads; thread; thread = thread->GlobalNext )
119 if( thread->TID == TID ) {
120 gpCurrentThread = thread;
124 Log_Error("Threads", "_SetThread - Thread %i is not on global list", TID);
127 tThread *Threads_GetThread(int TID)
130 for( thread = gpThreads; thread; thread = thread->GlobalNext )
132 if( thread->TID == TID )
139 * \brief Clone a thread control block (with a different TID)
141 tThread *Threads_CloneTCB(tThread *TemplateThread)
143 tThread *ret = malloc(sizeof(tThread));
145 memcpy(ret, TemplateThread, sizeof(tThread));
147 ret->TID = giThreads_NextThreadID ++;
149 ret->ThreadName = strdup(TemplateThread->ThreadName);
151 ret->WaitingThreads = NULL;
152 ret->WaitingThreadsEnd = NULL;
154 // Add to the end of the queue
155 // TODO: Handle concurrency issues
156 ret->GlobalNext = gpThreads;
162 tUID Threads_GetUID() { return gpCurrentThread->UID; }
163 tGID Threads_GetGID() { return gpCurrentThread->GID; }
164 tTID Threads_GetTID() { return gpCurrentThread->TID; }
165 tPID Threads_GetPID() { return gpCurrentThread->PID; }
167 int Threads_SetUID(int *Errno, tUID NewUID)
169 if(Threads_GetUID() != 0) {
170 if(Errno) *Errno = -EACCES;
174 gpCurrentThread->UID = NewUID;
178 int Threads_SetGID(int *Errno, tGID NewGID)
180 if(Threads_GetUID() != 0) {
181 if(Errno) *Errno = -EACCES;
185 gpCurrentThread->GID = NewGID;
189 int *Threads_GetErrno(void) { return &gpCurrentThread->_errno; }
190 char **Threads_GetCWD(void) { return &gpCurrentThread->Process->CWD; }
191 char **Threads_GetChroot(void) { return &gpCurrentThread->Process->Chroot; }
192 int *Threads_GetMaxFD(void) { return &gpCurrentThread->Process->MaxFD; };
194 int Threads_WaitTID(int TID, int *Status)
198 Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
202 // Any peer/child thread
204 Log_Error("Threads", "TODO: Threads_WaitTID(TID=0) - Any Child/Sibling");
210 Log_Error("Threads", "TODO: Threads_WaitTID(TID<0) - TGID");
217 tThread *thread = Threads_GetThread(TID);
218 tThread *us = gpCurrentThread;
219 if(!thread) return -1;
224 if(thread->WaitingThreadsEnd)
226 thread->WaitingThreadsEnd->Next = us;
227 thread->WaitingThreadsEnd = us;
231 thread->WaitingThreads = us;
232 thread->WaitingThreadsEnd = us;
235 while(thread->State != 0)
238 Log_Debug("Threads", "Huh?... state = %i", thread->State);
241 if(Status) *Status = thread->ExitStatus;
242 thread->WaitingThreads = thread->WaitingThreads->Next;
251 void Threads_Sleep(void)
253 // TODO: Add to a sleeping queue
257 void Threads_Yield(void)
262 void Threads_Exit(int TID, int Status)
266 // VFS_Handles_Cleanup();
268 gpCurrentThread->ExitStatus = Status;
271 if( gpCurrentThread->Parent )
273 // Wait for the thread to be waited upon
274 while( gpCurrentThread->WaitingThreads == NULL )
279 while( (toWake = gpCurrentThread->WaitingThreads) )
281 Log_Debug("Threads", "Threads_Exit - Waking %p %i '%s'", toWake, toWake->TID, toWake->ThreadName);
283 Threads_Wake(toWake);
285 while(gpCurrentThread->WaitingThreads == toWake)
290 int Threads_Wake(tThread *Thread)
293 kill( Thread->KernelTID, SIGUSR1 );
297 int Threads_WakeTID(tTID TID)
300 thread = Threads_GetThread(TID);
301 if( !thread ) return -1;
302 return Threads_Wake(thread);
305 int Threads_CreateRootProcess(void)
307 tThread *thread = Threads_CloneTCB(&gThreadZero);
308 thread->PID = thread->TID;
310 // Handle list is created on first open
315 int Threads_Fork(void)
317 tThread *thread = Threads_CloneTCB(gpCurrentThread);
318 thread->PID = thread->TID;
319 // Duplicate the VFS handles (and nodes) from vfs_handle.c
321 VFS_CloneHandleList(thread->PID);
326 int Mutex_Acquire(tMutex *Mutex)
328 if(!Mutex->Protector.IsValid) {
329 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
330 Mutex->Protector.IsValid = 1;
332 pthread_mutex_lock( &Mutex->Protector.Mutex );
336 void Mutex_Release(tMutex *Mutex)
338 pthread_mutex_unlock( &Mutex->Protector.Mutex );
341 void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name)
343 memset(Sem, 0, sizeof(tSemaphore));
344 // HACK: Use `Sem->Protector` as space for the semaphore pointer
345 *(void**)(&Sem->Protector) = SDL_CreateSemaphore(InitValue);
348 int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
350 SDL_SemWait( *(void**)(&Sem->Protector) );
354 int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
357 for( i = 0; i < AmmountToAdd; i ++ )
358 SDL_SemPost( *(void**)(&Sem->Protector) );