85 lines
2.2 KiB
C++
85 lines
2.2 KiB
C++
// Copyright (C) 2003 Intel Corporation
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Timer.h
|
|
//
|
|
// This file contains the definition of the Timer class
|
|
// The timer class wraps the TimerManager into an easy-to-use
|
|
// object-oriented interface.
|
|
//
|
|
// Usage:
|
|
//
|
|
// Option 1: Construct an instance of the "Timer" class with an external
|
|
// callback function. When calling the "start" method, the callback
|
|
// function will be registered with the timer manager and start working.
|
|
//
|
|
// Option 2: Subclass the "Timer" class and reimplement the virtual "run"
|
|
// method. When calling the "start" method, the "run" method will be called
|
|
// on timer expiration
|
|
//
|
|
// Implementation overview:
|
|
// Calling the "start" method will register a new timer, which will call the
|
|
// "run" method. The default implementation of the "run" method will call
|
|
// the Callback function given in the constructor.
|
|
//////////////////////////////////////////////////////////////////////////
|
|
#ifndef _LAD_TIMER_H
|
|
#define _LAD_TIMER_H
|
|
|
|
#ifndef NULL
|
|
#define NULL 0
|
|
#endif
|
|
|
|
#ifndef __IMR_WIN__
|
|
#include <sys/timeb.h>
|
|
#include "Lock.h"
|
|
#else
|
|
// Redefining FD_SETSIZE must be done before including the windows tcp/ip header files.
|
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx
|
|
#define FD_SETSIZE 1024
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
#include <Windows.h>
|
|
#endif
|
|
|
|
#include "TimerManager.h"
|
|
typedef void (*CallbackFunction) (void*);
|
|
|
|
class Timer {
|
|
public:
|
|
Timer (CallbackFunction func = NULL, void* param = NULL);
|
|
virtual ~Timer();
|
|
|
|
void start(long msecs, bool recurring = true);
|
|
void stop();
|
|
|
|
#ifdef __IMR_WIN__
|
|
static unsigned long tickCount() { return GetTickCount(); }
|
|
#else
|
|
static unsigned long tickCount();
|
|
#endif
|
|
protected:
|
|
virtual void run();
|
|
|
|
#ifndef __IMR_WIN__
|
|
static Semaphore _sem;
|
|
static struct timeb _base;
|
|
#endif
|
|
|
|
private:
|
|
|
|
struct _CallbackParam: public CallbackParam{
|
|
Timer *self;
|
|
_CallbackParam(Timer* t){self = t;}
|
|
_CallbackParam():self(0){}
|
|
virtual ~_CallbackParam(){}
|
|
};
|
|
|
|
static void _internalCallback(SPtr<CallbackParam> p);
|
|
CallbackFunction _func;
|
|
void * _param;
|
|
int _timerId;
|
|
};
|
|
|
|
#endif //_LAD_TIMER_H
|
|
|