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