2004-05-06 01:51:59 +02:00
|
|
|
/*
|
|
|
|
* Implementation of IReferenceClock
|
|
|
|
*
|
|
|
|
* Copyright 2004 Raphael Junqueira
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-05-06 01:51:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "quartz_private.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(quartz);
|
|
|
|
|
2019-03-16 15:11:47 +01:00
|
|
|
static int cookie_counter;
|
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
struct advise_sink
|
|
|
|
{
|
|
|
|
struct list entry;
|
2019-03-15 01:11:01 +01:00
|
|
|
HANDLE handle;
|
|
|
|
REFERENCE_TIME due_time, period;
|
2019-03-16 15:11:47 +01:00
|
|
|
int cookie;
|
2004-05-06 01:51:59 +02:00
|
|
|
};
|
|
|
|
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock
|
|
|
|
{
|
2019-03-15 01:10:59 +01:00
|
|
|
IReferenceClock IReferenceClock_iface;
|
|
|
|
LONG ref;
|
|
|
|
|
|
|
|
BOOL thread_created;
|
|
|
|
HANDLE thread, notify_event, stop_event;
|
|
|
|
REFERENCE_TIME last_time;
|
2019-03-15 01:11:01 +01:00
|
|
|
CRITICAL_SECTION cs;
|
2019-03-15 01:10:59 +01:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
struct list sinks;
|
2019-03-16 15:11:50 +01:00
|
|
|
};
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-16 15:11:50 +01:00
|
|
|
static inline struct system_clock *impl_from_IReferenceClock(IReferenceClock *iface)
|
2010-12-08 22:59:35 +01:00
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
return CONTAINING_RECORD(iface, struct system_clock, IReferenceClock_iface);
|
2010-12-08 22:59:35 +01:00
|
|
|
}
|
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
static DWORD WINAPI SystemClockAdviseThread(void *param)
|
2019-03-15 01:10:57 +01:00
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = param;
|
2019-03-16 15:11:48 +01:00
|
|
|
struct advise_sink *sink, *cursor;
|
|
|
|
REFERENCE_TIME current_time;
|
|
|
|
DWORD timeout = INFINITE;
|
|
|
|
HANDLE handles[2] = {clock->stop_event, clock->notify_event};
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
TRACE("Starting advise thread for clock %p.\n", clock);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
for (;;)
|
2019-03-15 01:10:57 +01:00
|
|
|
{
|
2019-03-16 15:11:48 +01:00
|
|
|
EnterCriticalSection(&clock->cs);
|
2019-03-15 01:10:57 +01:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
current_time = GetTickCount64() * 10000;
|
2019-03-15 01:10:57 +01:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(sink, cursor, &clock->sinks, struct advise_sink, entry)
|
|
|
|
{
|
|
|
|
if (sink->due_time <= current_time)
|
|
|
|
{
|
|
|
|
if (sink->period)
|
|
|
|
{
|
|
|
|
DWORD periods = ((current_time - sink->due_time) / sink->period) + 1;
|
|
|
|
ReleaseSemaphore(sink->handle, periods, NULL);
|
|
|
|
sink->due_time += periods * sink->period;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetEvent(sink->handle);
|
|
|
|
list_remove(&sink->entry);
|
|
|
|
heap_free(sink);
|
2019-04-06 21:25:51 +02:00
|
|
|
continue;
|
2019-03-16 15:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = min(timeout, (sink->due_time - current_time) / 10000);
|
|
|
|
}
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
LeaveCriticalSection(&clock->cs);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
if (WaitForMultipleObjects(2, handles, FALSE, timeout) == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
2019-03-15 01:10:59 +01:00
|
|
|
|
2019-03-16 15:11:50 +01:00
|
|
|
static void notify_thread(struct system_clock *clock)
|
2019-03-15 01:10:59 +01:00
|
|
|
{
|
|
|
|
if (!InterlockedCompareExchange(&clock->thread_created, TRUE, FALSE))
|
|
|
|
{
|
|
|
|
clock->notify_event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
clock->stop_event = CreateEventW(NULL, TRUE, FALSE, NULL);
|
2019-06-21 03:13:16 +02:00
|
|
|
clock->thread = CreateThread(NULL, 0, SystemClockAdviseThread, clock, 0, NULL);
|
2004-12-16 15:25:15 +01:00
|
|
|
}
|
2019-03-15 01:10:59 +01:00
|
|
|
SetEvent(clock->notify_event);
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2019-03-16 15:11:49 +01:00
|
|
|
static HRESULT WINAPI SystemClockImpl_QueryInterface(IReferenceClock *iface, REFIID iid, void **out)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = impl_from_IReferenceClock(iface);
|
2019-03-16 15:11:49 +01:00
|
|
|
TRACE("clock %p, iid %s, out %p.\n", clock, debugstr_guid(iid), out);
|
2005-01-06 20:36:47 +01:00
|
|
|
|
2019-03-16 15:11:49 +01:00
|
|
|
if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IReferenceClock))
|
|
|
|
{
|
|
|
|
IReferenceClock_AddRef(iface);
|
|
|
|
*out = iface;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2005-01-06 20:36:47 +01:00
|
|
|
|
2019-03-16 15:11:49 +01:00
|
|
|
WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
|
|
|
|
*out = NULL;
|
|
|
|
return E_NOINTERFACE;
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2019-03-16 15:11:49 +01:00
|
|
|
static ULONG WINAPI SystemClockImpl_AddRef(IReferenceClock *iface)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = impl_from_IReferenceClock(iface);
|
2019-03-16 15:11:49 +01:00
|
|
|
ULONG refcount = InterlockedIncrement(&clock->ref);
|
|
|
|
|
|
|
|
TRACE("%p increasing refcount to %u.\n", clock, refcount);
|
|
|
|
|
|
|
|
return refcount;
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:10:59 +01:00
|
|
|
static ULONG WINAPI SystemClockImpl_Release(IReferenceClock *iface)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = impl_from_IReferenceClock(iface);
|
2019-03-15 01:10:59 +01:00
|
|
|
ULONG refcount = InterlockedDecrement(&clock->ref);
|
|
|
|
|
|
|
|
TRACE("%p decreasing refcount to %u.\n", clock, refcount);
|
|
|
|
|
|
|
|
if (!refcount)
|
|
|
|
{
|
|
|
|
if (clock->thread)
|
|
|
|
{
|
|
|
|
SetEvent(clock->stop_event);
|
|
|
|
WaitForSingleObject(clock->thread, INFINITE);
|
|
|
|
CloseHandle(clock->thread);
|
|
|
|
CloseHandle(clock->notify_event);
|
|
|
|
CloseHandle(clock->stop_event);
|
|
|
|
}
|
2019-03-15 01:11:01 +01:00
|
|
|
clock->cs.DebugInfo->Spare[0] = 0;
|
|
|
|
DeleteCriticalSection(&clock->cs);
|
2019-03-15 01:10:59 +01:00
|
|
|
heap_free(clock);
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
2019-03-15 01:10:59 +01:00
|
|
|
return refcount;
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 15:54:05 +01:00
|
|
|
static HRESULT WINAPI SystemClockImpl_GetTime(IReferenceClock *iface, REFERENCE_TIME *time)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = impl_from_IReferenceClock(iface);
|
2019-03-13 15:54:05 +01:00
|
|
|
REFERENCE_TIME ret;
|
|
|
|
HRESULT hr;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-13 15:54:05 +01:00
|
|
|
TRACE("clock %p, time %p.\n", clock, time);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-13 15:54:05 +01:00
|
|
|
if (!time) {
|
|
|
|
return E_POINTER;
|
|
|
|
}
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-13 15:54:05 +01:00
|
|
|
ret = GetTickCount64() * 10000;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
EnterCriticalSection(&clock->cs);
|
2019-03-13 15:54:05 +01:00
|
|
|
|
|
|
|
hr = (ret == clock->last_time) ? S_FALSE : S_OK;
|
|
|
|
*time = clock->last_time = ret;
|
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
LeaveCriticalSection(&clock->cs);
|
2019-03-13 15:54:05 +01:00
|
|
|
|
|
|
|
return hr;
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
static HRESULT WINAPI SystemClockImpl_AdviseTime(IReferenceClock *iface,
|
|
|
|
REFERENCE_TIME base, REFERENCE_TIME offset, HEVENT event, DWORD_PTR *cookie)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = impl_from_IReferenceClock(iface);
|
2019-03-15 01:10:57 +01:00
|
|
|
struct advise_sink *sink;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
TRACE("clock %p, base %s, offset %s, event %#lx, cookie %p.\n",
|
|
|
|
clock, wine_dbgstr_longlong(base), wine_dbgstr_longlong(offset), event, cookie);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (!event)
|
|
|
|
return E_INVALIDARG;
|
2019-03-14 18:12:21 +01:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (base + offset <= 0)
|
|
|
|
return E_INVALIDARG;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (!cookie)
|
|
|
|
return E_POINTER;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (!(sink = heap_alloc_zero(sizeof(*sink))))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
sink->handle = (HANDLE)event;
|
|
|
|
sink->due_time = base + offset;
|
|
|
|
sink->period = 0;
|
2019-03-16 15:11:47 +01:00
|
|
|
sink->cookie = InterlockedIncrement(&cookie_counter);
|
2019-03-15 01:10:57 +01:00
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
EnterCriticalSection(&clock->cs);
|
2019-03-16 15:11:48 +01:00
|
|
|
list_add_tail(&clock->sinks, &sink->entry);
|
2019-03-15 01:11:01 +01:00
|
|
|
LeaveCriticalSection(&clock->cs);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:59 +01:00
|
|
|
notify_thread(clock);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-16 15:11:47 +01:00
|
|
|
*cookie = sink->cookie;
|
2019-03-15 01:10:57 +01:00
|
|
|
return S_OK;
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
static HRESULT WINAPI SystemClockImpl_AdvisePeriodic(IReferenceClock* iface,
|
|
|
|
REFERENCE_TIME start, REFERENCE_TIME period, HSEMAPHORE semaphore, DWORD_PTR *cookie)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = impl_from_IReferenceClock(iface);
|
2019-03-15 01:10:57 +01:00
|
|
|
struct advise_sink *sink;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
TRACE("clock %p, start %s, period %s, semaphore %#lx, cookie %p.\n",
|
|
|
|
clock, wine_dbgstr_longlong(start), wine_dbgstr_longlong(period), semaphore, cookie);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (!semaphore)
|
|
|
|
return E_INVALIDARG;
|
2019-03-14 18:12:21 +01:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (start <= 0 || period <= 0)
|
|
|
|
return E_INVALIDARG;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (!cookie)
|
|
|
|
return E_POINTER;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
if (!(sink = heap_alloc_zero(sizeof(*sink))))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
sink->handle = (HANDLE)semaphore;
|
|
|
|
sink->due_time = start;
|
|
|
|
sink->period = period;
|
2019-03-16 15:11:47 +01:00
|
|
|
sink->cookie = InterlockedIncrement(&cookie_counter);
|
2019-03-15 01:10:57 +01:00
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
EnterCriticalSection(&clock->cs);
|
2019-03-16 15:11:48 +01:00
|
|
|
list_add_tail(&clock->sinks, &sink->entry);
|
2019-03-15 01:11:01 +01:00
|
|
|
LeaveCriticalSection(&clock->cs);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:59 +01:00
|
|
|
notify_thread(clock);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-16 15:11:47 +01:00
|
|
|
*cookie = sink->cookie;
|
2019-03-15 01:10:57 +01:00
|
|
|
return S_OK;
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
static HRESULT WINAPI SystemClockImpl_Unadvise(IReferenceClock *iface, DWORD_PTR cookie)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *clock = impl_from_IReferenceClock(iface);
|
2019-03-15 01:10:57 +01:00
|
|
|
struct advise_sink *sink;
|
|
|
|
|
|
|
|
TRACE("clock %p, cookie %#lx.\n", clock, cookie);
|
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
EnterCriticalSection(&clock->cs);
|
2019-03-15 01:10:57 +01:00
|
|
|
|
2019-03-16 15:11:48 +01:00
|
|
|
LIST_FOR_EACH_ENTRY(sink, &clock->sinks, struct advise_sink, entry)
|
2019-03-15 01:10:57 +01:00
|
|
|
{
|
2019-03-16 15:11:47 +01:00
|
|
|
if (sink->cookie == cookie)
|
2019-03-15 01:10:57 +01:00
|
|
|
{
|
|
|
|
list_remove(&sink->entry);
|
|
|
|
heap_free(sink);
|
2019-03-15 01:11:01 +01:00
|
|
|
LeaveCriticalSection(&clock->cs);
|
2019-03-15 01:10:57 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:11:01 +01:00
|
|
|
LeaveCriticalSection(&clock->cs);
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-15 01:10:57 +01:00
|
|
|
return S_FALSE;
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +02:00
|
|
|
static const IReferenceClockVtbl SystemClock_Vtbl =
|
2004-05-06 01:51:59 +02:00
|
|
|
{
|
|
|
|
SystemClockImpl_QueryInterface,
|
|
|
|
SystemClockImpl_AddRef,
|
|
|
|
SystemClockImpl_Release,
|
|
|
|
SystemClockImpl_GetTime,
|
|
|
|
SystemClockImpl_AdviseTime,
|
|
|
|
SystemClockImpl_AdvisePeriodic,
|
|
|
|
SystemClockImpl_Unadvise
|
|
|
|
};
|
|
|
|
|
2019-03-14 18:12:21 +01:00
|
|
|
HRESULT QUARTZ_CreateSystemClock(IUnknown *outer, void **out)
|
|
|
|
{
|
2019-03-16 15:11:50 +01:00
|
|
|
struct system_clock *object;
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-14 18:12:21 +01:00
|
|
|
TRACE("outer %p, out %p.\n", outer, out);
|
|
|
|
|
|
|
|
if (!(object = heap_alloc_zero(sizeof(*object))))
|
|
|
|
{
|
|
|
|
*out = NULL;
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2007-03-08 12:03:06 +01:00
|
|
|
|
2019-03-14 18:12:21 +01:00
|
|
|
object->IReferenceClock_iface.lpVtbl = &SystemClock_Vtbl;
|
2019-03-16 15:11:48 +01:00
|
|
|
list_init(&object->sinks);
|
2019-03-15 01:11:01 +01:00
|
|
|
InitializeCriticalSection(&object->cs);
|
|
|
|
object->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SystemClockImpl.cs");
|
2004-05-06 01:51:59 +02:00
|
|
|
|
2019-03-14 18:12:21 +01:00
|
|
|
return SystemClockImpl_QueryInterface(&object->IReferenceClock_iface, &IID_IReferenceClock, out);
|
2004-05-06 01:51:59 +02:00
|
|
|
}
|