]> Dogcows Code - chaz/yoink/blob - src/moof/runloop.cc
remove some unused stlplus modules
[chaz/yoink] / src / moof / runloop.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #if HAVE_CONFIG_H
11 #include "config.h"
12 #endif
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
31 int runloop::run_once()
32 {
33 #if ENABLE_THREADS
34 thread_id_ = thread::current_identifier();
35 #endif
36
37 //log_debug("------------------------------------");
38 int expired = 0;
39 MOOF_MUTEX_LOCK(timers_mutex_);
40
41 std::sort(timers_.begin(), timers_.end(), comp);
42 for (timers_it_ = timers_.begin();
43 timers_it_ != timers_.end(); ++timers_it_)
44 {
45 if ((*timers_it_)->fire_if_expired()) ++expired;
46 }
47 return expired;
48 }
49
50 int runloop::run()
51 {
52 stop_ = false;
53 while (!stop_)
54 {
55 if (run_once() == 0);//timer::sleep(SCALAR(0.0));
56 // TODO: maybe sleep(0.0001) will actually return sooner than
57 // sleep(0)... if the kernel interprets sleep(0) as we don't need
58 // to process for an arbitrarily long timespan while specifying a
59 // value lets the kernel know when we need control back...
60 }
61 return code_;
62 }
63
64 void runloop::stop(int code)
65 {
66 code_ = code;
67 stop_ = true;
68 }
69
70 void runloop::add_timer(timer& timer)
71 {
72 #if ENABLE_THREADS
73 if (thread_id_ != thread::current_identifier())
74 {
75 MOOF_MUTEX_LOCK(timers_mutex_);
76 timers_.push_back(&timer);
77 timers_it_ = timers_.end();
78 }
79 else
80 #endif
81 {
82 timers_.push_back(&timer);
83 timers_it_ = timers_.end();
84 }
85 }
86
87 void runloop::remove_timer(timer& timer)
88 {
89 #if ENABLE_THREADS
90 if (thread_id_ != thread::current_identifier())
91 {
92 MOOF_MUTEX_LOCK(timers_mutex_);
93 timers_.erase(std::find(timers_.begin(), timers_.end(), &timer));
94 timers_it_ = timers_.end();
95 }
96 else
97 #endif
98 {
99 timers_.erase(std::find(timers_.begin(), timers_.end(), &timer));
100 timers_it_ = timers_.end();
101 }
102 }
103
104
105 } // namespace moof
106
This page took 0.037308 seconds and 4 git commands to generate.