1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
12 typedef unsigned int uint
;
24 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
27 // ==================================================================================================================
29 // Pthred emulation for windows
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))
39 int emulate_pthread_mutex_lock( volatile MUTEX_TYPE
*mx
)
41 if( *mx
== NULL
) /* static initializer? */
43 HANDLE p
= CreateMutex( NULL
, FALSE
, NULL
);
44 if( InterlockedCompareExchangePointer( (PVOID
*)mx
, (PVOID
)p
, NULL
) != NULL
)
48 return WaitForSingleObject( *mx
, INFINITE
) == WAIT_FAILED
;
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}
59 int vg_thread_run( void *pfunc
, void *data
)
63 HANDLE hThread
= CreateThread
65 NULL
, // Thread attributes
66 0, // Stack size (0 = use default)
67 pfunc
, // Thread start address
68 data
, // Parameter to pass to the thread
73 if ( hThread
== NULL
)
75 // Thread creation failed.
76 // More details can be retrieved by calling GetLastError()
81 CloseHandle( hThread
);
88 if( pthread_create( &hThread
, NULL
, pfunc
, data
) )
94 pthread_detach( hThread
);