d5f04652fbc450dd2fe9c95c70248066ec984fd4
[tpg/acess2.git] / Tools / nativelib / threads.c
1 /*
2  * Acess2 libnative (Kernel Simulation Library)
3  * - By John Hodge (thePowersGang)
4  *
5  * threads.c
6  * - Threads handling
7  */
8 #include <acess.h>
9 #include <threads.h>
10 #include <threads_int.h>
11
12 // === GLOBALS ===
13  int    gbThreads_MultithreadingEnabled;
14 tThread __thread        *lpThreads_This;
15
16 // === CODE ===
17 tThread *Proc_GetCurThread(void)
18 {
19         return lpThreads_This;
20 }
21
22 void Threads_PostEvent(tThread *Thread, Uint32 Events)
23 {
24         Threads_int_LockMutex(Thread->Protector);
25         Thread->PendingEvents |= Events;
26         if( Thread->WaitingEvents & Events )
27                 Threads_int_SemaphoreSignal(Thread->WaitSemaphore);
28         Threads_int_ReleaseMutex(Thread->Protector);
29 }
30
31 Uint32 Threads_WaitEvents(Uint32 Events)
32 {
33         Thread->WaitingEvents = Events;
34         Threads_int_SemaphoreWaitAll(Thread->WaitSemaphore);
35         Thread->WaitingEvents = 0;
36         Uint32  rv = Thread->PendingEvents;
37         return rv;
38 }
39
40 void Threads_ClearEvent(Uint32 Mask)
41 {
42         Threads_int_LockMutex(Thread->Protector);
43         lpThreads_This->PendingEvents &= ~Mask;
44         Threads_int_ReleaseMutex(Thread->Protector);
45 }
46
47 tUID Threads_GetUID(void) { return 0; }
48 tGID Threads_GetGID(void) { return 0; }
49
50 tTID Threads_GetTID(void) { return 0; }
51
52 int *Threads_GetMaxFD(void) { static int max_fd=32; return &max_fd; }
53 char **Threads_GetCWD(void) { static char *cwd; return &cwd; }
54 char **Threads_GetChroot(void) { static char *chroot; return &chroot; }
55
56 void Threads_Yield(void)
57 {
58         Log_Warning("Threads", "Threads_Yield DEFINITELY shouldn't be used");
59 }
60
61 void Threads_Sleep(void)
62 {
63         Log_Warning("Threads", "Threads_Sleep shouldn't be used");
64 }
65
66 int Threads_SetName(const char *Name)
67 {
68         Log_Notice("Threads", "TODO: Threads_SetName('%s')", Name);
69         return 0;
70 }
71
72 int *Threads_GetErrno(void) __attribute__ ((weak));
73
74 int *Threads_GetErrno(void)// __attribute__ ((weak))
75 {
76         static int a_errno;
77         return &a_errno;
78 }
79
80 struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
81 {
82         if( !gbThreads_MultithreadingEnabled )
83         {
84                 Log_Error("Threads", "Multithreading is disabled in this build");
85                 return NULL;
86         }
87         else
88         {
89                 tThread *ret = malloc( sizeof(tThread) );
90                 ret->SpawnFcn = Fcn;
91                 ret->SpawnData = Data;
92                 Threads_int_CreateThread(ret);
93                 Log_Error("Threads", "TODO - Use pthreads to impliment Proc_SpawnWorker");
94                 return NULL;
95         }
96 }
97

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