]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/AtmoThread.cpp
cf96680a41368610bc23cab8e9a98ee9bb3ebfca
[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_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          canc = vlc_savecancel ();
78          pThread->Execute();
79          vlc_restorecancel (canc);
80       }
81       return NULL;
82 }
83
84 #else
85
86 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
87 {
88            CThread *aThread = (CThread *)lpParameter;
89            if(aThread)
90               return aThread->Execute();
91            else
92                   return (DWORD)-1;
93 }
94
95 #endif
96
97
98 DWORD CThread::Execute(void)
99 {
100   /*
101     to do implement! override!
102
103         while(!bTerminated) {
104          ...
105         }
106   */    
107  return 0;
108 }
109
110 void CThread::Terminate(void)
111 {
112    // Set Termination Flag and EventObject!
113    // and wait for Termination
114    m_bTerminated = ATMO_TRUE;
115
116 #if defined(_ATMO_VLC_PLUGIN_)
117    if(m_pAtmoThread)
118    {
119       vlc_mutex_lock( &m_TerminateLock );
120       vlc_cond_signal( &m_TerminateCond  );
121       vlc_mutex_unlock( &m_TerminateLock );
122       vlc_object_kill( m_pAtmoThread );
123
124       vlc_thread_join( m_pAtmoThread );
125    }
126 #else
127    SetEvent(m_hTerminateEvent);
128    WaitForSingleObject(m_hThread,INFINITE);
129 #endif
130 }
131
132 void CThread::Run()
133 {
134    m_bTerminated = ATMO_FALSE;
135
136 #if defined(_ATMO_VLC_PLUGIN_)
137    m_pAtmoThread->b_die = false;
138    if(vlc_thread_create( m_pAtmoThread,
139                          "Atmo-CThread-Class",
140                          CThread::ThreadProc,
141                          VLC_THREAD_PRIORITY_LOW,
142                          false ))
143    {
144       msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
145    }
146
147 #else
148
149    ResetEvent(m_hTerminateEvent);
150    ResumeThread(m_hThread);
151
152 #endif
153 }
154
155 /*
156    does a sleep if the sleep was interrupted through
157    the thread kill event return false...
158 */
159 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
160 {
161 #if defined(_ATMO_VLC_PLUGIN_)
162      vlc_mutex_lock( &m_TerminateLock );
163      int value = vlc_cond_timedwait(&m_TerminateCond,
164                                     &m_TerminateLock,
165                                     mdate() + (mtime_t)(millisekunden * 1000));
166      vlc_mutex_unlock( &m_TerminateLock );
167      return (value != 0);
168
169 #else
170      DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
171          return (res == WAIT_TIMEOUT);
172 #endif
173 }
174