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).
8 #include "AtmoThread.h"
10 #if defined(_ATMO_VLC_PLUGIN_)
12 CThread::CThread(vlc_object_t *pOwner)
14 m_bTerminated = ATMO_FALSE;
15 m_pAtmoThread = (atmo_thread_t *)vlc_object_create( pOwner,
16 sizeof(atmo_thread_t) );
19 m_pAtmoThread->p_thread = this;
20 this->m_pOwner = pOwner;
22 vlc_object_attach( m_pAtmoThread, m_pOwner);
24 vlc_mutex_init( &m_TerminateLock );
25 vlc_cond_init( &m_TerminateCond );
31 CThread::CThread(void)
33 m_bTerminated = ATMO_FALSE;
35 m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
36 this, CREATE_SUSPENDED, &m_dwThreadID);
38 m_hTerminateEvent = CreateEvent(NULL,0,0,NULL);
45 #if defined(_ATMO_VLC_PLUGIN_)
47 CThread::~CThread(void)
51 vlc_mutex_destroy( &m_TerminateLock );
52 vlc_cond_destroy( &m_TerminateCond );
53 vlc_object_release(m_pAtmoThread);
59 CThread::~CThread(void)
61 CloseHandle(m_hThread);
62 CloseHandle(m_hTerminateEvent);
67 #if defined(_ATMO_VLC_PLUGIN_)
69 void *CThread::ThreadProc(vlc_object_t *obj)
71 atmo_thread_t *pAtmoThread = (atmo_thread_t *)obj;
72 CThread *pThread = (CThread *)pAtmoThread->p_thread;
76 canc = vlc_savecancel ();
78 vlc_restorecancel (canc);
85 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
87 CThread *pThread = (CThread *)lpParameter;
89 return pThread->Execute();
97 DWORD CThread::Execute(void)
100 to do implement! override!
102 while(!bTerminated) {
109 void CThread::Terminate(void)
111 // Set Termination Flag and EventObject!
112 // and wait for Termination
114 #if defined(_ATMO_VLC_PLUGIN_)
117 vlc_mutex_lock( &m_TerminateLock );
118 m_bTerminated = ATMO_TRUE;
119 vlc_cond_signal( &m_TerminateCond );
120 vlc_mutex_unlock( &m_TerminateLock );
122 vlc_object_kill( m_pAtmoThread );
123 vlc_thread_join( m_pAtmoThread );
126 m_bTerminated = ATMO_TRUE;
127 SetEvent(m_hTerminateEvent);
128 WaitForSingleObject(m_hThread,INFINITE);
134 m_bTerminated = ATMO_FALSE;
136 #if defined(_ATMO_VLC_PLUGIN_)
137 m_pAtmoThread->b_die = false;
138 if(vlc_thread_create( m_pAtmoThread,
139 "Atmo-CThread-Class",
141 VLC_THREAD_PRIORITY_LOW ))
143 msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
148 ResetEvent(m_hTerminateEvent);
149 ResumeThread(m_hThread);
155 does a sleep if the sleep was interrupted through
156 the thread kill event return false...
158 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
160 #if defined(_ATMO_VLC_PLUGIN_)
162 vlc_mutex_lock( &m_TerminateLock );
163 vlc_cond_timedwait(&m_TerminateCond,
165 mdate() + (mtime_t)(millisekunden * 1000));
166 temp = m_bTerminated;
167 vlc_mutex_unlock( &m_TerminateLock );
171 DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
172 return (res == WAIT_TIMEOUT);