2 * Acess2 libnative (Kernel Simulation Library)
3 * - By John Hodge (thePowersGang)
10 #include <threads_int.h>
14 void Threads_int_Init(void) __attribute__((constructor(101)));
15 tThread *Threads_int_CreateTCB(tThread *Parent);
18 tThread *gThreads_List;
19 tThread __thread *lpThreads_This;
20 int giThreads_NextTID = 1;
21 tShortSpinlock glThreadListLock;
24 void Threads_int_Init(void)
26 if( !lpThreads_This ) {
27 lpThreads_This = Threads_int_CreateTCB(NULL);
28 Threads_SetName("ThreadZero");
32 tThread *Proc_GetCurThread(void)
34 return lpThreads_This;
37 tUID Threads_GetUID(void) { return 0; }
38 tGID Threads_GetGID(void) { return 0; }
40 tTID Threads_GetTID(void) { return lpThreads_This ? lpThreads_This->TID : 0; }
42 int *Threads_GetMaxFD(void) { return &lpThreads_This->Process->MaxFDs; }
43 char **Threads_GetCWD(void) { return &lpThreads_This->Process->CWD; }
44 char **Threads_GetChroot(void) { return &lpThreads_This->Process->Chroot; }
45 void **Threads_GetHandlesPtr(void) { return &lpThreads_This->Process->Handles; }
47 void Threads_Yield(void)
49 Log_KernelPanic("Threads", "Threads_Yield DEFINITELY shouldn't be used (%p)",
50 __builtin_return_address(0));
53 void Threads_Sleep(void)
55 Log_Warning("Threads", "Threads_Sleep shouldn't be used");
58 int Threads_SetName(const char *Name)
63 if( lpThreads_This->ThreadName )
64 free(lpThreads_This->ThreadName);
65 lpThreads_This->ThreadName = strdup(Name);
70 int *Threads_GetErrno(void) __attribute__ ((weak));
72 int *Threads_GetErrno(void)
78 tThread *Threads_RemActive(void)
80 return lpThreads_This;
83 void Threads_AddActive(tThread *Thread)
85 Thread->Status = THREAD_STAT_ACTIVE;
86 // Increment state-change semaphore
87 Threads_int_SemSignal(Thread->WaitSemaphore);
90 struct sProcess *Threads_int_CreateProcess(void)
92 struct sProcess *ret = calloc(sizeof(struct sProcess), 1);
99 tThread *Threads_int_CreateTCB(tThread *Parent)
101 tThread *ret = calloc( sizeof(tThread), 1 );
102 ret->TID = giThreads_NextTID ++;
103 ret->WaitSemaphore = Threads_int_SemCreate();
104 //ret->Protector = Threads_int_MutexCreate();
108 ret->Process = Threads_int_CreateProcess();
111 ret->Process = Parent->Process;
113 ret->ProcNext = ret->Process->Threads;
114 ret->Process->Threads = ret;
116 ret->Next = gThreads_List;
122 struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
124 if( !Threads_int_ThreadingEnabled() )
126 Log_Error("Threads", "Multithreading is disabled in this build");
131 tThread *ret = Threads_int_CreateTCB(lpThreads_This);
133 ret->SpawnData = Data;
134 Threads_int_CreateThread(ret);
139 void Threads_int_WaitForStatusEnd(enum eThreadStatus Status)
141 tThread *us = Proc_GetCurThread();
142 assert(Status != THREAD_STAT_ACTIVE);
143 assert(Status != THREAD_STAT_DEAD);
144 LOG("%i(%s) - %i", us->TID, us->ThreadName, Status);
145 while( us->Status == Status )
147 Threads_int_SemWaitAll(us->WaitSemaphore);
148 if( us->Status == Status )
149 Log_Warning("Threads", "Thread %p(%i %s) rescheduled while in %s state",
150 us, us->TID, us->ThreadName, casTHREAD_STAT[Status]);
154 int Threads_int_Sleep(enum eThreadStatus Status, void *Ptr, int Num, tThread **ListHead, tThread **ListTail, tShortSpinlock *Lock)
156 tThread *us = Proc_GetCurThread();
158 // - Mark as sleeping
160 us->WaitPointer = Ptr;
161 us->RetStatus = Num; // Use RetStatus as a temp variable
166 (*ListTail)->Next = us;
174 us->Next = *ListHead;
184 Threads_int_WaitForStatusEnd(Status);
185 us->WaitPointer = NULL;
186 return us->RetStatus;