File: ethread_win.cpp

package info (click to toggle)
codelite 12.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (73 lines) | stat: -rw-r--r-- 1,450 bytes parent folder | download | duplicates (5)
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
#if defined(__WXMSW__)

#include "ethread_win.h"
#include <process.h>
#include "ethread.h"

static unsigned __stdcall startFunc(void* arg)
{
	eThread *thread = reinterpret_cast<eThread*>(arg);
	if(thread) {
		// call the thread main loop
		thread->start();
	}
	
	// terminate the thread
	_endthreadex( 0 );

	return 0;
}

eThreadImpl::eThreadImpl()
: m_stopEvent(INVALID_HANDLE_VALUE)
, m_handle(INVALID_HANDLE_VALUE)
{
	m_stopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
}

eThreadImpl::~eThreadImpl()
{
	CloseHandle(m_stopEvent);
	m_stopEvent = INVALID_HANDLE_VALUE;
}

void eThreadImpl::run(eThread *thread)
{
	m_handle = (HANDLE)_beginthreadex
	           (
	               NULL,             // default security
	               0,				// default stack size
	               &startFunc, 		// entry point
	               thread,
	               0,
	               (unsigned int *)&m_tid
	           );
}

bool eThreadImpl::testDestroy()
{
	DWORD dwRet = WaitForSingleObject(m_stopEvent, 0);
	if(dwRet == WAIT_OBJECT_0) {
		// the event is signaled
		return true;
	}
	return false;
}

void eThreadImpl::requestStop()
{
	SetEvent(m_stopEvent);
}

void eThreadImpl::wait(long timeout)
{
	if(m_handle != INVALID_HANDLE_VALUE){
		if(WaitForSingleObject(m_handle, timeout) == WAIT_OBJECT_0){
			CloseHandle(m_handle);
			m_handle = INVALID_HANDLE_VALUE;
		}
	}
}

#endif // __WXMSW__