]> Dogcows Code - chaz/yoink/blob - src/moof/runloop.hh
initial runloop implementation
[chaz/yoink] / src / moof / runloop.hh
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 #ifndef _MOOF_RUNLOOP_HH_
13 #define _MOOF_RUNLOOP_HH_
14
15 /**
16 * \file runloop.hh
17 * Thread timer management class.
18 */
19
20 #include <set>
21
22 #include <boost/noncopyable.hpp>
23
24 #include <moof/backend.hh>
25 #include <moof/thread.hh>
26
27
28 namespace moof {
29
30
31 // forward declarations
32 class timer;
33
34
35 /**
36 * A runloop is a loop with scheduled timers.
37 */
38 class runloop
39 {
40 public:
41
42 /**
43 * Construct a runloop.
44 */
45 runloop() :
46 stop_(false),
47 thread_id_(0) {}
48
49 /**
50 * Deconstruct the runloop.
51 */
52 ~runloop();
53
54
55 /**
56 * Start running the runloop.
57 * \return The exit code.
58 */
59 int run();
60
61 /**
62 * Stop the runloop.
63 * \param code The exit code.
64 */
65 void stop(int code = 0);
66
67
68 /** Get the runloop of the current thread.
69 * \return The current runloop or 0 if none is running in the current
70 * thread.
71 */
72 static runloop* current();
73
74
75 private:
76
77 friend class timer;
78
79 void add_timer(timer* timer);
80 void remove_timer(timer* timer);
81
82
83 bool stop_;
84 int code_;
85
86 typedef std::set<timer*> timer_table;
87 timer_table timers_;
88 timer_table::iterator timers_it_;
89
90 #if ENABLE_THREADS
91 MOOF_DECLARE_MUTEX(timers_mutex_);
92 uint32_t thread_id_;
93 #endif
94
95
96 backend backend_;
97 };
98
99
100 } // namespace moof
101
102 #endif // _MOOF_RUNLOOP_HH_
103
This page took 0.032906 seconds and 4 git commands to generate.