#include <mutex.h>
#include <semaphore.h>
#include <events.h>
+#include <threads_int.h>
#undef CLONE_VM // Such a hack
#undef off_t
} tState;
#endif
-typedef struct sProcess
-{
- int nThreads;
- int NativePID;
- char *CWD;
- char *Chroot;
- int MaxFD;
-} tProcess;
-
-typedef struct sThread
-{
- struct sThread *GlobalNext;
- struct sThread *Next;
-
- int KernelTID;
-
- tTID TID, PID;
- tUID UID, GID;
-
- struct sThread *Parent;
-
- char *ThreadName;
-
- int State; // 0: Dead, 1: Active, 2: Paused, 3: Asleep
- int ExitStatus;
- int _errno;
-
- // Threads waiting for this thread to exit.
- // Quit logic:
- // - Wait for `WaitingThreads` to be non-null (maybe?)
- // - Wake first in the queue, wait for it to be removed
- // - Repeat
- // - Free thread and quit kernel thread
- struct sThread *WaitingThreads;
- struct sThread *WaitingThreadsEnd;
-
- tProcess *Process;
-
- Uint32 Events, WaitMask;
- SDL_sem *EventSem;
-
-} tThread;
-
// === PROTOTYPES ===
int Threads_Wake(tThread *Thread);
.MaxFD = 100
};
tThread gThreadZero = {
- .State=1,
+ .Status=THREAD_STAT_ACTIVE,
.ThreadName="ThreadZero",
.Process = &gProcessZero
};
Log_Error("Threads", "_SetThread - Thread %i is not on global list", TID);
}
-tThread *Threads_GetThread(int TID)
+tThread *Threads_GetThread(Uint TID)
{
tThread *thread;
for( thread = gpThreads; thread; thread = thread->GlobalNext )
{
- if( thread->TID == TID )
+ if( thread->TID == TID ) {
return thread;
+ }
}
return NULL;
}
if(!thread) return -1;
us->Next = NULL;
- us->State = 3;
+ us->Status = THREAD_STAT_WAITING;
// TODO: Locking
if(thread->WaitingThreadsEnd)
{
int Threads_Wake(tThread *Thread)
{
- Thread->State = 0;
+ Thread->Status = THREAD_STAT_ACTIVE;
Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP);
return 0;
}
return thread->PID;
}
+// --------------------------------------------------------------------
+// Mutexes
+// --------------------------------------------------------------------
int Mutex_Acquire(tMutex *Mutex)
{
if(!Mutex->Protector.IsValid) {
pthread_mutex_unlock( &Mutex->Protector.Mutex );
}
+// --------------------------------------------------------------------
+// Semaphores
+// --------------------------------------------------------------------
void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name)
{
memset(Sem, 0, sizeof(tSemaphore));
return AmmountToAdd;
}
+// --------------------------------------------------------------------
+// Event handling
+// --------------------------------------------------------------------
Uint32 Threads_WaitEvents(Uint32 Mask)
{
Uint32 rv;
}
}
+void Threads_ClearEvent(Uint32 EventMask)
+{
+ gpCurrentThread->Events &= ~EventMask;
+}
+