bc610f6b9192f8818426e58c334a23d581430848
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
12 typedef unsigned int uint
;
14 #define vg_min( A, B ) ((A)<(B)?(A):(B))
15 #define vg_max( A, B ) ((A)>(B)?(A):(B))
16 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
19 // ==================================================================================================================
21 // Pthred emulation for windows
24 #define MUTEX_TYPE HANDLE
25 #define MUTEX_INITIALIZER NULL
26 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
27 #define MUTEX_CLEANUP(x) (CloseHandle(x))
28 #define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
29 #define MUTEX_UNLOCK(x) (ReleaseMutex(x))
31 int emulate_pthread_mutex_lock( volatile MUTEX_TYPE
*mx
)
33 if( *mx
== NULL
) /* static initializer? */
35 HANDLE p
= CreateMutex( NULL
, FALSE
, NULL
);
36 if( InterlockedCompareExchangePointer( (PVOID
*)mx
, (PVOID
)p
, NULL
) != NULL
)
40 return WaitForSingleObject( *mx
, INFINITE
) == WAIT_FAILED
;
44 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
45 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
46 #define MUTEX_TYPE pthread_mutex_t
47 #define MUTEX_INITIALIZER {0}
51 int vg_thread_run( void *pfunc
, void *data
)
55 HANDLE hThread
= CreateThread
57 NULL
, // Thread attributes
58 0, // Stack size (0 = use default)
59 pfunc
, // Thread start address
60 data
, // Parameter to pass to the thread
65 if ( hThread
== NULL
)
67 // Thread creation failed.
68 // More details can be retrieved by calling GetLastError()
73 CloseHandle( hThread
);
80 if( pthread_create( &hThread
, NULL
, pfunc
, data
) )
86 pthread_detach( hThread
);