cmd.exe: Correctly parse IF ELSE plus multipart/multiline.
[wine.git] / dlls / quartz / systemclock.c
blobff1a28ad8220c56345a9e859757419e4491cd8af
1 /*
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"
25 #include <assert.h>
27 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
29 typedef struct SystemClockAdviseEntry SystemClockAdviseEntry;
30 struct SystemClockAdviseEntry {
31 SystemClockAdviseEntry* next;
32 SystemClockAdviseEntry* prev;
34 HANDLE hEvent;
35 REFERENCE_TIME rtBaseTime;
36 REFERENCE_TIME rtIntervalTime;
39 typedef struct SystemClockImpl {
40 const IReferenceClockVtbl *lpVtbl;
41 LONG ref;
43 /** IReferenceClock */
44 HANDLE adviseThread;
45 DWORD adviseThreadId;
46 BOOL adviseThreadActive;
47 REFERENCE_TIME lastRefTime;
48 DWORD lastTimeTickCount;
49 CRITICAL_SECTION safe;
51 SystemClockAdviseEntry* pSingleShotAdvise;
52 SystemClockAdviseEntry* pPeriodicAdvise;
53 } SystemClockImpl;
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) {
69 prev_it = it;
71 if (NULL == prev_it) {
72 pEntry->prev = NULL;
73 if (NULL != (*pQueue)) pEntry->next = (*pQueue)->next;
74 /*assert( NULL == pEntry->next->prev );*/
75 if (NULL != pEntry->next) pEntry->next->prev = pEntry;
76 (*pQueue) = pEntry;
77 } else {
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;
94 DWORD tmpTimeOut;
95 MSG msg;
96 HRESULT hr;
97 REFERENCE_TIME curTime;
98 SystemClockAdviseEntry* it = NULL;
100 TRACE("(%p): Main Loop\n", This);
102 while (TRUE) {
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);
108 if (FAILED(hr)) {
109 timeOut = INFINITE;
110 goto outrefresh;
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);
119 CoTaskMemFree(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;
137 outrefresh:
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);
145 } else {
146 switch (msg.message) {
147 case WM_QUIT:
148 case ADVISE_EXIT:
149 goto outofthread;
150 case ADVISE_ADD_SINGLESHOT:
151 case ADVISE_ADD_PERIODIC:
152 /** set timeout to 0 to do a rescan now */
153 timeOut = 0;
154 break;
155 case ADVISE_REMOVE:
156 /** hmmmm what we can do here ... */
157 timeOut = INFINITE;
158 break;
159 default:
160 ERR("Unhandled message %u. Critical Path\n", msg.message);
161 break;
167 outofthread:
168 TRACE("(%p): Exiting\n", This);
169 return 0;
171 /*static DWORD WINAPI SystemClockAdviseThread(LPVOID lpParam) { */
173 static BOOL SystemClockPostMessageToAdviseThread(SystemClockImpl* This, UINT iMsg) {
174 if (FALSE == This->adviseThreadActive) {
175 BOOL res;
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;
180 while(1) {
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))
184 Sleep(0);
185 else
186 break;
188 return res;
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);
199 return ref;
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);
209 *ppobj = This;
210 return S_OK;
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);
221 if (ref == 0) {
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);
228 CoTaskMemFree(This);
230 return ref;
233 static HRESULT WINAPI SystemClockImpl_GetTime(IReferenceClock* iface, REFERENCE_TIME* pTime) {
234 SystemClockImpl *This = (SystemClockImpl *)iface;
235 DWORD curTimeTickCount;
236 HRESULT hr = S_OK;
238 TRACE("(%p, %p)\n", This, pTime);
240 if (NULL == pTime) {
241 return E_POINTER;
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;
255 return hr;
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) {
266 return E_INVALIDARG;
268 if (0 >= rtBaseTime + rtStreamTime) {
269 return E_INVALIDARG;
271 if (NULL == pdwAdviseCookie) {
272 return E_POINTER;
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);
291 return S_OK;
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) {
302 return E_INVALIDARG;
304 if (0 >= rtStartTime || 0 >= rtPeriodTime) {
305 return E_INVALIDARG;
307 if (NULL == pdwAdviseCookie) {
308 return E_POINTER;
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);
327 return S_OK;
330 static HRESULT WINAPI SystemClockImpl_Unadvise(IReferenceClock* iface, DWORD_PTR dwAdviseCookie) {
331 SystemClockImpl *This = (SystemClockImpl *)iface;
332 SystemClockAdviseEntry* pEntry = NULL;
333 SystemClockAdviseEntry* it = NULL;
334 HRESULT ret = S_OK;
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) ;
341 if (it != pEntry) {
342 for (it = This->pSingleShotAdvise; NULL != it && it != pEntry; it = it->next) ;
343 if (it != pEntry) {
344 ret = S_FALSE;
345 goto out;
349 QUARTZ_RemoveAviseEntryFromQueue(This, pEntry);
350 CoTaskMemFree(pEntry);
352 SystemClockPostMessageToAdviseThread(This, ADVISE_REMOVE);
354 out:
355 LeaveCriticalSection(&This->safe);
356 return ret;
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));
376 if (NULL == obj) {
377 *ppv = NULL;
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);