mc 1.9
[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];
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 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
36
37 /* Pthred emulation for windows */
38 #ifdef _WIN32
39 #include <windows.h>
40 #define MUTEX_TYPE HANDLE
41 #define MUTEX_INITIALIZER NULL
42 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
43
44 /* TODO: Why is this defined but never used? */
45 #define MUTEX_CLEANUP(x) (CloseHandle(x))
46 #define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
47 #define MUTEX_UNLOCK(x) (ReleaseMutex(x))
48
49 int emulate_pthread_mutex_lock( volatile MUTEX_TYPE *mx )
50 {
51 if( *mx == NULL ) /* static initializer? */
52 {
53 HANDLE p = CreateMutex( NULL, FALSE, NULL );
54 if( InterlockedCompareExchangePointer( (PVOID*)mx, (PVOID)p, NULL )
55 != NULL )
56 CloseHandle(p);
57 }
58
59 return WaitForSingleObject( *mx, INFINITE ) == WAIT_FAILED;
60 }
61 #else
62 #include <pthread.h>
63 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
64 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
65 #define MUTEX_TYPE pthread_mutex_t
66 #define MUTEX_INITIALIZER {0}
67 #endif
68
69
70 int vg_thread_run( void *pfunc, void *data )
71 {
72 #ifdef _WIN32
73 HANDLE hThread = CreateThread
74 (
75 NULL, /* Thread attributes */
76 0, /* Stack size (0 = use default) */
77 pfunc, /* Thread start address */
78 data, /* Parameter to pass to the thread */
79 0, /* Creation flags */
80 NULL /* Thread id */
81 );
82
83 if ( hThread == NULL )
84 {
85 /*
86 * Thread creation failed.
87 * More details can be retrieved by calling GetLastError()
88 */
89 return 1;
90 }
91 else
92 {
93 CloseHandle( hThread );
94 return 0;
95 }
96 #else
97 pthread_t hThread;
98 if( pthread_create( &hThread, NULL, pfunc, data ) )
99 {
100 return 1;
101 }
102 else
103 {
104 pthread_detach( hThread );
105 return 0;
106 }
107 #endif
108 }