Usermode/libaxwin4 - Handle demarshal failure
[tpg/acess2.git] / AcessNative / acesskernel_src / threads.c
index 9d12517..74d85fc 100644 (file)
  * threads.c
  * - Thread and process handling
  */
-#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 DEBUG  1
 #include <arch.h>
-#undef NULL    // Remove acess definition
 #include <acess.h>
 #include <mutex.h>
-#include <semaphore.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <stdint.h>
-#include "/usr/include/signal.h"
-#include <SDL/SDL.h>
+#include "../../KernelLand/Kernel/include/semaphore.h"
+typedef signed long long int   time_t;
+#include "../../Usermode/Libraries/ld-acess.so_src/include_exp/acess/syscall_types.h"
+#include <rwlock.h>
+#include <events.h>
+#include <threads_int.h>
+#include <limits.h>
+#include "include/threads_glue.h"
+#include <stdbool.h>
+
+#define THREAD_EVENT_WAKEUP    0x80000000
 
 // === IMPORTS ===
-void   VFS_CloneHandleList(int PID);
+extern void    VFS_CloneHandleList(int PID);
+extern void    VFS_CloneHandlesFromList(int PID, int nFD, int FDs[]);
+extern void    VFS_ClearHandles(int PID);
 
 // === STRUCTURES ===
-#if 0
-typedef struct sState
-{
-       void    *CurState;
-       Uint    SP, BP, IP;
-}      tState;
-#endif
-
-typedef struct sThread
-{
-       struct sThread  *GlobalNext;
-       struct sThread  *Next;
-
-        int    KernelTID;
-
-       tTID    TID, PID;
-       tUID    UID, GID;
-
-       struct sThread  *Parent;
-
-       char    *ThreadName;
-
-        int    State;  // 0: Dead, 1: Active, 2: Paused, 3: Asleep
-        int    ExitStatus;
-       #if 0
-       tState  CurState;
-       #endif
-
-       // Threads waiting for this thread to exit.
-       // Quit logic:
-       // - Wait for `WaitingThreads` to be non-null (maybe?)
-       // - Wake first in the queue, wait for it to be removed
-       // - Repeat
-       // - Free thread and quit kernel thread
-       struct sThread  *WaitingThreads;
-       struct sThread  *WaitingThreadsEnd;
-
-       // Config?
-       Uint    Config[NUM_CFG_ENTRIES];
-}      tThread;
-
 // === PROTOTYPES ===
  int   Threads_Wake(tThread *Thread);
 
 // === GLOBALS ===
+tProcess gProcessZero = {
+       .NativePID = 0,
+       .CWD = "/",
+       .Chroot = "/",
+       .MaxFD = 100
+};
 tThread        gThreadZero = {
-       State: 1,
-       ThreadName: "ThreadZero"
+       .Status=THREAD_STAT_ACTIVE,
+       .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;
-       for( thread = gpThreads; thread; thread = thread->GlobalNext )
+       for( tThread *thread = gpThreads; thread; thread = thread->GlobalNext )
        {
                Log_Log("Threads", "TID %i (%s), PID %i",
-                       thread->TID, thread->ThreadName, thread->PID);
+                       thread->TID, thread->ThreadName, thread->Process->PID);
                Log_Log("Threads", "User: %i, Group: %i",
-                       thread->UID, thread->GID);
+                       thread->Process->UID, thread->Process->GID);
                Log_Log("Threads", "Kernel Thread ID: %i",
                        thread->KernelTID);
        }
 }
 
-tThread        *Threads_GetThread(int TID)
+void Threads_SetThread(int TID, void *Client)
 {
-       tThread *thread;
-       for( thread = gpThreads; thread; thread = thread->GlobalNext )
+       for( tThread *thread = gpThreads; thread; thread = thread->GlobalNext )
+       {
+               if( thread->TID == TID ) {
+                       gpCurrentThread = thread;
+                       thread->ClientPtr = Client;
+                       return ;
+               }
+       }
+       Log_Error("Threads", "_SetThread - Thread %i is not on global list", TID);
+}
+
+tThread        *Threads_GetThread(Uint TID)
+{
+       for( tThread *thread = gpThreads; thread; thread = thread->GlobalNext )
        {
-               if( thread->TID == TID )
+               if( thread->TID == TID ) {
                        return thread;
+               }
        }
        return NULL;
 }
@@ -106,19 +92,34 @@ tThread    *Threads_GetThread(int TID)
 /**
  * \brief Clone a thread control block (with a different TID)
  */
-tThread *Threads_CloneTCB(tThread *TemplateThread)
+tThread *Threads_CloneTCB(tThread *TemplateThread, bool bNewProcess)
 {
        tThread *ret = malloc(sizeof(tThread));
-       
        memcpy(ret, TemplateThread, sizeof(tThread));
        
        ret->TID = giThreads_NextThreadID ++;
        
        ret->ThreadName = strdup(TemplateThread->ThreadName);
+       Threads_Glue_SemInit( &ret->EventSem, 0 );
        
        ret->WaitingThreads = NULL;
        ret->WaitingThreadsEnd = NULL;
-       
+
+       if( bNewProcess )
+       {
+               tProcess *proc = malloc( sizeof(tProcess) );
+               memcpy(proc, ret->Process, sizeof(tProcess));
+               proc->nThreads = 0;
+               proc->CWD = strdup(proc->CWD);
+               proc->Chroot = strdup(proc->Chroot);
+               
+               proc->PID = ret->TID;
+               
+               ret->Process = proc;
+       }       
+
+       ret->Process->nThreads ++;
+
        // Add to the end of the queue
        // TODO: Handle concurrency issues
        ret->GlobalNext = gpThreads;
@@ -127,43 +128,83 @@ tThread *Threads_CloneTCB(tThread *TemplateThread)
        return ret;
 }
 
-tUID Threads_GetUID() { return gpCurrentThread->UID; }
-tGID Threads_GetGID() { return gpCurrentThread->GID; }
+void Threads_int_Destroy(tThread *Thread)
+{
+       // Clear WaitingThreads
+       
+       Threads_Glue_SemDestroy(Thread->EventSem);
+       free(Thread->ThreadName);
+       Thread->Process->nThreads --;
+}
+
+void Threads_Terminate(void)
+{
+       tThread *us = gpCurrentThread;
+       tProcess *proc = us->Process;
+
+       if( us->TID == proc->PID )
+       {
+               // If we're the process leader, then tear down the entire process
+               VFS_ClearHandles(proc->PID);
+               tThread **next_ptr = &gpThreads;
+               for( tThread *thread = gpThreads; thread; thread = *next_ptr )
+               {
+                       if( thread->Process == proc ) {
+                               Threads_int_Destroy(thread);
+                       }
+                       else {
+                               next_ptr = &thread->Next;
+                       }
+               }
+       }
+       else
+       {
+               // Just a lowly thread, remove from process
+               Threads_int_Destroy(us);
+       }
+       
+       if( proc->nThreads == 0 )
+       {
+               free(proc->Chroot);
+               free(proc->CWD);
+               free(proc);
+       }
+}
+
+tUID Threads_GetUID() { return gpCurrentThread->Process->UID; }
+tGID Threads_GetGID() { return gpCurrentThread->Process->GID; }
 tTID Threads_GetTID() { return gpCurrentThread->TID; }
-tPID Threads_GetPID() { return gpCurrentThread->PID; }
+tPID Threads_GetPID() { return gpCurrentThread->Process->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;
        }
        
-       gpCurrentThread->UID = NewUID;
+       gpCurrentThread->Process->UID = NewUID;
        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;
        }
        
-       gpCurrentThread->GID = NewGID;
+       gpCurrentThread->Process->GID = 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; }
+static tProcess *proc(tProcess *Proc) { return Proc ? Proc : gpCurrentThread->Process; }
+char **Threads_GetCWD   (tProcess *Proc) { return &proc(Proc)->CWD; }
+char **Threads_GetChroot(tProcess *Proc) { return &proc(Proc)->Chroot; }
+int *Threads_GetMaxFD   (tProcess *Proc) { return &proc(Proc)->MaxFD; };
 
-int Threads_WaitTID(int TID, int *Status)
+tTID Threads_WaitTID(int TID, int *Status)
 {
        // Any Child
        if(TID == -1) {
@@ -184,14 +225,15 @@ int Threads_WaitTID(int TID, int *Status)
        }
        
        // Specific Thread
-       if(TID > 0) {
-               
+       if(TID > 0)
+       {
                tThread *thread = Threads_GetThread(TID);
                tThread *us = gpCurrentThread;
                if(!thread)     return -1;
                
                us->Next = NULL;
-               us->State = 3;
+               us->Status = THREAD_STAT_WAITING;
+               // TODO: Locking
                if(thread->WaitingThreadsEnd)
                {
                        thread->WaitingThreadsEnd->Next = us;
@@ -203,10 +245,9 @@ int Threads_WaitTID(int TID, int *Status)
                        thread->WaitingThreadsEnd = us;
                }
                
-               while(thread->State != 0)
-                       pause();
+               Threads_WaitEvents( THREAD_EVENT_WAKEUP );
                
-               if(Status)      *Status = thread->ExitStatus;
+               if(Status)      *Status = thread->RetStatus;
                thread->WaitingThreads = thread->WaitingThreads->Next;
                us->Next = NULL;
                
@@ -216,41 +257,44 @@ int Threads_WaitTID(int TID, int *Status)
        return 0;
 }
 
-void Threads_Sleep(void)
-{
-       // TODO: Add to a sleeping queue
-       pause();
-}
-
-void Threads_Yield(void)
-{
-//     yield();
-}
-
 void Threads_Exit(int TID, int Status)
 {
        tThread *toWake;
        
 //     VFS_Handles_Cleanup();
+
+       gpCurrentThread->RetStatus = 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 )
+                       Threads_Glue_Yield();
+       }
        #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)
-                       SDL_Delay(10);
+                       Threads_Glue_Yield();
        }
 }
 
+void Threads_Sleep()
+{
+       gpCurrentThread->Status = THREAD_STAT_SLEEPING;
+       Threads_int_WaitForStatusEnd(THREAD_STAT_SLEEPING);
+}
+
 int Threads_Wake(tThread *Thread)
 {
-       kill( Thread->KernelTID, SIGUSR1 );
+       Thread->Status = THREAD_STAT_ACTIVE;
+       Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP);
        return 0;
 }
 
@@ -264,90 +308,119 @@ int Threads_WakeTID(tTID TID)
 
 int Threads_CreateRootProcess(void)
 {
-       tThread *thread = Threads_CloneTCB(&gThreadZero);
-       thread->PID = thread->TID;
+       tThread *thread = Threads_CloneTCB(&gThreadZero, true);
        
        // Handle list is created on first open
        
-       return thread->PID;
+       return thread->Process->PID;
 }
 
 int Threads_Fork(void)
 {
-       tThread *thread = Threads_CloneTCB(gpCurrentThread);
-       thread->PID = thread->TID;
+       tThread *thread = Threads_CloneTCB(gpCurrentThread, true);
+
        // Duplicate the VFS handles (and nodes) from vfs_handle.c
+       VFS_CloneHandleList(thread->Process->PID);
        
-       VFS_CloneHandleList(thread->PID);
-       
-       return thread->PID;
+       return thread->Process->PID;
 }
 
-int Mutex_Acquire(tMutex *Mutex)
+int Threads_Spawn(int nFD, int FDs[], struct s_sys_spawninfo *info)
 {
-       if(!Mutex->Protector.IsValid) {
-               pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
-               Mutex->Protector.IsValid = 1;
+       tThread *thread = Threads_CloneTCB(gpCurrentThread, true);
+       if( info )
+       {
+               // TODO: PGID?
+               //if( info->flags & SPAWNFLAG_NEWPGID )
+               //      thread->PGID = thread->PID;
+               if( thread->Process->UID == 0 )
+               {
+                       if( info->gid )
+                               thread->Process->GID = info->gid;
+                       if( info->uid )
+                               thread->Process->UID = info->uid;
+               }
        }
-       pthread_mutex_lock( &Mutex->Protector.Mutex );
-       return 0;
+       
+       VFS_CloneHandlesFromList(thread->Process->PID, nFD, FDs);
+
+       return thread->Process->PID;
 }
 
-void Mutex_Release(tMutex *Mutex)
+// ----
+// ----
+void Threads_int_Terminate(tThread *Thread)
 {
-       pthread_mutex_unlock( &Mutex->Protector.Mutex );
+       Thread->RetStatus = -1;
+       Threads_AddActive(Thread);
 }
 
-void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name)
+void Threads_int_WaitForStatusEnd(enum eThreadStatus Status)
 {
-       memset(Sem, 0, sizeof(tSemaphore));
-       // HACK: Use `Sem->Protector` as space for the semaphore pointer
-       *(void**)(&Sem->Protector) = SDL_CreateSemaphore(InitValue);
+       tThread *us = Proc_GetCurThread();
+       ASSERT(Status != THREAD_STAT_ACTIVE);
+       ASSERT(Status != THREAD_STAT_DEAD);
+       LOG("%i(%s) - %i", us->TID, us->ThreadName, Status);
+       while( us->Status == Status )
+       {
+               if( Threads_Glue_SemWait(gpCurrentThread->EventSem, INT_MAX) == -1 ) {
+                       Log_Warning("Threads", "Wait on eventsem of %p, %p failed",
+                               gpCurrentThread, gpCurrentThread->EventSem);
+                       return ;
+               }
+               if( us->Status == Status )
+                       Log_Warning("Threads", "Thread %p(%i %s) rescheduled while in %s state",
+                               us, us->TID, us->ThreadName, casTHREAD_STAT[Status]);
+       }
 }
 
-int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
+int Threads_int_Sleep(enum eThreadStatus Status, void *Ptr, int Num, tThread **ListHead, tThread **ListTail, tShortSpinlock *Lock)
 {
-       SDL_SemWait( *(void**)(&Sem->Protector) );
-       return 1;
+       tThread *us = Proc_GetCurThread();
+       us->Next = NULL;
+       // - Mark as sleeping
+       us->Status = Status;
+       us->WaitPointer = Ptr;
+       us->RetStatus = Num;    // Use RetStatus as a temp variable
+               
+       // - Add to waiting
+       if( ListTail ) {
+               if(*ListTail) {
+                       (*ListTail)->Next = us;
+               }
+               else {
+                       *ListHead = us;
+               }
+               *ListTail = us;
+       }
+       else {
+               *ListHead = us;
+       }
+       
+       if( Lock ) {
+               SHORTREL( Lock );
+       }
+       Threads_int_WaitForStatusEnd(Status);
+       us->WaitPointer = NULL;
+       return us->RetStatus;
 }
 
-int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
+void Threads_AddActive(tThread *Thread)
 {
-        int    i;
-       for( i = 0; i < AmmountToAdd; i ++ )
-               SDL_SemPost( *(void**)(&Sem->Protector) );
-       return AmmountToAdd;
+       LOG("%i(%s)", Thread->TID, Thread->ThreadName);
+       Thread->Status = THREAD_STAT_ACTIVE;
+       Threads_Glue_SemSignal(Thread->EventSem, 1);
 }
 
-#if 0
-void Threads_Sleep()
+// --------------------------------------------------------------------
+// Signals
+// --------------------------------------------------------------------
+void Threads_PostSignal(int SigNum)
 {
-       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 ;
-       }
+       Log_Error("Threads", "TODO: %s", __func__);
 }
-
-int SaveState(tState *To)
+void Threads_SignalGroup(tPGID PGID, int SignalNum)
 {
-       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));
+       Log_Error("Threads", "TODO: %s", __func__);
 }
-#endif
 

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