3 * - Acess kernel emulation on another OS using SDL and UDP
6 * - Thread and process handling
9 #undef CLONE_VM // Such a hack
13 #include "/usr/include/signal.h"
24 typedef struct sThread
33 struct sThread *Parent;
37 int State; // 0: Dead, 1: Active, 2: Paused, 3: Asleep
43 Uint Config[NUM_CFG_ENTRIES];
47 tThread gThread_Zero = {
49 ThreadName: "ThreadZero"
51 tThread *gpThreads = &gThread_Zero;
52 __thread tThread *gpCurrentThread = &gThread_Zero;
55 tThread *Threads_GetThread(int TID)
58 for( thread = gpThreads; thread; thread = thread->Next )
60 if( thread->TID == TID )
66 tUID Threads_GetUID() { return gpCurrentThread->UID; }
67 tGID Threads_GetGID() { return gpCurrentThread->GID; }
68 tTID Threads_GetTID() { return gpCurrentThread->TID; }
69 tPID Threads_GetPID() { return gpCurrentThread->PID; }
71 Uint *Threads_GetCfgPtr(int Index)
73 if( Index < 0 || Index >= NUM_CFG_ENTRIES )
75 if( !gpCurrentThread )
77 return &gpCurrentThread->Config[Index];
80 void Threads_Sleep(void)
85 void Threads_Yield(void)
90 int Threads_WakeTID(tTID TID)
93 thread = Threads_GetThread(TID);
94 if( !thread ) return -1;
95 kill( thread->KernelTID, SIGUSR1 );
99 void Mutex_Acquire(tMutex *Mutex)
101 if(!Mutex->Protector.IsValid) {
102 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
103 Mutex->Protector.IsValid = 1;
105 pthread_mutex_lock( &Mutex->Protector.Mutex );
108 void Mutex_Release(tMutex *Mutex)
110 pthread_mutex_unlock( &Mutex->Protector.Mutex );
116 gpCurrentThread->State = 3;
117 if( setjmp(&gpCurrentThread->CurState) == 0 ) {
118 // Return to user wait
119 // Hmm... maybe I should have a "kernel" thread for every "user" thread
122 // Just woken up, return
127 int SaveState(tState *To)
130 __asm__ __volatile__(
136 // If we just returned
140 __asm__ __volatile__ ("mov %%esp, %1" : "=r"(To->SP));
141 __asm__ __volatile__ ("mov %%ebp, %1" : "=r"(To->BP));