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;
77 // give feedback I'am running?
78 vlc_thread_ready( pThread->m_pAtmoThread );
80 canc = vlc_savecancel ();
82 vlc_restorecancel (canc);
89 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
91 CThread *aThread = (CThread *)lpParameter;
93 return aThread->Execute();
101 DWORD CThread::Execute(void)
104 to do implement! override!
106 while(!bTerminated) {
113 void CThread::Terminate(void)
115 // Set Termination Flag and EventObject!
116 // and wait for Termination
117 m_bTerminated = ATMO_TRUE;
119 #if defined(_ATMO_VLC_PLUGIN_)
122 vlc_mutex_lock( &m_TerminateLock );
123 vlc_cond_signal( &m_TerminateCond );
124 vlc_mutex_unlock( &m_TerminateLock );
125 vlc_object_kill( m_pAtmoThread );
127 vlc_thread_join( m_pAtmoThread );
130 SetEvent(m_hTerminateEvent);
131 WaitForSingleObject(m_hThread,INFINITE);
137 m_bTerminated = ATMO_FALSE;
139 #if defined(_ATMO_VLC_PLUGIN_)
140 m_pAtmoThread->b_die = false;
141 if(vlc_thread_create( m_pAtmoThread,
142 "Atmo-CThread-Class",
144 VLC_THREAD_PRIORITY_LOW,
147 msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
152 ResetEvent(m_hTerminateEvent);
153 ResumeThread(m_hThread);
159 does a sleep if the sleep was interrupted through
160 the thread kill event return false...
162 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
164 #if defined(_ATMO_VLC_PLUGIN_)
165 vlc_mutex_lock( &m_TerminateLock );
166 int value = vlc_cond_timedwait(&m_TerminateCond,
168 mdate() + (mtime_t)(millisekunden * 1000));
169 vlc_mutex_unlock( &m_TerminateLock );
173 DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
174 return (res == WAIT_TIMEOUT);