-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
102 lines (88 loc) · 2.09 KB
/
thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "thread.h"
#include "utils.h"
#include "memory.h"
#ifdef _WIN32
# include <windows.h>
#endif
TThread Thread_Start( int (__stdcall *func)(void*), void * ptr ) {
#ifdef _WIN32
TThread thread = CreateThread( 0, 0, (LPTHREAD_START_ROUTINE)func, ptr, 0, 0 );
if( !thread ) {
Util_RaiseError( "Unable to start thread!" );
}
return thread;
#endif
}
TEvent Event_Create() {
#ifdef _WIN32
return CreateEvent( 0, 0, 0, 0 );
#endif
}
TCriticalSection * CriticalSection_Create( void ) {
#ifdef _WIN32
CRITICAL_SECTION * cs = (CRITICAL_SECTION*)malloc( sizeof( CRITICAL_SECTION ) );
InitializeCriticalSection( cs );
return (TCriticalSection)cs;
#endif
}
void CriticalSection_Enter( TCriticalSection * cs ) {
if( !cs ) {
return;
}
#ifdef _WIN32
CRITICAL_SECTION * criticalSection = (CRITICAL_SECTION*)cs;
EnterCriticalSection( criticalSection );
#endif
}
bool CriticalSection_TryEnter( TCriticalSection * cs ) {
if( !cs ) {
return false;
}
#ifdef _WIN32
CRITICAL_SECTION * criticalSection = (CRITICAL_SECTION*)cs;
return TryEnterCriticalSection( criticalSection );
#endif
}
void CriticalSection_Leave( TCriticalSection * cs ) {
if( !cs ) {
return;
}
#ifdef _WIN32
CRITICAL_SECTION * criticalSection = (CRITICAL_SECTION*)cs;
LeaveCriticalSection( criticalSection );
#endif
}
void CriticalSection_Delete( TCriticalSection * cs ) {
if( !cs ) {
return;
}
#ifdef _WIN32
CRITICAL_SECTION * criticalSection = (CRITICAL_SECTION*)cs;
DeleteCriticalSection( criticalSection );
#endif
}
void Event_Set( TEvent event ) {
#ifdef _WIN32
SetEvent( event );
#endif
}
void Event_Destroy( TEvent * event ) {
#ifdef _WIN32
CloseHandle( event );
#endif
}
void Event_Reset( TEvent * event ) {
#ifdef _WIN32
ResetEvent( event );
#endif
}
int Event_WaitMultiple( int count, TEvent * event ) {
#ifdef _WIN32
return WaitForMultipleObjects( count, event, 0, INFINITE );
#endif
}
int Event_WaitSingle( TEvent * event ) {
#ifdef _WIN32
return WaitForSingleObject( event, INFINITE );
#endif
}