+tThread *Threads_RemActive(void)
+{
+ return lpThreads_This;
+}
+
+void Threads_AddActive(tThread *Thread)
+{
+ Thread->Status = THREAD_STAT_ACTIVE;
+ // Increment state-change semaphore
+ LOG("Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
+ Threads_int_SemSignal(Thread->WaitSemaphore);
+}
+
+struct sProcess *Threads_int_CreateProcess(void)
+{
+ struct sProcess *ret = calloc(sizeof(struct sProcess), 1);
+
+ ret->MaxFDs = 32;
+
+ return ret;
+}
+
+tThread *Threads_int_CreateTCB(tThread *Parent)
+{
+ tThread *ret = calloc( sizeof(tThread), 1 );
+ ret->TID = giThreads_NextTID ++;
+ ret->WaitSemaphore = Threads_int_SemCreate();
+ //ret->Protector = Threads_int_MutexCreate();
+
+ if( !Parent )
+ {
+ ret->Process = Threads_int_CreateProcess();
+ }
+ else
+ ret->Process = Parent->Process;
+
+ ret->ProcNext = ret->Process->Threads;
+ ret->Process->Threads = ret;
+
+ ret->Next = gThreads_List;
+ gThreads_List = ret;
+
+ return ret;
+}
+
+struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
+{
+ if( !Threads_int_ThreadingEnabled() )
+ {
+ Log_Error("Threads", "Multithreading is disabled in this build");
+ return NULL;
+ }
+ else
+ {
+ tThread *ret = Threads_int_CreateTCB(lpThreads_This);
+ ret->SpawnFcn = Fcn;
+ ret->SpawnData = Data;
+ Threads_int_CreateThread(ret);
+ return ret;
+ }
+}
+
+void Threads_int_WaitForStatusEnd(enum eThreadStatus Status)
+{
+ 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 )
+ {
+ Threads_int_SemWaitAll(us->WaitSemaphore);
+ if( us->Status == Status )
+ Log_Warning("Threads", "Thread %p(%i %s) rescheduled while in %s state",
+ us, us->TID, us->ThreadName, casTHREAD_STAT[Status]);
+ }
+ LOG("%p(%i %s) Awake", us, us->TID, us->ThreadName);
+}
+
+int Threads_int_Sleep(enum eThreadStatus Status, void *Ptr, int Num, tThread **ListHead, tThread **ListTail, tShortSpinlock *Lock)
+{
+ 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 if(ListHead) {
+ us->Next = *ListHead;
+ *ListHead = us;
+ }
+ else {
+ // nothing
+ }
+
+ if( Lock ) {
+ SHORTREL( Lock );
+ }
+ Threads_int_WaitForStatusEnd(Status);
+ us->WaitPointer = NULL;
+ return us->RetStatus;
+}
+