⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.c

📁 内容正如其名
💻 C
字号:
/******************************************************************************  Copyright(C) 2005,2006 Frank ZHANG  All Rights Reserved.      This program is free software; you can redistribute it and/or modify it  under the terms of the GNU General Public License as published by the Free  Software Foundation; either version 2 of the License, or (at your option)  any later version.  This program is distributed in the hope that it will be useful, but WITHOUT  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for   more details.      You should have received a copy of the GNU General Public License along with  this program; if not, write to the Free Software Foundation, Inc., 59 Temple  Place, Suite 330, Boston, MA 02111-1307 USA.  ******************************************************************************   If you want to distribute this software with modifications under any terms  other than the GPL or distribute the software linked with proprietary  applications that are not distributed in full compliance with the GNU Public  License, a Commercial License is needed.    Commercial licensing and support of this software are available at a fee. For  more information, See http://www.openmgcp.org  ******************************************************************************/#define _WIN32_WINNT 0x0500#include <wtypes.h>#include <winbase.h>#include <windows.h>#include <assert.h>#include <stdio.h>#include "debg.h"#include "list.h"#include "timer.h"/* Timer struct */typedef struct Timer_tag{  HANDLE hTimer;  struct sigevent evp;  struct itimerspec TimerSpec;} Timer;VOID CALLBACK TimerRoutine(PVOID lpParam, BOOL TimerOrWaitFired){  struct sigevent *pevp =  (struct sigevent*)lpParam;  //printf("TimerRoutine lpParam is NULL\n");  pevp->sigev_notify_function(pevp->sigev_value);}/* The timer_create() function shall create a per-process timer using the specifiedclock, clock_id, as the timing base. The timer_create() function shall return,in the location referenced by timerid, a timer ID of type timer_t used to identify the timer in timer requests. This timer ID shall be unique within thecalling process until the timer is deleted. The particular clock, clock_id, is defined in <time.h>. The timer whose ID is returned shall be in a disarmed state upon return from timer_create(). */int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid){  Timer *pNewTimer = (Timer*)calloc(1, sizeof(Timer));  if (pNewTimer == NULL)  {    printf("\nFail to create new timer!");    return -1;  }  memcpy(&pNewTimer->evp, evp, sizeof(struct sigevent));  *timerid = (timer_t)pNewTimer;  return 0;}int timer_delete(timer_t timerid){  Timer *pTimer = (Timer*)timerid;  if (pTimer->hTimer == NULL)    return -1;  if (DeleteTimerQueueTimer(NULL, pTimer->hTimer, NULL))//INVALID_HANDLE_VALUE))  {    free(pTimer);    return 0;  }    free(pTimer);  return -1;} /* The timer_gettime() function shall store the amount of time until thespecified timer, timerid, expires and the reload value of the timer into thespace pointed to by the value argument. The it_value member of this structureshall contain the amount of time before the timer expires, or zero if the timeris disarmed. This value is returned as the interval until timer expiration,even if the timer was armed with absolute time. The it_interval member of valueshall contain the reload value last set by timer_settime(). */int timer_gettime(timer_t timerid, struct itimerspec *value){  Timer *pTimer = (Timer*)timerid;  if (value == NULL)    return -1;  /* Save the old value */  memcpy(value, &pTimer->TimerSpec, sizeof(struct itimerspec));  return 0;} /* The timer_settime() function shall set the time until the next expiration ofthe timer specified by timerid from the it_value member of the value argumentand arm the timer if the it_value member of value is non-zero. If the specifiedtimer was already armed when timer_settime() is called, this call shall resetthe time until next expiration to the value specified. If the it_value memberof value is zero, the timer shall be disarmed. The effect of disarming orresetting a timer with pending expiration notifications is unspecified. */int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,                  struct itimerspec *ovalue){  Timer *pTimer = (Timer*)timerid;  DWORD Period = value->it_interval.tv_sec*1000 + value->it_interval.tv_nsec/1000000;    assert(pTimer);  assert(value);  // Set a timer to call the timer routine.  if (!CreateTimerQueueTimer(&pTimer->hTimer, NULL,                             (WAITORTIMERCALLBACK)TimerRoutine,                             &pTimer->evp, 0, Period, 0))  {      printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());      return -1;  }  assert(pTimer->hTimer);    if (ovalue != NULL)  {    /* Save the old value */    memcpy(ovalue, &pTimer->TimerSpec, sizeof(struct itimerspec));  }  /* Updata the value */  memcpy(&pTimer->TimerSpec, value, sizeof(struct itimerspec));  return 0;} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -