2012-09-18 23:51:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2021-03-24 20:32:43 +03:00
|
|
|
* Copyright 2021 David Fort <contact@hardening-consulting.com>
|
2012-09-18 23:51:33 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-29 19:57:29 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-01-27 06:56:07 +04:00
|
|
|
#include <winpr/file.h>
|
2014-01-27 02:21:12 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
#ifndef _WIN32
|
2015-03-13 01:51:59 +03:00
|
|
|
#include <unistd.h>
|
2014-01-27 06:56:07 +04:00
|
|
|
#include <errno.h>
|
2014-01-25 07:44:23 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#include "event.h"
|
2013-07-29 19:57:29 +04:00
|
|
|
#include "synch.h"
|
2012-09-18 23:51:33 +04:00
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
2013-07-29 19:57:29 +04:00
|
|
|
#include "../handle/handle.h"
|
2021-03-24 20:32:43 +03:00
|
|
|
#include "../thread/thread.h"
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("synch.timer")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2015-03-11 17:11:20 +03:00
|
|
|
static BOOL TimerCloseHandle(HANDLE handle);
|
|
|
|
|
|
|
|
static BOOL TimerIsHandled(HANDLE handle)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_TIMER* pTimer = (WINPR_TIMER*)handle;
|
2015-03-11 17:11:20 +03:00
|
|
|
|
2015-03-27 10:09:46 +03:00
|
|
|
if (!pTimer || (pTimer->Type != HANDLE_TYPE_TIMER))
|
2015-03-11 17:11:20 +03:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:57:01 +03:00
|
|
|
static int TimerGetFd(HANDLE handle)
|
|
|
|
{
|
2017-11-14 15:55:58 +03:00
|
|
|
WINPR_TIMER* timer = (WINPR_TIMER*)handle;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!TimerIsHandled(handle))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return timer->fd;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static DWORD TimerCleanupHandle(HANDLE handle)
|
2015-03-11 17:11:20 +03:00
|
|
|
{
|
2015-03-11 19:57:01 +03:00
|
|
|
int length;
|
|
|
|
UINT64 expirations;
|
2017-11-14 15:55:58 +03:00
|
|
|
WINPR_TIMER* timer = (WINPR_TIMER*)handle;
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (!TimerIsHandled(handle))
|
|
|
|
return WAIT_FAILED;
|
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
if (timer->bManualReset)
|
|
|
|
return WAIT_OBJECT_0;
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef TIMER_IMPL_TIMERFD
|
|
|
|
do
|
|
|
|
{
|
|
|
|
length = read(timer->fd, (void*)&expirations, sizeof(UINT64));
|
|
|
|
} while (length < 0 && errno == EINTR);
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
if (length != 8)
|
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
if (length < 0)
|
2015-03-11 19:57:01 +03:00
|
|
|
{
|
2018-03-08 15:46:46 +03:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case ETIMEDOUT:
|
|
|
|
case EAGAIN:
|
|
|
|
return WAIT_TIMEOUT;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
WLog_ERR(TAG, "timer read() failure [%d] %s", errno, strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "timer read() failure - incorrect number of bytes read");
|
|
|
|
}
|
|
|
|
|
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
#else
|
|
|
|
if (!winpr_event_reset(&timer->event))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "timer reset() failure");
|
|
|
|
return WAIT_FAILED;
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-11 19:57:01 +03:00
|
|
|
|
|
|
|
return WAIT_OBJECT_0;
|
2015-03-11 17:11:20 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
WINPR_APC_ITEM apcItem;
|
|
|
|
WINPR_TIMER* timer;
|
|
|
|
} TimerDeleter;
|
|
|
|
|
|
|
|
static void TimerPostDelete_APC(LPVOID arg)
|
|
|
|
{
|
|
|
|
TimerDeleter* deleter = (TimerDeleter*)arg;
|
|
|
|
free(deleter->timer);
|
|
|
|
deleter->apcItem.markedForFree = TRUE;
|
|
|
|
deleter->apcItem.markedForRemove = TRUE;
|
|
|
|
}
|
|
|
|
|
2017-11-14 15:55:58 +03:00
|
|
|
BOOL TimerCloseHandle(HANDLE handle)
|
|
|
|
{
|
2015-03-11 17:11:20 +03:00
|
|
|
WINPR_TIMER* timer;
|
2019-11-06 17:24:51 +03:00
|
|
|
timer = (WINPR_TIMER*)handle;
|
2015-03-11 17:11:20 +03:00
|
|
|
|
2017-11-14 15:55:58 +03:00
|
|
|
if (!TimerIsHandled(handle))
|
|
|
|
return FALSE;
|
2015-03-11 17:11:20 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef TIMER_IMPL_TIMERFD
|
|
|
|
if (timer->fd != -1)
|
|
|
|
close(timer->fd);
|
2015-03-11 17:11:20 +03:00
|
|
|
#endif
|
2021-03-24 20:32:43 +03:00
|
|
|
|
|
|
|
#ifdef TIMER_IMPL_POSIX
|
|
|
|
timer_delete(timer->tid);
|
2017-11-15 11:11:12 +03:00
|
|
|
#endif
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef TIMER_IMPL_DISPATCH
|
2018-03-08 15:46:46 +03:00
|
|
|
dispatch_release(timer->queue);
|
|
|
|
dispatch_release(timer->source);
|
|
|
|
#endif
|
2014-01-26 09:06:27 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#if defined(TIMER_IMPL_POSIX) || defined(TIMER_IMPL_DISPATCH)
|
|
|
|
winpr_event_uninit(&timer->event);
|
|
|
|
#endif
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
free(timer->name);
|
|
|
|
if (timer->apcItem.linked)
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
TimerDeleter* deleter;
|
|
|
|
WINPR_APC_ITEM* apcItem;
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
switch (apc_remove(&timer->apcItem))
|
2014-01-25 07:44:23 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
case APC_REMOVE_OK:
|
|
|
|
break;
|
|
|
|
case APC_REMOVE_DELAY_FREE:
|
2014-01-25 07:44:23 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WINPR_THREAD* thread = winpr_GetCurrentThread();
|
|
|
|
if (!thread)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
deleter = calloc(1, sizeof(*deleter));
|
|
|
|
if (!deleter)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "unable to allocate a timer deleter");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
deleter->timer = timer;
|
|
|
|
apcItem = &deleter->apcItem;
|
|
|
|
apcItem->type = APC_TYPE_HANDLE_FREE;
|
|
|
|
apcItem->alwaysSignaled = TRUE;
|
|
|
|
apcItem->completion = TimerPostDelete_APC;
|
|
|
|
apcItem->completionArgs = deleter;
|
|
|
|
apc_register(thread, apcItem);
|
|
|
|
return TRUE;
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
case APC_REMOVE_ERROR:
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "unable to remove timer from APC list");
|
|
|
|
break;
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
|
|
|
|
free(timer);
|
|
|
|
return TRUE;
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
|
|
|
|
#ifdef TIMER_IMPL_POSIX
|
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
static void WaitableTimerSignalHandler(int signum, siginfo_t* siginfo, void* arg)
|
|
|
|
{
|
|
|
|
WINPR_TIMER* timer = siginfo->si_value.sival_ptr;
|
2021-03-24 20:32:43 +03:00
|
|
|
UINT64 data = 1;
|
2019-02-08 11:15:04 +03:00
|
|
|
WINPR_UNUSED(arg);
|
2018-03-08 15:46:46 +03:00
|
|
|
|
|
|
|
if (!timer || (signum != SIGALRM))
|
|
|
|
return;
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!winpr_event_set(&timer->event))
|
|
|
|
WLog_ERR(TAG, "error when notifying event");
|
2018-03-08 15:46:46 +03:00
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
static INIT_ONCE TimerSignalHandler_InitOnce = INIT_ONCE_STATIC_INIT;
|
2014-01-25 07:44:23 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
static BOOL InstallTimerSignalHandler(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context)
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
sigaddset(&action.sa_mask, SIGALRM);
|
|
|
|
action.sa_flags = SA_RESTART | SA_SIGINFO;
|
|
|
|
action.sa_sigaction = WaitableTimerSignalHandler;
|
|
|
|
sigaction(SIGALRM, &action, NULL);
|
|
|
|
return TRUE;
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
2014-01-26 09:06:27 +04:00
|
|
|
#endif
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef TIMER_IMPL_DISPATCH
|
2018-03-08 15:46:46 +03:00
|
|
|
static void WaitableTimerHandler(void* arg)
|
|
|
|
{
|
|
|
|
UINT64 data = 1;
|
|
|
|
WINPR_TIMER* timer = (WINPR_TIMER*)arg;
|
|
|
|
|
|
|
|
if (!timer)
|
|
|
|
return;
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!winpr_event_set(&timer->event))
|
2018-03-08 15:46:46 +03:00
|
|
|
WLog_ERR(TAG, "failed to write to pipe");
|
|
|
|
|
|
|
|
if (timer->lPeriod == 0)
|
|
|
|
{
|
|
|
|
if (timer->running)
|
|
|
|
dispatch_suspend(timer->source);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
timer->running = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int InitializeWaitableTimer(WINPR_TIMER* timer)
|
2014-01-25 07:44:23 +04:00
|
|
|
{
|
2016-06-07 00:06:12 +03:00
|
|
|
int result = 0;
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef TIMER_IMPL_TIMERFD
|
|
|
|
timer->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
|
|
|
|
if (timer->fd <= 0)
|
|
|
|
return -1;
|
|
|
|
#elif defined(TIMER_IMPL_POSIX)
|
|
|
|
struct sigevent sigev;
|
|
|
|
InitOnceExecuteOnce(&TimerSignalHandler_InitOnce, InstallTimerSignalHandler, NULL, NULL);
|
|
|
|
ZeroMemory(&sigev, sizeof(struct sigevent));
|
|
|
|
sigev.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
sigev.sigev_signo = SIGALRM;
|
|
|
|
sigev.sigev_value.sival_ptr = (void*)timer;
|
|
|
|
|
|
|
|
if ((timer_create(CLOCK_MONOTONIC, &sigev, &(timer->tid))) != 0)
|
2014-01-25 07:44:23 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "timer_create");
|
|
|
|
return -1;
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
#elif !defined(TIMER_IMPL_DISPATCH)
|
|
|
|
WLog_ERR(TAG, "%s: os specific implementation is missing", __FUNCTION__);
|
|
|
|
result = -1;
|
2014-01-26 09:06:27 +04:00
|
|
|
#endif
|
2014-01-25 07:44:23 +04:00
|
|
|
|
|
|
|
timer->bInit = TRUE;
|
2016-06-07 00:06:12 +03:00
|
|
|
return result;
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
static BOOL timer_drain_fd(int fd)
|
|
|
|
{
|
|
|
|
UINT64 expr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = read(fd, &expr, sizeof(expr));
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
return ret >= 0;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static HANDLE_OPS ops = { TimerIsHandled, TimerCloseHandle,
|
|
|
|
TimerGetFd, TimerCleanupHandle,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL };
|
2015-03-16 02:29:37 +03:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
/**
|
|
|
|
* Waitable Timer
|
|
|
|
*/
|
|
|
|
|
2017-11-14 15:55:58 +03:00
|
|
|
HANDLE CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes, BOOL bManualReset,
|
|
|
|
LPCSTR lpTimerName)
|
2014-01-25 07:44:23 +04:00
|
|
|
{
|
|
|
|
HANDLE handle = NULL;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER* timer;
|
2019-02-07 19:26:26 +03:00
|
|
|
|
|
|
|
if (lpTimerAttributes)
|
|
|
|
WLog_WARN(TAG, "%s [%s] does not support lpTimerAttributes", __FUNCTION__, lpTimerName);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
timer = (WINPR_TIMER*)calloc(1, sizeof(WINPR_TIMER));
|
2017-11-14 15:55:58 +03:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if (timer)
|
|
|
|
{
|
2015-07-14 12:39:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(timer, HANDLE_TYPE_TIMER, WINPR_FD_READ);
|
2019-11-06 17:24:51 +03:00
|
|
|
handle = (HANDLE)timer;
|
2014-01-25 07:44:23 +04:00
|
|
|
timer->fd = -1;
|
|
|
|
timer->lPeriod = 0;
|
|
|
|
timer->bManualReset = bManualReset;
|
|
|
|
timer->pfnCompletionRoutine = NULL;
|
|
|
|
timer->lpArgToCompletionRoutine = NULL;
|
|
|
|
timer->bInit = FALSE;
|
2019-02-08 11:15:04 +03:00
|
|
|
|
|
|
|
if (lpTimerName)
|
|
|
|
timer->name = strdup(lpTimerName);
|
|
|
|
|
2015-03-16 02:29:37 +03:00
|
|
|
timer->ops = &ops;
|
2021-03-24 20:32:43 +03:00
|
|
|
#if defined(TIMER_IMPL_DISPATCH) || defined(TIMER_IMPL_POSIX)
|
|
|
|
if (!winpr_event_init(&timer->event))
|
2018-03-08 15:46:46 +03:00
|
|
|
goto fail;
|
2021-04-22 10:26:59 +03:00
|
|
|
timer->fd = timer->event.fds[0];
|
2021-03-24 20:32:43 +03:00
|
|
|
#endif
|
2018-03-08 15:46:46 +03:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#if defined(TIMER_IMPL_DISPATCH)
|
2018-03-08 15:46:46 +03:00
|
|
|
timer->queue = dispatch_queue_create(TAG, DISPATCH_QUEUE_SERIAL);
|
|
|
|
|
|
|
|
if (!timer->queue)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
timer->source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timer->queue);
|
|
|
|
|
|
|
|
if (!timer->source)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
dispatch_set_context(timer->source, timer);
|
|
|
|
dispatch_source_set_event_handler_f(timer->source, WaitableTimerHandler);
|
|
|
|
#endif
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
|
|
|
|
return handle;
|
2021-03-24 20:32:43 +03:00
|
|
|
|
|
|
|
#if defined(TIMER_IMPL_DISPATCH) || defined(TIMER_IMPL_POSIX)
|
2018-03-08 15:46:46 +03:00
|
|
|
fail:
|
|
|
|
TimerCloseHandle(handle);
|
|
|
|
return NULL;
|
2018-05-11 12:00:01 +03:00
|
|
|
#endif
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
|
|
|
|
2017-11-14 15:55:58 +03:00
|
|
|
HANDLE CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes, BOOL bManualReset,
|
|
|
|
LPCWSTR lpTimerName)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2018-03-08 15:46:46 +03:00
|
|
|
int rc;
|
|
|
|
HANDLE handle;
|
|
|
|
LPSTR name = NULL;
|
|
|
|
rc = ConvertFromUnicode(CP_UTF8, 0, lpTimerName, -1, &name, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
handle = CreateWaitableTimerA(lpTimerAttributes, bManualReset, name);
|
|
|
|
free(name);
|
|
|
|
return handle;
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
2017-11-14 15:55:58 +03:00
|
|
|
HANDLE CreateWaitableTimerExA(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCSTR lpTimerName,
|
|
|
|
DWORD dwFlags, DWORD dwDesiredAccess)
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2019-02-07 19:26:26 +03:00
|
|
|
BOOL bManualReset = (dwFlags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
if (dwDesiredAccess != 0)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "%s [%s] does not support dwDesiredAccess 0x%08" PRIx32, __FUNCTION__,
|
|
|
|
lpTimerName, dwDesiredAccess);
|
2019-02-07 19:26:26 +03:00
|
|
|
|
2013-07-29 19:57:29 +04:00
|
|
|
return CreateWaitableTimerA(lpTimerAttributes, bManualReset, lpTimerName);
|
|
|
|
}
|
|
|
|
|
2017-11-14 15:55:58 +03:00
|
|
|
HANDLE CreateWaitableTimerExW(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCWSTR lpTimerName,
|
|
|
|
DWORD dwFlags, DWORD dwDesiredAccess)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2018-03-08 15:46:46 +03:00
|
|
|
int rc;
|
|
|
|
HANDLE handle;
|
|
|
|
LPSTR name = NULL;
|
|
|
|
rc = ConvertFromUnicode(CP_UTF8, 0, lpTimerName, -1, &name, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
handle = CreateWaitableTimerExA(lpTimerAttributes, name, dwFlags, dwDesiredAccess);
|
|
|
|
free(name);
|
|
|
|
return handle;
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
static void timerAPC(LPVOID arg)
|
|
|
|
{
|
|
|
|
WINPR_TIMER* timer = (WINPR_TIMER*)arg;
|
|
|
|
|
|
|
|
if (!timer->lPeriod)
|
|
|
|
{
|
|
|
|
/* this is a one time shot timer with a completion, let's remove us from
|
|
|
|
the APC list */
|
|
|
|
switch (apc_remove(&timer->apcItem))
|
|
|
|
{
|
|
|
|
case APC_REMOVE_OK:
|
|
|
|
case APC_REMOVE_DELAY_FREE:
|
|
|
|
break;
|
|
|
|
case APC_REMOVE_ERROR:
|
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "error removing the APC routine");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer->pfnCompletionRoutine)
|
|
|
|
timer->pfnCompletionRoutine(timer->lpArgToCompletionRoutine, 0, 0);
|
|
|
|
|
|
|
|
#ifdef TIMER_IMPL_TIMERFD
|
|
|
|
while (timer_drain_fd(timer->fd))
|
|
|
|
;
|
|
|
|
#else
|
|
|
|
winpr_event_reset(&timer->event);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
BOOL SetWaitableTimer(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
|
2019-11-06 17:24:51 +03:00
|
|
|
PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine,
|
|
|
|
BOOL fResume)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2013-07-29 19:57:29 +04:00
|
|
|
ULONG Type;
|
2015-07-03 10:36:58 +03:00
|
|
|
WINPR_HANDLE* Object;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER* timer;
|
2014-08-18 19:22:22 +04:00
|
|
|
LONGLONG seconds = 0;
|
2013-07-29 19:57:29 +04:00
|
|
|
LONGLONG nanoseconds = 0;
|
2014-07-23 22:22:30 +04:00
|
|
|
int status = 0;
|
2013-07-29 19:57:29 +04:00
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hTimer, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type != HANDLE_TYPE_TIMER)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!lpDueTime)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (lPeriod < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-02-07 19:26:26 +03:00
|
|
|
if (fResume)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "%s does not support fResume", __FUNCTION__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
timer = (WINPR_TIMER*)Object;
|
2013-07-29 19:57:29 +04:00
|
|
|
timer->lPeriod = lPeriod; /* milliseconds */
|
|
|
|
timer->pfnCompletionRoutine = pfnCompletionRoutine;
|
|
|
|
timer->lpArgToCompletionRoutine = lpArgToCompletionRoutine;
|
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if (!timer->bInit)
|
|
|
|
{
|
|
|
|
if (InitializeWaitableTimer(timer) < 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#if defined(TIMER_IMPL_TIMERFD) || defined(TIMER_IMPL_POSIX)
|
2013-07-29 19:57:29 +04:00
|
|
|
ZeroMemory(&(timer->timeout), sizeof(struct itimerspec));
|
|
|
|
|
2013-07-29 21:07:39 +04:00
|
|
|
if (lpDueTime->QuadPart < 0)
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
|
|
|
LONGLONG due = lpDueTime->QuadPart * (-1);
|
|
|
|
/* due time is in 100 nanosecond intervals */
|
|
|
|
seconds = (due / 10000000);
|
|
|
|
nanoseconds = ((due % 10000000) * 100);
|
|
|
|
}
|
2013-07-29 21:07:39 +04:00
|
|
|
else if (lpDueTime->QuadPart == 0)
|
|
|
|
{
|
|
|
|
seconds = nanoseconds = 0;
|
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
else
|
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_ERR(TAG, "absolute time not implemented");
|
2013-07-29 21:07:39 +04:00
|
|
|
return FALSE;
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lPeriod > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
timer->timeout.it_interval.tv_sec = (lPeriod / 1000); /* seconds */
|
2013-07-29 19:57:29 +04:00
|
|
|
timer->timeout.it_interval.tv_nsec = ((lPeriod % 1000) * 1000000); /* nanoseconds */
|
|
|
|
}
|
|
|
|
|
2013-07-29 21:07:39 +04:00
|
|
|
if (lpDueTime->QuadPart != 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
timer->timeout.it_value.tv_sec = seconds; /* seconds */
|
2013-07-29 21:07:39 +04:00
|
|
|
timer->timeout.it_value.tv_nsec = nanoseconds; /* nanoseconds */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
timer->timeout.it_value.tv_sec = timer->timeout.it_interval.tv_sec; /* seconds */
|
2013-07-29 21:07:39 +04:00
|
|
|
timer->timeout.it_value.tv_nsec = timer->timeout.it_interval.tv_nsec; /* nanoseconds */
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef TIMER_IMPL_TIMERFD
|
|
|
|
status = timerfd_settime(timer->fd, 0, &(timer->timeout), NULL);
|
|
|
|
if (status)
|
2014-01-25 07:44:23 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "timerfd_settime failure: %d", status);
|
|
|
|
return FALSE;
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
#else
|
|
|
|
status = timer_settime(timer->tid, 0, &(timer->timeout), NULL);
|
|
|
|
if (status != 0)
|
2013-07-29 21:07:39 +04:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "timer_settime failure");
|
|
|
|
return FALSE;
|
2013-07-29 21:07:39 +04:00
|
|
|
}
|
2021-03-24 20:32:43 +03:00
|
|
|
#endif
|
|
|
|
#endif
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
#ifdef TIMER_IMPL_DISPATCH
|
2018-03-08 15:46:46 +03:00
|
|
|
if (lpDueTime->QuadPart < 0)
|
|
|
|
{
|
|
|
|
LONGLONG due = lpDueTime->QuadPart * (-1);
|
|
|
|
/* due time is in 100 nanosecond intervals */
|
|
|
|
seconds = (due / 10000000);
|
|
|
|
nanoseconds = due * 100;
|
|
|
|
}
|
|
|
|
else if (lpDueTime->QuadPart == 0)
|
|
|
|
{
|
|
|
|
seconds = nanoseconds = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "absolute time not implemented");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:32:43 +03:00
|
|
|
if (!winpr_event_reset(&timer->event))
|
2018-10-24 14:17:14 +03:00
|
|
|
{
|
2021-03-24 20:32:43 +03:00
|
|
|
WLog_ERR(TAG, "error when resetting timer event");
|
2018-03-08 15:46:46 +03:00
|
|
|
}
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
{
|
|
|
|
if (timer->running)
|
|
|
|
dispatch_suspend(timer->source);
|
|
|
|
|
|
|
|
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, nanoseconds);
|
|
|
|
uint64_t interval = DISPATCH_TIME_FOREVER;
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
if (lPeriod > 0)
|
|
|
|
interval = lPeriod * 1000000;
|
|
|
|
|
|
|
|
dispatch_source_set_timer(timer->source, start, interval, 0);
|
|
|
|
dispatch_resume(timer->source);
|
|
|
|
timer->running = TRUE;
|
|
|
|
}
|
2014-01-26 09:06:27 +04:00
|
|
|
#endif
|
2021-03-24 20:32:43 +03:00
|
|
|
|
|
|
|
if (pfnCompletionRoutine)
|
|
|
|
{
|
|
|
|
WINPR_APC_ITEM* apcItem = &timer->apcItem;
|
|
|
|
|
|
|
|
/* install our APC routine that will call the completion */
|
|
|
|
apcItem->type = APC_TYPE_TIMER;
|
|
|
|
apcItem->alwaysSignaled = FALSE;
|
|
|
|
apcItem->pollFd = timer->fd;
|
|
|
|
apcItem->pollMode = WINPR_FD_READ;
|
|
|
|
apcItem->completion = timerAPC;
|
|
|
|
apcItem->completionArgs = timer;
|
|
|
|
|
|
|
|
if (!apcItem->linked)
|
|
|
|
{
|
|
|
|
WINPR_THREAD* thread = winpr_GetCurrentThread();
|
|
|
|
if (!thread)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
apc_register(thread, apcItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (timer->apcItem.linked)
|
|
|
|
{
|
|
|
|
apc_remove(&timer->apcItem);
|
|
|
|
}
|
|
|
|
}
|
2012-09-18 23:51:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
BOOL SetWaitableTimerEx(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
|
2019-11-06 17:24:51 +03:00
|
|
|
PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine,
|
|
|
|
PREASON_CONTEXT WakeContext, ULONG TolerableDelay)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return SetWaitableTimer(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine,
|
|
|
|
lpArgToCompletionRoutine, FALSE);
|
2012-09-18 23:51:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE OpenWaitableTimerA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpTimerName)
|
|
|
|
{
|
2019-02-07 19:26:26 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
2012-09-18 23:51:33 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE OpenWaitableTimerW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpTimerName)
|
|
|
|
{
|
2019-02-07 19:26:26 +03:00
|
|
|
/* TODO: Implement */
|
|
|
|
WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
|
2012-09-18 23:51:33 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CancelWaitableTimer(HANDLE hTimer)
|
|
|
|
{
|
2018-03-08 15:46:46 +03:00
|
|
|
ULONG Type;
|
|
|
|
WINPR_HANDLE* Object;
|
|
|
|
WINPR_TIMER* timer;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hTimer, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type != HANDLE_TYPE_TIMER)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
timer = (WINPR_TIMER*)Object;
|
|
|
|
#if defined(__APPLE__)
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
if (timer->running)
|
|
|
|
dispatch_suspend(timer->source);
|
2018-10-24 14:17:14 +03:00
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
timer->running = FALSE;
|
|
|
|
#endif
|
2012-09-18 23:51:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-09-19 02:36:13 +04:00
|
|
|
|
2014-01-25 02:48:55 +04:00
|
|
|
/**
|
|
|
|
* Timer-Queue Timer
|
|
|
|
*/
|
|
|
|
|
2014-01-26 21:37:32 +04:00
|
|
|
/**
|
|
|
|
* Design, Performance, and Optimization of Timer Strategies for Real-time ORBs:
|
|
|
|
* http://www.cs.wustl.edu/~schmidt/Timer_Queue.html
|
|
|
|
*/
|
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
static void timespec_add_ms(struct timespec* tspec, UINT32 ms)
|
2014-01-27 05:39:13 +04:00
|
|
|
{
|
|
|
|
UINT64 ns = tspec->tv_nsec + (ms * 1000000);
|
|
|
|
tspec->tv_sec += (ns / 1000000000);
|
|
|
|
tspec->tv_nsec = (ns % 1000000000);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void timespec_gettimeofday(struct timespec* tspec)
|
2014-01-27 05:39:13 +04:00
|
|
|
{
|
|
|
|
struct timeval tval;
|
|
|
|
gettimeofday(&tval, NULL);
|
|
|
|
tspec->tv_sec = tval.tv_sec;
|
|
|
|
tspec->tv_nsec = tval.tv_usec * 1000;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static int timespec_compare(const struct timespec* tspec1, const struct timespec* tspec2)
|
2014-01-27 05:39:13 +04:00
|
|
|
{
|
2014-03-04 01:57:43 +04:00
|
|
|
if (tspec1->tv_sec == tspec2->tv_sec)
|
|
|
|
return (tspec1->tv_nsec - tspec2->tv_nsec);
|
|
|
|
else
|
|
|
|
return (tspec1->tv_sec - tspec2->tv_sec);
|
2014-01-27 05:39:13 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void timespec_copy(struct timespec* dst, struct timespec* src)
|
2014-01-27 05:39:13 +04:00
|
|
|
{
|
|
|
|
dst->tv_sec = src->tv_sec;
|
|
|
|
dst->tv_nsec = src->tv_nsec;
|
|
|
|
}
|
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
static void InsertTimerQueueTimer(WINPR_TIMER_QUEUE_TIMER** pHead, WINPR_TIMER_QUEUE_TIMER* timer)
|
2014-01-26 21:37:32 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-01-27 05:39:13 +04:00
|
|
|
if (!(*pHead))
|
2014-01-26 21:37:32 +04:00
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
*pHead = timer;
|
2014-03-04 01:57:43 +04:00
|
|
|
timer->next = NULL;
|
2014-01-27 05:39:13 +04:00
|
|
|
return;
|
2014-01-26 21:37:32 +04:00
|
|
|
}
|
2014-01-27 05:39:13 +04:00
|
|
|
|
|
|
|
node = *pHead;
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
while (node->next)
|
2014-01-26 21:37:32 +04:00
|
|
|
{
|
2014-03-04 01:57:43 +04:00
|
|
|
if (timespec_compare(&(timer->ExpirationTime), &(node->ExpirationTime)) > 0)
|
|
|
|
{
|
|
|
|
if (timespec_compare(&(timer->ExpirationTime), &(node->next->ExpirationTime)) < 0)
|
|
|
|
break;
|
|
|
|
}
|
2014-01-27 05:39:13 +04:00
|
|
|
|
2014-01-26 21:37:32 +04:00
|
|
|
node = node->next;
|
|
|
|
}
|
2014-01-27 05:39:13 +04:00
|
|
|
|
|
|
|
if (node->next)
|
2014-03-04 01:57:43 +04:00
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
timer->next = node->next->next;
|
2014-03-04 01:57:43 +04:00
|
|
|
node->next = timer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->next = timer;
|
|
|
|
timer->next = NULL;
|
|
|
|
}
|
2014-01-26 21:37:32 +04:00
|
|
|
}
|
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
static void RemoveTimerQueueTimer(WINPR_TIMER_QUEUE_TIMER** pHead, WINPR_TIMER_QUEUE_TIMER* timer)
|
2014-01-27 06:56:07 +04:00
|
|
|
{
|
2014-03-04 01:57:43 +04:00
|
|
|
BOOL found = FALSE;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* prevNode;
|
2014-01-27 06:56:07 +04:00
|
|
|
|
|
|
|
if (timer == *pHead)
|
|
|
|
{
|
|
|
|
*pHead = timer->next;
|
2014-03-04 01:57:43 +04:00
|
|
|
timer->next = NULL;
|
2014-01-27 06:56:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = *pHead;
|
|
|
|
prevNode = NULL;
|
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
if (node == timer)
|
2014-03-04 01:57:43 +04:00
|
|
|
{
|
|
|
|
found = TRUE;
|
2014-01-27 06:56:07 +04:00
|
|
|
break;
|
2014-03-04 01:57:43 +04:00
|
|
|
}
|
2014-01-27 06:56:07 +04:00
|
|
|
|
|
|
|
prevNode = node;
|
|
|
|
node = node->next;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-03-04 01:57:43 +04:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
if (prevNode)
|
|
|
|
{
|
|
|
|
prevNode->next = timer->next;
|
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-03-04 01:57:43 +04:00
|
|
|
timer->next = NULL;
|
|
|
|
}
|
2014-01-27 06:56:07 +04:00
|
|
|
}
|
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
static int FireExpiredTimerQueueTimers(WINPR_TIMER_QUEUE* timerQueue)
|
2014-01-27 02:21:12 +04:00
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
struct timespec CurrentTime;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
2014-01-27 02:21:12 +04:00
|
|
|
|
2014-01-27 19:37:38 +04:00
|
|
|
if (!timerQueue->activeHead)
|
2014-01-27 02:21:12 +04:00
|
|
|
return 0;
|
|
|
|
|
2014-01-27 05:39:13 +04:00
|
|
|
timespec_gettimeofday(&CurrentTime);
|
2014-01-27 19:37:38 +04:00
|
|
|
node = timerQueue->activeHead;
|
2014-01-27 02:21:12 +04:00
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
if (timespec_compare(&CurrentTime, &(node->ExpirationTime)) >= 0)
|
2014-01-27 02:21:12 +04:00
|
|
|
{
|
|
|
|
node->Callback(node->Parameter, TRUE);
|
|
|
|
node->FireCount++;
|
2014-01-27 19:37:38 +04:00
|
|
|
timerQueue->activeHead = node->next;
|
2014-01-27 06:56:07 +04:00
|
|
|
node->next = NULL;
|
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
if (node->Period)
|
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
timespec_add_ms(&(node->ExpirationTime), node->Period);
|
2014-01-27 19:37:38 +04:00
|
|
|
InsertTimerQueueTimer(&(timerQueue->activeHead), node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InsertTimerQueueTimer(&(timerQueue->inactiveHead), node);
|
2014-01-27 02:21:12 +04:00
|
|
|
}
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2014-03-04 01:57:43 +04:00
|
|
|
node = timerQueue->activeHead;
|
2014-01-27 02:21:12 +04:00
|
|
|
}
|
2014-01-27 05:49:48 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-01-27 02:21:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void* TimerQueueThread(void* arg)
|
2014-01-27 02:21:12 +04:00
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
int status;
|
2014-01-27 05:49:48 +04:00
|
|
|
struct timespec timeout;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue = (WINPR_TIMER_QUEUE*)arg;
|
2014-01-27 02:21:12 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
pthread_mutex_lock(&(timerQueue->cond_mutex));
|
2014-01-27 19:37:38 +04:00
|
|
|
timespec_gettimeofday(&timeout);
|
|
|
|
|
|
|
|
if (!timerQueue->activeHead)
|
2014-01-27 05:49:48 +04:00
|
|
|
{
|
2014-03-04 01:57:43 +04:00
|
|
|
timespec_add_ms(&timeout, 50);
|
2014-01-27 05:49:48 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-27 19:37:38 +04:00
|
|
|
if (timespec_compare(&timeout, &(timerQueue->activeHead->ExpirationTime)) < 0)
|
|
|
|
{
|
|
|
|
timespec_copy(&timeout, &(timerQueue->activeHead->ExpirationTime));
|
|
|
|
}
|
2014-01-27 05:49:48 +04:00
|
|
|
}
|
2014-01-27 05:39:13 +04:00
|
|
|
|
2014-01-27 05:49:48 +04:00
|
|
|
status = pthread_cond_timedwait(&(timerQueue->cond), &(timerQueue->cond_mutex), &timeout);
|
2014-01-27 02:21:12 +04:00
|
|
|
FireExpiredTimerQueueTimers(timerQueue);
|
2014-01-27 05:39:13 +04:00
|
|
|
pthread_mutex_unlock(&(timerQueue->cond_mutex));
|
2014-01-27 06:56:07 +04:00
|
|
|
|
2017-11-14 15:55:58 +03:00
|
|
|
if ((status != ETIMEDOUT) && (status != 0))
|
|
|
|
break;
|
|
|
|
|
2014-01-27 06:56:07 +04:00
|
|
|
if (timerQueue->bCancelled)
|
|
|
|
break;
|
2014-01-27 02:21:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-08 15:46:46 +03:00
|
|
|
static int StartTimerQueueThread(WINPR_TIMER_QUEUE* timerQueue)
|
2014-01-27 02:21:12 +04:00
|
|
|
{
|
|
|
|
pthread_cond_init(&(timerQueue->cond), NULL);
|
2014-01-27 05:39:13 +04:00
|
|
|
pthread_mutex_init(&(timerQueue->cond_mutex), NULL);
|
2014-01-27 02:21:12 +04:00
|
|
|
pthread_mutex_init(&(timerQueue->mutex), NULL);
|
|
|
|
pthread_attr_init(&(timerQueue->attr));
|
|
|
|
timerQueue->param.sched_priority = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
pthread_attr_setschedparam(&(timerQueue->attr), &(timerQueue->param));
|
|
|
|
pthread_attr_setschedpolicy(&(timerQueue->attr), SCHED_FIFO);
|
|
|
|
pthread_create(&(timerQueue->thread), &(timerQueue->attr), TimerQueueThread, timerQueue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-25 02:48:55 +04:00
|
|
|
HANDLE CreateTimerQueue(void)
|
|
|
|
{
|
|
|
|
HANDLE handle = NULL;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
2019-11-06 17:24:51 +03:00
|
|
|
timerQueue = (WINPR_TIMER_QUEUE*)calloc(1, sizeof(WINPR_TIMER_QUEUE));
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
if (timerQueue)
|
|
|
|
{
|
2015-07-14 12:39:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(timerQueue, HANDLE_TYPE_TIMER_QUEUE, WINPR_FD_READ);
|
2019-11-06 17:24:51 +03:00
|
|
|
handle = (HANDLE)timerQueue;
|
2014-01-27 19:37:38 +04:00
|
|
|
timerQueue->activeHead = NULL;
|
2014-03-04 01:57:43 +04:00
|
|
|
timerQueue->inactiveHead = NULL;
|
2014-01-27 06:56:07 +04:00
|
|
|
timerQueue->bCancelled = FALSE;
|
2014-01-26 21:37:32 +04:00
|
|
|
StartTimerQueueThread(timerQueue);
|
2014-01-25 02:48:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
BOOL DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
|
2014-01-25 02:48:55 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
void* rvalue;
|
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* nextNode;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
if (!TimerQueue)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
|
2014-01-27 06:56:07 +04:00
|
|
|
/* Cancel and delete timer queue timers */
|
|
|
|
pthread_mutex_lock(&(timerQueue->cond_mutex));
|
|
|
|
timerQueue->bCancelled = TRUE;
|
|
|
|
pthread_cond_signal(&(timerQueue->cond));
|
|
|
|
pthread_mutex_unlock(&(timerQueue->cond_mutex));
|
|
|
|
pthread_join(timerQueue->thread, &rvalue);
|
2016-06-06 14:18:00 +03:00
|
|
|
/**
|
|
|
|
* Quote from MSDN regarding CompletionEvent:
|
|
|
|
* If this parameter is INVALID_HANDLE_VALUE, the function waits for
|
|
|
|
* all callback functions to complete before returning.
|
|
|
|
* If this parameter is NULL, the function marks the timer for
|
|
|
|
* deletion and returns immediately.
|
|
|
|
*
|
|
|
|
* Note: The current WinPR implementation implicitly waits for any
|
|
|
|
* callback functions to complete (see pthread_join above)
|
|
|
|
*/
|
2014-01-27 06:56:07 +04:00
|
|
|
{
|
2014-01-27 19:37:38 +04:00
|
|
|
/* Move all active timers to the inactive timer list */
|
|
|
|
node = timerQueue->activeHead;
|
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
InsertTimerQueueTimer(&(timerQueue->inactiveHead), node);
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
timerQueue->activeHead = NULL;
|
|
|
|
/* Once all timers are inactive, free them */
|
|
|
|
node = timerQueue->inactiveHead;
|
2014-01-27 06:56:07 +04:00
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
nextNode = node->next;
|
|
|
|
free(node);
|
|
|
|
node = nextNode;
|
|
|
|
}
|
|
|
|
|
2014-01-27 19:37:38 +04:00
|
|
|
timerQueue->inactiveHead = NULL;
|
2014-01-27 06:56:07 +04:00
|
|
|
}
|
|
|
|
/* Delete timer queue */
|
2014-01-27 02:21:12 +04:00
|
|
|
pthread_cond_destroy(&(timerQueue->cond));
|
2014-01-27 05:39:13 +04:00
|
|
|
pthread_mutex_destroy(&(timerQueue->cond_mutex));
|
2014-01-27 02:21:12 +04:00
|
|
|
pthread_mutex_destroy(&(timerQueue->mutex));
|
2014-01-27 05:39:13 +04:00
|
|
|
pthread_attr_destroy(&(timerQueue->attr));
|
2014-01-25 02:48:55 +04:00
|
|
|
free(timerQueue);
|
|
|
|
|
2014-01-27 06:56:07 +04:00
|
|
|
if (CompletionEvent && (CompletionEvent != INVALID_HANDLE_VALUE))
|
|
|
|
SetEvent(CompletionEvent);
|
|
|
|
|
2014-01-25 02:48:55 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
BOOL DeleteTimerQueue(HANDLE TimerQueue)
|
2014-01-25 02:48:55 +04:00
|
|
|
{
|
2014-01-27 02:21:12 +04:00
|
|
|
return DeleteTimerQueueEx(TimerQueue, NULL);
|
2014-01-25 02:48:55 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL CreateTimerQueueTimer(PHANDLE phNewTimer, HANDLE TimerQueue, WAITORTIMERCALLBACK Callback,
|
|
|
|
PVOID Parameter, DWORD DueTime, DWORD Period, ULONG Flags)
|
2014-01-25 02:48:55 +04:00
|
|
|
{
|
2014-01-27 19:37:38 +04:00
|
|
|
struct timespec CurrentTime;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* timer;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
2014-04-26 07:36:35 +04:00
|
|
|
if (!TimerQueue)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-01-27 19:37:38 +04:00
|
|
|
timespec_gettimeofday(&CurrentTime);
|
2019-11-06 17:24:51 +03:00
|
|
|
timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
|
|
|
|
timer = (WINPR_TIMER_QUEUE_TIMER*)malloc(sizeof(WINPR_TIMER_QUEUE_TIMER));
|
2014-01-25 02:48:55 +04:00
|
|
|
|
2014-04-26 07:36:35 +04:00
|
|
|
if (!timer)
|
2014-01-25 02:48:55 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2015-07-14 12:39:41 +03:00
|
|
|
WINPR_HANDLE_SET_TYPE_AND_MODE(timer, HANDLE_TYPE_TIMER_QUEUE_TIMER, WINPR_FD_READ);
|
2019-11-06 17:24:51 +03:00
|
|
|
*((UINT_PTR*)phNewTimer) = (UINT_PTR)(HANDLE)timer;
|
2014-01-27 19:37:38 +04:00
|
|
|
timespec_copy(&(timer->StartTime), &CurrentTime);
|
2014-01-27 05:39:13 +04:00
|
|
|
timespec_add_ms(&(timer->StartTime), DueTime);
|
|
|
|
timespec_copy(&(timer->ExpirationTime), &(timer->StartTime));
|
2014-01-25 07:44:23 +04:00
|
|
|
timer->Flags = Flags;
|
|
|
|
timer->DueTime = DueTime;
|
|
|
|
timer->Period = Period;
|
|
|
|
timer->Callback = Callback;
|
|
|
|
timer->Parameter = Parameter;
|
2019-11-06 17:24:51 +03:00
|
|
|
timer->timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
|
2014-01-27 02:21:12 +04:00
|
|
|
timer->FireCount = 0;
|
2014-03-04 01:57:43 +04:00
|
|
|
timer->next = NULL;
|
2014-01-27 06:56:07 +04:00
|
|
|
pthread_mutex_lock(&(timerQueue->cond_mutex));
|
2014-01-27 19:37:38 +04:00
|
|
|
InsertTimerQueueTimer(&(timerQueue->activeHead), timer);
|
2014-01-27 06:56:07 +04:00
|
|
|
pthread_cond_signal(&(timerQueue->cond));
|
|
|
|
pthread_mutex_unlock(&(timerQueue->cond_mutex));
|
2014-01-25 02:48:55 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ChangeTimerQueueTimer(HANDLE TimerQueue, HANDLE Timer, ULONG DueTime, ULONG Period)
|
|
|
|
{
|
2014-01-27 19:37:38 +04:00
|
|
|
struct timespec CurrentTime;
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* timer;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
if (!TimerQueue || !Timer)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-01-27 19:37:38 +04:00
|
|
|
timespec_gettimeofday(&CurrentTime);
|
2019-11-06 17:24:51 +03:00
|
|
|
timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
|
|
|
|
timer = (WINPR_TIMER_QUEUE_TIMER*)Timer;
|
2014-01-27 06:56:07 +04:00
|
|
|
pthread_mutex_lock(&(timerQueue->cond_mutex));
|
2014-01-27 19:37:38 +04:00
|
|
|
RemoveTimerQueueTimer(&(timerQueue->activeHead), timer);
|
2014-03-04 01:57:43 +04:00
|
|
|
RemoveTimerQueueTimer(&(timerQueue->inactiveHead), timer);
|
2014-01-27 06:56:07 +04:00
|
|
|
timer->DueTime = DueTime;
|
|
|
|
timer->Period = Period;
|
2014-03-04 01:57:43 +04:00
|
|
|
timer->next = NULL;
|
2014-01-27 19:37:38 +04:00
|
|
|
timespec_copy(&(timer->StartTime), &CurrentTime);
|
2014-01-27 06:56:07 +04:00
|
|
|
timespec_add_ms(&(timer->StartTime), DueTime);
|
|
|
|
timespec_copy(&(timer->ExpirationTime), &(timer->StartTime));
|
2014-01-27 19:37:38 +04:00
|
|
|
InsertTimerQueueTimer(&(timerQueue->activeHead), timer);
|
2014-01-27 06:56:07 +04:00
|
|
|
pthread_cond_signal(&(timerQueue->cond));
|
|
|
|
pthread_mutex_unlock(&(timerQueue->cond_mutex));
|
2014-01-25 02:48:55 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL DeleteTimerQueueTimer(HANDLE TimerQueue, HANDLE Timer, HANDLE CompletionEvent)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* timer;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
if (!TimerQueue || !Timer)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
|
|
|
|
timer = (WINPR_TIMER_QUEUE_TIMER*)Timer;
|
2014-01-27 06:56:07 +04:00
|
|
|
pthread_mutex_lock(&(timerQueue->cond_mutex));
|
2016-06-06 14:18:00 +03:00
|
|
|
/**
|
|
|
|
* Quote from MSDN regarding CompletionEvent:
|
|
|
|
* If this parameter is INVALID_HANDLE_VALUE, the function waits for
|
|
|
|
* all callback functions to complete before returning.
|
|
|
|
* If this parameter is NULL, the function marks the timer for
|
|
|
|
* deletion and returns immediately.
|
|
|
|
*
|
|
|
|
* Note: The current WinPR implementation implicitly waits for any
|
|
|
|
* callback functions to complete (see cond_mutex usage)
|
|
|
|
*/
|
|
|
|
RemoveTimerQueueTimer(&(timerQueue->activeHead), timer);
|
2014-01-27 06:56:07 +04:00
|
|
|
pthread_cond_signal(&(timerQueue->cond));
|
|
|
|
pthread_mutex_unlock(&(timerQueue->cond_mutex));
|
2014-01-25 02:48:55 +04:00
|
|
|
free(timer);
|
|
|
|
|
2014-01-27 06:56:07 +04:00
|
|
|
if (CompletionEvent && (CompletionEvent != INVALID_HANDLE_VALUE))
|
|
|
|
SetEvent(CompletionEvent);
|
|
|
|
|
2014-01-25 02:48:55 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-19 02:36:13 +04:00
|
|
|
#endif
|