Fix ARCH=native compiling
[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
22 #define NORETURN        __attribute__((noreturn))
23 #include <logdebug.h>   // Kernel land, but uses standards
24 #include <errno.h>
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         for( int i = 0; i < AmmountToAdd; i ++ )
70                 SDL_SemPost( Ptr );
71         return AmmountToAdd;
72 }
73
74 void Threads_Glue_SemDestroy( void *Ptr )
75 {
76         SDL_DestroySemaphore(Ptr);
77 }
78
79 // ---
80 // Short Locks
81 // ---
82 void Threads_int_ShortLock(void **MutexPtr)
83 {
84         if( !*MutexPtr ) {
85                 *MutexPtr = malloc( sizeof(pthread_mutex_t) );
86                 pthread_mutexattr_t     attr;
87                 pthread_mutexattr_init(&attr);
88                 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
89                 pthread_mutex_init(*MutexPtr, NULL);
90         }
91         if( pthread_mutex_lock(*MutexPtr) ) {
92                 fprintf(stderr, "ERROR: Mutex pointer %p double locked\n", MutexPtr);
93                 AcessNative_Exit();
94         }
95 }
96
97 void Threads_int_ShortRel(void **MutexPtr)
98 {
99         pthread_mutex_unlock(*MutexPtr);
100 }
101
102 int Threads_int_ShortHas(void **Ptr)
103 {
104         if( !*Ptr )
105                 return 0;
106         int rv = pthread_mutex_trylock(*Ptr);
107         if( rv == 0 ) {
108                 pthread_mutex_unlock(*Ptr);
109                 return 0;
110         }
111         if( rv == EBUSY ) {
112                 return 0;
113         }
114         return 1;
115 }
116

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