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