]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/AtmoThread.cpp
Remove useless vlc_object_detach() before vlc_object_release()
[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     m_bTerminated  = ATMO_FALSE;
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         vlc_cond_init( &m_TerminateCond );
26     }
27 }
28
29 #else
30
31 CThread::CThread(void)
32 {
33   m_bTerminated  = ATMO_FALSE;
34
35   m_hThread = CreateThread(NULL, 0, CThread::ThreadProc ,
36                            this, CREATE_SUSPENDED, &m_dwThreadID);
37
38   m_hTerminateEvent = CreateEvent(NULL,0,0,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_release(m_pAtmoThread);
54   }
55 }
56
57 #else
58
59 CThread::~CThread(void)
60 {
61   CloseHandle(m_hThread);       
62   CloseHandle(m_hTerminateEvent);
63 }
64
65 #endif
66
67 #if defined(_ATMO_VLC_PLUGIN_)
68
69 void *CThread::ThreadProc(vlc_object_t *obj)
70 {
71       atmo_thread_t *pAtmoThread = (atmo_thread_t *)obj;
72       CThread *pThread = (CThread *)pAtmoThread->p_thread;
73       if(pThread) {
74          int canc;
75
76          canc = vlc_savecancel ();
77          pThread->Execute();
78          vlc_restorecancel (canc);
79       }
80       return NULL;
81 }
82
83 #else
84
85 DWORD WINAPI CThread::ThreadProc(LPVOID lpParameter)
86 {
87            CThread *pThread = (CThread *)lpParameter;
88            if(pThread)
89               return pThread->Execute();
90            else
91                   return (DWORD)-1;
92 }
93
94 #endif
95
96
97 DWORD CThread::Execute(void)
98 {
99   /*
100     to do implement! override!
101
102         while(!bTerminated) {
103          ...
104         }
105   */    
106  return 0;
107 }
108
109 void CThread::Terminate(void)
110 {
111    // Set Termination Flag and EventObject!
112    // and wait for Termination
113
114 #if defined(_ATMO_VLC_PLUGIN_)
115    if(m_pAtmoThread)
116    {
117       vlc_mutex_lock( &m_TerminateLock );
118       m_bTerminated = ATMO_TRUE;
119       vlc_cond_signal( &m_TerminateCond  );
120       vlc_mutex_unlock( &m_TerminateLock );
121
122       vlc_object_kill( m_pAtmoThread );
123       vlc_thread_join( m_pAtmoThread );
124    }
125 #else
126    m_bTerminated = ATMO_TRUE;
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    {
143       msg_Err( m_pOwner, "cannot launch one of the AtmoLight threads");
144    }
145
146 #else
147
148    ResetEvent(m_hTerminateEvent);
149    ResumeThread(m_hThread);
150
151 #endif
152 }
153
154 /*
155    does a sleep if the sleep was interrupted through
156    the thread kill event return false...
157 */
158 ATMO_BOOL CThread::ThreadSleep(DWORD millisekunden)
159 {
160 #if defined(_ATMO_VLC_PLUGIN_)
161      ATMO_BOOL temp;
162      vlc_mutex_lock( &m_TerminateLock );
163      vlc_cond_timedwait(&m_TerminateCond,
164                         &m_TerminateLock,
165                         mdate() + (mtime_t)(millisekunden * 1000));
166      temp = m_bTerminated;
167      vlc_mutex_unlock( &m_TerminateLock );
168      return !temp;
169
170 #else
171      DWORD res = WaitForSingleObject(m_hTerminateEvent,millisekunden);
172          return (res == WAIT_TIMEOUT);
173 #endif
174 }
175