3 * - Acess kernel emulation on another OS using SDL and UDP
6 * - Thread and process handling
9 #undef CLONE_VM // Such a hack
12 #include <sys/types.h>
14 #include "/usr/include/signal.h"
25 typedef struct sThread
34 struct sThread *Parent;
38 int State; // 0: Dead, 1: Active, 2: Paused, 3: Asleep
44 Uint Config[NUM_CFG_ENTRIES];
48 tThread gThread_Zero = {
50 ThreadName: "ThreadZero"
52 tThread *gpThreads = &gThread_Zero;
53 __thread tThread *gpCurrentThread = &gThread_Zero;
56 tThread *Threads_GetThread(int TID)
59 for( thread = gpThreads; thread; thread = thread->Next )
61 if( thread->TID == TID )
67 tUID Threads_GetUID() { return gpCurrentThread->UID; }
68 tGID Threads_GetGID() { return gpCurrentThread->GID; }
69 tTID Threads_GetTID() { return gpCurrentThread->TID; }
70 tPID Threads_GetPID() {
71 return SDL_ThreadID();
72 //return gpCurrentThread->PID;
75 Uint *Threads_GetCfgPtr(int Index)
77 if( Index < 0 || Index >= NUM_CFG_ENTRIES )
79 if( !gpCurrentThread )
81 return &gpCurrentThread->Config[Index];
84 void Threads_Sleep(void)
89 void Threads_Yield(void)
94 int Threads_WakeTID(tTID TID)
97 thread = Threads_GetThread(TID);
98 if( !thread ) return -1;
99 kill( thread->KernelTID, SIGUSR1 );
103 void Mutex_Acquire(tMutex *Mutex)
105 if(!Mutex->Protector.IsValid) {
106 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
107 Mutex->Protector.IsValid = 1;
109 pthread_mutex_lock( &Mutex->Protector.Mutex );
112 void Mutex_Release(tMutex *Mutex)
114 pthread_mutex_unlock( &Mutex->Protector.Mutex );
120 gpCurrentThread->State = 3;
121 if( setjmp(&gpCurrentThread->CurState) == 0 ) {
122 // Return to user wait
123 // Hmm... maybe I should have a "kernel" thread for every "user" thread
126 // Just woken up, return
131 int SaveState(tState *To)
134 __asm__ __volatile__(
140 // If we just returned
144 __asm__ __volatile__ ("mov %%esp, %1" : "=r"(To->SP));
145 __asm__ __volatile__ ("mov %%ebp, %1" : "=r"(To->BP));