AcessNative - Fix some compilation issues
[tpg/acess2.git] / AcessNative / acesskernel_src / threads_glue.c
1 /*
2  * Acess2 Native Kernel
3  * - Acess kernel emulation on another OS using SDL and UDP
4  *
5  * threads.c
6  * - Thread and process handling
7  */
8 #include "include/threads_glue.h"
9
10 #define _ACESS_H
11 typedef void    **tShortSpinlock;
12 #include <rwlock.h>
13
14 // - Native headers
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <stdint.h>
18 //#include "/usr/include/signal.h"
19 #include <SDL/SDL.h>
20 #include <pthread.h>
21 #include <assert.h>
22
23 #define NORETURN        __attribute__((noreturn))
24 #include <logdebug.h>   // Kernel land, but uses standards
25
26 // === CODE ===
27 void Threads_Glue_Yield(void)
28 {
29         SDL_Delay(10);
30 }
31
32 void Threads_Glue_AcquireMutex(void **Lock)
33 {
34         if(!*Lock) {
35                 *Lock = malloc( sizeof(pthread_mutex_t) );
36                 pthread_mutex_init( *Lock, NULL );
37         }
38         pthread_mutex_lock( *Lock );
39 }
40
41 void Threads_Glue_ReleaseMutex(void **Lock)
42 {
43         pthread_mutex_unlock( *Lock );
44 }
45
46 void Threads_Glue_SemInit(void **Ptr, int Val)
47 {
48         *Ptr = SDL_CreateSemaphore(Val);
49         if( !*Ptr ) {
50                 Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError());
51         }
52 }
53
54 int Threads_Glue_SemWait(void *Ptr, int Max)
55 {
56          int    have = 0;
57         assert(Ptr);
58         do {
59                 if( SDL_SemWait( Ptr ) == -1 ) {
60                         return -1;
61                 }
62                 have ++;
63         } while( SDL_SemValue(Ptr) && have < Max );
64         return have;
65 }
66
67 int Threads_Glue_SemSignal( void *Ptr, int AmmountToAdd )
68 {
69          int    i;
70         for( i = 0; i < AmmountToAdd; i ++ )
71                 SDL_SemPost( Ptr );
72         return AmmountToAdd;
73 }
74
75 // --------------------------------------------------------------------
76 // Event handling
77 // --------------------------------------------------------------------
78 int RWLock_AcquireRead(tRWLock *Lock)
79 {
80         if( !Lock->ReaderWaiting ) {
81                 Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t));
82                 pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 );
83         }
84         pthread_rwlock_rdlock( (void*)Lock->ReaderWaiting );
85         return 0;
86 }
87 int RWLock_AcquireWrite(tRWLock *Lock)
88 {
89         if( !Lock->ReaderWaiting ) {
90                 Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t));
91                 pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 );
92         }
93         pthread_rwlock_wrlock( (void*)Lock->ReaderWaiting );
94         return 0;
95 }
96 void RWLock_Release(tRWLock *Lock)
97 {
98         pthread_rwlock_unlock( (void*)Lock->ReaderWaiting );
99 }
100

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