123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986 |
- /**
- * 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
- #include <winpr/crt.h>
- #include <winpr/file.h>
- #include <winpr/sysinfo.h>
- #include <winpr/synch.h>
- #ifndef _WIN32
- #include <unistd.h>
- #include <errno.h>
- #include <sys/time.h>
- #include <signal.h>
- #endif
- #include "synch.h"
- #ifndef _WIN32
- #include "../handle/handle.h"
- #include "../log.h"
- #define TAG WINPR_TAG("synch.timer")
- static BOOL TimerCloseHandle(HANDLE handle);
- static BOOL TimerIsHandled(HANDLE handle)
- {
- WINPR_TIMER* pTimer = (WINPR_TIMER*)handle;
- if (!pTimer || (pTimer->Type != HANDLE_TYPE_TIMER))
- {
- SetLastError(ERROR_INVALID_HANDLE);
- return FALSE;
- }
- return TRUE;
- }
- static int TimerGetFd(HANDLE handle)
- {
- WINPR_TIMER* timer = (WINPR_TIMER*)handle;
- if (!TimerIsHandled(handle))
- return -1;
- return timer->fd;
- }
- static DWORD TimerCleanupHandle(HANDLE handle)
- {
- int length;
- UINT64 expirations;
- WINPR_TIMER* timer = (WINPR_TIMER*)handle;
- if (!TimerIsHandled(handle))
- return WAIT_FAILED;
- if (timer->bManualReset)
- return WAIT_OBJECT_0;
- length = read(timer->fd, (void*)&expirations, sizeof(UINT64));
- if (length != 8)
- {
- if (length == -1)
- {
- switch (errno)
- {
- case ETIMEDOUT:
- case EAGAIN:
- return WAIT_TIMEOUT;
- default:
- break;
- }
- 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;
- }
- return WAIT_OBJECT_0;
- }
- BOOL TimerCloseHandle(HANDLE handle)
- {
- WINPR_TIMER* timer;
- timer = (WINPR_TIMER*)handle;
- if (!TimerIsHandled(handle))
- return FALSE;
- if (!timer->lpArgToCompletionRoutine)
- {
- #ifdef HAVE_SYS_TIMERFD_H
- if (timer->fd != -1)
- close(timer->fd);
- #endif
- }
- else
- {
- #ifdef WITH_POSIX_TIMER
- timer_delete(timer->tid);
- #endif
- }
- #if defined(__APPLE__)
- dispatch_release(timer->queue);
- dispatch_release(timer->source);
- if (timer->pipe[0] != -1)
- close(timer->pipe[0]);
- if (timer->pipe[1] != -1)
- close(timer->pipe[1]);
- #endif
- free(timer->name);
- free(timer);
- return TRUE;
- }
- #ifdef WITH_POSIX_TIMER
- static BOOL g_WaitableTimerSignalHandlerInstalled = FALSE;
- static void WaitableTimerHandler(void* arg)
- {
- WINPR_TIMER* timer = (WINPR_TIMER*)arg;
- if (!timer)
- return;
- if (timer->pfnCompletionRoutine)
- {
- timer->pfnCompletionRoutine(timer->lpArgToCompletionRoutine, 0, 0);
- if (timer->lPeriod)
- {
- timer->timeout.it_interval.tv_sec = (timer->lPeriod / 1000); /* seconds */
- timer->timeout.it_interval.tv_nsec =
- ((timer->lPeriod % 1000) * 1000000); /* nanoseconds */
- if ((timer_settime(timer->tid, 0, &(timer->timeout), NULL)) != 0)
- {
- WLog_ERR(TAG, "timer_settime");
- }
- }
- }
- }
- static void WaitableTimerSignalHandler(int signum, siginfo_t* siginfo, void* arg)
- {
- WINPR_TIMER* timer = siginfo->si_value.sival_ptr;
- WINPR_UNUSED(arg);
- if (!timer || (signum != SIGALRM))
- return;
- WaitableTimerHandler(timer);
- }
- static int InstallWaitableTimerSignalHandler(void)
- {
- 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 = WaitableTimerSignalHandler;
- sigaction(SIGALRM, &action, NULL);
- g_WaitableTimerSignalHandlerInstalled = TRUE;
- }
- return 0;
- }
- #endif
- #if defined(__APPLE__)
- static void WaitableTimerHandler(void* arg)
- {
- UINT64 data = 1;
- WINPR_TIMER* timer = (WINPR_TIMER*)arg;
- if (!timer)
- return;
- if (timer->pfnCompletionRoutine)
- timer->pfnCompletionRoutine(timer->lpArgToCompletionRoutine, 0, 0);
- if (write(timer->pipe[1], &data, sizeof(data)) != sizeof(data))
- WLog_ERR(TAG, "failed to write to pipe");
- if (timer->lPeriod == 0)
- {
- if (timer->running)
- dispatch_suspend(timer->source);
- timer->running = FALSE;
- }
- }
- #endif
- static int InitializeWaitableTimer(WINPR_TIMER* timer)
- {
- int result = 0;
- if (!timer->lpArgToCompletionRoutine)
- {
- #ifdef HAVE_SYS_TIMERFD_H
- timer->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
- if (timer->fd <= 0)
- return -1;
- #elif defined(__APPLE__)
- #else
- WLog_ERR(TAG, "%s: os specific implementation is missing", __FUNCTION__);
- result = -1;
- #endif
- }
- else
- {
- #ifdef WITH_POSIX_TIMER
- 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)
- {
- WLog_ERR(TAG, "timer_create");
- return -1;
- }
- #elif defined(__APPLE__)
- #else
- WLog_ERR(TAG, "%s: os specific implementation is missing", __FUNCTION__);
- result = -1;
- #endif
- }
- timer->bInit = TRUE;
- return result;
- }
- static HANDLE_OPS ops = { TimerIsHandled, TimerCloseHandle,
- TimerGetFd, TimerCleanupHandle,
- NULL, NULL,
- NULL, NULL,
- NULL, NULL,
- NULL, NULL,
- NULL, NULL,
- NULL, NULL,
- NULL, NULL,
- NULL, NULL };
- /**
- * Waitable Timer
- */
- HANDLE CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes, BOOL bManualReset,
- LPCSTR lpTimerName)
- {
- HANDLE handle = NULL;
- WINPR_TIMER* timer;
- if (lpTimerAttributes)
- WLog_WARN(TAG, "%s [%s] does not support lpTimerAttributes", __FUNCTION__, lpTimerName);
- timer = (WINPR_TIMER*)calloc(1, sizeof(WINPR_TIMER));
- if (timer)
- {
- WINPR_HANDLE_SET_TYPE_AND_MODE(timer, HANDLE_TYPE_TIMER, WINPR_FD_READ);
- handle = (HANDLE)timer;
- timer->fd = -1;
- timer->lPeriod = 0;
- timer->bManualReset = bManualReset;
- timer->pfnCompletionRoutine = NULL;
- timer->lpArgToCompletionRoutine = NULL;
- timer->bInit = FALSE;
- if (lpTimerName)
- timer->name = strdup(lpTimerName);
- timer->ops = &ops;
- #if defined(__APPLE__)
- if (pipe(timer->pipe) != 0)
- goto fail;
- 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);
- timer->fd = timer->pipe[0];
- if (fcntl(timer->fd, F_SETFL, O_NONBLOCK) < 0)
- goto fail;
- #endif
- }
- return handle;
- #if defined(__APPLE__)
- fail:
- TimerCloseHandle(handle);
- return NULL;
- #endif
- }
- HANDLE CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes, BOOL bManualReset,
- LPCWSTR lpTimerName)
- {
- 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;
- }
- HANDLE CreateWaitableTimerExA(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCSTR lpTimerName,
- DWORD dwFlags, DWORD dwDesiredAccess)
- {
- BOOL bManualReset = (dwFlags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? TRUE : FALSE;
- if (dwDesiredAccess != 0)
- WLog_WARN(TAG, "%s [%s] does not support dwDesiredAccess 0x%08" PRIx32, __FUNCTION__,
- lpTimerName, dwDesiredAccess);
- return CreateWaitableTimerA(lpTimerAttributes, bManualReset, lpTimerName);
- }
- HANDLE CreateWaitableTimerExW(LPSECURITY_ATTRIBUTES lpTimerAttributes, LPCWSTR lpTimerName,
- DWORD dwFlags, DWORD dwDesiredAccess)
- {
- 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;
- }
- BOOL SetWaitableTimer(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
- PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine,
- BOOL fResume)
- {
- ULONG Type;
- WINPR_HANDLE* Object;
- WINPR_TIMER* timer;
- #if defined(WITH_POSIX_TIMER) || defined(__APPLE__)
- LONGLONG seconds = 0;
- LONGLONG nanoseconds = 0;
- #ifdef HAVE_SYS_TIMERFD_H
- int status = 0;
- #endif /* HAVE_SYS_TIMERFD_H */
- #endif /* WITH_POSIX_TIMER */
- 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;
- if (fResume)
- {
- WLog_ERR(TAG, "%s does not support fResume", __FUNCTION__);
- return FALSE;
- }
- timer = (WINPR_TIMER*)Object;
- timer->lPeriod = lPeriod; /* milliseconds */
- timer->pfnCompletionRoutine = pfnCompletionRoutine;
- timer->lpArgToCompletionRoutine = lpArgToCompletionRoutine;
- if (!timer->bInit)
- {
- if (InitializeWaitableTimer(timer) < 0)
- return FALSE;
- }
- #ifdef WITH_POSIX_TIMER
- ZeroMemory(&(timer->timeout), sizeof(struct itimerspec));
- if (lpDueTime->QuadPart < 0)
- {
- LONGLONG due = lpDueTime->QuadPart * (-1);
- /* due time is in 100 nanosecond intervals */
- seconds = (due / 10000000);
- nanoseconds = ((due % 10000000) * 100);
- }
- else if (lpDueTime->QuadPart == 0)
- {
- seconds = nanoseconds = 0;
- }
- else
- {
- WLog_ERR(TAG, "absolute time not implemented");
- return FALSE;
- }
- if (lPeriod > 0)
- {
- timer->timeout.it_interval.tv_sec = (lPeriod / 1000); /* seconds */
- timer->timeout.it_interval.tv_nsec = ((lPeriod % 1000) * 1000000); /* nanoseconds */
- }
- 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 */
- }
- if (!timer->pfnCompletionRoutine)
- {
- #ifdef HAVE_SYS_TIMERFD_H
- status = timerfd_settime(timer->fd, 0, &(timer->timeout), NULL);
- if (status)
- {
- WLog_ERR(TAG, "timerfd_settime failure: %d", status);
- return FALSE;
- }
- #endif
- }
- else
- {
- if ((timer_settime(timer->tid, 0, &(timer->timeout), NULL)) != 0)
- {
- WLog_ERR(TAG, "timer_settime");
- return FALSE;
- }
- }
- #elif defined(__APPLE__)
- 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;
- }
- {
- /* Clean out old data from FD */
- BYTE buffer[32];
- while (read(timer->fd, buffer, sizeof(buffer)) > 0)
- ;
- }
- {
- if (timer->running)
- dispatch_suspend(timer->source);
- dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, nanoseconds);
- uint64_t interval = DISPATCH_TIME_FOREVER;
- if (lPeriod > 0)
- interval = lPeriod * 1000000;
- dispatch_source_set_timer(timer->source, start, interval, 0);
- dispatch_resume(timer->source);
- timer->running = TRUE;
- }
- #endif
- return TRUE;
- }
- BOOL SetWaitableTimerEx(HANDLE hTimer, const LARGE_INTEGER* lpDueTime, LONG lPeriod,
- PTIMERAPCROUTINE pfnCompletionRoutine, LPVOID lpArgToCompletionRoutine,
- PREASON_CONTEXT WakeContext, ULONG TolerableDelay)
- {
- return SetWaitableTimer(hTimer, lpDueTime, lPeriod, pfnCompletionRoutine,
- lpArgToCompletionRoutine, FALSE);
- }
- HANDLE OpenWaitableTimerA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpTimerName)
- {
- /* TODO: Implement */
- WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
- return NULL;
- }
- HANDLE OpenWaitableTimerW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpTimerName)
- {
- /* TODO: Implement */
- WLog_ERR(TAG, "%s not implemented", __FUNCTION__);
- return NULL;
- }
- BOOL CancelWaitableTimer(HANDLE hTimer)
- {
- 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__)
- if (timer->running)
- dispatch_suspend(timer->source);
- timer->running = FALSE;
- #endif
- return TRUE;
- }
- /**
- * Timer-Queue Timer
- */
- /**
- * Design, Performance, and Optimization of Timer Strategies for Real-time ORBs:
- * http://www.cs.wustl.edu/~schmidt/Timer_Queue.html
- */
- static void timespec_add_ms(struct timespec* tspec, UINT32 ms)
- {
- UINT64 ns = tspec->tv_nsec + (ms * 1000000);
- tspec->tv_sec += (ns / 1000000000);
- tspec->tv_nsec = (ns % 1000000000);
- }
- static UINT64 timespec_to_ms(struct timespec* tspec)
- {
- UINT64 ms;
- ms = tspec->tv_sec * 1000;
- ms += tspec->tv_nsec / 1000000;
- return ms;
- }
- 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)
- {
- if (tspec1->tv_sec == tspec2->tv_sec)
- return (tspec1->tv_nsec - tspec2->tv_nsec);
- else
- return (tspec1->tv_sec - tspec2->tv_sec);
- }
- static void timespec_copy(struct timespec* dst, struct timespec* src)
- {
- dst->tv_sec = src->tv_sec;
- dst->tv_nsec = src->tv_nsec;
- }
- static void InsertTimerQueueTimer(WINPR_TIMER_QUEUE_TIMER** pHead, WINPR_TIMER_QUEUE_TIMER* timer)
- {
- WINPR_TIMER_QUEUE_TIMER* node;
- if (!(*pHead))
- {
- *pHead = timer;
- timer->next = NULL;
- return;
- }
- node = *pHead;
- while (node->next)
- {
- if (timespec_compare(&(timer->ExpirationTime), &(node->ExpirationTime)) > 0)
- {
- if (timespec_compare(&(timer->ExpirationTime), &(node->next->ExpirationTime)) < 0)
- break;
- }
- node = node->next;
- }
- if (node->next)
- {
- timer->next = node->next->next;
- node->next = timer;
- }
- else
- {
- node->next = timer;
- timer->next = NULL;
- }
- }
- static void RemoveTimerQueueTimer(WINPR_TIMER_QUEUE_TIMER** pHead, WINPR_TIMER_QUEUE_TIMER* timer)
- {
- BOOL found = FALSE;
- WINPR_TIMER_QUEUE_TIMER* node;
- WINPR_TIMER_QUEUE_TIMER* prevNode;
- if (timer == *pHead)
- {
- *pHead = timer->next;
- timer->next = NULL;
- return;
- }
- node = *pHead;
- prevNode = NULL;
- while (node)
- {
- if (node == timer)
- {
- found = TRUE;
- break;
- }
- prevNode = node;
- node = node->next;
- }
- if (found)
- {
- if (prevNode)
- {
- prevNode->next = timer->next;
- }
- timer->next = NULL;
- }
- }
- static int FireExpiredTimerQueueTimers(WINPR_TIMER_QUEUE* timerQueue)
- {
- struct timespec CurrentTime;
- WINPR_TIMER_QUEUE_TIMER* node;
- if (!timerQueue->activeHead)
- return 0;
- timespec_gettimeofday(&CurrentTime);
- node = timerQueue->activeHead;
- while (node)
- {
- if (timespec_compare(&CurrentTime, &(node->ExpirationTime)) >= 0)
- {
- node->Callback(node->Parameter, TRUE);
- node->FireCount++;
- timerQueue->activeHead = node->next;
- node->next = NULL;
- if (node->Period)
- {
- timespec_add_ms(&(node->ExpirationTime), node->Period);
- InsertTimerQueueTimer(&(timerQueue->activeHead), node);
- }
- else
- {
- InsertTimerQueueTimer(&(timerQueue->inactiveHead), node);
- }
- node = timerQueue->activeHead;
- }
- else
- {
- break;
- }
- }
- return 0;
- }
- static void* TimerQueueThread(void* arg)
- {
- int status;
- struct timespec timeout;
- WINPR_TIMER_QUEUE* timerQueue = (WINPR_TIMER_QUEUE*)arg;
- while (1)
- {
- pthread_mutex_lock(&(timerQueue->cond_mutex));
- timespec_gettimeofday(&timeout);
- if (!timerQueue->activeHead)
- {
- timespec_add_ms(&timeout, 50);
- }
- else
- {
- if (timespec_compare(&timeout, &(timerQueue->activeHead->ExpirationTime)) < 0)
- {
- timespec_copy(&timeout, &(timerQueue->activeHead->ExpirationTime));
- }
- }
- status = pthread_cond_timedwait(&(timerQueue->cond), &(timerQueue->cond_mutex), &timeout);
- FireExpiredTimerQueueTimers(timerQueue);
- pthread_mutex_unlock(&(timerQueue->cond_mutex));
- if ((status != ETIMEDOUT) && (status != 0))
- break;
- if (timerQueue->bCancelled)
- break;
- }
- return NULL;
- }
- static int StartTimerQueueThread(WINPR_TIMER_QUEUE* timerQueue)
- {
- pthread_cond_init(&(timerQueue->cond), NULL);
- pthread_mutex_init(&(timerQueue->cond_mutex), NULL);
- 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;
- }
- HANDLE CreateTimerQueue(void)
- {
- HANDLE handle = NULL;
- WINPR_TIMER_QUEUE* timerQueue;
- timerQueue = (WINPR_TIMER_QUEUE*)calloc(1, sizeof(WINPR_TIMER_QUEUE));
- if (timerQueue)
- {
- WINPR_HANDLE_SET_TYPE_AND_MODE(timerQueue, HANDLE_TYPE_TIMER_QUEUE, WINPR_FD_READ);
- handle = (HANDLE)timerQueue;
- timerQueue->activeHead = NULL;
- timerQueue->inactiveHead = NULL;
- timerQueue->bCancelled = FALSE;
- StartTimerQueueThread(timerQueue);
- }
- return handle;
- }
- BOOL DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
- {
- void* rvalue;
- WINPR_TIMER_QUEUE* timerQueue;
- WINPR_TIMER_QUEUE_TIMER* node;
- WINPR_TIMER_QUEUE_TIMER* nextNode;
- if (!TimerQueue)
- return FALSE;
- timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
- /* 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);
- /**
- * 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)
- */
- {
- /* 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;
- while (node)
- {
- nextNode = node->next;
- free(node);
- node = nextNode;
- }
- timerQueue->inactiveHead = NULL;
- }
- /* Delete timer queue */
- pthread_cond_destroy(&(timerQueue->cond));
- pthread_mutex_destroy(&(timerQueue->cond_mutex));
- pthread_mutex_destroy(&(timerQueue->mutex));
- pthread_attr_destroy(&(timerQueue->attr));
- free(timerQueue);
- if (CompletionEvent && (CompletionEvent != INVALID_HANDLE_VALUE))
- SetEvent(CompletionEvent);
- return TRUE;
- }
- BOOL DeleteTimerQueue(HANDLE TimerQueue)
- {
- return DeleteTimerQueueEx(TimerQueue, NULL);
- }
- BOOL CreateTimerQueueTimer(PHANDLE phNewTimer, HANDLE TimerQueue, WAITORTIMERCALLBACK Callback,
- PVOID Parameter, DWORD DueTime, DWORD Period, ULONG Flags)
- {
- struct timespec CurrentTime;
- WINPR_TIMER_QUEUE* timerQueue;
- WINPR_TIMER_QUEUE_TIMER* timer;
- if (!TimerQueue)
- return FALSE;
- timespec_gettimeofday(&CurrentTime);
- timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
- timer = (WINPR_TIMER_QUEUE_TIMER*)malloc(sizeof(WINPR_TIMER_QUEUE_TIMER));
- if (!timer)
- return FALSE;
- WINPR_HANDLE_SET_TYPE_AND_MODE(timer, HANDLE_TYPE_TIMER_QUEUE_TIMER, WINPR_FD_READ);
- *((UINT_PTR*)phNewTimer) = (UINT_PTR)(HANDLE)timer;
- timespec_copy(&(timer->StartTime), &CurrentTime);
- timespec_add_ms(&(timer->StartTime), DueTime);
- timespec_copy(&(timer->ExpirationTime), &(timer->StartTime));
- timer->Flags = Flags;
- timer->DueTime = DueTime;
- timer->Period = Period;
- timer->Callback = Callback;
- timer->Parameter = Parameter;
- timer->timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
- timer->FireCount = 0;
- timer->next = NULL;
- pthread_mutex_lock(&(timerQueue->cond_mutex));
- InsertTimerQueueTimer(&(timerQueue->activeHead), timer);
- pthread_cond_signal(&(timerQueue->cond));
- pthread_mutex_unlock(&(timerQueue->cond_mutex));
- return TRUE;
- }
- BOOL ChangeTimerQueueTimer(HANDLE TimerQueue, HANDLE Timer, ULONG DueTime, ULONG Period)
- {
- struct timespec CurrentTime;
- WINPR_TIMER_QUEUE* timerQueue;
- WINPR_TIMER_QUEUE_TIMER* timer;
- if (!TimerQueue || !Timer)
- return FALSE;
- timespec_gettimeofday(&CurrentTime);
- timerQueue = (WINPR_TIMER_QUEUE*)TimerQueue;
- timer = (WINPR_TIMER_QUEUE_TIMER*)Timer;
- pthread_mutex_lock(&(timerQueue->cond_mutex));
- RemoveTimerQueueTimer(&(timerQueue->activeHead), timer);
- RemoveTimerQueueTimer(&(timerQueue->inactiveHead), timer);
- timer->DueTime = DueTime;
- timer->Period = Period;
- timer->next = NULL;
- timespec_copy(&(timer->StartTime), &CurrentTime);
- timespec_add_ms(&(timer->StartTime), DueTime);
- timespec_copy(&(timer->ExpirationTime), &(timer->StartTime));
- InsertTimerQueueTimer(&(timerQueue->activeHead), timer);
- pthread_cond_signal(&(timerQueue->cond));
- pthread_mutex_unlock(&(timerQueue->cond_mutex));
- 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;
- pthread_mutex_lock(&(timerQueue->cond_mutex));
- /**
- * 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);
- pthread_cond_signal(&(timerQueue->cond));
- pthread_mutex_unlock(&(timerQueue->cond_mutex));
- free(timer);
- if (CompletionEvent && (CompletionEvent != INVALID_HANDLE_VALUE))
- SetEvent(CompletionEvent);
- return TRUE;
- }
- #endif
|