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