AcessNative - Adding network support
[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
25 // === CODE ===
26 void Threads_Glue_Yield(void)
27 {
28         SDL_Delay(10);
29 }
30
31 void Threads_Glue_AcquireMutex(void **Lock)
32 {
33         if(!*Lock) {
34                 *Lock = malloc( sizeof(pthread_mutex_t) );
35                 pthread_mutex_init( *Lock, NULL );
36         }
37         pthread_mutex_lock( *Lock );
38 }
39
40 void Threads_Glue_ReleaseMutex(void **Lock)
41 {
42         pthread_mutex_unlock( *Lock );
43 }
44
45 void Threads_Glue_SemInit(void **Ptr, int Val)
46 {
47         *Ptr = SDL_CreateSemaphore(Val);
48         if( !*Ptr ) {
49                 Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError());
50         }
51 }
52
53 int Threads_Glue_SemWait(void *Ptr, int Max)
54 {
55          int    have = 0;
56         assert(Ptr);
57         do {
58                 if( SDL_SemWait( Ptr ) == -1 ) {
59                         return -1;
60                 }
61                 have ++;
62         } while( SDL_SemValue(Ptr) && have < Max );
63         return have;
64 }
65
66 int Threads_Glue_SemSignal( void *Ptr, int AmmountToAdd )
67 {
68         for( int i = 0; i < AmmountToAdd; i ++ )
69                 SDL_SemPost( Ptr );
70         return AmmountToAdd;
71 }
72
73 void Threads_Glue_SemDestroy( void *Ptr )
74 {
75         SDL_DestroySemaphore(Ptr);
76 }
77
78 // ---
79 // Short Locks
80 // ---
81 void Threads_int_ShortLock(void **MutexPtr)
82 {
83         if( !*MutexPtr ) {
84                 *MutexPtr = malloc( sizeof(pthread_mutex_t) );
85                 pthread_mutex_init(*MutexPtr, NULL);
86         }
87         pthread_mutex_lock(*MutexPtr);
88 }
89
90 void Threads_int_ShortRel(void **MutexPtr)
91 {
92         pthread_mutex_unlock(*MutexPtr);
93 }
94
95

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