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];
48 __thread tThread *gpCurrentThread;
51 tThread *Threads_GetThread(int TID)
54 for( thread = gpThreads; thread; thread = thread->Next )
56 if( thread->TID == TID )
62 tUID Threads_GetUID() { return gpCurrentThread->UID; }
63 tGID Threads_GetGID() { return gpCurrentThread->GID; }
64 tTID Threads_GetTID() { return gpCurrentThread->TID; }
65 tPID Threads_GetPID() { return gpCurrentThread->PID; }
67 Uint *Threads_GetCfgPtr(int Index)
69 return &gpCurrentThread->Config[Index];
72 void Threads_Sleep(void)
77 void Threads_Yield(void)
82 int Threads_WakeTID(tTID TID)
85 thread = Threads_GetThread(TID);
86 if( !thread ) return -1;
87 kill( thread->KernelTID, SIGUSR1 );
91 void Mutex_Acquire(tMutex *Mutex)
93 if(!Mutex->Protector.IsValid) {
94 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
95 Mutex->Protector.IsValid = 1;
97 pthread_mutex_lock( &Mutex->Protector.Mutex );
100 void Mutex_Release(tMutex *Mutex)
102 pthread_mutex_unlock( &Mutex->Protector.Mutex );
108 gpCurrentThread->State = 3;
109 if( setjmp(&gpCurrentThread->CurState) == 0 ) {
110 // Return to user wait
111 // Hmm... maybe I should have a "kernel" thread for every "user" thread
114 // Just woken up, return
119 int SaveState(tState *To)
122 __asm__ __volatile__(
128 // If we just returned
132 __asm__ __volatile__ ("mov %%esp, %1" : "=r"(To->SP));
133 __asm__ __volatile__ ("mov %%ebp, %1" : "=r"(To->BP));