Cucumber/Kernel/include/Tier1/CTimer.h

53 lines
1.8 KiB
C
Raw Normal View History

2011-04-04 16:25:20 +00:00
#ifndef __CTIMER_H__
#define __CTIMER_H__
2011-05-03 11:59:20 +00:00
#include "Tier1/CInterruptDispatcher.h"
#include "Tier1/Util/CLinearList.h"
2011-06-27 15:59:54 +00:00
#include "Tier1/CTask.h"
#include "Tier1/CSemaphore.h"
2011-05-03 11:59:20 +00:00
#include "types.h"
2011-04-04 16:25:20 +00:00
namespace cb {
2011-05-03 11:59:20 +00:00
typedef bool(*TTimerCallback)(u32);
2011-06-27 15:59:54 +00:00
typedef void(*TTimerFastHook)(u32, u32, u32, u32, u32, u32, u32, u32, u32);
2011-05-03 11:59:20 +00:00
typedef struct {
TTimerCallback Callback;
u32 Interval;
s32 Times;
u32 NextCall;
u32 Extra;
2011-06-27 15:59:54 +00:00
CTask *Task;
2011-05-03 11:59:20 +00:00
} TCallbackInfo;
2011-04-04 16:25:20 +00:00
class CTimer {
2011-05-03 11:59:20 +00:00
public:
static void Create(u32 Interval, s32 Times, TTimerCallback Callback,
u32 Extra = 0);
2011-06-27 15:59:54 +00:00
// This is like ::Create, but faster, and there can only be one, and it's fixed interval
static void SetFastTimerHook(TTimerFastHook Hook);
2011-05-03 11:59:20 +00:00
inline static u32 GetTicks(void)
{
2011-05-08 15:16:00 +00:00
if (!m_bInitialized)
2011-05-03 11:59:20 +00:00
Initialize();
2011-06-27 15:59:54 +00:00
m_GetTicksSemaphore.Acquire();
volatile u32 Ticks = m_nTicks;
m_GetTicksSemaphore.Release();
return Ticks;
2011-05-03 11:59:20 +00:00
}
private:
static CLinearList<TCallbackInfo> m_Callbacks;
2011-05-08 15:16:00 +00:00
volatile static u32 m_nTicks;
2011-05-03 11:59:20 +00:00
static void Initialize(void);
2011-06-27 15:59:54 +00:00
static void Dispatch(u32 edi, u32 esi, u32 ebp, u32 esp, u32 ebx, u32 edx, u32 ecx, u32 eax, u32 eip);
2011-05-08 15:16:00 +00:00
static bool m_bInitialized;
2011-06-27 15:59:54 +00:00
static TTimerFastHook m_FastHook;
2011-05-03 11:59:20 +00:00
2011-06-27 15:59:54 +00:00
static CSemaphore m_GetTicksSemaphore;
2011-05-03 11:59:20 +00:00
// Called when the number of ticks is just about to overflow.
// Reschedules all the callbacks.
static void Reschedule(void);
2011-04-04 16:25:20 +00:00
};
};
#endif