Kernel/VTerm - "Fix" wrapping issue in VTerm (why was old behavior there?)
[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 <stdbool.h>
24 #include <logdebug.h>   // Kernel land, but uses standards
25 #include <errno.h>
26
27 // === CODE ===
28 void Threads_Glue_Yield(void)
29 {
30         SDL_Delay(10);
31 }
32
33 void Threads_Glue_AcquireMutex(void **Lock)
34 {
35         if(!*Lock) {
36                 *Lock = malloc( sizeof(pthread_mutex_t) );
37                 pthread_mutex_init( *Lock, NULL );
38         }
39         pthread_mutex_lock( *Lock );
40 }
41
42 void Threads_Glue_ReleaseMutex(void **Lock)
43 {
44         pthread_mutex_unlock( *Lock );
45 }
46
47 void Threads_Glue_SemInit(void **Ptr, int Val)
48 {
49         *Ptr = SDL_CreateSemaphore(Val);
50         if( !*Ptr ) {
51                 Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError());
52         }
53 }
54
55 int Threads_Glue_SemWait(void *Ptr, int Max)
56 {
57          int    have = 0;
58         assert(Ptr);
59         do {
60                 if( SDL_SemWait( Ptr ) == -1 ) {
61                         return -1;
62                 }
63                 have ++;
64         } while( SDL_SemValue(Ptr) && have < Max );
65         return have;
66 }
67
68 int Threads_Glue_SemSignal( void *Ptr, int AmmountToAdd )
69 {
70         for( int i = 0; i < AmmountToAdd; i ++ )
71                 SDL_SemPost( Ptr );
72         return AmmountToAdd;
73 }
74
75 void Threads_Glue_SemDestroy( void *Ptr )
76 {
77         SDL_DestroySemaphore(Ptr);
78 }
79
80 // ---
81 // Short Locks
82 // ---
83 void Threads_int_ShortLock(void **MutexPtr)
84 {
85         if( !*MutexPtr ) {
86                 *MutexPtr = malloc( sizeof(pthread_mutex_t) );
87                 pthread_mutexattr_t     attr;
88                 pthread_mutexattr_init(&attr);
89                 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
90                 pthread_mutex_init(*MutexPtr, NULL);
91         }
92         if( pthread_mutex_lock(*MutexPtr) ) {
93                 fprintf(stderr, "ERROR: Mutex pointer %p double locked\n", MutexPtr);
94                 AcessNative_Exit();
95         }
96 }
97
98 void Threads_int_ShortRel(void **MutexPtr)
99 {
100         pthread_mutex_unlock(*MutexPtr);
101 }
102
103 int Threads_int_ShortHas(void **Ptr)
104 {
105         if( !*Ptr )
106                 return 0;
107         int rv = pthread_mutex_trylock(*Ptr);
108         if( rv == 0 ) {
109                 pthread_mutex_unlock(*Ptr);
110                 return 0;
111         }
112         if( rv == EBUSY ) {
113                 return 0;
114         }
115         return 1;
116 }
117

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