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)
12 #define off_t _acess_off_t
14 #undef NULL // Remove acess definition
17 #include <semaphore.h>
19 #include <threads_int.h>
21 #undef CLONE_VM // Such a hack
26 #include <sys/types.h>
28 #include "/usr/include/signal.h"
31 #define THREAD_EVENT_WAKEUP 0x80000000
34 void VFS_CloneHandleList(int PID);
46 int Threads_Wake(tThread *Thread);
49 tProcess gProcessZero = {
55 tThread gThreadZero = {
56 .Status=THREAD_STAT_ACTIVE,
57 .ThreadName="ThreadZero",
58 .Process = &gProcessZero
60 tThread *gpThreads = &gThreadZero;
61 __thread tThread *gpCurrentThread = &gThreadZero;
62 int giThreads_NextThreadID = 1;
65 tThread *Proc_GetCurThread(void)
67 return gpCurrentThread;
70 void Threads_Dump(void)
73 for( thread = gpThreads; thread; thread = thread->GlobalNext )
75 Log_Log("Threads", "TID %i (%s), PID %i",
76 thread->TID, thread->ThreadName, thread->PID);
77 Log_Log("Threads", "User: %i, Group: %i",
78 thread->UID, thread->GID);
79 Log_Log("Threads", "Kernel Thread ID: %i",
84 void Threads_SetThread(int TID)
87 for( thread = gpThreads; thread; thread = thread->GlobalNext )
89 if( thread->TID == TID ) {
90 gpCurrentThread = thread;
94 Log_Error("Threads", "_SetThread - Thread %i is not on global list", TID);
97 tThread *Threads_GetThread(Uint TID)
100 for( thread = gpThreads; thread; thread = thread->GlobalNext )
102 if( thread->TID == TID ) {
110 * \brief Clone a thread control block (with a different TID)
112 tThread *Threads_CloneTCB(tThread *TemplateThread)
114 tThread *ret = malloc(sizeof(tThread));
116 memcpy(ret, TemplateThread, sizeof(tThread));
118 ret->TID = giThreads_NextThreadID ++;
120 ret->ThreadName = strdup(TemplateThread->ThreadName);
121 ret->EventSem = SDL_CreateSemaphore(0);
123 ret->WaitingThreads = NULL;
124 ret->WaitingThreadsEnd = NULL;
126 // Add to the end of the queue
127 // TODO: Handle concurrency issues
128 ret->GlobalNext = gpThreads;
134 tUID Threads_GetUID() { return gpCurrentThread->UID; }
135 tGID Threads_GetGID() { return gpCurrentThread->GID; }
136 tTID Threads_GetTID() { return gpCurrentThread->TID; }
137 tPID Threads_GetPID() { return gpCurrentThread->PID; }
139 int Threads_SetUID(tUID NewUID)
141 if(Threads_GetUID() != 0) {
146 gpCurrentThread->UID = NewUID;
150 int Threads_SetGID(tGID NewGID)
152 if(Threads_GetUID() != 0) {
157 gpCurrentThread->GID = NewGID;
161 int *Threads_GetErrno(void) { return &gpCurrentThread->_errno; }
162 char **Threads_GetCWD(void) { return &gpCurrentThread->Process->CWD; }
163 char **Threads_GetChroot(void) { return &gpCurrentThread->Process->Chroot; }
164 int *Threads_GetMaxFD(void) { return &gpCurrentThread->Process->MaxFD; };
166 tTID 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;
194 us->Status = THREAD_STAT_WAITING;
196 if(thread->WaitingThreadsEnd)
198 thread->WaitingThreadsEnd->Next = us;
199 thread->WaitingThreadsEnd = us;
203 thread->WaitingThreads = us;
204 thread->WaitingThreadsEnd = us;
207 Threads_WaitEvents( THREAD_EVENT_WAKEUP );
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();
236 gpCurrentThread->ExitStatus = Status;
239 if( gpCurrentThread->Parent )
241 // Wait for the thread to be waited upon
242 while( gpCurrentThread->WaitingThreads == NULL )
247 while( (toWake = gpCurrentThread->WaitingThreads) )
249 Log_Debug("Threads", "Threads_Exit - Waking %p %i '%s'", toWake, toWake->TID, toWake->ThreadName);
251 Threads_Wake(toWake);
253 while(gpCurrentThread->WaitingThreads == toWake)
258 int Threads_Wake(tThread *Thread)
260 Thread->Status = THREAD_STAT_ACTIVE;
261 Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP);
265 int Threads_WakeTID(tTID TID)
268 thread = Threads_GetThread(TID);
269 if( !thread ) return -1;
270 return Threads_Wake(thread);
273 int Threads_CreateRootProcess(void)
275 tThread *thread = Threads_CloneTCB(&gThreadZero);
276 thread->PID = thread->TID;
278 // Handle list is created on first open
283 int Threads_Fork(void)
285 tThread *thread = Threads_CloneTCB(gpCurrentThread);
286 thread->PID = thread->TID;
287 // Duplicate the VFS handles (and nodes) from vfs_handle.c
289 VFS_CloneHandleList(thread->PID);
294 // --------------------------------------------------------------------
296 // --------------------------------------------------------------------
297 int Mutex_Acquire(tMutex *Mutex)
299 if(!Mutex->Protector.IsValid) {
300 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
301 Mutex->Protector.IsValid = 1;
303 pthread_mutex_lock( &Mutex->Protector.Mutex );
307 void Mutex_Release(tMutex *Mutex)
309 pthread_mutex_unlock( &Mutex->Protector.Mutex );
312 // --------------------------------------------------------------------
314 // --------------------------------------------------------------------
315 void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name)
317 memset(Sem, 0, sizeof(tSemaphore));
318 // HACK: Use `Sem->Protector` as space for the semaphore pointer
319 *(void**)(&Sem->Protector) = SDL_CreateSemaphore(InitValue);
322 int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
324 SDL_SemWait( *(void**)(&Sem->Protector) );
328 int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
331 for( i = 0; i < AmmountToAdd; i ++ )
332 SDL_SemPost( *(void**)(&Sem->Protector) );
336 // --------------------------------------------------------------------
338 // --------------------------------------------------------------------
339 Uint32 Threads_WaitEvents(Uint32 Mask)
343 Log_Debug("Threads", "Mask = %x, ->Events = %x", Mask, gpCurrentThread->Events);
345 gpCurrentThread->WaitMask = Mask;
346 if( !(gpCurrentThread->Events & Mask) )
348 SDL_SemWait( gpCurrentThread->EventSem );
350 rv = gpCurrentThread->Events & Mask;
351 gpCurrentThread->Events &= ~Mask;
352 gpCurrentThread->WaitMask = -1;
357 void Threads_PostEvent(tThread *Thread, Uint32 Events)
359 Thread->Events |= Events;
360 Log_Debug("Threads", "Trigger event %x (->Events = %p)", Events, Thread->Events);
362 if( Thread->WaitMask & Events ) {
363 SDL_SemPost( Thread->EventSem );
364 // Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
368 void Threads_ClearEvent(Uint32 EventMask)
370 gpCurrentThread->Events &= ~EventMask;