2 * Implementation of IReferenceClock
4 * Copyright 2004 Raphael Junqueira
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "quartz_private.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
29 typedef struct SystemClockAdviseEntry SystemClockAdviseEntry;
30 struct SystemClockAdviseEntry {
31 SystemClockAdviseEntry* next;
32 SystemClockAdviseEntry* prev;
35 REFERENCE_TIME rtBaseTime;
36 REFERENCE_TIME rtIntervalTime;
39 typedef struct SystemClockImpl {
40 const IReferenceClockVtbl *lpVtbl;
43 /** IReferenceClock */
46 BOOL adviseThreadActive;
47 REFERENCE_TIME lastRefTime;
48 DWORD lastTimeTickCount;
49 CRITICAL_SECTION safe;
51 SystemClockAdviseEntry* pSingleShotAdvise;
52 SystemClockAdviseEntry* pPeriodicAdvise;
56 static void QUARTZ_RemoveAviseEntryFromQueue(SystemClockImpl* This, SystemClockAdviseEntry* pEntry) {
57 if (pEntry->prev) pEntry->prev->next = pEntry->next;
58 if (pEntry->next) pEntry->next->prev = pEntry->prev;
59 if (This->pSingleShotAdvise == pEntry) This->pSingleShotAdvise = pEntry->next;
60 if (This->pPeriodicAdvise == pEntry) This->pPeriodicAdvise = pEntry->next;
63 static void QUARTZ_InsertAviseEntryFromQueue(SystemClockImpl* This, SystemClockAdviseEntry* pEntry, SystemClockAdviseEntry** pQueue) {
64 SystemClockAdviseEntry* prev_it = NULL;
65 SystemClockAdviseEntry* it = NULL;
66 REFERENCE_TIME bornTime = pEntry->rtBaseTime + pEntry->rtIntervalTime;
68 for (it = *pQueue; NULL != it && (it->rtBaseTime + it->rtIntervalTime) < bornTime; it = it->next) {
71 if (NULL == prev_it) {
73 if (NULL != (*pQueue)) pEntry->next = (*pQueue)->next;
74 /*assert( NULL == pEntry->next->prev );*/
75 if (NULL != pEntry->next) pEntry->next->prev = pEntry;
78 pEntry->prev = prev_it;
79 pEntry->next = prev_it->next;
80 prev_it->next = pEntry;
81 if (NULL != pEntry->next) pEntry->next->prev = pEntry;
85 #define MAX_REFTIME (REFERENCE_TIME)(0x7FFFFFFFFFFFFFFF)
86 #define ADVISE_EXIT (WM_APP + 0)
87 #define ADVISE_REMOVE (WM_APP + 2)
88 #define ADVISE_ADD_SINGLESHOT (WM_APP + 4)
89 #define ADVISE_ADD_PERIODIC (WM_APP + 8)
91 static DWORD WINAPI SystemClockAdviseThread(LPVOID lpParam) {
92 SystemClockImpl* This = (SystemClockImpl*) lpParam;
93 DWORD timeOut = INFINITE;
97 REFERENCE_TIME curTime;
98 SystemClockAdviseEntry* it = NULL;
100 TRACE("(%p): Main Loop\n", This);
103 if (timeOut > 0) MsgWaitForMultipleObjects(0, NULL, FALSE, timeOut, QS_POSTMESSAGE|QS_SENDMESSAGE|QS_TIMER);
105 EnterCriticalSection(&This->safe);
106 /*timeOut = IReferenceClock_OnTimerUpdated(This); */
107 hr = IReferenceClock_GetTime((IReferenceClock*) This, &curTime);
113 /** First SingleShots Advice: sorted list */
114 for (it = This->pSingleShotAdvise; NULL != it && (it->rtBaseTime + it->rtIntervalTime) <= curTime; it = it->next) {
115 /** send event ... */
116 SetEvent((HANDLE) it->hEvent);
117 /** ... and Release it */
118 QUARTZ_RemoveAviseEntryFromQueue(This, it);
121 if (NULL != it) timeOut = (DWORD) ((it->rtBaseTime + it->rtIntervalTime) - curTime) / (REFERENCE_TIME)10000;
123 /** Now Periodics Advice: semi sorted list (sort cannot be used) */
124 for (it = This->pPeriodicAdvise; NULL != it; it = it->next) {
125 if (it->rtBaseTime <= curTime) {
126 DWORD nPeriods = (DWORD) ((curTime - it->rtBaseTime) / it->rtIntervalTime);
127 /** Release the semaphore ... */
128 ReleaseSemaphore((HANDLE) it->hEvent, nPeriods, NULL);
129 /** ... and refresh time */
130 it->rtBaseTime += nPeriods * it->rtIntervalTime;
131 /*assert( it->rtBaseTime + it->rtIntervalTime < curTime );*/
133 tmpTimeOut = (DWORD) ((it->rtBaseTime + it->rtIntervalTime) - curTime) / (REFERENCE_TIME)10000;
134 if (timeOut > tmpTimeOut) timeOut = tmpTimeOut;
138 LeaveCriticalSection(&This->safe);
140 while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
141 /** if hwnd we suppose that is a windows event ... */
142 if (NULL != msg.hwnd) {
143 TranslateMessage(&msg);
144 DispatchMessageA(&msg);
146 switch (msg.message) {
150 case ADVISE_ADD_SINGLESHOT:
151 case ADVISE_ADD_PERIODIC:
152 /** set timeout to 0 to do a rescan now */
156 /** hmmmm what we can do here ... */
160 ERR("Unhandled message %u. Critical Path\n", msg.message);
168 TRACE("(%p): Exiting\n", This);
171 /*static DWORD WINAPI SystemClockAdviseThread(LPVOID lpParam) { */
173 static BOOL SystemClockPostMessageToAdviseThread(SystemClockImpl* This, UINT iMsg) {
174 if (FALSE == This->adviseThreadActive) {
176 This->adviseThread = CreateThread(NULL, 0, SystemClockAdviseThread, This, 0, &This->adviseThreadId);
177 if (NULL == This->adviseThread) return FALSE;
178 SetThreadPriority(This->adviseThread, THREAD_PRIORITY_TIME_CRITICAL);
179 This->adviseThreadActive = TRUE;
181 res = PostThreadMessageA(This->adviseThreadId, iMsg, 0, 0);
182 /* Let the thread creates its message queue (with MsgWaitForMultipleObjects call) by yielding and retrying */
183 if (!res && (GetLastError() == ERROR_INVALID_THREAD_ID))
190 return PostThreadMessageA(This->adviseThreadId, iMsg, 0, 0);
193 static ULONG WINAPI SystemClockImpl_AddRef(IReferenceClock* iface) {
194 SystemClockImpl *This = (SystemClockImpl *)iface;
195 ULONG ref = InterlockedIncrement(&This->ref);
197 TRACE("(%p): AddRef from %d\n", This, ref - 1);
202 static HRESULT WINAPI SystemClockImpl_QueryInterface(IReferenceClock* iface, REFIID riid, void** ppobj) {
203 SystemClockImpl *This = (SystemClockImpl *)iface;
204 TRACE("(%p, %s,%p)\n", This, debugstr_guid(riid), ppobj);
206 if (IsEqualIID (riid, &IID_IUnknown) ||
207 IsEqualIID (riid, &IID_IReferenceClock)) {
208 SystemClockImpl_AddRef(iface);
213 WARN("(%p, %s,%p): not found\n", This, debugstr_guid(riid), ppobj);
214 return E_NOINTERFACE;
217 static ULONG WINAPI SystemClockImpl_Release(IReferenceClock* iface) {
218 SystemClockImpl *This = (SystemClockImpl *)iface;
219 ULONG ref = InterlockedDecrement(&This->ref);
220 TRACE("(%p): ReleaseRef to %d\n", This, ref);
222 if (SystemClockPostMessageToAdviseThread(This, ADVISE_EXIT)) {
223 WaitForSingleObject(This->adviseThread, INFINITE);
224 CloseHandle(This->adviseThread);
226 This->safe.DebugInfo->Spare[0] = 0;
227 DeleteCriticalSection(&This->safe);
233 static HRESULT WINAPI SystemClockImpl_GetTime(IReferenceClock* iface, REFERENCE_TIME* pTime) {
234 SystemClockImpl *This = (SystemClockImpl *)iface;
235 DWORD curTimeTickCount;
238 TRACE("(%p, %p)\n", This, pTime);
244 curTimeTickCount = GetTickCount();
246 EnterCriticalSection(&This->safe);
247 /** TODO: safe this not using * 10000 */
248 This->lastRefTime += (REFERENCE_TIME) (DWORD) (curTimeTickCount - This->lastTimeTickCount) * (REFERENCE_TIME) 10000;
249 This->lastTimeTickCount = curTimeTickCount;
250 LeaveCriticalSection(&This->safe);
252 *pTime = This->lastRefTime;
253 if (This->lastTimeTickCount == curTimeTickCount) hr = S_FALSE;
254 This->lastTimeTickCount = curTimeTickCount;
258 static HRESULT WINAPI SystemClockImpl_AdviseTime(IReferenceClock* iface, REFERENCE_TIME rtBaseTime, REFERENCE_TIME rtStreamTime, HEVENT hEvent, DWORD_PTR* pdwAdviseCookie) {
259 SystemClockImpl *This = (SystemClockImpl *)iface;
260 SystemClockAdviseEntry* pEntry = NULL;
262 TRACE("(%p, 0x%s, 0x%s, %ld, %p)\n", This, wine_dbgstr_longlong(rtBaseTime),
263 wine_dbgstr_longlong(rtStreamTime), hEvent, pdwAdviseCookie);
265 if ((HEVENT) 0 == hEvent) {
268 if (0 >= rtBaseTime + rtStreamTime) {
271 if (NULL == pdwAdviseCookie) {
274 pEntry = CoTaskMemAlloc(sizeof(SystemClockAdviseEntry));
275 if (NULL == pEntry) {
276 return E_OUTOFMEMORY;
278 ZeroMemory(pEntry, sizeof(SystemClockAdviseEntry));
280 pEntry->hEvent = (HANDLE) hEvent;
281 pEntry->rtBaseTime = rtBaseTime + rtStreamTime;
282 pEntry->rtIntervalTime = 0;
284 EnterCriticalSection(&This->safe);
285 QUARTZ_InsertAviseEntryFromQueue(This, pEntry, &This->pSingleShotAdvise);
286 LeaveCriticalSection(&This->safe);
288 SystemClockPostMessageToAdviseThread(This, ADVISE_ADD_SINGLESHOT);
290 *pdwAdviseCookie = (DWORD_PTR) (pEntry);
294 static HRESULT WINAPI SystemClockImpl_AdvisePeriodic(IReferenceClock* iface, REFERENCE_TIME rtStartTime, REFERENCE_TIME rtPeriodTime, HSEMAPHORE hSemaphore, DWORD_PTR* pdwAdviseCookie) {
295 SystemClockImpl *This = (SystemClockImpl *)iface;
296 SystemClockAdviseEntry* pEntry = NULL;
298 TRACE("(%p, 0x%s, 0x%s, %ld, %p)\n", This, wine_dbgstr_longlong(rtStartTime),
299 wine_dbgstr_longlong(rtPeriodTime), hSemaphore, pdwAdviseCookie);
301 if ((HSEMAPHORE) 0 == hSemaphore) {
304 if (0 >= rtStartTime || 0 >= rtPeriodTime) {
307 if (NULL == pdwAdviseCookie) {
310 pEntry = CoTaskMemAlloc(sizeof(SystemClockAdviseEntry));
311 if (NULL == pEntry) {
312 return E_OUTOFMEMORY;
314 ZeroMemory(pEntry, sizeof(SystemClockAdviseEntry));
316 pEntry->hEvent = (HANDLE) hSemaphore;
317 pEntry->rtBaseTime = rtStartTime;
318 pEntry->rtIntervalTime = rtPeriodTime;
320 EnterCriticalSection(&This->safe);
321 QUARTZ_InsertAviseEntryFromQueue(This, pEntry, &This->pPeriodicAdvise);
322 LeaveCriticalSection(&This->safe);
324 SystemClockPostMessageToAdviseThread(This, ADVISE_ADD_PERIODIC);
326 *pdwAdviseCookie = (DWORD_PTR) (pEntry);
330 static HRESULT WINAPI SystemClockImpl_Unadvise(IReferenceClock* iface, DWORD_PTR dwAdviseCookie) {
331 SystemClockImpl *This = (SystemClockImpl *)iface;
332 SystemClockAdviseEntry* pEntry = NULL;
333 SystemClockAdviseEntry* it = NULL;
335 TRACE("(%p, %lu)\n", This, dwAdviseCookie);
337 pEntry = (SystemClockAdviseEntry*) dwAdviseCookie;
339 EnterCriticalSection(&This->safe);
340 for (it = This->pPeriodicAdvise; NULL != it && it != pEntry; it = it->next) ;
342 for (it = This->pSingleShotAdvise; NULL != it && it != pEntry; it = it->next) ;
349 QUARTZ_RemoveAviseEntryFromQueue(This, pEntry);
350 CoTaskMemFree(pEntry);
352 SystemClockPostMessageToAdviseThread(This, ADVISE_REMOVE);
355 LeaveCriticalSection(&This->safe);
359 static const IReferenceClockVtbl SystemClock_Vtbl =
361 SystemClockImpl_QueryInterface,
362 SystemClockImpl_AddRef,
363 SystemClockImpl_Release,
364 SystemClockImpl_GetTime,
365 SystemClockImpl_AdviseTime,
366 SystemClockImpl_AdvisePeriodic,
367 SystemClockImpl_Unadvise
370 HRESULT QUARTZ_CreateSystemClock(IUnknown * pUnkOuter, LPVOID * ppv) {
371 SystemClockImpl* obj = NULL;
373 TRACE("(%p,%p)\n", ppv, pUnkOuter);
375 obj = CoTaskMemAlloc(sizeof(SystemClockImpl));
378 return E_OUTOFMEMORY;
380 ZeroMemory(obj, sizeof(SystemClockImpl));
382 obj->lpVtbl = &SystemClock_Vtbl;
383 obj->ref = 0; /* will be inited by QueryInterface */
385 obj->lastTimeTickCount = GetTickCount();
386 InitializeCriticalSection(&obj->safe);
387 obj->safe.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SystemClockImpl.safe");
389 return SystemClockImpl_QueryInterface((IReferenceClock*) obj, &IID_IReferenceClock, ppv);