Pol  Revision:cb584c9
schedule.h
Go to the documentation of this file.
1 
8 #ifndef __SCHEDULE_H
9 #define __SCHEDULE_H
10 
11 #include <ctime>
12 #include <functional>
13 
14 #include "../clib/compilerspecifics.h"
15 #include "polclock.h"
16 
17 namespace Pol
18 {
19 namespace Core
20 {
21 class ScheduledTask;
22 class SchComparer : public std::less<ScheduledTask*>
23 {
24 public:
25  bool operator()( const ScheduledTask* x, const ScheduledTask* y ) const;
26 };
27 
29 {
30 public:
31  static bool is_dirty() { return dirty_; }
32  static void cleanse() { dirty_ = false; }
33  static void mark_dirty() { dirty_ = true; }
34 
35 private:
36  static bool dirty_;
37 };
38 
40 {
41 public:
42  explicit ScheduledTask( polclock_t next_run_clock );
43  virtual ~ScheduledTask();
44 
45  bool ready( polclock_t now );
46  bool late( polclock_t now );
47  polticks_t ticks_late( polclock_t now );
48 
49  polticks_t ticks_left( polclock_t now ) const;
50  polclock_t clocksleft( polclock_t now );
51  polclock_t next_run_clock() const;
52  virtual void execute( polclock_t now ) = 0;
53 
54  virtual void cancel( void );
55 
56 protected:
57  bool cancelled;
60  friend class SchComparer;
61  friend void check_scheduled_tasks( polclock_t* clocksleft, bool* pactivity );
62  friend void check_scheduled_tasks2( void );
64 };
65 
67 {
68  return next_run_clock_;
69 }
70 
71 void check_scheduled_tasks( polclock_t* clocksleft, bool* pactivity );
72 void check_scheduled_tasks2( void );
74 
76 {
77 public:
78  PeriodicTask( void ( *f )( void ), int n_secs, const char* name );
79  PeriodicTask( void ( *f )( void ), int initial_wait_seconds, int periodic_seconds,
80  const char* name );
81  virtual ~PeriodicTask(){};
82 
83  void set_secs( int n_secs );
84 
85  virtual void execute( polclock_t now ) POL_OVERRIDE;
86  void start();
87 
88 private:
91  void ( *f )( void );
92  const char* name_;
93 };
94 
95 class OneShotTask : public ScheduledTask
96 {
97 public:
98  OneShotTask( OneShotTask** handle, polclock_t run_when );
99  virtual void cancel( void ) POL_OVERRIDE;
100 
101 protected:
102  // oneshots can't be deleted, only cancelled.
103  virtual ~OneShotTask();
104  virtual void execute( polclock_t now ) POL_OVERRIDE;
105 
106  virtual void on_run() = 0;
107 
108 private:
110 };
111 
112 
113 template <class T>
115 {
116 public:
117  OneShotTaskInst( OneShotTask** handle, polclock_t run_when, void ( *f )( T data ), T data )
118  : OneShotTask( handle, run_when ), data_( data ), f_( f )
119  {
120  }
121  virtual ~OneShotTaskInst(){};
122 
123  virtual void on_run() POL_OVERRIDE;
124 
125 private:
126  T data_;
127  void ( *f_ )( T data );
128 };
129 
130 template <class T>
132 {
133  ( *f_ )( data_ );
134 }
135 }
136 }
137 #endif
polclock_t last_run_clock_
Definition: schedule.h:59
#define POL_OVERRIDE
void check_scheduled_tasks2(void)
virtual ~PeriodicTask()
Definition: schedule.h:81
polclock_t next_run_clock_
Definition: schedule.h:58
polclock_t calc_scheduler_clocksleft(polclock_t now)
Definition: schedule.cpp:245
virtual void on_run() POL_OVERRIDE
Definition: schedule.h:131
bool operator()(const ScheduledTask *x, const ScheduledTask *y) const
Definition: schedule.cpp:25
OneShotTaskInst(OneShotTask **handle, polclock_t run_when, void(*f)(T data), T data)
Definition: schedule.h:117
const char * name_
Definition: schedule.h:92
OneShotTask ** handle
Definition: schedule.h:109
polclock_t next_run_clock() const
Definition: schedule.h:66
static bool is_dirty()
Definition: schedule.h:31
polclock_t n_clocks
Definition: schedule.h:90
polclock_t n_initial_clocks
Definition: schedule.h:89
int polclock_t
Definition: polclock.h:26
int polticks_t
Definition: polclock.h:27
std::unordered_map< u64, ScriptDiffData > data
Definition: osmod.cpp:966
static bool dirty_
Definition: schedule.h:36
void check_scheduled_tasks(polclock_t *clocksleft, bool *pactivity)
Definition: schedule.cpp:186
std::string name
Definition: osmod.cpp:943
static void cleanse()
Definition: schedule.h:32
Definition: berror.cpp:12
static void mark_dirty()
Definition: schedule.h:33