5f29f7601346a2fabb3ea2383d97574c32aad339
[tpg/acess2.git] / AcessNative / acesskernel_src / threads.c
1 /*
2  * Acess2 Native Kernel
3  * - Acess kernel emulation on another OS using SDL and UDP
4  *
5  * threads.c
6  * - Thread and process handling
7  */
8 #define _SIGNAL_H_
9 #undef CLONE_VM // Such a hack
10 #include <acess.h>
11 #include <unistd.h>
12 #include <stdint.h>
13 #include "/usr/include/signal.h"
14
15 // === STRUCTURES ===
16 #if 0
17 typedef struct sState
18 {
19         void    *CurState;
20         Uint    SP, BP, IP;
21 }       tState;
22 #endif
23
24 typedef struct sThread
25 {
26         struct sThread  *Next;
27
28          int    KernelTID;
29
30         tTID    TID, PID;
31         tUID    UID, GID;
32
33         struct sThread  *Parent;
34
35         char    *ThreadName;
36
37          int    State;  // 0: Dead, 1: Active, 2: Paused, 3: Asleep
38         #if 0
39         tState  CurState;
40         #endif
41
42         // Config?
43         Uint    Config[NUM_CFG_ENTRIES];
44 }       tThread;
45
46 // === GLOBALS ===
47 tThread *gpThreads;
48 __thread tThread        *gpCurrentThread;
49
50 // === CODE ===
51 tThread *Threads_GetThread(int TID)
52 {
53         tThread *thread;
54         for( thread = gpThreads; thread; thread = thread->Next )
55         {
56                 if( thread->TID == TID )
57                         return thread;
58         }
59         return NULL;
60 }
61
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; }
66
67 Uint *Threads_GetCfgPtr(int Index)
68 {
69         return &gpCurrentThread->Config[Index];
70 }
71
72 void Threads_Sleep(void)
73 {
74         pause();
75 }
76
77 void Threads_Yield(void)
78 {
79 //      yield();
80 }
81
82 int Threads_WakeTID(tTID TID)
83 {
84         tThread *thread;
85         thread = Threads_GetThread(TID);
86         if( !thread )   return -1;
87         kill( thread->KernelTID, SIGUSR1 );
88         return 0;
89 }
90
91 void Mutex_Acquire(tMutex *Mutex)
92 {
93         if(!Mutex->Protector.IsValid) {
94                 pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
95                 Mutex->Protector.IsValid = 1;
96         }
97         pthread_mutex_lock( &Mutex->Protector.Mutex );
98 }
99
100 void Mutex_Release(tMutex *Mutex)
101 {
102         pthread_mutex_unlock( &Mutex->Protector.Mutex );
103 }
104
105 #if 0
106 void Threads_Sleep()
107 {
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
112         }
113         else {
114                 // Just woken up, return
115                 return ;
116         }
117 }
118
119 int SaveState(tState *To)
120 {
121         Uint    ip;
122         __asm__ __volatile__(
123                 "call 1f;\n\t"
124                 "1f:\n\t"
125                 "pop %%eax"
126                 : "=a" (ip)
127                 : );
128         // If we just returned
129         if(!ip) return 1;
130
131         To->IP = ip;
132         __asm__ __volatile__ ("mov %%esp, %1" : "=r"(To->SP));
133         __asm__ __volatile__ ("mov %%ebp, %1" : "=r"(To->BP));
134 }
135 #endif
136

UCC git Repository :: git.ucc.asn.au