7 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
9 typedef unsigned int uint
;
24 typedef struct vg_tex2d vg_tex2d
;
33 #define VG_STATIC static
36 #define vg_static_assert _Static_assert
37 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
38 #define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
43 #ifdef I_THINK_THIS_IS_WHAT_MSCV_WANTS_BUT_HAVNT_TESTED_IT_YET
45 #define VG_DEPRECATED __declspec(deprecated)
46 #define VG_THREAD_LOCAL __declspec( thread )
50 #define VG_THREAD_LOCAL __thread
51 #define VG_DEPRECATED __attribute__((deprecated))
55 typedef HANDLE vg_semaphore
;
56 typedef HANDLE vg_mutex
;
57 typedef u64 vg_timespec
;
61 #include <semaphore.h>
63 #define VG_DEPRECATED __attribute__((deprecated))
64 #define VG_THREAD_LOCAL __thread
66 typedef sem_t vg_semaphore
;
67 typedef pthread_mutex_t vg_mutex
;
68 typedef struct timespec vg_timespec
;
72 VG_STATIC
void vg_strncpy( const char *src
, char *dst
, u32 len
)
74 for( u32 i
=0; i
<len
; i
++ )
83 #define VG_REQUIRED_ASSET( TYPE, DECL, FN, PATH, ... ) \
84 TYPE DECL = FN( PATH,##__VA_ARGS__ ); \
85 vg_required( DECL, "Resource is required but failed to load: '" PATH "'" );
89 char *strcpy(char* destination
, const char* source
);
91 char *strncpy(char *restrict dest
, const char *restrict src
, size_t n
);
93 char *strcat(char *restrict dest
, const char *restrict src
);
95 char *strncat(char *restrict dest
, const char *restrict src
, size_t n
);
106 VG_STATIC
int vg_thread_run( void *pfunc
, void *data
);
107 VG_STATIC
void vg_thread_exit(void);
108 VG_STATIC
void vg_set_thread_name( const char *name
);
109 VG_STATIC
int vg_semaphore_init( vg_semaphore
*sem
, u32 value
);
110 VG_STATIC
int vg_semaphore_trywait( vg_semaphore
*sem
);
111 VG_STATIC
int vg_semaphore_wait( vg_semaphore
*sem
);
112 VG_STATIC
int vg_semaphore_post( vg_semaphore
*sem
);
113 VG_STATIC
void vg_semaphore_free( vg_semaphore
*sem
);
114 VG_STATIC
int vg_mutex_init( vg_mutex
*mutex
);
115 VG_STATIC
int vg_mutex_lock( vg_mutex
*mutex
);
116 VG_STATIC
int vg_mutex_unlock( vg_mutex
*mutex
);
117 VG_STATIC
void vg_mutex_free( vg_mutex
*mutex
);
118 VG_STATIC
void vg_sleep_ms( long msec
);
119 VG_STATIC
double vg_time_diff( vg_timespec start
, vg_timespec end
);
123 VG_STATIC
int vg_thread_run( void *pfunc
, void *data
)
125 HANDLE hThread
= CreateThread
127 NULL
, /* Thread attributes */
128 0, /* Stack size (0 = use default) */
129 pfunc
, /* Thread start address */
130 data
, /* Parameter to pass to the thread */
131 0, /* Creation flags */
135 if ( hThread
== NULL
)
138 * Thread creation failed.
139 * More details can be retrieved by calling GetLastError()
145 CloseHandle( hThread
);
150 VG_STATIC
void vg_thread_exit(void)
155 VG_STATIC
void vg_set_thread_name( const char *name
)
157 /* I believe this is a meaningless concept in windows */
160 VG_STATIC
int vg_semaphore_init( vg_semaphore
*sem
, u32 value
);
161 VG_STATIC
int vg_semaphore_trywait( vg_semaphore
*sem
);
162 VG_STATIC
int vg_semaphore_wait( vg_semaphore
*sem
);
163 VG_STATIC
int vg_semaphore_post( vg_semaphore
*sem
);
164 VG_STATIC
void vg_semaphore_free( vg_semaphore
*sem
);
165 VG_STATIC
int vg_mutex_init( vg_mutex
*mutex
);
166 VG_STATIC
int vg_mutex_lock( vg_mutex
*mutex
);
167 VG_STATIC
int vg_mutex_unlock( vg_mutex
*mutex
);
168 VG_STATIC
void vg_mutex_free( vg_mutex
*mutex
);
169 VG_STATIC
void vg_sleep_ms( long msec
);
170 VG_STATIC
double vg_time_diff( vg_timespec start
, vg_timespec end
);
174 VG_STATIC
int vg_thread_run( void *pfunc
, void *data
)
177 if( pthread_create( &hThread
, NULL
, pfunc
, data
) )
183 pthread_detach( hThread
);
189 VG_STATIC
void vg_thread_exit(void)
194 VG_STATIC
void vg_set_thread_name( const char *name
)
196 /* not defined but links?? */
198 pthread_setname_np(pthread_self());
202 VG_STATIC
int vg_semaphore_init( vg_semaphore
*sem
, u32 value
)
204 return !sem_init( sem
, 0, value
);
207 VG_STATIC
int vg_semaphore_trywait( vg_semaphore
*sem
)
209 return !sem_trywait( sem
);
212 VG_STATIC
int vg_semaphore_wait( vg_semaphore
*sem
)
214 return !sem_wait( sem
);
217 VG_STATIC
int vg_semaphore_post( vg_semaphore
*sem
)
219 return !sem_post( sem
);
222 VG_STATIC
void vg_semaphore_free( vg_semaphore
*sem
)
227 VG_STATIC
int vg_mutex_init( vg_mutex
*mutex
)
229 memset( mutex
, 0, sizeof(vg_mutex
) );
233 VG_STATIC
int vg_mutex_lock( vg_mutex
*mutex
)
235 if( !pthread_mutex_lock( mutex
) )
241 VG_STATIC
int vg_mutex_unlock( vg_mutex
*mutex
)
243 if( !pthread_mutex_unlock( mutex
) )
249 VG_STATIC
void vg_mutex_free( vg_mutex
*mutex
)
254 VG_STATIC
void vg_sleep_ms( long msec
)
258 ts
.tv_sec
= msec
/ 1000;
259 ts
.tv_nsec
= (msec
% 1000) * 1000000;
260 nanosleep( &ts
, &ts
);
263 /* diff two timespecs in MS */
264 VG_STATIC
double vg_time_diff( struct timespec start
, struct timespec end
)
266 double elapsed
= 1000.0*end
.tv_sec
+ 1e-6*end
.tv_nsec
267 - (1000.0*start
.tv_sec
+ 1e-6*start
.tv_nsec
);
274 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
275 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))