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 Log_Debug("Threads", "Looking for TID %i", TID);
101 for( thread = gpThreads; thread; thread = thread->GlobalNext )
103 if( thread->TID == TID ) {
104 Log_Debug("Threads", "Found %p", thread);
105 Log_Debug("Threads", "- Name = %s", thread->ThreadName);
113 * \brief Clone a thread control block (with a different TID)
115 tThread *Threads_CloneTCB(tThread *TemplateThread)
117 tThread *ret = malloc(sizeof(tThread));
119 memcpy(ret, TemplateThread, sizeof(tThread));
121 ret->TID = giThreads_NextThreadID ++;
123 ret->ThreadName = strdup(TemplateThread->ThreadName);
124 ret->EventSem = SDL_CreateSemaphore(0);
126 ret->WaitingThreads = NULL;
127 ret->WaitingThreadsEnd = NULL;
129 // Add to the end of the queue
130 // TODO: Handle concurrency issues
131 ret->GlobalNext = gpThreads;
137 tUID Threads_GetUID() { return gpCurrentThread->UID; }
138 tGID Threads_GetGID() { return gpCurrentThread->GID; }
139 tTID Threads_GetTID() { return gpCurrentThread->TID; }
140 tPID Threads_GetPID() { return gpCurrentThread->PID; }
142 int Threads_SetUID(tUID NewUID)
144 if(Threads_GetUID() != 0) {
149 gpCurrentThread->UID = NewUID;
153 int Threads_SetGID(tGID NewGID)
155 if(Threads_GetUID() != 0) {
160 gpCurrentThread->GID = NewGID;
164 int *Threads_GetErrno(void) { return &gpCurrentThread->_errno; }
165 char **Threads_GetCWD(void) { return &gpCurrentThread->Process->CWD; }
166 char **Threads_GetChroot(void) { return &gpCurrentThread->Process->Chroot; }
167 int *Threads_GetMaxFD(void) { return &gpCurrentThread->Process->MaxFD; };
169 tTID Threads_WaitTID(int TID, int *Status)
173 Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
177 // Any peer/child thread
179 Log_Error("Threads", "TODO: Threads_WaitTID(TID=0) - Any Child/Sibling");
185 Log_Error("Threads", "TODO: Threads_WaitTID(TID<0) - TGID");
192 tThread *thread = Threads_GetThread(TID);
193 tThread *us = gpCurrentThread;
194 if(!thread) return -1;
197 us->Status = THREAD_STAT_WAITING;
199 if(thread->WaitingThreadsEnd)
201 thread->WaitingThreadsEnd->Next = us;
202 thread->WaitingThreadsEnd = us;
206 thread->WaitingThreads = us;
207 thread->WaitingThreadsEnd = us;
210 Threads_WaitEvents( THREAD_EVENT_WAKEUP );
212 if(Status) *Status = thread->ExitStatus;
213 thread->WaitingThreads = thread->WaitingThreads->Next;
222 void Threads_Sleep(void)
224 // TODO: Add to a sleeping queue
228 void Threads_Yield(void)
233 void Threads_Exit(int TID, int Status)
237 // VFS_Handles_Cleanup();
239 gpCurrentThread->ExitStatus = Status;
242 if( gpCurrentThread->Parent )
244 // Wait for the thread to be waited upon
245 while( gpCurrentThread->WaitingThreads == NULL )
250 while( (toWake = gpCurrentThread->WaitingThreads) )
252 Log_Debug("Threads", "Threads_Exit - Waking %p %i '%s'", toWake, toWake->TID, toWake->ThreadName);
254 Threads_Wake(toWake);
256 while(gpCurrentThread->WaitingThreads == toWake)
261 int Threads_Wake(tThread *Thread)
263 Thread->Status = THREAD_STAT_ACTIVE;
264 Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP);
268 int Threads_WakeTID(tTID TID)
271 thread = Threads_GetThread(TID);
272 if( !thread ) return -1;
273 return Threads_Wake(thread);
276 int Threads_CreateRootProcess(void)
278 tThread *thread = Threads_CloneTCB(&gThreadZero);
279 thread->PID = thread->TID;
281 // Handle list is created on first open
286 int Threads_Fork(void)
288 tThread *thread = Threads_CloneTCB(gpCurrentThread);
289 thread->PID = thread->TID;
290 // Duplicate the VFS handles (and nodes) from vfs_handle.c
292 VFS_CloneHandleList(thread->PID);
297 // --------------------------------------------------------------------
299 // --------------------------------------------------------------------
300 int Mutex_Acquire(tMutex *Mutex)
302 if(!Mutex->Protector.IsValid) {
303 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
304 Mutex->Protector.IsValid = 1;
306 pthread_mutex_lock( &Mutex->Protector.Mutex );
310 void Mutex_Release(tMutex *Mutex)
312 pthread_mutex_unlock( &Mutex->Protector.Mutex );
315 // --------------------------------------------------------------------
317 // --------------------------------------------------------------------
318 void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name)
320 memset(Sem, 0, sizeof(tSemaphore));
321 // HACK: Use `Sem->Protector` as space for the semaphore pointer
322 *(void**)(&Sem->Protector) = SDL_CreateSemaphore(InitValue);
325 int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
327 SDL_SemWait( *(void**)(&Sem->Protector) );
331 int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
334 for( i = 0; i < AmmountToAdd; i ++ )
335 SDL_SemPost( *(void**)(&Sem->Protector) );
339 // --------------------------------------------------------------------
341 // --------------------------------------------------------------------
342 Uint32 Threads_WaitEvents(Uint32 Mask)
346 Log_Debug("Threads", "Mask = %x, ->Events = %x", Mask, gpCurrentThread->Events);
348 gpCurrentThread->WaitMask = Mask;
349 if( !(gpCurrentThread->Events & Mask) )
351 SDL_SemWait( gpCurrentThread->EventSem );
353 rv = gpCurrentThread->Events & Mask;
354 gpCurrentThread->Events &= ~Mask;
355 gpCurrentThread->WaitMask = -1;
360 void Threads_PostEvent(tThread *Thread, Uint32 Events)
362 Thread->Events |= Events;
363 Log_Debug("Threads", "Trigger event %x (->Events = %p)", Events, Thread->Events);
365 if( Thread->WaitMask & Events ) {
366 SDL_SemPost( Thread->EventSem );
367 // Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
371 void Threads_ClearEvent(Uint32 EventMask)
373 gpCurrentThread->Events &= ~EventMask;