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 sThread
37 struct sThread *GlobalNext;
45 struct sThread *Parent;
49 int State; // 0: Dead, 1: Active, 2: Paused, 3: Asleep
55 // Threads waiting for this thread to exit.
57 // - Wait for `WaitingThreads` to be non-null (maybe?)
58 // - Wake first in the queue, wait for it to be removed
60 // - Free thread and quit kernel thread
61 struct sThread *WaitingThreads;
62 struct sThread *WaitingThreadsEnd;
65 Uint Config[NUM_CFG_ENTRIES];
69 int Threads_Wake(tThread *Thread);
72 tThread gThreadZero = {
74 ThreadName: "ThreadZero"
76 tThread *gpThreads = &gThreadZero;
77 __thread tThread *gpCurrentThread = &gThreadZero;
78 int giThreads_NextThreadID = 1;
81 void Threads_Dump(void)
84 for( thread = gpThreads; thread; thread = thread->GlobalNext )
86 Log_Log("Threads", "TID %i (%s), PID %i",
87 thread->TID, thread->ThreadName, thread->PID);
88 Log_Log("Threads", "User: %i, Group: %i",
89 thread->UID, thread->GID);
90 Log_Log("Threads", "Kernel Thread ID: %i",
95 tThread *Threads_GetThread(int TID)
98 for( thread = gpThreads; thread; thread = thread->GlobalNext )
100 if( thread->TID == TID )
107 * \brief Clone a thread control block (with a different TID)
109 tThread *Threads_CloneTCB(tThread *TemplateThread)
111 tThread *ret = malloc(sizeof(tThread));
113 memcpy(ret, TemplateThread, sizeof(tThread));
115 ret->TID = giThreads_NextThreadID ++;
117 ret->ThreadName = strdup(TemplateThread->ThreadName);
119 ret->WaitingThreads = NULL;
120 ret->WaitingThreadsEnd = NULL;
122 // Add to the end of the queue
123 // TODO: Handle concurrency issues
124 ret->GlobalNext = gpThreads;
130 tUID Threads_GetUID() { return gpCurrentThread->UID; }
131 tGID Threads_GetGID() { return gpCurrentThread->GID; }
132 tTID Threads_GetTID() { return gpCurrentThread->TID; }
133 tPID Threads_GetPID() { return gpCurrentThread->PID; }
135 int Threads_SetUID(int *Errno, tUID NewUID)
137 if(Threads_GetUID() != 0) {
138 if(Errno) *Errno = -EACCES;
142 gpCurrentThread->UID = NewUID;
146 int Threads_SetGID(int *Errno, tGID NewGID)
148 if(Threads_GetUID() != 0) {
149 if(Errno) *Errno = -EACCES;
153 gpCurrentThread->GID = NewGID;
157 Uint *Threads_GetCfgPtr(int Index)
159 if( Index < 0 || Index >= NUM_CFG_ENTRIES )
161 if( !gpCurrentThread )
163 return &gpCurrentThread->Config[Index];
166 int Threads_WaitTID(int TID, int *Status)
170 Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
174 // Any peer/child thread
176 Log_Error("Threads", "TODO: Threads_WaitTID(TID=0) - Any Child/Sibling");
182 Log_Error("Threads", "TODO: Threads_WaitTID(TID<0) - TGID");
189 tThread *thread = Threads_GetThread(TID);
190 tThread *us = gpCurrentThread;
191 if(!thread) return -1;
195 if(thread->WaitingThreadsEnd)
197 thread->WaitingThreadsEnd->Next = us;
198 thread->WaitingThreadsEnd = us;
202 thread->WaitingThreads = us;
203 thread->WaitingThreadsEnd = us;
206 while(thread->State != 0)
209 if(Status) *Status = thread->ExitStatus;
210 thread->WaitingThreads = thread->WaitingThreads->Next;
219 void Threads_Sleep(void)
221 // TODO: Add to a sleeping queue
225 void Threads_Yield(void)
230 void Threads_Exit(int TID, int Status)
234 // VFS_Handles_Cleanup();
237 // Wait for the thread to be waited upon
238 while( gpCurrentThread->WaitingThreads == NULL )
242 while( (toWake = gpCurrentThread->WaitingThreads) )
244 Threads_Wake(toWake);
246 while(gpCurrentThread->WaitingThreads == toWake)
251 int Threads_Wake(tThread *Thread)
253 kill( Thread->KernelTID, SIGUSR1 );
257 int Threads_WakeTID(tTID TID)
260 thread = Threads_GetThread(TID);
261 if( !thread ) return -1;
262 return Threads_Wake(thread);
265 int Threads_CreateRootProcess(void)
267 tThread *thread = Threads_CloneTCB(&gThreadZero);
268 thread->PID = thread->TID;
270 // Handle list is created on first open
275 int Threads_Fork(void)
277 tThread *thread = Threads_CloneTCB(gpCurrentThread);
278 thread->PID = thread->TID;
279 // Duplicate the VFS handles (and nodes) from vfs_handle.c
281 VFS_CloneHandleList(thread->PID);
286 int Mutex_Acquire(tMutex *Mutex)
288 if(!Mutex->Protector.IsValid) {
289 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
290 Mutex->Protector.IsValid = 1;
292 pthread_mutex_lock( &Mutex->Protector.Mutex );
296 void Mutex_Release(tMutex *Mutex)
298 pthread_mutex_unlock( &Mutex->Protector.Mutex );
301 void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name)
303 memset(Sem, 0, sizeof(tSemaphore));
304 // HACK: Use `Sem->Protector` as space for the semaphore pointer
305 *(void**)(&Sem->Protector) = SDL_CreateSemaphore(InitValue);
308 int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
310 SDL_SemWait( *(void**)(&Sem->Protector) );
314 int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
317 for( i = 0; i < AmmountToAdd; i ++ )
318 SDL_SemPost( *(void**)(&Sem->Protector) );
325 gpCurrentThread->State = 3;
326 if( setjmp(&gpCurrentThread->CurState) == 0 ) {
327 // Return to user wait
328 // Hmm... maybe I should have a "kernel" thread for every "user" thread
331 // Just woken up, return
336 int SaveState(tState *To)
339 __asm__ __volatile__(
345 // If we just returned
349 __asm__ __volatile__ ("mov %%esp, %1" : "=r"(To->SP));
350 __asm__ __volatile__ ("mov %%ebp, %1" : "=r"(To->BP));