]> Dogcows Code - chaz/yoink/blob - src/moof/runloop.cc
build system enhancements
[chaz/yoink] / src / moof / runloop.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include "config.h"
13
14 #include <algorithm>
15
16 #include "hash.hh"
17 #include "log.hh"
18 #include "runloop.hh"
19 #include "timer.hh"
20
21
22 namespace moof {
23
24
25 bool comp(timer* a, timer* b)
26 {
27 return a->expiration() < b->expiration();
28 }
29
30 void runloop::run_once()
31 {
32 #if ENABLE_THREADS
33 thread_id_ = thread::current_identifier();
34 #endif
35
36 //log_debug("------------------------------------");
37 //scalar next_event = SCALAR(0.0);
38 {
39 MOOF_MUTEX_LOCK(timers_mutex_);
40
41 for (timers_it_ = timers_.begin();
42 timers_it_ != timers_.end();
43 ++timers_it_)
44 {
45 (*timers_it_)->fire_if_expired();
46 }
47
48 std::sort(timers_.begin(), timers_.end(), comp);
49 //next_event = timers_[0]->expiration();
50 }
51 }
52
53 int runloop::run()
54 {
55 stop_ = false;
56 while (!stop_)
57 {
58 run_once();
59 //timer::sleep(next_event, timer::absolute);
60 timer::sleep(SCALAR(0.0));
61 }
62
63 return code_;
64 }
65
66
67 void runloop::stop(int code)
68 {
69 code_ = code;
70 stop_ = true;
71 }
72
73
74 void runloop::add_timer(timer& timer)
75 {
76 #if ENABLE_THREADS
77 if (thread_id_ != thread::current_identifier())
78 {
79 MOOF_MUTEX_LOCK(timers_mutex_);
80 timers_.push_back(&timer);
81 timers_it_ = timers_.end();
82 }
83 else
84 #endif
85 {
86 timers_.push_back(&timer);
87 timers_it_ = timers_.end();
88 }
89 }
90
91 void runloop::remove_timer(timer& timer)
92 {
93 #if ENABLE_THREADS
94 if (thread_id_ != thread::current_identifier())
95 {
96 MOOF_MUTEX_LOCK(timers_mutex_);
97 timers_.erase(std::find(timers_.begin(), timers_.end(), &timer));
98 timers_it_ = timers_.end();
99 }
100 else
101 #endif
102 {
103 timers_.erase(std::find(timers_.begin(), timers_.end(), &timer));
104 timers_it_ = timers_.end();
105 }
106 }
107
108
109 } // namespace moof
110
This page took 0.035075 seconds and 4 git commands to generate.