1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
12 typedef unsigned int uint
;
25 #define vg_static_assert _Static_assert
27 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
30 // ==================================================================================================================
32 // Pthred emulation for windows
35 #define MUTEX_TYPE HANDLE
36 #define MUTEX_INITIALIZER NULL
37 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
38 #define MUTEX_CLEANUP(x) (CloseHandle(x)) //TODO: Why is this defined but never used?
39 #define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
40 #define MUTEX_UNLOCK(x) (ReleaseMutex(x))
42 int emulate_pthread_mutex_lock( volatile MUTEX_TYPE
*mx
)
44 if( *mx
== NULL
) /* static initializer? */
46 HANDLE p
= CreateMutex( NULL
, FALSE
, NULL
);
47 if( InterlockedCompareExchangePointer( (PVOID
*)mx
, (PVOID
)p
, NULL
) != NULL
)
51 return WaitForSingleObject( *mx
, INFINITE
) == WAIT_FAILED
;
55 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
56 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
57 #define MUTEX_TYPE pthread_mutex_t
58 #define MUTEX_INITIALIZER {0}
62 int vg_thread_run( void *pfunc
, void *data
)
66 HANDLE hThread
= CreateThread
68 NULL
, // Thread attributes
69 0, // Stack size (0 = use default)
70 pfunc
, // Thread start address
71 data
, // Parameter to pass to the thread
76 if ( hThread
== NULL
)
78 // Thread creation failed.
79 // More details can be retrieved by calling GetLastError()
84 CloseHandle( hThread
);
91 if( pthread_create( &hThread
, NULL
, pfunc
, data
) )
97 pthread_detach( hThread
);