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;
21 void Threads_int_Init(void)
23 lpThreads_This = Threads_int_CreateTCB(NULL);
26 tThread *Proc_GetCurThread(void)
28 return lpThreads_This;
31 void Threads_PostEvent(tThread *Thread, Uint32 Events)
37 Threads_int_MutexLock(Thread->Protector);
38 Thread->PendingEvents |= Events;
39 if( Thread->WaitingEvents & Events )
40 Threads_int_SemSignal(Thread->WaitSemaphore);
41 Threads_int_MutexRelease(Thread->Protector);
44 Uint32 Threads_WaitEvents(Uint32 Events)
46 if( Threads_int_ThreadingEnabled() ) {
47 Log_Notice("Threads", "_WaitEvents: Threading disabled");
50 lpThreads_This->WaitingEvents = Events;
51 Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore);
52 lpThreads_This->WaitingEvents = 0;
53 Uint32 rv = lpThreads_This->PendingEvents;
57 void Threads_ClearEvent(Uint32 Mask)
59 if( Threads_int_ThreadingEnabled() ) {
60 Log_Notice("Threads", "_ClearEvent: Threading disabled");
63 Threads_int_MutexLock(lpThreads_This->Protector);
64 lpThreads_This->PendingEvents &= ~Mask;
65 Threads_int_MutexRelease(lpThreads_This->Protector);
68 tUID Threads_GetUID(void) { return 0; }
69 tGID Threads_GetGID(void) { return 0; }
71 tTID Threads_GetTID(void) { return 0; }
73 int *Threads_GetMaxFD(void) { return &lpThreads_This->Process->MaxFDs; }
74 char **Threads_GetCWD(void) { return &lpThreads_This->Process->CWD; }
75 char **Threads_GetChroot(void) { return &lpThreads_This->Process->Chroot; }
76 void **Threads_GetHandlesPtr(void) { return &lpThreads_This->Process->Handles; }
78 void Threads_Yield(void)
80 Log_KernelPanic("Threads", "Threads_Yield DEFINITELY shouldn't be used (%p)",
81 __builtin_return_address(0));
84 void Threads_Sleep(void)
86 Log_Warning("Threads", "Threads_Sleep shouldn't be used");
89 int Threads_SetName(const char *Name)
94 if( lpThreads_This->Name )
95 free(lpThreads_This->Name);
96 lpThreads_This->Name = strdup(Name);
101 int *Threads_GetErrno(void) __attribute__ ((weak));
103 int *Threads_GetErrno(void)
109 struct sProcess *Threads_int_CreateProcess(void)
111 struct sProcess *ret = calloc(sizeof(struct sProcess), 1);
118 tThread *Threads_int_CreateTCB(tThread *Parent)
120 tThread *ret = calloc( sizeof(tThread), 1 );
121 ret->WaitSemaphore = Threads_int_SemCreate();
122 ret->Protector = Threads_int_MutexCreate();
126 ret->Process = Threads_int_CreateProcess();
129 ret->Process = Parent->Process;
131 ret->ProcNext = ret->Process->Threads;
132 ret->Process->Threads = ret;
134 ret->Next = gThreads_List;
140 struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
142 if( !Threads_int_ThreadingEnabled() )
144 Log_Error("Threads", "Multithreading is disabled in this build");
149 tThread *ret = Threads_int_CreateTCB(lpThreads_This);
151 ret->SpawnData = Data;
152 Threads_int_CreateThread(ret);