nativelib - Compile fixes
[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 tThread __thread        *lpThreads_This;
14
15 // === CODE ===
16 tThread *Proc_GetCurThread(void)
17 {
18         return lpThreads_This;
19 }
20
21 void Threads_PostEvent(tThread *Thread, Uint32 Events)
22 {
23         if( !Thread ) {
24                 // nope.avi
25                 return ;
26         }
27         Threads_int_MutexLock(Thread->Protector);
28         Thread->PendingEvents |= Events;
29         if( Thread->WaitingEvents & Events )
30                 Threads_int_SemSignal(Thread->WaitSemaphore);
31         Threads_int_MutexRelease(Thread->Protector);
32 }
33
34 Uint32 Threads_WaitEvents(Uint32 Events)
35 {
36         if( !lpThreads_This ) {
37                 Log_Notice("Threads", "_WaitEvents: Threading disabled");
38                 return 0;
39         }
40         lpThreads_This->WaitingEvents = Events;
41         Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore);
42         lpThreads_This->WaitingEvents = 0;
43         Uint32  rv = lpThreads_This->PendingEvents;
44         return rv;
45 }
46
47 void Threads_ClearEvent(Uint32 Mask)
48 {
49         if( !lpThreads_This ) {
50                 Log_Notice("Threads", "_ClearEvent: Threading disabled");
51                 return ;
52         }
53         Threads_int_MutexLock(lpThreads_This->Protector);
54         lpThreads_This->PendingEvents &= ~Mask;
55         Threads_int_MutexRelease(lpThreads_This->Protector);
56 }
57
58 tUID Threads_GetUID(void) { return 0; }
59 tGID Threads_GetGID(void) { return 0; }
60
61 tTID Threads_GetTID(void) { return 0; }
62
63 int *Threads_GetMaxFD(void) { static int max_fd=32; return &max_fd; }
64 char **Threads_GetCWD(void) { static char *cwd; return &cwd; }
65 char **Threads_GetChroot(void) { static char *chroot; return &chroot; }
66
67 void Threads_Yield(void)
68 {
69         Log_Warning("Threads", "Threads_Yield DEFINITELY shouldn't be used");
70 }
71
72 void Threads_Sleep(void)
73 {
74         Log_Warning("Threads", "Threads_Sleep shouldn't be used");
75 }
76
77 int Threads_SetName(const char *Name)
78 {
79         Log_Notice("Threads", "TODO: Threads_SetName('%s')", Name);
80         return 0;
81 }
82
83 int *Threads_GetErrno(void) __attribute__ ((weak));
84
85 int *Threads_GetErrno(void)// __attribute__ ((weak))
86 {
87         static int a_errno;
88         return &a_errno;
89 }
90
91 struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
92 {
93         if( !lpThreads_This )
94         {
95                 Log_Error("Threads", "Multithreading is disabled in this build");
96                 return NULL;
97         }
98         else
99         {
100                 tThread *ret = malloc( sizeof(tThread) );
101                 ret->SpawnFcn = Fcn;
102                 ret->SpawnData = Data;
103                 Threads_int_CreateThread(ret);
104                 return ret;
105         }
106 }
107

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