Kernel - Added (possibly buggy) support for WaitTID(TID>0)
[tpg/acess2.git] / KernelLand / Kernel / threads.c
index e22994b..a8697d6 100644 (file)
@@ -11,6 +11,7 @@
 #include <hal_proc.h>
 #include <semaphore.h>
 #include <vfs_threads.h>       // VFS Handle maintainence
+#include <events.h>
 
 // Configuration
 #define DEBUG_TRACE_TICKETS    0       // Trace ticket counts
@@ -73,6 +74,7 @@ tGID  Threads_GetGID(void);
  int   Threads_SetUID(Uint *Errno, tUID ID);
  int   Threads_SetGID(Uint *Errno, tUID ID);
 #endif
+void   Threads_int_DumpThread(tThread *thread);
 void   Threads_Dump(void);
 void   Threads_DumpActive(void);
 
@@ -210,7 +212,7 @@ int Threads_SetName(const char *NewName)
        if( IsHeap(oldname) )   free( oldname );        
        cur->ThreadName = strdup(NewName);
 
-       Log_Debug("Threads", "Thread renamed to '%s'", NewName);        
+//     Log_Debug("Threads", "Thread renamed to '%s'", NewName);        
 
        return 0;
 }
@@ -343,9 +345,16 @@ tThread *Threads_CloneTCB(Uint Flags)
                newproc->nThreads = 1;
                // Reference all handles in the VFS
                VFS_ReferenceUserHandles();
+
+               newproc->FirstThread = new;
+               new->ProcessNext = NULL;
        }
        else {
                new->Process->nThreads ++;
+               new->Process = cur->Process;
+               // TODO: Locking
+               new->ProcessNext = new->Process->FirstThread;
+               new->Process->FirstThread = new;
        }
        
        // Messages are not inherited
@@ -434,9 +443,30 @@ tThread *Threads_CloneThreadZero(void)
 tTID Threads_WaitTID(int TID, int *Status)
 {      
        // Any Child
-       if(TID == -1) {
-               Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
-               return -1;
+       if(TID == -1)
+       {
+               Uint32 ev = Threads_WaitEvents(THREAD_EVENT_DEADCHILD);
+               tTID    ret = -1;
+               if( ev & THREAD_EVENT_DEADCHILD )
+               {
+                       // A child died, get the TID
+                       tThread *us = Proc_GetCurThread();
+                       ASSERT(us->LastDeadChild);
+                       ret = us->LastDeadChild->TID;
+                       // - Mark as dead (as opposed to undead)
+                       ASSERT(us->LastDeadChild->Status == THREAD_STAT_ZOMBIE);
+                       us->LastDeadChild->Status = THREAD_STAT_DEAD;
+                       // - Set return status
+                       if(Status)
+                               *Status = us->LastDeadChild->RetStatus;
+                       us->LastDeadChild = NULL;
+                       Mutex_Release(&us->DeadChildLock);
+               }
+               else
+               {
+                       Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
+               }
+               return ret;
        }
        
        // Any peer/child thread
@@ -452,33 +482,13 @@ tTID Threads_WaitTID(int TID, int *Status)
        }
        
        // Specific Thread
-       if(TID > 0) {
-               tThread *t = Threads_GetThread(TID);
+       if(TID > 0)
+       {
                tTID    ret;
-               
-               // Wait for the thread to die!
-               // TODO: Handle child also being suspended if wanted
-               while(t->Status != THREAD_STAT_ZOMBIE) {
-                       Threads_Sleep();
-                       Log_Debug("Threads", "%i waiting for %i, t->Status = %i",
-                               Threads_GetTID(), t->TID, t->Status);
-               }
-               
-               // Set return status
-               ret = t->TID;
-               switch(t->Status)
+               while( (ret = Threads_WaitTID(-1, Status)) != TID )
                {
-               case THREAD_STAT_ZOMBIE:
-                       // Kill the thread
-                       t->Status = THREAD_STAT_DEAD;
-                       // TODO: Child return value?
-                       if(Status)      *Status = t->RetStatus;
-                       // add to delete queue
-                       Threads_Delete( t );
-                       break;
-               default:
-                       if(Status)      *Status = -1;
-                       break;
+                       if( ret == -1 )
+                               break;
                }
                return ret;
        }
@@ -496,15 +506,13 @@ tThread *Threads_GetThread(Uint TID)
        tThread *thread;
        
        // Search global list
-       for(thread = gAllThreads;
-               thread;
-               thread = thread->GlobalNext)
+       for( thread = gAllThreads; thread; thread = thread->GlobalNext )
        {
                if(thread->TID == TID)
                        return thread;
        }
 
-       Log("Unable to find TID %i on main list\n", TID);
+       Log_Notice("Threads", "Unable to find TID %i on main list\n", TID);
        
        return NULL;
 }
@@ -679,8 +687,10 @@ void Threads_Kill(tThread *Thread, int Status)
 
        Thread->Status = THREAD_STAT_ZOMBIE;
        SHORTREL( &glThreadListLock );
-       // TODO: Send something like SIGCHLD
-       Threads_Wake( Thread->Parent );
+       // TODO: It's possible that we could be timer-preempted here, should disable that... somehow
+       Mutex_Acquire( &Thread->Parent->DeadChildLock );        // released by parent
+       Thread->Parent->LastDeadChild = Thread;
+       Threads_PostEvent( Thread->Parent, THREAD_EVENT_DEADCHILD );
        
        Log("Thread %i went *hurk* (%i)", Thread->TID, Status);
        
@@ -759,11 +769,11 @@ int Threads_Wake(tThread *Thread)
                return -EALREADY;
        
        case THREAD_STAT_SLEEPING:
-               SHORTLOCK( &glThreadListLock );
                // Remove from sleeping queue
+               SHORTLOCK( &glThreadListLock );
                Threads_int_DelFromQueue(&gSleepingThreads, Thread);
-               
                SHORTREL( &glThreadListLock );
+               
                Threads_AddActive( Thread );
                
                #if DEBUG_TRACE_STATE
@@ -829,7 +839,7 @@ int Threads_Wake(tThread *Thread)
                return -ENOTIMPL;
        
        default:
-               Warning("Threads_Wake - Unknown process status (%i)\n", Thread->Status);
+               Log_Warning("Threads", "Threads_Wake - Unknown process status (%i)", Thread->Status);
                return -EINTERNAL;
        }
 }
@@ -1049,6 +1059,36 @@ char **Threads_GetCWD(void)
 }
 // ---
 
+void Threads_int_DumpThread(tThread *thread)
+{
+       Log(" %p %i (%i) - %s (CPU %i) - %i (%s)",
+               thread, thread->TID, thread->Process->PID, thread->ThreadName, thread->CurCPU,
+               thread->Status, casTHREAD_STAT[thread->Status]
+               );
+       switch(thread->Status)
+       {
+       case THREAD_STAT_MUTEXSLEEP:
+               Log("  Mutex Pointer: %p", thread->WaitPointer);
+               break;
+       case THREAD_STAT_SEMAPHORESLEEP:
+               Log("  Semaphore Pointer: %p", thread->WaitPointer);
+               Log("  Semaphore Name: %s:%s", 
+                       ((tSemaphore*)thread->WaitPointer)->ModName,
+                       ((tSemaphore*)thread->WaitPointer)->Name
+                       );
+               break;
+       case THREAD_STAT_ZOMBIE:
+               Log("  Return Status: %i", thread->RetStatus);
+               break;
+       default:        break;
+       }
+       Log("  Priority %i, Quantum %i", thread->Priority, thread->Quantum);
+       Log("  KStack %p", thread->KernelStack);
+       if( thread->bInstrTrace )
+               Log("  Tracing Enabled");
+       Proc_DumpThreadCPUState(thread);
+}
+
 /**
  * \fn void Threads_Dump(void)
  */
@@ -1071,16 +1111,10 @@ void Threads_DumpActive(void)
        #endif
                for(thread=list->Head;thread;thread=thread->Next)
                {
-                       Log(" %p %i (%i) - %s (CPU %i)",
-                               thread, thread->TID, thread->Process->PID, thread->ThreadName, thread->CurCPU);
+                       Threads_int_DumpThread(thread);
                        if(thread->Status != THREAD_STAT_ACTIVE)
                                Log("  ERROR State (%i) != THREAD_STAT_ACTIVE (%i)",
                                        thread->Status, THREAD_STAT_ACTIVE);
-                       Log("  Priority %i, Quantum %i", thread->Priority, thread->Quantum);
-                       Log("  KStack 0x%x", thread->KernelStack);
-                       if( thread->bInstrTrace )
-                               Log("  Tracing Enabled");
-                       Proc_DumpThreadCPUState(thread);
                }
        
        #if SCHEDULER_TYPE == SCHED_RR_PRI
@@ -1102,31 +1136,7 @@ void Threads_Dump(void)
        Log("All Threads:");
        for(thread=gAllThreads;thread;thread=thread->GlobalNext)
        {
-               Log(" %p %i (%i) - %s (CPU %i)",
-                       thread, thread->TID, thread->Process->PID, thread->ThreadName, thread->CurCPU);
-               Log("  State %i (%s)", thread->Status, casTHREAD_STAT[thread->Status]);
-               switch(thread->Status)
-               {
-               case THREAD_STAT_MUTEXSLEEP:
-                       Log("  Mutex Pointer: %p", thread->WaitPointer);
-                       break;
-               case THREAD_STAT_SEMAPHORESLEEP:
-                       Log("  Semaphore Pointer: %p", thread->WaitPointer);
-                       Log("  Semaphore Name: %s:%s", 
-                               ((tSemaphore*)thread->WaitPointer)->ModName,
-                               ((tSemaphore*)thread->WaitPointer)->Name
-                               );
-                       break;
-               case THREAD_STAT_ZOMBIE:
-                       Log("  Return Status: %i", thread->RetStatus);
-                       break;
-               default:        break;
-               }
-               Log("  Priority %i, Quantum %i", thread->Priority, thread->Quantum);
-               Log("  KStack 0x%x", thread->KernelStack);
-               if( thread->bInstrTrace )
-                       Log("  Tracing Enabled");
-               Proc_DumpThreadCPUState(thread);
+               Threads_int_DumpThread(thread);
        }
 }
 

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