]> Dogcows Code - chaz/yoink/blob - src/moof/runloop.hh
further implementing runloop support
[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 <vector>
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 : public boost::noncopyable
39 {
40 public:
41
42 /**
43 * Construct a runloop.
44 */
45 runloop() :
46 stop_(false)
47 {
48 #if ENABLE_THREADS
49 thread_id_ = 0;
50 #endif
51 }
52
53 /**
54 * Deconstruct the runloop.
55 */
56 ~runloop();
57
58
59 /**
60 * Start running the runloop.
61 * \return The exit code.
62 */
63 int run();
64
65 /**
66 * Stop the runloop.
67 * \param code The exit code.
68 */
69 void stop(int code = 0);
70
71
72 /** Get the runloop of the current thread.
73 * \return The current runloop or 0 if none is running in the current
74 * thread.
75 */
76 static runloop* current();
77
78
79 void add_timer(timer& timer);
80 void remove_timer(timer& timer);
81
82
83 private:
84
85 bool stop_;
86 int code_;
87
88 typedef std::vector<timer*> timer_table;
89 timer_table timers_;
90 timer_table::iterator timers_it_;
91
92 #if ENABLE_THREADS
93 MOOF_DECLARE_MUTEX(timers_mutex_);
94 uint32_t thread_id_;
95 #endif
96
97
98 backend backend_;
99 };
100
101
102 } // namespace moof
103
104 #endif // _MOOF_RUNLOOP_HH_
105
This page took 0.036125 seconds and 4 git commands to generate.