Remove unhandled error warning
[vlc.git] / modules / video_filter / atmo / AtmoThread.cpp
1 /*
2  * AtmoThread.cpp: Base thread class for all threads inside AtmoWin
3  *
4  * See the README.txt file for copyright information and how to reach the author(s).
5  *
6  * $Id$
7  */
8 #include "AtmoThread.h"
9
10 #if defined(_ATMO_VLC_PLUGIN_)
11
12 CThread::CThread(vlc_object_t *pOwner)
13 {
14     m_pAtmoThread = (atmo_thread_t *)vlc_object_create( pOwner,
15                                                         sizeof(atmo_thread_t) );
16     if(m_pAtmoThread)
17     {
18         m_pAtmoThread->p_thread = this;
19         this->m_pOwner = pOwner;
20
21         vlc_object_attach( m_pAtmoThread, m_pOwner);
22
23         vlc_mutex_init( &m_TerminateLock );
24         vlc_cond_init( &m_TerminateCond );
25     }
26 }
27
28 #else
29
30 CThread::CThread(void)
31 {
32   m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
33                            this, CREATE_SUSPENDED, &m_dwThreadID);
34   m_hTerminateEvent = CreateEvent(NULL,ATMO_FALSE,ATMO_FALSE,NULL);
35 }
36
37 #endif
38
39
40
41 #if defined(_ATMO_VLC_PLUGIN_)
42
43 CThread::~CThread(void)
44 {
45   if(m_pAtmoThread)
46   {
47       vlc_mutex_destroy( &m_TerminateLock );
48       vlc_cond_destroy( &m_TerminateCond );
49       vlc_object_detach(m_pAtmoThread);
50       vlc_object_release(m_pAtmoThread);
51   }
52 }
53
54 #else
55
56 CThread::~CThread(void)
57 {
58   CloseHandle(m_hThread);       
59   CloseHandle(m_hTerminateEvent);
60 }
61
62 #endif
63
64 #if defined(_ATMO_VLC_PLUGIN_)
65
66 void *CThread::ThreadProc(vlc_object_t *obj)
67 {
68       atmo_thread_t *pAtmoThread = (atmo_thread_t *)obj;
69       CThread *pThread = (CThread *)pAtmoThread->p_thread;
70       if(pThread) {
71          int canc;
72
73          canc = vlc_savecancel ();
74          pThread->Execute();
75          vlc_restorecancel (canc);
76       }
77       return NULL;
78 }
79
80 #else
81
82 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
83 {
84            CThread *aThread = (CThread *)lpParameter;
85            if(aThread)
86               return aThread->Execute();
87            else
88                   return (DWORD)-1;
89 }
90
91 #endif
92
93
94 DWORD CThread::Execute(void)
95 {
96   /*
97     to do implement! override!
98
99         while(!bTerminated) {
100          ...
101         }
102   */    
103  return 0;
104 }
105
106 void CThread::Terminate(void)
107 {
108    // Set Termination Flag and EventObject!
109    // and wait for Termination
110    m_bTerminated = ATMO_TRUE;
111
112 #if defined(_ATMO_VLC_PLUGIN_)
113    if(m_pAtmoThread)
114    {
115       vlc_mutex_lock( &m_TerminateLock );
116       vlc_cond_signal( &m_TerminateCond  );
117       vlc_mutex_unlock( &m_TerminateLock );
118       vlc_object_kill( m_pAtmoThread );
119
120       vlc_thread_join( m_pAtmoThread );
121    }
122 #else
123    SetEvent(m_hTerminateEvent);
124    WaitForSingleObject(m_hThread,INFINITE);
125 #endif
126 }
127
128 void CThread::Run()
129 {
130    m_bTerminated = ATMO_FALSE;
131
132 #if defined(_ATMO_VLC_PLUGIN_)
133    m_pAtmoThread->b_die = false;
134    if(vlc_thread_create( m_pAtmoThread,
135                          "Atmo-CThread-Class",
136                          CThread::ThreadProc,
137                          VLC_THREAD_PRIORITY_LOW ))
138    {
139       msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
140    }
141
142 #else
143
144    ResetEvent(m_hTerminateEvent);
145    ResumeThread(m_hThread);
146
147 #endif
148 }
149
150 /*
151    does a sleep if the sleep was interrupted through
152    the thread kill event return false...
153 */
154 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
155 {
156 #if defined(_ATMO_VLC_PLUGIN_)
157      vlc_mutex_lock( &m_TerminateLock );
158      int value = vlc_cond_timedwait(&m_TerminateCond,
159                                     &m_TerminateLock,
160                                     mdate() + (mtime_t)(millisekunden * 1000));
161      vlc_mutex_unlock( &m_TerminateLock );
162      return (value != 0);
163
164 #else
165      DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
166          return (res == WAIT_TIMEOUT);
167 #endif
168 }
169