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)
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 err = vlc_cond_init( m_pAtmoThread, &m_TerminateCond );
27 msg_Err( m_pAtmoThread, "vlc_cond_init failed %d",err);
34 CThread::CThread(void)
36 m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
37 this, CREATE_SUSPENDED, &m_dwThreadID);
38 m_hTerminateEvent = CreateEvent(NULL,ATMO_FALSE,ATMO_FALSE,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_detach(m_pAtmoThread);
54 vlc_object_release(m_pAtmoThread);
60 CThread::~CThread(void)
62 CloseHandle(m_hThread);
63 CloseHandle(m_hTerminateEvent);
68 #if defined(_ATMO_VLC_PLUGIN_)
70 void *CThread::ThreadProc(vlc_object_t *obj)
72 atmo_thread_t *pAtmoThread = (atmo_thread_t *)obj;
73 CThread *pThread = (CThread *)pAtmoThread->p_thread;
75 // give feedback I'am running?
76 vlc_thread_ready( pThread->m_pAtmoThread );
86 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
88 CThread *aThread = (CThread *)lpParameter;
90 return aThread->Execute();
98 DWORD CThread::Execute(void)
101 to do implement! override!
103 while(!bTerminated) {
110 void CThread::Terminate(void)
112 // Set Termination Flag and EventObject!
113 // and wait for Termination
114 m_bTerminated = ATMO_TRUE;
116 #if defined(_ATMO_VLC_PLUGIN_)
119 vlc_mutex_lock( &m_TerminateLock );
120 vlc_cond_signal( &m_TerminateCond );
121 vlc_mutex_unlock( &m_TerminateLock );
122 vlc_object_kill( m_pAtmoThread );
124 vlc_thread_join( m_pAtmoThread );
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,
144 msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
149 ResetEvent(m_hTerminateEvent);
150 ResumeThread(m_hThread);
156 does a sleep if the sleep was interrupted through
157 the thread kill event return false...
159 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
161 #if defined(_ATMO_VLC_PLUGIN_)
162 vlc_mutex_lock( &m_TerminateLock );
163 int value = vlc_cond_timedwait(&m_TerminateCond,
165 mdate() + (mtime_t)(millisekunden * 1000));
166 vlc_mutex_unlock( &m_TerminateLock );
170 DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
171 return (res == WAIT_TIMEOUT);