89315b65c86dee6765865811bc47343d5106f127
[vg.git] / src / vg / vg_platform.h
1 /* Copyright (C) 2021-2022 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]; /* TODO why this is 4x4 too? */
23 typedef v4f m4x4f[4];
24 typedef v3f boxf[2];
25
26 // Resource types
27 typedef struct vg_tex2d vg_tex2d;
28
29 struct vg_achievement
30 {
31 int is_set;
32 const char *name;
33 };
34
35 #define vg_static_assert _Static_assert
36 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
37
38 /* Pthred emulation for windows */
39 #ifdef _WIN32
40 #include <windows.h>
41 #define MUTEX_TYPE HANDLE
42 #define MUTEX_INITIALIZER NULL
43 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
44
45 /* TODO: Why is this defined but never used? */
46 #define MUTEX_CLEANUP(x) (CloseHandle(x))
47 #define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
48 #define MUTEX_UNLOCK(x) (ReleaseMutex(x))
49
50 int emulate_pthread_mutex_lock( volatile MUTEX_TYPE *mx )
51 {
52 if( *mx == NULL ) /* static initializer? */
53 {
54 HANDLE p = CreateMutex( NULL, FALSE, NULL );
55 if( InterlockedCompareExchangePointer( (PVOID*)mx, (PVOID)p, NULL )
56 != 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 HANDLE hThread = CreateThread
75 (
76 NULL, /* Thread attributes */
77 0, /* Stack size (0 = use default) */
78 pfunc, /* Thread start address */
79 data, /* Parameter to pass to the thread */
80 0, /* Creation flags */
81 NULL /* Thread id */
82 );
83
84 if ( hThread == NULL )
85 {
86 /*
87 * Thread creation failed.
88 * More details can be retrieved by calling GetLastError()
89 */
90 return 1;
91 }
92 else
93 {
94 CloseHandle( hThread );
95 return 0;
96 }
97 #else
98 pthread_t hThread;
99 if( pthread_create( &hThread, NULL, pfunc, data ) )
100 {
101 return 1;
102 }
103 else
104 {
105 pthread_detach( hThread );
106 return 0;
107 }
108 #endif
109 }