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_pAtmoThread, &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_destroy(m_pAtmoThread);
60 CThread::~CThread(void)
62 CloseHandle(m_hThread);
63 CloseHandle(m_hTerminateEvent);
68 #if defined(_ATMO_VLC_PLUGIN_)
70 void CThread::ThreadProc(atmo_thread_t *pAtmoThread)
72 CThread *pThread = (CThread *)pAtmoThread->p_thread;
74 // give feedback I'am running?
75 vlc_thread_ready( pThread->m_pAtmoThread );
84 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
86 CThread *aThread = (CThread *)lpParameter;
88 return aThread->Execute();
96 DWORD CThread::Execute(void)
99 to do implement! override!
101 while(!bTerminated) {
108 void CThread::Terminate(void)
110 // Set Termination Flag and EventObject!
111 // and wait for Termination
112 m_bTerminated = ATMO_TRUE;
114 #if defined(_ATMO_VLC_PLUGIN_)
117 vlc_mutex_lock( &m_TerminateLock );
118 vlc_cond_signal( &m_TerminateCond );
119 vlc_mutex_unlock( &m_TerminateLock );
120 vlc_object_kill( m_pAtmoThread );
122 vlc_thread_join( m_pAtmoThread );
125 SetEvent(m_hTerminateEvent);
126 WaitForSingleObject(m_hThread,INFINITE);
132 m_bTerminated = ATMO_FALSE;
134 #if defined(_ATMO_VLC_PLUGIN_)
135 m_pAtmoThread->b_die = VLC_FALSE;
136 if(vlc_thread_create( m_pAtmoThread,
137 "Atmo-CThread-Class",
139 VLC_THREAD_PRIORITY_LOW,
142 msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
147 ResetEvent(m_hTerminateEvent);
148 ResumeThread(m_hThread);
154 does a sleep if the sleep was interrupted through
155 the thread kill event return false...
157 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
159 #if defined(_ATMO_VLC_PLUGIN_)
160 vlc_mutex_lock( &m_TerminateLock );
161 int value = vlc_cond_timedwait(&m_TerminateCond,
163 mdate() + (mtime_t)(millisekunden * 1000));
164 vlc_mutex_unlock( &m_TerminateLock );
168 DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
169 return (res == WAIT_TIMEOUT);