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_pAtmoThread = (atmo_thread_t *)vlc_object_create( pOwner,
15 sizeof(atmo_thread_t) );
18 m_pAtmoThread->p_thread = this;
19 this->m_pOwner = pOwner;
21 vlc_object_attach( m_pAtmoThread, m_pOwner);
23 vlc_mutex_init( &m_TerminateLock );
24 vlc_cond_init( &m_TerminateCond );
30 CThread::CThread(void)
32 m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
33 this, CREATE_SUSPENDED, &m_dwThreadID);
34 m_hTerminateEvent = CreateEvent(NULL,ATMO_FALSE,ATMO_FALSE,NULL);
41 #if defined(_ATMO_VLC_PLUGIN_)
43 CThread::~CThread(void)
47 vlc_mutex_destroy( &m_TerminateLock );
48 vlc_cond_destroy( &m_TerminateCond );
49 vlc_object_detach(m_pAtmoThread);
50 vlc_object_release(m_pAtmoThread);
56 CThread::~CThread(void)
58 CloseHandle(m_hThread);
59 CloseHandle(m_hTerminateEvent);
64 #if defined(_ATMO_VLC_PLUGIN_)
66 void *CThread::ThreadProc(vlc_object_t *obj)
68 atmo_thread_t *pAtmoThread = (atmo_thread_t *)obj;
69 CThread *pThread = (CThread *)pAtmoThread->p_thread;
73 canc = vlc_savecancel ();
75 vlc_restorecancel (canc);
82 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
84 CThread *aThread = (CThread *)lpParameter;
86 return aThread->Execute();
94 DWORD CThread::Execute(void)
97 to do implement! override!
106 void CThread::Terminate(void)
108 // Set Termination Flag and EventObject!
109 // and wait for Termination
110 m_bTerminated = ATMO_TRUE;
112 #if defined(_ATMO_VLC_PLUGIN_)
115 vlc_mutex_lock( &m_TerminateLock );
116 vlc_cond_signal( &m_TerminateCond );
117 vlc_mutex_unlock( &m_TerminateLock );
118 vlc_object_kill( m_pAtmoThread );
120 vlc_thread_join( m_pAtmoThread );
123 SetEvent(m_hTerminateEvent);
124 WaitForSingleObject(m_hThread,INFINITE);
130 m_bTerminated = ATMO_FALSE;
132 #if defined(_ATMO_VLC_PLUGIN_)
133 m_pAtmoThread->b_die = false;
134 if(vlc_thread_create( m_pAtmoThread,
135 "Atmo-CThread-Class",
137 VLC_THREAD_PRIORITY_LOW ))
139 msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
144 ResetEvent(m_hTerminateEvent);
145 ResumeThread(m_hThread);
151 does a sleep if the sleep was interrupted through
152 the thread kill event return false...
154 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
156 #if defined(_ATMO_VLC_PLUGIN_)
157 vlc_mutex_lock( &m_TerminateLock );
158 int value = vlc_cond_timedwait(&m_TerminateCond,
160 mdate() + (mtime_t)(millisekunden * 1000));
161 vlc_mutex_unlock( &m_TerminateLock );
165 DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
166 return (res == WAIT_TIMEOUT);