AcessNative - Fixing for recent kernel changes
[tpg/acess2.git] / AcessNative / acesskernel_src / threads.c
index 9d12517..1e7d0c3 100644 (file)
@@ -32,6 +32,15 @@ typedef struct sState
 }      tState;
 #endif
 
+typedef struct sProcess
+{
+        int    nThreads;
+        int    NativePID;
+       char    *CWD;
+       char    *Chroot;
+        int    MaxFD;
+} tProcess;
+
 typedef struct sThread
 {
        struct sThread  *GlobalNext;
@@ -48,9 +57,7 @@ typedef struct sThread
 
         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:
@@ -61,23 +68,35 @@ typedef struct sThread
        struct sThread  *WaitingThreads;
        struct sThread  *WaitingThreadsEnd;
 
-       // Config?
-       Uint    Config[NUM_CFG_ENTRIES];
+       tProcess        *Process;       
+
 }      tThread;
 
 // === 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;
@@ -92,6 +111,19 @@ void Threads_Dump(void)
        }
 }
 
+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;
@@ -154,14 +186,10 @@ int Threads_SetGID(int *Errno, tGID NewGID)
        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)
 {
@@ -192,6 +220,7 @@ int Threads_WaitTID(int TID, int *Status)
                
                us->Next = NULL;
                us->State = 3;
+               // TODO: Locking
                if(thread->WaitingThreadsEnd)
                {
                        thread->WaitingThreadsEnd->Next = us;
@@ -204,7 +233,10 @@ int Threads_WaitTID(int TID, int *Status)
                }
                
                while(thread->State != 0)
+               {
                        pause();
+                       Log_Debug("Threads", "Huh?... state = %i", thread->State);
+               }
                
                if(Status)      *Status = thread->ExitStatus;
                thread->WaitingThreads = thread->WaitingThreads->Next;
@@ -232,15 +264,22 @@ void Threads_Exit(int TID, int Status)
        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)
@@ -250,6 +289,7 @@ void Threads_Exit(int TID, int Status)
 
 int Threads_Wake(tThread *Thread)
 {
+       Thread->State = 0;
        kill( Thread->KernelTID, SIGUSR1 );
        return 0;
 }
@@ -319,35 +359,3 @@ int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
        return AmmountToAdd;
 }
 
-#if 0
-void Threads_Sleep()
-{
-       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 ;
-       }
-}
-
-int SaveState(tState *To)
-{
-       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));
-}
-#endif
-

UCC git Repository :: git.ucc.asn.au