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 VG_STATIC
void vg_fatal_exit_loop( const char *error
);
84 VG_STATIC
void vg_required( void *ptr
, const char *path
)
88 vg_fatal_exit_loop( path
);
92 #define VG_REQUIRED_ASSET( TYPE, DECL, FN, PATH, ... ) \
93 TYPE DECL = FN( PATH,##__VA_ARGS__ ); \
94 vg_required( DECL, "Resource is required but failed to load: '" PATH "'" );
98 char *strcpy(char* destination
, const char* source
);
100 char *strncpy(char *restrict dest
, const char *restrict src
, size_t n
);
102 char *strcat(char *restrict dest
, const char *restrict src
);
104 char *strncat(char *restrict dest
, const char *restrict src
, size_t n
);
115 VG_STATIC
int vg_thread_run( void *pfunc
, void *data
);
116 VG_STATIC
void vg_thread_exit(void);
117 VG_STATIC
void vg_set_thread_name( const char *name
);
118 VG_STATIC
int vg_semaphore_init( vg_semaphore
*sem
, u32 value
);
119 VG_STATIC
int vg_semaphore_trywait( vg_semaphore
*sem
);
120 VG_STATIC
int vg_semaphore_wait( vg_semaphore
*sem
);
121 VG_STATIC
int vg_semaphore_post( vg_semaphore
*sem
);
122 VG_STATIC
void vg_semaphore_free( vg_semaphore
*sem
);
123 VG_STATIC
int vg_mutex_init( vg_mutex
*mutex
);
124 VG_STATIC
int vg_mutex_lock( vg_mutex
*mutex
);
125 VG_STATIC
int vg_mutex_unlock( vg_mutex
*mutex
);
126 VG_STATIC
void vg_mutex_free( vg_mutex
*mutex
);
127 VG_STATIC
void vg_sleep_ms( long msec
);
128 VG_STATIC
double vg_time_diff( vg_timespec start
, vg_timespec end
);
132 VG_STATIC
int vg_thread_run( void *pfunc
, void *data
)
134 HANDLE hThread
= CreateThread
136 NULL
, /* Thread attributes */
137 0, /* Stack size (0 = use default) */
138 pfunc
, /* Thread start address */
139 data
, /* Parameter to pass to the thread */
140 0, /* Creation flags */
144 if ( hThread
== NULL
)
147 * Thread creation failed.
148 * More details can be retrieved by calling GetLastError()
154 CloseHandle( hThread
);
159 VG_STATIC
void vg_thread_exit(void)
164 VG_STATIC
void vg_set_thread_name( const char *name
)
166 /* I believe this is a meaningless concept in windows */
169 VG_STATIC
int vg_semaphore_init( vg_semaphore
*sem
, u32 value
);
170 VG_STATIC
int vg_semaphore_trywait( vg_semaphore
*sem
);
171 VG_STATIC
int vg_semaphore_wait( vg_semaphore
*sem
);
172 VG_STATIC
int vg_semaphore_post( vg_semaphore
*sem
);
173 VG_STATIC
void vg_semaphore_free( vg_semaphore
*sem
);
174 VG_STATIC
int vg_mutex_init( vg_mutex
*mutex
);
175 VG_STATIC
int vg_mutex_lock( vg_mutex
*mutex
);
176 VG_STATIC
int vg_mutex_unlock( vg_mutex
*mutex
);
177 VG_STATIC
void vg_mutex_free( vg_mutex
*mutex
);
178 VG_STATIC
void vg_sleep_ms( long msec
);
179 VG_STATIC
double vg_time_diff( vg_timespec start
, vg_timespec end
);
183 VG_STATIC
int vg_thread_run( void *pfunc
, void *data
)
186 if( pthread_create( &hThread
, NULL
, pfunc
, data
) )
192 pthread_detach( hThread
);
198 VG_STATIC
void vg_thread_exit(void)
203 VG_STATIC
void vg_set_thread_name( const char *name
)
205 /* not defined but links?? */
207 pthread_setname_np(pthread_self());
211 VG_STATIC
int vg_semaphore_init( vg_semaphore
*sem
, u32 value
)
213 return !sem_init( sem
, 0, value
);
216 VG_STATIC
int vg_semaphore_trywait( vg_semaphore
*sem
)
218 return !sem_trywait( sem
);
221 VG_STATIC
int vg_semaphore_wait( vg_semaphore
*sem
)
223 return !sem_wait( sem
);
226 VG_STATIC
int vg_semaphore_post( vg_semaphore
*sem
)
228 return !sem_post( sem
);
231 VG_STATIC
void vg_semaphore_free( vg_semaphore
*sem
)
236 VG_STATIC
int vg_mutex_init( vg_mutex
*mutex
)
238 memset( mutex
, 0, sizeof(vg_mutex
) );
242 VG_STATIC
int vg_mutex_lock( vg_mutex
*mutex
)
244 if( !pthread_mutex_lock( mutex
) )
250 VG_STATIC
int vg_mutex_unlock( vg_mutex
*mutex
)
252 if( !pthread_mutex_unlock( mutex
) )
258 VG_STATIC
void vg_mutex_free( vg_mutex
*mutex
)
263 VG_STATIC
void vg_sleep_ms( long msec
)
267 ts
.tv_sec
= msec
/ 1000;
268 ts
.tv_nsec
= (msec
% 1000) * 1000000;
269 nanosleep( &ts
, &ts
);
272 /* diff two timespecs in MS */
273 VG_STATIC
double vg_time_diff( struct timespec start
, struct timespec end
)
275 double elapsed
= 1000.0*end
.tv_sec
+ 1e-6*end
.tv_nsec
276 - (1000.0*start
.tv_sec
+ 1e-6*start
.tv_nsec
);
283 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
284 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))