2 * AtmoThread.cpp: Base thread class for all threads inside AtmoWin
4 * See the README.txt file for copyright information and how to reach the author(s).
14 #include "AtmoThread.h"
16 #if defined(_ATMO_VLC_PLUGIN_)
18 CThread::CThread(vlc_object_t *pOwner)
20 m_bTerminated = ATMO_FALSE;
21 vlc_mutex_init( &m_TerminateLock );
22 vlc_cond_init( &m_TerminateCond );
24 m_HasThread = ATMO_FALSE;
29 CThread::CThread(void)
31 m_bTerminated = ATMO_FALSE;
33 m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
34 this, CREATE_SUSPENDED, &m_dwThreadID);
36 m_hTerminateEvent = CreateEvent(NULL,0,0,NULL);
43 #if defined(_ATMO_VLC_PLUGIN_)
45 CThread::~CThread(void)
47 assert(m_HasThread == ATMO_FALSE);
48 vlc_mutex_destroy( &m_TerminateLock );
49 vlc_cond_destroy( &m_TerminateCond );
54 CThread::~CThread(void)
56 CloseHandle(m_hThread);
57 CloseHandle(m_hTerminateEvent);
62 #if defined(_ATMO_VLC_PLUGIN_)
64 void *CThread::ThreadProc(void *obj)
66 CThread *pThread = static_cast<CThread*>(obj);
68 int canc = vlc_savecancel ();
70 vlc_restorecancel (canc);
77 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
79 CThread *pThread = (CThread *)lpParameter;
81 return pThread->Execute();
89 DWORD CThread::Execute(void)
92 to do implement! override!
101 void CThread::Terminate(void)
103 // Set Termination Flag and EventObject!
104 // and wait for Termination
106 #if defined(_ATMO_VLC_PLUGIN_)
107 if(m_HasThread != ATMO_FALSE)
109 vlc_mutex_lock( &m_TerminateLock );
110 m_bTerminated = ATMO_TRUE;
111 vlc_cond_signal( &m_TerminateCond );
112 vlc_mutex_unlock( &m_TerminateLock );
114 vlc_cancel( m_Thread );
115 vlc_join( m_Thread, NULL );
118 m_bTerminated = ATMO_TRUE;
119 SetEvent(m_hTerminateEvent);
120 WaitForSingleObject(m_hThread,INFINITE);
126 m_bTerminated = ATMO_FALSE;
128 #if defined(_ATMO_VLC_PLUGIN_)
129 if (vlc_clone( &m_Thread, CThread::ThreadProc, this, VLC_THREAD_PRIORITY_LOW))
131 m_HasThread = ATMO_FALSE;
132 msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
136 m_HasThread = ATMO_TRUE;;
141 ResetEvent(m_hTerminateEvent);
142 ResumeThread(m_hThread);
148 does a sleep if the sleep was interrupted through
149 the thread kill event return false...
151 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
153 #if defined(_ATMO_VLC_PLUGIN_)
155 vlc_mutex_lock( &m_TerminateLock );
156 vlc_cond_timedwait(&m_TerminateCond,
158 mdate() + (mtime_t)(millisekunden * 1000));
159 temp = m_bTerminated;
160 vlc_mutex_unlock( &m_TerminateLock );
164 DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
165 return (res == WAIT_TIMEOUT);