forked from ambilight-4-mediaportal/AtmoWin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtmoThread.cpp
More file actions
168 lines (131 loc) · 2.89 KB
/
Copy pathAtmoThread.cpp
File metadata and controls
168 lines (131 loc) · 2.89 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* AtmoThread.cpp: Base thread class for all threads inside AtmoWin
*
* See the README.txt file for copyright information and how to reach the author(s).
*
* $Id: 50c9c4b574cf3409b74b6f3b777df23d3fbade7a $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include "AtmoThread.h"
#if defined(_ATMO_VLC_PLUGIN_)
CThread::CThread(vlc_object_t *pOwner)
{
m_bTerminated = ATMO_FALSE;
vlc_mutex_init( &m_TerminateLock );
vlc_cond_init( &m_TerminateCond );
m_pOwner = pOwner;
m_HasThread = ATMO_FALSE;
}
#else
CThread::CThread(void)
{
m_bTerminated = ATMO_FALSE;
m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
this, CREATE_SUSPENDED, &m_dwThreadID);
m_hTerminateEvent = CreateEvent(NULL,0,0,NULL);
}
#endif
#if defined(_ATMO_VLC_PLUGIN_)
CThread::~CThread(void)
{
assert(m_HasThread == ATMO_FALSE);
vlc_mutex_destroy( &m_TerminateLock );
vlc_cond_destroy( &m_TerminateCond );
}
#else
CThread::~CThread(void)
{
CloseHandle(m_hThread);
CloseHandle(m_hTerminateEvent);
}
#endif
#if defined(_ATMO_VLC_PLUGIN_)
void *CThread::ThreadProc(void *obj)
{
CThread *pThread = static_cast<CThread*>(obj);
int canc = vlc_savecancel ();
pThread->Execute();
vlc_restorecancel (canc);
return NULL;
}
#else
DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
{
CThread *pThread = (CThread *)lpParameter;
if(pThread)
return pThread->Execute();
else
return (DWORD)-1;
}
#endif
DWORD CThread::Execute(void)
{
/*
to do implement! override!
while(!bTerminated) {
...
}
*/
return 0;
}
void CThread::Terminate(void)
{
// Set Termination Flag and EventObject!
// and wait for Termination
#if defined(_ATMO_VLC_PLUGIN_)
if(m_HasThread != ATMO_FALSE)
{
vlc_mutex_lock( &m_TerminateLock );
m_bTerminated = ATMO_TRUE;
vlc_cond_signal( &m_TerminateCond );
vlc_mutex_unlock( &m_TerminateLock );
vlc_cancel( m_Thread );
vlc_join( m_Thread, NULL );
}
#else
m_bTerminated = ATMO_TRUE;
SetEvent(m_hTerminateEvent);
WaitForSingleObject(m_hThread,INFINITE);
#endif
}
void CThread::Run()
{
m_bTerminated = ATMO_FALSE;
#if defined(_ATMO_VLC_PLUGIN_)
if (vlc_clone( &m_Thread, CThread::ThreadProc, this, VLC_THREAD_PRIORITY_LOW))
{
m_HasThread = ATMO_FALSE;
msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
}
else
{
m_HasThread = ATMO_TRUE;;
}
#else
ResetEvent(m_hTerminateEvent);
ResumeThread(m_hThread);
#endif
}
/*
does a sleep if the sleep was interrupted through
the thread kill event return false...
*/
ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
{
#if defined(_ATMO_VLC_PLUGIN_)
ATMO_BOOL temp;
vlc_mutex_lock( &m_TerminateLock );
vlc_cond_timedwait(&m_TerminateCond,
&m_TerminateLock,
mdate() + (mtime_t)(millisekunden * 1000));
temp = m_bTerminated;
vlc_mutex_unlock( &m_TerminateLock );
return !temp;
#else
DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
return (res == WAIT_TIMEOUT);
#endif
}