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