FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
timer.h
1 #ifndef __MY_TIMER_H__
2 #define __MY_TIMER_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of SAFSlib.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include <sys/time.h>
24 
25 #include <map>
26 
27 #include "concurrency.h"
28 
29 class thread;
30 
31 namespace safs
32 {
33 
34 static inline int64_t get_curr_time_us()
35 {
36  struct timeval curr;
37  gettimeofday(&curr, NULL);
38  int64_t curr_us = curr.tv_sec;
39  curr_us = curr_us * 1000000 + curr.tv_usec;
40  return curr_us;
41 }
42 
43 class timer_task
44 {
45  static atomic_number<long> task_count;
46  long task_id;
47  int64_t abs_timeout;
48  long timeout;
49 public:
50  // timeout: in microsecond
51  timer_task(long timeout) {
52  task_id = task_count.inc(1);
53  int64_t curr = get_curr_time_us();
54  abs_timeout = curr + timeout;
55  this->timeout = timeout;
56  }
57 
58  virtual ~timer_task() {
59  }
60 
61  long get_id() const {
62  return task_id;
63  }
64 
65  int64_t get_timeout() const {
66  return timeout;
67  }
68 
69  int64_t get_abs_timeout() const {
70  return abs_timeout;
71  }
72 
73  void renew() {
74  int64_t curr = get_curr_time_us();
75  abs_timeout = curr + timeout;
76  }
77 
78  virtual void run() = 0;
79 };
80 
81 class timer
82 {
83  static atomic_integer timer_count;
84 
85  // I treat this as a sorted list.
86  // key is the absolute timeout.
87  std::multimap<int64_t, timer_task *> task_list;
88  spin_lock lock;
89  timer_t timerid;
90  thread *t;
91  int timer_id;
92 
93  void set_timeout(int64_t timeout);
94  void init_timer();
95  int64_t _add_task(timer_task *task);
96 public:
97  timer();
98 
99  timer(thread *t);
100 
101  bool add_task(timer_task *task);
102 
103  void run_tasks();
104 
105  void print_state();
106 
107  int64_t get_next_timeout();
108 
109  int get_id() const {
110  return timer_id;
111  }
112 };
113 
114 class periodic_timer
115 {
116  static atomic_integer timer_count;
117  int timer_id;
118  timer_t timerid;
119  class timer_task *task;
120  thread *t;
121 
122  void set_timeout(int64_t timeout);
123 public:
124  periodic_timer(thread *t, timer_task *task);
125  void run_task();
126  int get_id() const {
127  return timer_id;
128  }
129 
130  bool is_enabled() const;
131  bool start();
132 };
133 
134 }
135 
136 #endif
137