+/**
+ * \fn tThread *Threads_CloneTCB(Uint *Err, Uint Flags)
+ * \brief Clone the TCB of the current thread
+ */
+tThread *Threads_CloneThreadZero(void)
+{
+ tThread *cur, *new;
+ int i;
+ cur = Proc_GetCurThread();
+
+ // Allocate and duplicate
+ new = malloc(sizeof(tThread));
+ if(new == NULL) {
+ return NULL;
+ }
+ memcpy(new, &gThreadZero, sizeof(tThread));
+
+ new->CurCPU = -1;
+ new->Next = NULL;
+ memset( &new->IsLocked, 0, sizeof(new->IsLocked));
+ new->Status = THREAD_STAT_PREINIT;
+ new->RetStatus = 0;
+
+ // Get Thread ID
+ new->TID = giNextTID++;
+ new->Parent = 0;
+
+ // Clone Name
+ new->ThreadName = NULL;
+
+ // Messages are not inherited
+ new->Messages = NULL;
+ new->LastMessage = NULL;
+
+ // Set State
+ new->Remaining = new->Quantum = cur->Quantum;
+ new->Priority = cur->Priority;
+
+ // Set Signal Handlers
+ new->CurFaultNum = 0;
+ new->FaultHandler = cur->FaultHandler;
+
+ 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;
+ }
+ }
+
+ // Maintain a global list of threads
+ SHORTLOCK( &glThreadListLock );
+ new->GlobalPrev = NULL; // Protect against bugs
+ new->GlobalNext = gAllThreads;
+ gAllThreads->GlobalPrev = new;
+ gAllThreads = new;
+ SHORTREL( &glThreadListLock );
+
+ return new;
+}
+