#define _SIGNAL_H_ // Stop the acess signal.h being used
#define _HEAP_H_ // Stop heap.h being imported (collides with stdlib heap)
#define _VFS_EXT_H // Stop vfs_ext.h being imported (collides with fd_set)
-#undef CLONE_VM // Such a hack
+
+#define off_t _acess_off_t
#include <arch.h>
#undef NULL // Remove acess definition
#include <acess.h>
#include <mutex.h>
#include <semaphore.h>
+#include <events.h>
+
+#undef CLONE_VM // Such a hack
+#undef off_t
+
+// - Native headers
#include <unistd.h>
#include <sys/types.h>
#include <stdint.h>
#include "/usr/include/signal.h"
#include <SDL/SDL.h>
+#define THREAD_EVENT_WAKEUP 0x80000000
+
// === IMPORTS ===
void VFS_CloneHandleList(int PID);
} tState;
#endif
-typedef struct sThread
+typedef struct sProcess
+{
+ int nThreads;
+ int NativePID;
+ char *CWD;
+ char *Chroot;
+ int MaxFD;
+} tProcess;
+
+struct sThread
{
struct sThread *GlobalNext;
struct sThread *Next;
int State; // 0: Dead, 1: Active, 2: Paused, 3: Asleep
int ExitStatus;
- #if 0
- tState CurState;
- #endif
+ int _errno;
// Threads waiting for this thread to exit.
// Quit logic:
struct sThread *WaitingThreads;
struct sThread *WaitingThreadsEnd;
- // Config?
- Uint Config[NUM_CFG_ENTRIES];
-} tThread;
+ tProcess *Process;
+
+ Uint32 Events, WaitMask;
+ SDL_sem *EventSem;
+
+};
// === PROTOTYPES ===
int Threads_Wake(tThread *Thread);
// === GLOBALS ===
+tProcess gProcessZero = {
+ .NativePID = 0,
+ .CWD = "/",
+ .Chroot = "/",
+ .MaxFD = 100
+};
tThread gThreadZero = {
- State: 1,
- ThreadName: "ThreadZero"
+ .State=1,
+ .ThreadName="ThreadZero",
+ .Process = &gProcessZero
};
tThread *gpThreads = &gThreadZero;
__thread tThread *gpCurrentThread = &gThreadZero;
int giThreads_NextThreadID = 1;
// === CODE ===
+tThread *Proc_GetCurThread(void)
+{
+ return gpCurrentThread;
+}
+
void Threads_Dump(void)
{
tThread *thread;
}
}
+void Threads_SetThread(int TID)
+{
+ tThread *thread;
+ for( thread = gpThreads; thread; thread = thread->GlobalNext )
+ {
+ if( thread->TID == TID ) {
+ gpCurrentThread = thread;
+ return ;
+ }
+ }
+ Log_Error("Threads", "_SetThread - Thread %i is not on global list", TID);
+}
+
tThread *Threads_GetThread(int TID)
{
tThread *thread;
ret->TID = giThreads_NextThreadID ++;
ret->ThreadName = strdup(TemplateThread->ThreadName);
+ ret->EventSem = SDL_CreateSemaphore(0);
ret->WaitingThreads = NULL;
ret->WaitingThreadsEnd = NULL;
tTID Threads_GetTID() { return gpCurrentThread->TID; }
tPID Threads_GetPID() { return gpCurrentThread->PID; }
-int Threads_SetUID(int *Errno, tUID NewUID)
+int Threads_SetUID(tUID NewUID)
{
if(Threads_GetUID() != 0) {
- if(Errno) *Errno = -EACCES;
+ errno = EACCES;
return -1;
}
return 0;
}
-int Threads_SetGID(int *Errno, tGID NewGID)
+int Threads_SetGID(tGID NewGID)
{
if(Threads_GetUID() != 0) {
- if(Errno) *Errno = -EACCES;
+ errno = -EACCES;
return -1;
}
return 0;
}
-Uint *Threads_GetCfgPtr(int Index)
-{
- if( Index < 0 || Index >= NUM_CFG_ENTRIES )
- return NULL;
- if( !gpCurrentThread )
- return NULL;
- return &gpCurrentThread->Config[Index];
-}
+int *Threads_GetErrno(void) { return &gpCurrentThread->_errno; }
+char **Threads_GetCWD(void) { return &gpCurrentThread->Process->CWD; }
+char **Threads_GetChroot(void) { return &gpCurrentThread->Process->Chroot; }
+int *Threads_GetMaxFD(void) { return &gpCurrentThread->Process->MaxFD; };
-int Threads_WaitTID(int TID, int *Status)
+tTID Threads_WaitTID(int TID, int *Status)
{
// Any Child
if(TID == -1) {
us->Next = NULL;
us->State = 3;
+ // TODO: Locking
if(thread->WaitingThreadsEnd)
{
thread->WaitingThreadsEnd->Next = us;
thread->WaitingThreadsEnd = us;
}
- while(thread->State != 0)
- pause();
+ Threads_WaitEvents( THREAD_EVENT_WAKEUP );
if(Status) *Status = thread->ExitStatus;
thread->WaitingThreads = thread->WaitingThreads->Next;
tThread *toWake;
// VFS_Handles_Cleanup();
+
+ gpCurrentThread->ExitStatus = Status;
#if 1
- // Wait for the thread to be waited upon
- while( gpCurrentThread->WaitingThreads == NULL )
- SDL_Delay(10);
+ if( gpCurrentThread->Parent )
+ {
+ // Wait for the thread to be waited upon
+ while( gpCurrentThread->WaitingThreads == NULL )
+ SDL_Delay(10);
+ }
#endif
while( (toWake = gpCurrentThread->WaitingThreads) )
{
+ Log_Debug("Threads", "Threads_Exit - Waking %p %i '%s'", toWake, toWake->TID, toWake->ThreadName);
+
Threads_Wake(toWake);
while(gpCurrentThread->WaitingThreads == toWake)
int Threads_Wake(tThread *Thread)
{
- kill( Thread->KernelTID, SIGUSR1 );
+ Thread->State = 0;
+ Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP);
return 0;
}
return AmmountToAdd;
}
-#if 0
-void Threads_Sleep()
+Uint32 Threads_WaitEvents(Uint32 Mask)
{
- gpCurrentThread->State = 3;
- if( setjmp(&gpCurrentThread->CurState) == 0 ) {
- // Return to user wait
- // Hmm... maybe I should have a "kernel" thread for every "user" thread
- }
- else {
- // Just woken up, return
- return ;
+ Uint32 rv;
+
+ Log_Debug("Threads", "Mask = %x, ->Events = %x", Mask, gpCurrentThread->Events);
+
+ gpCurrentThread->WaitMask = Mask;
+ if( !(gpCurrentThread->Events & Mask) )
+ {
+ SDL_SemWait( gpCurrentThread->EventSem );
}
+ rv = gpCurrentThread->Events & Mask;
+ gpCurrentThread->Events &= ~Mask;
+ gpCurrentThread->WaitMask = -1;
+
+ return rv;
}
-int SaveState(tState *To)
+void Threads_PostEvent(tThread *Thread, Uint32 Events)
{
- Uint ip;
- __asm__ __volatile__(
- "call 1f;\n\t"
- "1f:\n\t"
- "pop %%eax"
- : "=a" (ip)
- : );
- // If we just returned
- if(!ip) return 1;
-
- To->IP = ip;
- __asm__ __volatile__ ("mov %%esp, %1" : "=r"(To->SP));
- __asm__ __volatile__ ("mov %%ebp, %1" : "=r"(To->BP));
+ Thread->Events |= Events;
+ Log_Debug("Threads", "Trigger event %x (->Events = %p)", Events, Thread->Events);
+
+ if( Thread->WaitMask & Events ) {
+ SDL_SemPost( Thread->EventSem );
+// Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
+ }
}
-#endif