Tools/nativelib - Many features implimented
[tpg/acess2.git] / Tools / nativelib / threads_int.c
1 /*
2  * Acess2 libnative (Kernel Simulation Library)
3  * - By John Hodge (thePowersGang)
4  *
5  * threads_int.c
6  * - Internal threading functions
7  */
8 #include <stddef.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <acess_logging.h>
12 #include <threads_int.h>
13 #include <pthread_weak.h>
14
15 // === TYPES ===
16 typedef struct sThread  tThread;
17 struct sThreadIntMutex { int lock; };
18 struct sThreadIntSem { int val; };
19
20 // === PROTOTYPES ===
21
22 // === CODE ===
23 int Threads_int_ThreadingEnabled(void)
24 {
25         Log_Debug("Threads", "pthread_create = %p", pthread_create);
26         return !!pthread_create;
27 }
28
29 tThreadIntMutex *Threads_int_MutexCreate(void)
30 {
31         if( pthread_mutex_init )
32         {
33                 tThreadIntMutex *ret = malloc(sizeof(pthread_mutex_t));
34                 pthread_mutex_init( (void*)ret, NULL );
35                 return ret;
36         }
37         else
38         {
39                 return calloc(sizeof(tThreadIntMutex), 1);
40         }
41 }
42
43 void Threads_int_MutexLock(tThreadIntMutex *Mutex)
44 {
45         if( !Mutex ) {
46                 return ;
47         }
48         if( pthread_mutex_lock )
49         {
50                 pthread_mutex_lock( (void*)Mutex );
51         }
52         else
53         {
54                 if( Mutex->lock )
55                         Log_KernelPanic("Threads", "Double mutex lock");
56                 Mutex->lock = 1;
57         }
58 }
59
60 void Threads_int_MutexRelease(tThreadIntMutex *Mutex)
61 {
62         if( !Mutex ) {
63                 return ;
64         }
65
66         if( pthread_mutex_unlock )
67         {
68                 pthread_mutex_unlock( (void*)Mutex );
69         }
70         else
71         {
72                 if( !Mutex->lock )
73                         Log_Notice("Threads", "Release of non-locked mutex %p", Mutex);
74                 Mutex->lock = 0;
75         }
76 }
77
78 tThreadIntSem *Threads_int_SemCreate(void)
79 {
80         if( sem_init )
81         {
82                 tThreadIntSem *ret = malloc(sizeof(sem_t));
83                 sem_init( (void*)ret, 0, 0 );
84                 return ret;
85         }
86         else
87         {
88                 return calloc(sizeof(tThreadIntSem), 1);
89         }
90 }
91
92 void Threads_int_SemSignal(tThreadIntSem *Sem)
93 {
94         if( sem_wait )
95         {
96                 sem_wait( (void*)Sem );
97         }
98         else
99         {
100                 Sem->val ++;
101         }
102 }
103
104 void Threads_int_SemWaitAll(tThreadIntSem *Sem)
105 {
106         if( sem_post )
107         {
108                 sem_post( (void*)Sem );
109         }
110         else
111         {
112                 if( !Sem->val )
113                         Log_KernelPanic("Threads", "Waiting on empty semaphre %p", Sem);
114                 Sem->val = 0;
115         }
116 }
117
118 void *Threads_int_ThreadRoot(void *ThreadPtr)
119 {
120         tThread *thread = ThreadPtr;
121         lpThreads_This = thread;
122         Log_Debug("Threads", "SpawnFcn: %p, SpawnData: %p", thread->SpawnFcn, thread->SpawnData);
123         thread->SpawnFcn(thread->SpawnData);
124         return NULL;
125 }
126
127 int Threads_int_CreateThread(tThread *Thread)
128 {
129         if( pthread_create )
130         {
131                 pthread_t *pthread = malloc(sizeof(pthread_t));
132                 Thread->ThreadHandle = pthread;
133                 return pthread_create(pthread, NULL, &Threads_int_ThreadRoot, Thread);
134         }
135         else
136         {
137                 Log_KernelPanic("Threads", "Link with pthreads to use threading");
138                 return -1;
139         }
140 }
141

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