b1986b96d088e26732166a5020c6d6c2602e9d60
[fishladder.git] / vg / vg_platform.h
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 typedef uint8_t u8;
4 typedef uint16_t u16;
5 typedef uint32_t u32;
6 typedef uint64_t u64;
7 typedef int8_t i8;
8 typedef int16_t i16;
9 typedef int32_t i32;
10 typedef int64_t i64;
11
12 typedef unsigned int uint;
13
14 typedef int v2i[2];
15 typedef int v3i[3];
16 typedef int v4i[4];
17 typedef float v2f[2];
18 typedef float v3f[3];
19 typedef float v4f[4];
20 typedef v2f m2x2f[2];
21 typedef v3f m3x3f[3];
22 typedef v3f m4x3f[4];
23 typedef v3f boxf[2];
24
25 // Resource types
26 typedef struct vg_tex2d vg_tex2d;
27
28 struct vg_achievement
29 {
30 int is_set;
31 const char *name;
32 };
33
34 #define vg_static_assert _Static_assert
35
36 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
37
38 // THREADING
39 // ==================================================================================================================
40
41 // Pthred emulation for windows
42 #ifdef _WIN32
43 #include <windows.h>
44 #define MUTEX_TYPE HANDLE
45 #define MUTEX_INITIALIZER NULL
46 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
47 #define MUTEX_CLEANUP(x) (CloseHandle(x)) //TODO: Why is this defined but never used?
48 #define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
49 #define MUTEX_UNLOCK(x) (ReleaseMutex(x))
50
51 int emulate_pthread_mutex_lock( volatile MUTEX_TYPE *mx )
52 {
53 if( *mx == NULL ) /* static initializer? */
54 {
55 HANDLE p = CreateMutex( NULL, FALSE, NULL );
56 if( InterlockedCompareExchangePointer( (PVOID*)mx, (PVOID)p, NULL ) != NULL )
57 CloseHandle(p);
58 }
59
60 return WaitForSingleObject( *mx, INFINITE ) == WAIT_FAILED;
61 }
62 #else
63 #include <pthread.h>
64 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
65 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
66 #define MUTEX_TYPE pthread_mutex_t
67 #define MUTEX_INITIALIZER {0}
68 #endif
69
70
71 int vg_thread_run( void *pfunc, void *data )
72 {
73 #ifdef _WIN32
74
75 HANDLE hThread = CreateThread
76 (
77 NULL, // Thread attributes
78 0, // Stack size (0 = use default)
79 pfunc, // Thread start address
80 data, // Parameter to pass to the thread
81 0, // Creation flags
82 NULL // Thread id
83 );
84
85 if ( hThread == NULL )
86 {
87 // Thread creation failed.
88 // More details can be retrieved by calling GetLastError()
89 return 1;
90 }
91 else
92 {
93 CloseHandle( hThread );
94 return 0;
95 }
96
97 #else
98
99 pthread_t hThread;
100 if( pthread_create( &hThread, NULL, pfunc, data ) )
101 {
102 return 1;
103 }
104 else
105 {
106 pthread_detach( hThread );
107 return 0;
108 }
109
110 #endif
111 }