Fixed crashing in stopping parser.
[wine] / dlls / quartz / sysclock.c
1 /*
2  * Implementation of CLSID_SystemClock.
3  *
4  * Copyright (C) Hidenori TAKESHIMA <hidenori@a2.ctktv.ne.jp>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winerror.h"
28 #include "strmif.h"
29 #include "uuids.h"
30
31 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
33
34 #include "quartz_private.h"
35 #include "sysclock.h"
36
37
38 /***************************************************************************
39  *
40  *      new/delete for CLSID_SystemClock
41  *
42  */
43
44 /* can I use offsetof safely? - FIXME? */
45 static QUARTZ_IFEntry IFEntries[] =
46 {
47   { &IID_IReferenceClock, offsetof(CSystemClock,refclk)-offsetof(CSystemClock,unk) },
48 };
49
50
51 static void QUARTZ_DestroySystemClock(IUnknown* punk)
52 {
53         CSystemClock_THIS(punk,unk);
54
55         CSystemClock_UninitIReferenceClock( This );
56 }
57
58 HRESULT QUARTZ_CreateSystemClock(IUnknown* punkOuter,void** ppobj)
59 {
60         CSystemClock*   psc;
61         HRESULT hr;
62
63         TRACE("(%p,%p)\n",punkOuter,ppobj);
64
65         psc = (CSystemClock*)QUARTZ_AllocObj( sizeof(CSystemClock) );
66         if ( psc == NULL )
67                 return E_OUTOFMEMORY;
68
69         QUARTZ_IUnkInit( &psc->unk, punkOuter );
70         hr = CSystemClock_InitIReferenceClock( psc );
71         if ( FAILED(hr) )
72         {
73                 QUARTZ_FreeObj( psc );
74                 return hr;
75         }
76
77         psc->unk.pEntries = IFEntries;
78         psc->unk.dwEntries = sizeof(IFEntries)/sizeof(IFEntries[0]);
79         psc->unk.pOnFinalRelease = QUARTZ_DestroySystemClock;
80
81         *ppobj = (void*)(&psc->unk);
82
83         return S_OK;
84 }
85
86
87 /***************************************************************************
88  *
89  *      CLSID_SystemClock::IReferenceClock
90  *
91  */
92
93 #define QUARTZ_MSG_ADDTIMER                     (WM_APP+0)
94 #define QUARTZ_MSG_REMOVETIMER          (WM_APP+1)
95 #define QUARTZ_MSG_EXITTHREAD           (WM_APP+2)
96
97
98 /****************************************************************************/
99
100 static QUARTZ_TimerEntry* IReferenceClock_AllocTimerEntry(CSystemClock* This)
101 {
102         QUARTZ_TimerEntry*      pEntry;
103         DWORD   dw;
104
105         pEntry = &This->m_timerEntries[0];
106         for ( dw = 0; dw < WINE_QUARTZ_SYSCLOCK_TIMER_MAX; dw++ )
107         {
108                 if ( pEntry->hEvent == (HANDLE)NULL )
109                         return pEntry;
110                 pEntry ++;
111         }
112
113         return NULL;
114 }
115
116 static QUARTZ_TimerEntry* IReferenceClock_SearchTimer(CSystemClock* This, DWORD dwAdvCookie)
117 {
118         QUARTZ_TimerEntry*      pEntry;
119         DWORD   dw;
120
121         pEntry = &This->m_timerEntries[0];
122         for ( dw = 0; dw < WINE_QUARTZ_SYSCLOCK_TIMER_MAX; dw++ )
123         {
124                 if ( pEntry->hEvent != (HANDLE)NULL &&
125                          pEntry->dwAdvCookie == dwAdvCookie )
126                         return pEntry;
127                 pEntry ++;
128         }
129
130         return NULL;
131 }
132
133 static DWORD IReferenceClock_OnTimerUpdated(CSystemClock* This)
134 {
135         QUARTZ_TimerEntry*      pEntry;
136         REFERENCE_TIME  rtCur;
137         REFERENCE_TIME  rtSignal;
138         REFERENCE_TIME  rtCount;
139         HRESULT hr;
140         LONG    lCount;
141         DWORD   dw;
142         DWORD   dwTimeout = INFINITE;
143         DWORD   dwTimeoutCur;
144
145         hr = IReferenceClock_GetTime((IReferenceClock*)(&This->refclk),&rtCur);
146         if ( hr != NOERROR )
147                 return INFINITE;
148
149         pEntry = &This->m_timerEntries[0];
150         for ( dw = 0; dw < WINE_QUARTZ_SYSCLOCK_TIMER_MAX; dw++ )
151         {
152                 if ( pEntry->hEvent != (HANDLE)NULL )
153                 {
154                         rtSignal = pEntry->rtStart + pEntry->rtInterval;
155                         if ( rtCur >= rtSignal )
156                         {
157                                 if ( pEntry->fPeriodic )
158                                 {
159                                         rtCount = ((rtCur - pEntry->rtStart) / pEntry->rtInterval);
160                                         lCount = ( rtCount > (REFERENCE_TIME)0x7fffffff ) ?
161                                                 (LONG)0x7fffffff : (LONG)rtCount;
162                                         if ( !ReleaseSemaphore( pEntry->hEvent, lCount, NULL ) )
163                                         {
164                                                 while ( lCount > 0 )
165                                                 {
166                                                         if ( !ReleaseSemaphore( pEntry->hEvent, 1, NULL ) )
167                                                                 break;
168                                                 }
169                                         }
170                                         dwTimeout = 0;
171                                 }
172                                 else
173                                 {
174                                         TRACE( "signal an event\n" );
175                                         SetEvent( pEntry->hEvent );
176                                         pEntry->hEvent = (HANDLE)NULL;
177                                 }
178                         }
179                         else
180                         {
181                                 rtCount = rtSignal - rtCur;
182                                 /* [100ns] -> [ms] */
183                                 rtCount = (rtCount+(REFERENCE_TIME)9999)/(REFERENCE_TIME)10000;
184                                 dwTimeoutCur = (rtCount >= 0xfffffffe) ? (DWORD)0xfffffffe : (DWORD)rtCount;
185                                 if ( dwTimeout > dwTimeoutCur )
186                                         dwTimeout = dwTimeoutCur;
187                         }
188                 }
189                 pEntry ++;
190         }
191
192         return dwTimeout;
193 }
194
195 static
196 DWORD WINAPI IReferenceClock_TimerEntry( LPVOID lpvParam )
197 {
198         CSystemClock*   This = (CSystemClock*)lpvParam;
199         MSG     msg;
200         DWORD   dwRes;
201         DWORD   dwTimeout;
202
203         /* initialize the message queue. */
204         PeekMessageA( &msg, (HWND)NULL, 0, 0, PM_NOREMOVE );
205         /* resume the owner thread. */
206         SetEvent( This->m_hEventInit );
207
208         TRACE( "Enter message loop.\n" );
209
210         /* message loop. */
211         dwTimeout = INFINITE;
212         while ( 1 )
213         {
214                 if ( dwTimeout > 0 )
215                 {
216                         dwRes = MsgWaitForMultipleObjects(
217                                 0, NULL, FALSE,
218                                 dwTimeout,
219                                 QS_ALLEVENTS );
220                 }
221
222                 EnterCriticalSection( &This->m_csClock );
223                 dwTimeout = IReferenceClock_OnTimerUpdated(This);
224                 LeaveCriticalSection( &This->m_csClock );
225                 TRACE( "catch an event / timeout %lu\n", dwTimeout );
226
227                 while ( PeekMessageA( &msg, (HWND)NULL, 0, 0, PM_REMOVE ) )
228                 {
229                         if ( msg.message == WM_QUIT )
230                                 goto quitthread;
231
232                         if ( msg.hwnd != (HWND)NULL )
233                         {
234                                 TranslateMessage( &msg );
235                                 DispatchMessageA( &msg );
236                         }
237                         else
238                         {
239                                 switch ( msg.message )
240                                 {
241                                 case QUARTZ_MSG_ADDTIMER:
242                                 case QUARTZ_MSG_REMOVETIMER:
243                                         dwTimeout = 0;
244                                         break;
245                                 case QUARTZ_MSG_EXITTHREAD:
246                                         PostQuitMessage(0);
247                                         break;
248                                 default:
249                                         FIXME( "invalid message %04u\n", (unsigned)msg.message );
250                                         break;
251                                 }
252                         }
253                 }
254         }
255
256 quitthread:
257         TRACE( "quit thread\n" );
258         return 0;
259 }
260
261 /****************************************************************************/
262
263 static HRESULT WINAPI
264 IReferenceClock_fnQueryInterface(IReferenceClock* iface,REFIID riid,void** ppobj)
265 {
266         CSystemClock_THIS(iface,refclk);
267
268         TRACE("(%p)->()\n",This);
269
270         return IUnknown_QueryInterface(This->unk.punkControl,riid,ppobj);
271 }
272
273 static ULONG WINAPI
274 IReferenceClock_fnAddRef(IReferenceClock* iface)
275 {
276         CSystemClock_THIS(iface,refclk);
277
278         TRACE("(%p)->()\n",This);
279
280         return IUnknown_AddRef(This->unk.punkControl);
281 }
282
283 static ULONG WINAPI
284 IReferenceClock_fnRelease(IReferenceClock* iface)
285 {
286         CSystemClock_THIS(iface,refclk);
287
288         TRACE("(%p)->()\n",This);
289
290         return IUnknown_Release(This->unk.punkControl);
291 }
292
293 static HRESULT WINAPI
294 IReferenceClock_fnGetTime(IReferenceClock* iface,REFERENCE_TIME* prtTime)
295 {
296         CSystemClock_THIS(iface,refclk);
297         DWORD   dwTimeCur;
298
299         TRACE( "(%p)->(%p)\n", This, prtTime );
300
301         if ( prtTime == NULL )
302                 return E_POINTER;
303
304         EnterCriticalSection( &This->m_csClock );
305
306         dwTimeCur = GetTickCount();
307         This->m_rtLast += (REFERENCE_TIME)(DWORD)(dwTimeCur - This->m_dwTimeLast) * (REFERENCE_TIME)10000;
308
309         This->m_dwTimeLast = dwTimeCur;
310
311         *prtTime = This->m_rtLast;
312
313         LeaveCriticalSection( &This->m_csClock );
314
315         return NOERROR;
316 }
317
318 static HRESULT WINAPI
319 IReferenceClock_fnAdviseTime(IReferenceClock* iface,REFERENCE_TIME rtBase,REFERENCE_TIME rtStream,HEVENT hEvent,DWORD_PTR* pdwAdvCookie)
320 {
321         CSystemClock_THIS(iface,refclk);
322         QUARTZ_TimerEntry*      pEntry;
323         HRESULT hr;
324         REFERENCE_TIME  rtCur;
325
326         TRACE( "(%p)->()\n", This );
327
328         if ( pdwAdvCookie == NULL )
329                 return E_POINTER;
330         if ( hEvent == (HANDLE)NULL )
331                 return E_INVALIDARG;
332
333         EnterCriticalSection( &This->m_csClock );
334
335         *pdwAdvCookie = (DWORD_PTR)(This->m_dwAdvCookieNext ++);
336
337         hr = IReferenceClock_GetTime(iface,&rtCur);
338         if ( hr != NOERROR )
339                 goto err;
340         if ( rtCur >= (rtBase+rtStream) )
341         {
342                 SetEvent(hEvent);
343                 hr = NOERROR;
344                 goto err;
345         }
346
347         pEntry = IReferenceClock_AllocTimerEntry(This);
348         if ( pEntry == NULL )
349         {
350                 hr = E_FAIL;
351                 goto err;
352         }
353
354         pEntry->dwAdvCookie = *pdwAdvCookie;
355         pEntry->fPeriodic = FALSE;
356         pEntry->hEvent = hEvent;
357         pEntry->rtStart = rtBase;
358         pEntry->rtInterval = rtStream;
359
360         if ( !PostThreadMessageA(
361                         This->m_idThreadTimer,
362                         QUARTZ_MSG_ADDTIMER,
363                         0, 0 ) )
364         {
365                 pEntry->hEvent = (HANDLE)NULL;
366                 hr = E_FAIL;
367                 goto err;
368         }
369
370         hr = NOERROR;
371 err:
372         LeaveCriticalSection( &This->m_csClock );
373
374         return hr;
375 }
376
377 static HRESULT WINAPI
378 IReferenceClock_fnAdvisePeriodic(IReferenceClock* iface,REFERENCE_TIME rtStart,REFERENCE_TIME rtPeriod,HSEMAPHORE hSemaphore,DWORD_PTR* pdwAdvCookie)
379 {
380         CSystemClock_THIS(iface,refclk);
381         QUARTZ_TimerEntry*      pEntry;
382         HRESULT hr;
383
384         TRACE( "(%p)->()\n", This );
385
386         if ( pdwAdvCookie == NULL )
387                 return E_POINTER;
388         if ( hSemaphore == (HSEMAPHORE)NULL )
389                 return E_INVALIDARG;
390
391         EnterCriticalSection( &This->m_csClock );
392
393         *pdwAdvCookie = (DWORD_PTR)(This->m_dwAdvCookieNext ++);
394
395         pEntry = IReferenceClock_AllocTimerEntry(This);
396         if ( pEntry == NULL )
397         {
398                 hr = E_FAIL;
399                 goto err;
400         }
401
402         pEntry->dwAdvCookie = *pdwAdvCookie;
403         pEntry->fPeriodic = TRUE;
404         pEntry->hEvent = (HANDLE)hSemaphore;
405         pEntry->rtStart = rtStart;
406         pEntry->rtInterval = rtPeriod;
407
408         if ( !PostThreadMessageA(
409                         This->m_idThreadTimer,
410                         QUARTZ_MSG_ADDTIMER,
411                         0, 0 ) )
412         {
413                 pEntry->hEvent = (HANDLE)NULL;
414                 hr = E_FAIL;
415                 goto err;
416         }
417
418         hr = NOERROR;
419 err:
420         LeaveCriticalSection( &This->m_csClock );
421
422         return hr;
423 }
424
425 static HRESULT WINAPI
426 IReferenceClock_fnUnadvise(IReferenceClock* iface,DWORD_PTR dwAdvCookie)
427 {
428         CSystemClock_THIS(iface,refclk);
429         QUARTZ_TimerEntry*      pEntry;
430
431         TRACE( "(%p)->(%lu)\n", This, (DWORD)dwAdvCookie );
432
433         EnterCriticalSection( &This->m_csClock );
434
435         pEntry = IReferenceClock_SearchTimer(This,(DWORD)dwAdvCookie);
436         if ( pEntry != NULL )
437         {
438                 pEntry->hEvent = (HANDLE)NULL;
439         }
440
441         LeaveCriticalSection( &This->m_csClock );
442
443         return NOERROR;
444 }
445
446 static ICOM_VTABLE(IReferenceClock) irefclk =
447 {
448         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
449         /* IUnknown fields */
450         IReferenceClock_fnQueryInterface,
451         IReferenceClock_fnAddRef,
452         IReferenceClock_fnRelease,
453         /* IReferenceClock fields */
454         IReferenceClock_fnGetTime,
455         IReferenceClock_fnAdviseTime,
456         IReferenceClock_fnAdvisePeriodic,
457         IReferenceClock_fnUnadvise,
458 };
459
460
461 HRESULT CSystemClock_InitIReferenceClock( CSystemClock* psc )
462 {
463         HANDLE  hEvents[2];
464
465         TRACE("(%p)\n",psc);
466         ICOM_VTBL(&psc->refclk) = &irefclk;
467
468         InitializeCriticalSection( &psc->m_csClock );
469         psc->m_dwTimeLast = GetTickCount();
470         psc->m_rtLast = (REFERENCE_TIME)0;
471         psc->m_hThreadTimer = (HANDLE)NULL;
472         psc->m_hEventInit = (HANDLE)NULL;
473         psc->m_idThreadTimer = 0;
474         psc->m_dwAdvCookieNext = 1;
475         ZeroMemory( psc->m_timerEntries, sizeof(psc->m_timerEntries) );
476
477         psc->m_hEventInit = CreateEventA( NULL, TRUE, FALSE, NULL );
478         if ( psc->m_hEventInit == (HANDLE)NULL )
479                 goto err;
480
481         psc->m_hThreadTimer = CreateThread(
482                 NULL, 0,
483                 IReferenceClock_TimerEntry,
484                 psc, 0, &psc->m_idThreadTimer );
485
486         if ( psc->m_hThreadTimer == (HANDLE)NULL )
487         {
488                 CloseHandle( psc->m_hEventInit );
489                 psc->m_hEventInit = (HANDLE)NULL;
490                 goto err;
491         }
492
493         hEvents[0] = psc->m_hEventInit;
494         hEvents[1] = psc->m_hThreadTimer;
495         if ( WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE )
496                         != WAIT_OBJECT_0 )
497         {
498                 CloseHandle( psc->m_hEventInit );
499                 psc->m_hEventInit = (HANDLE)NULL;
500                 CloseHandle( psc->m_hThreadTimer );
501                 psc->m_hThreadTimer = (HANDLE)NULL;
502                 goto err;
503         }
504
505         return NOERROR;
506
507 err:
508         DeleteCriticalSection( &psc->m_csClock );
509         return E_FAIL;
510 }
511
512 void CSystemClock_UninitIReferenceClock( CSystemClock* psc )
513 {
514         TRACE("(%p)\n",psc);
515
516         if ( psc->m_hThreadTimer != (HANDLE)NULL )
517         {
518                 if ( PostThreadMessageA(
519                         psc->m_idThreadTimer,
520                         QUARTZ_MSG_EXITTHREAD,
521                         0, 0 ) )
522                 {
523                         WaitForSingleObject( psc->m_hThreadTimer, INFINITE );
524                 }
525                 CloseHandle( psc->m_hThreadTimer );
526                 psc->m_hThreadTimer = (HANDLE)NULL;
527         }
528
529         DeleteCriticalSection( &psc->m_csClock );
530 }