+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; }