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);
28 tThread *Proc_GetCurThread(void)
30 return lpThreads_This;
33 void Threads_PostEvent(tThread *Thread, Uint32 Events)
39 SHORTLOCK( &Thread->IsLocked );
40 Thread->PendingEvents |= Events;
41 if( Thread->WaitingEvents & Events )
42 Threads_int_SemSignal(Thread->WaitSemaphore);
43 SHORTREL( &Thread->IsLocked );
46 Uint32 Threads_WaitEvents(Uint32 Events)
48 if( !Threads_int_ThreadingEnabled() ) {
49 Log_Notice("Threads", "_WaitEvents: Threading disabled");
52 lpThreads_This->WaitingEvents = Events;
53 Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore);
54 lpThreads_This->WaitingEvents = 0;
55 Uint32 rv = lpThreads_This->PendingEvents;
59 void Threads_ClearEvent(Uint32 Mask)
61 if( !Threads_int_ThreadingEnabled() ) {
62 Log_Notice("Threads", "_ClearEvent: Threading disabled");
65 SHORTLOCK(&lpThreads_This->IsLocked);
66 lpThreads_This->PendingEvents &= ~Mask;
67 SHORTREL(&lpThreads_This->IsLocked);
70 tUID Threads_GetUID(void) { return 0; }
71 tGID Threads_GetGID(void) { return 0; }
73 tTID Threads_GetTID(void) { return lpThreads_This->TID; }
75 int *Threads_GetMaxFD(void) { return &lpThreads_This->Process->MaxFDs; }
76 char **Threads_GetCWD(void) { return &lpThreads_This->Process->CWD; }
77 char **Threads_GetChroot(void) { return &lpThreads_This->Process->Chroot; }
78 void **Threads_GetHandlesPtr(void) { return &lpThreads_This->Process->Handles; }
80 void Threads_Yield(void)
82 Log_KernelPanic("Threads", "Threads_Yield DEFINITELY shouldn't be used (%p)",
83 __builtin_return_address(0));
86 void Threads_Sleep(void)
88 Log_Warning("Threads", "Threads_Sleep shouldn't be used");
91 void Threads_int_WaitForStatusEnd(enum eThreadStatus Status)
93 while( lpThreads_This->Status != Status )
94 Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore);
97 int Threads_SetName(const char *Name)
102 if( lpThreads_This->ThreadName )
103 free(lpThreads_This->ThreadName);
104 lpThreads_This->ThreadName = strdup(Name);
109 int *Threads_GetErrno(void) __attribute__ ((weak));
111 int *Threads_GetErrno(void)
117 tThread *Threads_RemActive(void)
119 return lpThreads_This;
122 void Threads_AddActive(tThread *Thread)
124 Thread->Status = THREAD_STAT_ACTIVE;
125 // Increment state-change semaphore
126 Threads_int_SemSignal(Thread->WaitSemaphore);
129 struct sProcess *Threads_int_CreateProcess(void)
131 struct sProcess *ret = calloc(sizeof(struct sProcess), 1);
138 tThread *Threads_int_CreateTCB(tThread *Parent)
140 tThread *ret = calloc( sizeof(tThread), 1 );
141 ret->TID = giThreads_NextTID ++;
142 ret->WaitSemaphore = Threads_int_SemCreate();
143 //ret->Protector = Threads_int_MutexCreate();
147 ret->Process = Threads_int_CreateProcess();
150 ret->Process = Parent->Process;
152 ret->ProcNext = ret->Process->Threads;
153 ret->Process->Threads = ret;
155 ret->Next = gThreads_List;
161 struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
163 if( !Threads_int_ThreadingEnabled() )
165 Log_Error("Threads", "Multithreading is disabled in this build");
170 tThread *ret = Threads_int_CreateTCB(lpThreads_This);
172 ret->SpawnData = Data;
173 Threads_int_CreateThread(ret);