-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread_win.cpp
100 lines (86 loc) · 1.64 KB
/
thread_win.cpp
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
/*
* Licensed under CLIFE license. See LICENCE.TXT
*
* Produced by: Jeff Lait
*
* CLIFE Development
*
* NAME: thread_win.h ( CLIFE, C++ )
*
* COMMENTS:
* Attempt at a threading class.
*/
#ifndef _3DS
#include "thread_win.h"
THREAD_WIN::THREAD_WIN()
{
mySelf = 0;
InitializeCriticalSection(&myCritSec);
// Default to idle.
myDoneEvent = CreateEvent(0, 1, 1, 0);
// Default to not started
myStartEvent = CreateEvent(0, 1, 0, 0);
}
THREAD_WIN::~THREAD_WIN()
{
join();
CloseHandle(myDoneEvent);
CloseHandle(myStartEvent);
CloseHandle(mySelf);
mySelf = 0;
DeleteCriticalSection(&myCritSec);
}
void
THREAD_WIN::start(THREADmainFunc func, void *data)
{
DWORD id;
// If we are still running, block until the current task completes.
if (mySelf)
join();
myCB = func;
myCBData = data;
if (!mySelf)
{
mySelf = CreateThread(NULL, 0,
(unsigned long(__stdcall *)(void *))THREAD::wrapper,
(void *)this, 0, &id);
}
jobsready();
}
void
THREAD_WIN::jobsready()
{
EnterCriticalSection(&myCritSec);
ResetEvent(myDoneEvent);
SetEvent(myStartEvent);
LeaveCriticalSection(&myCritSec);
}
void
THREAD_WIN::iamdonenow()
{
EnterCriticalSection(&myCritSec);
ResetEvent(myStartEvent);
SetEvent(myDoneEvent);
LeaveCriticalSection(&myCritSec);
}
void
THREAD_WIN::waittillimready()
{
WaitForSingleObject(myStartEvent, INFINITE);
}
void
THREAD_WIN::join()
{
WaitForSingleObject(myDoneEvent, INFINITE);
}
void
THREAD_WIN::kill()
{
if (mySelf)
{
TerminateThread(mySelf, 0);
CloseHandle(mySelf);
mySelf = 0;
}
}
#endif