2 * Acess2 libnative (Kernel Simulation Library)
3 * - By John Hodge (thePowersGang)
10 #include <threads_int.h>
13 void Threads_int_Init(void) __attribute__((constructor(101)));
14 tThread *Threads_int_CreateTCB(tThread *Parent);
17 tThread *gThreads_List;
18 tThread __thread *lpThreads_This;
19 int giThreads_NextTID = 1;
20 tShortSpinlock glThreadListLock;
23 void Threads_int_Init(void)
25 lpThreads_This = Threads_int_CreateTCB(NULL);
26 Threads_SetName("ThreadZero");
29 tThread *Proc_GetCurThread(void)
31 return lpThreads_This;
34 void Threads_PostEvent(tThread *Thread, Uint32 Events)
40 SHORTLOCK( &Thread->IsLocked );
41 Thread->PendingEvents |= Events;
42 if( Thread->WaitingEvents & Events )
43 Threads_int_SemSignal(Thread->WaitSemaphore);
44 SHORTREL( &Thread->IsLocked );
47 Uint32 Threads_WaitEvents(Uint32 Events)
49 if( !Threads_int_ThreadingEnabled() ) {
50 Log_Notice("Threads", "_WaitEvents: Threading disabled");
53 lpThreads_This->WaitingEvents = Events;
54 Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore);
55 lpThreads_This->WaitingEvents = 0;
56 Uint32 rv = lpThreads_This->PendingEvents;
60 void Threads_ClearEvent(Uint32 Mask)
62 if( !Threads_int_ThreadingEnabled() ) {
63 Log_Notice("Threads", "_ClearEvent: Threading disabled");
66 SHORTLOCK(&lpThreads_This->IsLocked);
67 lpThreads_This->PendingEvents &= ~Mask;
68 SHORTREL(&lpThreads_This->IsLocked);
71 tUID Threads_GetUID(void) { return 0; }
72 tGID Threads_GetGID(void) { return 0; }
74 tTID Threads_GetTID(void) { return lpThreads_This ? lpThreads_This->TID : 0; }
76 int *Threads_GetMaxFD(void) { return &lpThreads_This->Process->MaxFDs; }
77 char **Threads_GetCWD(void) { return &lpThreads_This->Process->CWD; }
78 char **Threads_GetChroot(void) { return &lpThreads_This->Process->Chroot; }
79 void **Threads_GetHandlesPtr(void) { return &lpThreads_This->Process->Handles; }
81 void Threads_Yield(void)
83 Log_KernelPanic("Threads", "Threads_Yield DEFINITELY shouldn't be used (%p)",
84 __builtin_return_address(0));
87 void Threads_Sleep(void)
89 Log_Warning("Threads", "Threads_Sleep shouldn't be used");
92 void Threads_int_WaitForStatusEnd(enum eThreadStatus Status)
94 while( lpThreads_This->Status != Status )
95 Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore);
98 int Threads_SetName(const char *Name)
100 if( !lpThreads_This )
103 if( lpThreads_This->ThreadName )
104 free(lpThreads_This->ThreadName);
105 lpThreads_This->ThreadName = strdup(Name);
110 int *Threads_GetErrno(void) __attribute__ ((weak));
112 int *Threads_GetErrno(void)
118 tThread *Threads_RemActive(void)
120 return lpThreads_This;
123 void Threads_AddActive(tThread *Thread)
125 Thread->Status = THREAD_STAT_ACTIVE;
126 // Increment state-change semaphore
127 Threads_int_SemSignal(Thread->WaitSemaphore);
130 struct sProcess *Threads_int_CreateProcess(void)
132 struct sProcess *ret = calloc(sizeof(struct sProcess), 1);
139 tThread *Threads_int_CreateTCB(tThread *Parent)
141 tThread *ret = calloc( sizeof(tThread), 1 );
142 ret->TID = giThreads_NextTID ++;
143 ret->WaitSemaphore = Threads_int_SemCreate();
144 //ret->Protector = Threads_int_MutexCreate();
148 ret->Process = Threads_int_CreateProcess();
151 ret->Process = Parent->Process;
153 ret->ProcNext = ret->Process->Threads;
154 ret->Process->Threads = ret;
156 ret->Next = gThreads_List;
162 struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
164 if( !Threads_int_ThreadingEnabled() )
166 Log_Error("Threads", "Multithreading is disabled in this build");
171 tThread *ret = Threads_int_CreateTCB(lpThreads_This);
173 ret->SpawnData = Data;
174 Threads_int_CreateThread(ret);