2012-09-18 23:51:33 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Synchronization Functions
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
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
|
|
|
|
|
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"
|
|
|
|
|
2014-01-26 09:06:27 +04:00
|
|
|
#ifdef WITH_POSIX_TIMER
|
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
static BOOL g_WaitableTimerSignalHandlerInstalled = FALSE;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
void WaitableTimerSignalHandler(int signum, siginfo_t* siginfo, void* arg)
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2014-01-25 07:44:23 +04:00
|
|
|
WINPR_TIMER* timer = siginfo->si_value.sival_ptr;
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if (!timer || (signum != SIGALRM))
|
|
|
|
return;
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if (timer->pfnCompletionRoutine)
|
2013-07-29 19:57:29 +04:00
|
|
|
{
|
2014-01-25 07:44:23 +04:00
|
|
|
timer->pfnCompletionRoutine(timer->lpArgToCompletionRoutine, 0, 0);
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if (timer->lPeriod)
|
|
|
|
{
|
|
|
|
timer->timeout.it_interval.tv_sec = (timer->lPeriod / 1000); /* seconds */
|
|
|
|
timer->timeout.it_interval.tv_nsec = ((timer->lPeriod % 1000) * 1000000); /* nanoseconds */
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if ((timer_settime(timer->tid, 0, &(timer->timeout), NULL)) != 0)
|
|
|
|
{
|
|
|
|
perror("timer_settime");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
int InstallWaitableTimerSignalHandler()
|
|
|
|
{
|
|
|
|
if (!g_WaitableTimerSignalHandlerInstalled)
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
sigaddset(&action.sa_mask, SIGALRM);
|
|
|
|
|
|
|
|
action.sa_flags = SA_RESTART | SA_SIGINFO;
|
|
|
|
action.sa_sigaction = (void*) &WaitableTimerSignalHandler;
|
|
|
|
|
|
|
|
sigaction(SIGALRM, &action, NULL);
|
|
|
|
|
|
|
|
g_WaitableTimerSignalHandlerInstalled = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-26 09:06:27 +04:00
|
|
|
#endif
|
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
int InitializeWaitableTimer(WINPR_TIMER* timer)
|
|
|
|
{
|
|
|
|
if (!timer->lpArgToCompletionRoutine)
|
|
|
|
{
|
2013-08-04 03:50:17 +04:00
|
|
|
#ifdef HAVE_TIMERFD_H
|
2014-01-26 09:06:27 +04:00
|
|
|
int status;
|
|
|
|
|
2013-07-29 19:57:29 +04:00
|
|
|
timer->fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
2014-01-25 07:44:23 +04:00
|
|
|
|
2013-07-29 19:57:29 +04:00
|
|
|
if (timer->fd <= 0)
|
2013-08-29 17:30:22 +04:00
|
|
|
{
|
|
|
|
free(timer);
|
2014-01-25 07:44:23 +04:00
|
|
|
return -1;
|
2013-08-29 17:30:22 +04:00
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
|
|
|
|
status = fcntl(timer->fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
if (status)
|
2013-08-29 17:30:22 +04:00
|
|
|
{
|
|
|
|
close(timer->fd);
|
2014-01-25 07:44:23 +04:00
|
|
|
return -1;
|
2013-08-29 17:30:22 +04:00
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
2014-01-25 07:44:23 +04:00
|
|
|
else
|
|
|
|
{
|
2014-01-26 09:06:27 +04:00
|
|
|
#ifdef WITH_POSIX_TIMER
|
2014-01-25 07:44:23 +04:00
|
|
|
struct sigevent sigev;
|
|
|
|
|
|
|
|
InstallWaitableTimerSignalHandler();
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
perror("timer_create");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-26 09:06:27 +04:00
|
|
|
#endif
|
2014-01-25 07:44:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
timer->bInit = TRUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waitable Timer
|
|
|
|
*/
|
|
|
|
|
|
|
|
HANDLE CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes, BOOL bManualReset, LPCSTR lpTimerName)
|
|
|
|
{
|
|
|
|
HANDLE handle = NULL;
|
|
|
|
WINPR_TIMER* timer;
|
|
|
|
|
|
|
|
timer = (WINPR_TIMER*) malloc(sizeof(WINPR_TIMER));
|
|
|
|
|
|
|
|
if (timer)
|
|
|
|
{
|
|
|
|
WINPR_HANDLE_SET_TYPE(timer, HANDLE_TYPE_TIMER);
|
|
|
|
handle = (HANDLE) timer;
|
|
|
|
|
|
|
|
timer->fd = -1;
|
|
|
|
timer->lPeriod = 0;
|
|
|
|
timer->bManualReset = bManualReset;
|
|
|
|
timer->pfnCompletionRoutine = NULL;
|
|
|
|
timer->lpArgToCompletionRoutine = NULL;
|
|
|
|
timer->bInit = FALSE;
|
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes, BOOL bManualReset, LPCWSTR lpTimerName)
|
2012-09-18 23:51:33 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-29 19:57:29 +04:00
|
|
|
HANDLE CreateWaitableTimerExA(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCSTR lpTimerName, DWORD dwFlags, DWORD dwDesiredAccess)
|
|
|
|
{
|
|
|
|
BOOL bManualReset;
|
|
|
|
|
|
|
|
bManualReset = (dwFlags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
return CreateWaitableTimerA(lpTimerAttributes, bManualReset, lpTimerName);
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
HANDLE CreateWaitableTimerExW(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCWSTR lpTimerName, DWORD dwFlags, DWORD dwDesiredAccess)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetWaitableTimer(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
|
|
|
|
PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine, BOOL fResume)
|
|
|
|
{
|
2013-07-29 19:57:29 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
int status = 0;
|
|
|
|
WINPR_TIMER* timer;
|
|
|
|
LONGLONG seconds = 0;
|
|
|
|
LONGLONG nanoseconds = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
timer = (WINPR_TIMER*) Object;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-26 09:06:27 +04:00
|
|
|
#ifdef WITH_POSIX_TIMER
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
printf("SetWaitableTimer: implement absolute time\n");
|
2013-07-29 21:07:39 +04:00
|
|
|
return FALSE;
|
2013-07-29 19:57:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lPeriod > 0)
|
|
|
|
{
|
|
|
|
timer->timeout.it_interval.tv_sec = (lPeriod / 1000); /* seconds */
|
|
|
|
timer->timeout.it_interval.tv_nsec = ((lPeriod % 1000) * 1000000); /* nanoseconds */
|
|
|
|
}
|
|
|
|
|
2013-07-29 21:07:39 +04:00
|
|
|
if (lpDueTime->QuadPart != 0)
|
|
|
|
{
|
|
|
|
timer->timeout.it_value.tv_sec = seconds; /* seconds */
|
|
|
|
timer->timeout.it_value.tv_nsec = nanoseconds; /* nanoseconds */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timer->timeout.it_value.tv_sec = timer->timeout.it_interval.tv_sec; /* seconds */
|
|
|
|
timer->timeout.it_value.tv_nsec = timer->timeout.it_interval.tv_nsec; /* nanoseconds */
|
|
|
|
}
|
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if (!timer->pfnCompletionRoutine)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_TIMERFD_H
|
|
|
|
status = timerfd_settime(timer->fd, 0, &(timer->timeout), NULL);
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-01-25 07:44:23 +04:00
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
printf("SetWaitableTimer timerfd_settime failure: %d\n", status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
2013-07-29 21:07:39 +04:00
|
|
|
{
|
2014-01-25 07:44:23 +04:00
|
|
|
if ((timer_settime(timer->tid, 0, &(timer->timeout), NULL)) != 0)
|
|
|
|
{
|
|
|
|
perror("timer_settime");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-07-29 21:07:39 +04:00
|
|
|
}
|
2013-07-29 19:57:29 +04:00
|
|
|
|
2014-01-26 09:06:27 +04:00
|
|
|
#endif
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL SetWaitableTimerEx(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
|
|
|
|
PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine, PREASON_CONTEXT WakeContext, ULONG TolerableDelay)
|
|
|
|
{
|
2013-07-29 19:57:29 +04:00
|
|
|
ULONG Type;
|
|
|
|
PVOID Object;
|
|
|
|
WINPR_TIMER* timer;
|
|
|
|
|
|
|
|
if (!winpr_Handle_GetInfo(hTimer, &Type, &Object))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (Type == HANDLE_TYPE_TIMER)
|
|
|
|
{
|
|
|
|
timer = (WINPR_TIMER*) Object;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:51:33 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE OpenWaitableTimerA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpTimerName)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE OpenWaitableTimerW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpTimerName)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CancelWaitableTimer(HANDLE hTimer)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2014-03-25 23:19:52 +04:00
|
|
|
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-03-25 23:19:52 +04:00
|
|
|
UINT64 timespec_to_ms(struct timespec* tspec)
|
2014-03-04 01:57:43 +04:00
|
|
|
{
|
|
|
|
UINT64 ms;
|
|
|
|
ms = tspec->tv_sec * 1000;
|
|
|
|
ms += tspec->tv_nsec / 1000000;
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2014-01-27 05:39:13 +04:00
|
|
|
static void timespec_gettimeofday(struct timespec* tspec)
|
|
|
|
{
|
|
|
|
struct timeval tval;
|
|
|
|
gettimeofday(&tval, NULL);
|
|
|
|
tspec->tv_sec = tval.tv_sec;
|
|
|
|
tspec->tv_nsec = tval.tv_usec * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int timespec_compare(const struct timespec* tspec1, const struct timespec* tspec2)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void timespec_copy(struct timespec* dst, struct timespec* src)
|
|
|
|
{
|
|
|
|
dst->tv_sec = src->tv_sec;
|
|
|
|
dst->tv_nsec = src->tv_nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertTimerQueueTimer(WINPR_TIMER_QUEUE_TIMER** pHead, WINPR_TIMER_QUEUE_TIMER* timer)
|
2014-01-26 21:37:32 +04:00
|
|
|
{
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
2014-03-04 01:57:43 +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-03-04 01:57:43 +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
|
|
|
}
|
|
|
|
|
2014-01-27 06:56:07 +04:00
|
|
|
void RemoveTimerQueueTimer(WINPR_TIMER_QUEUE_TIMER** pHead, WINPR_TIMER_QUEUE_TIMER* timer)
|
|
|
|
{
|
2014-03-04 01:57:43 +04:00
|
|
|
BOOL found = FALSE;
|
2014-01-27 06:56:07 +04:00
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* prevNode;
|
|
|
|
|
|
|
|
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-03-04 01:57:43 +04:00
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
if (prevNode)
|
|
|
|
{
|
|
|
|
prevNode->next = timer->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer->next = NULL;
|
|
|
|
}
|
2014-01-27 06:56:07 +04:00
|
|
|
}
|
|
|
|
|
2014-01-27 02:21:12 +04:00
|
|
|
int FireExpiredTimerQueueTimers(WINPR_TIMER_QUEUE* timerQueue)
|
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
struct timespec CurrentTime;
|
2014-01-27 02:21:12 +04:00
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
|
|
|
|
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 02:21:12 +04:00
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* TimerQueueThread(void* arg)
|
|
|
|
{
|
2014-01-27 05:39:13 +04:00
|
|
|
int status;
|
2014-01-27 05:49:48 +04:00
|
|
|
struct timespec timeout;
|
2014-01-27 02:21:12 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue = (WINPR_TIMER_QUEUE*) arg;
|
|
|
|
|
|
|
|
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 05:39:13 +04:00
|
|
|
|
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
|
|
|
|
|
|
|
if (timerQueue->bCancelled)
|
|
|
|
break;
|
2014-01-27 02:21:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StartTimerQueueThread(WINPR_TIMER_QUEUE* timerQueue)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
|
|
|
|
|
|
|
timerQueue = (WINPR_TIMER_QUEUE*) malloc(sizeof(WINPR_TIMER_QUEUE));
|
|
|
|
|
|
|
|
if (timerQueue)
|
|
|
|
{
|
2014-03-04 01:57:43 +04:00
|
|
|
ZeroMemory(timerQueue, sizeof(WINPR_TIMER_QUEUE));
|
|
|
|
|
2014-01-25 02:48:55 +04:00
|
|
|
WINPR_HANDLE_SET_TYPE(timerQueue, HANDLE_TYPE_TIMER_QUEUE);
|
|
|
|
handle = (HANDLE) timerQueue;
|
2014-01-27 02:21:12 +04:00
|
|
|
|
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-03-04 01:57:43 +04:00
|
|
|
|
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-01-27 06:56:07 +04:00
|
|
|
void* rvalue;
|
2014-01-25 02:48:55 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
2014-01-27 06:56:07 +04:00
|
|
|
WINPR_TIMER_QUEUE_TIMER* node;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* nextNode;
|
2014-01-25 02:48:55 +04:00
|
|
|
|
|
|
|
if (!TimerQueue)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (CompletionEvent == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* Wait for all callback functions to complete before returning */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Cancel all timers and return immediately */
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CreateTimerQueueTimer(PHANDLE phNewTimer, HANDLE TimerQueue,
|
|
|
|
WAITORTIMERCALLBACK Callback, PVOID Parameter, DWORD DueTime, DWORD Period, ULONG Flags)
|
|
|
|
{
|
2014-01-27 19:37:38 +04:00
|
|
|
struct timespec CurrentTime;
|
2014-01-26 21:37:32 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
2014-01-25 02:48:55 +04:00
|
|
|
WINPR_TIMER_QUEUE_TIMER* timer;
|
|
|
|
|
2014-04-26 07:36:35 +04:00
|
|
|
if (!TimerQueue)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-01-27 19:37:38 +04:00
|
|
|
timespec_gettimeofday(&CurrentTime);
|
|
|
|
|
2014-01-26 21:37:32 +04:00
|
|
|
timerQueue = (WINPR_TIMER_QUEUE*) TimerQueue;
|
2014-01-25 02:48:55 +04:00
|
|
|
timer = (WINPR_TIMER_QUEUE_TIMER*) malloc(sizeof(WINPR_TIMER_QUEUE_TIMER));
|
|
|
|
|
2014-04-26 07:36:35 +04:00
|
|
|
if (!timer)
|
2014-01-25 02:48:55 +04:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
WINPR_HANDLE_SET_TYPE(timer, HANDLE_TYPE_TIMER_QUEUE_TIMER);
|
|
|
|
*((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;
|
2014-01-26 21:37:32 +04:00
|
|
|
|
|
|
|
timer->timerQueue = (WINPR_TIMER_QUEUE*) TimerQueue;
|
2014-01-25 07:44:23 +04:00
|
|
|
|
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 02:21:12 +04:00
|
|
|
|
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 02:21:12 +04:00
|
|
|
|
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-01-25 02:48:55 +04:00
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* timer;
|
|
|
|
|
|
|
|
if (!TimerQueue || !Timer)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-01-27 19:37:38 +04:00
|
|
|
timespec_gettimeofday(&CurrentTime);
|
|
|
|
|
2014-01-25 02:48:55 +04: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 06:56:07 +04:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
WINPR_TIMER_QUEUE* timerQueue;
|
|
|
|
WINPR_TIMER_QUEUE_TIMER* timer;
|
|
|
|
|
|
|
|
if (!TimerQueue || !Timer)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
if (CompletionEvent == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* Wait for all callback functions to complete before returning */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Cancel timer and return immediately */
|
|
|
|
|
2014-01-27 19:37:38 +04:00
|
|
|
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
|