Fixed correctness in stdio, minor changes in ls to handle dirs with negative sizes
[tpg/acess2.git] / Kernel / threads.c
index 54438c3..dd7cfcd 100644 (file)
@@ -5,11 +5,17 @@
  */
 #include <common.h>
 #include <threads.h>
+#include <errno.h>
 
 // === CONSTANTS ===
 #define        DEFAULT_QUANTUM 10
 #define        DEFAULT_TICKETS 5
 #define MAX_TICKETS            10
+const enum eConfigTypes        cCONFIG_TYPES[] = {
+       CFGT_HEAPSTR,   // CFG_VFS_CWD
+       CFGT_INT,       // CFG_VFS_MAXFILES
+       CFGT_NULL
+};
 
 // === IMPORTS ===
 extern void    ArchThreads_Init();
@@ -22,6 +28,7 @@ void  Threads_Init();
 void   Threads_SetName(char *NewName);
 char   *Threads_GetName(int ID);
 void   Threads_SetTickets(int Num);
+tThread        *Threads_CloneTCB(Uint *Err, Uint Flags);
  int   Threads_WaitTID(int TID, int *status);
 tThread        *Threads_GetThread(Uint TID);
 void   Threads_AddToDelete(tThread *Thread);
@@ -148,6 +155,88 @@ void Threads_SetTickets(int Num)
        RELEASE( &giThreadListLock );
 }
 
+/**
+ * \fn tThread *Threads_CloneTCB(Uint *Err, Uint Flags)
+ */
+tThread *Threads_CloneTCB(Uint *Err, Uint Flags)
+{
+       tThread *cur, *new;
+        int    i;
+       cur = Proc_GetCurThread();
+       
+       new = malloc(sizeof(tThread));
+       if(new == NULL) {
+               *Err = -ENOMEM;
+               return NULL;
+       }
+       
+       new->Next = NULL;
+       new->IsLocked = 0;
+       new->Status = THREAD_STAT_ACTIVE;
+       new->RetStatus = 0;
+       
+       // Get Thread ID
+       new->TID = giNextTID++;
+       new->PTID = cur->TID;
+       
+       // Clone Name
+       new->ThreadName = malloc(strlen(cur->ThreadName)+1);
+       strcpy(new->ThreadName, cur->ThreadName);
+       
+       // Set Thread Group ID (PID)
+       if(Flags & CLONE_VM)
+               new->TGID = new->TID;
+       else
+               new->TGID = cur->TGID;
+       
+       // Messages are not inherited
+       new->Messages = NULL;
+       new->LastMessage = NULL;
+       
+       // Set State
+       new->Remaining = new->Quantum = cur->Quantum;
+       new->NumTickets = cur->NumTickets;
+       
+       // Set Signal Handlers
+       new->CurSignal = 0;
+       if(Flags & CLONE_VM)
+               memset(new->SignalHandlers, 0, sizeof(new->SignalHandlers));
+       else
+               memcpy(new->SignalHandlers, cur->SignalHandlers, sizeof(new->SignalHandlers));
+       memset(&new->SignalState, 0, sizeof(tTaskState));
+       
+       for( i = 0; i < NUM_CFG_ENTRIES; i ++ )
+       {
+               switch(cCONFIG_TYPES[i])
+               {
+               default:
+                       new->Config[i] = cur->Config[i];
+                       break;
+               case CFGT_HEAPSTR:
+                       if(cur->Config[i])
+                               new->Config[i] = (Uint) strdup( (void*)cur->Config[i] );
+                       else
+                               new->Config[i] = 0;
+                       break;
+               }
+       }
+       
+       return new;
+}
+
+/**
+ * \fn Uint *Threads_GetCfgPtr(int Id)
+ */
+Uint *Threads_GetCfgPtr(int Id)
+{
+       if(Id < 0 || Id >= NUM_CFG_ENTRIES) {
+               Warning("Threads_GetCfgPtr: Index %i is out of bounds", Id);
+               return NULL;
+       }
+       
+       return &Proc_GetCurThread()->Config[Id];
+}
+
 /**
  * \fn void Threads_WaitTID(int TID, int *status)
  * \brief Wait for a task to change state
@@ -176,7 +265,12 @@ int Threads_WaitTID(int TID, int *status)
                tThread *t = Threads_GetThread(TID);
                 int    initStatus = t->Status;
                 int    ret;
-               while(t->Status == initStatus)  Threads_Yield();
+               
+               if(initStatus != THREAD_STAT_ZOMBIE)
+                       while(t->Status == initStatus) {
+                               Threads_Yield();
+                       }
+               
                ret = t->RetStatus;
                switch(t->Status)
                {
@@ -273,6 +367,7 @@ void Threads_Exit(int TID, int Status)
                Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
        else
                Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF );
+       for(;;) HALT(); // Just in case
 }
 
 /**
@@ -344,6 +439,9 @@ void Threads_Kill(tThread *Thread, int Status)
        // Release spinlocks
        RELEASE( &Thread->IsLocked );   // Released first so that it IS released
        RELEASE( &giThreadListLock );
+       
+       //Log("Thread %i went *hurk*", Thread->TID);
+       
        if(Status != -1)        HALT();
 }
 
@@ -366,7 +464,7 @@ void Threads_Sleep()
        tThread *cur = Proc_GetCurThread();
        tThread *thread;
        
-       Log("Proc_Sleep: %i going to sleep", cur->TID);
+       //Log("Proc_Sleep: %i going to sleep", cur->TID);
        
        // Acquire Spinlock
        LOCK( &giThreadListLock );
@@ -408,7 +506,7 @@ void Threads_Sleep()
 }
 
 
-/**
+/**c0108919:
  * \fn void Threads_Wake( tThread *Thread )
  * \brief Wakes a sleeping/waiting thread up
  */
@@ -559,9 +657,10 @@ tThread *Threads_GetNextToRun(int CPU)
         int    ticket;
         int    number;
        
+       if(giNumActiveThreads == 0)     return NULL;
+       
        // Special case: 1 thread
-       if(giNumActiveThreads == 1)
-       {
+       if(giNumActiveThreads == 1) {
                return gActiveThreads;
        }
        
@@ -599,6 +698,6 @@ tThread *Threads_GetNextToRun(int CPU)
 void Threads_SegFault(tVAddr Addr)
 {
        //Threads_SendSignal( Proc_GetCurThread()->TID, SIGSEGV );
-       Log("Thread #%i committed a segfault at address %p\n", Proc_GetCurThread()->TID, Addr);
-       Threads_Exit( 0, 0 );
+       Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr);
+       Threads_Exit( 0, -1 );
 }

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