-/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+/*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
**] All rights reserved.
*
-* vi:ts=4 sw=4 tw=75
-*
* Distributable under the terms and conditions of the 2-clause BSD license;
* see the file COPYING for a complete text of the license.
*
-**************************************************************************/
+*****************************************************************************/
#ifndef _MOOF_RUNLOOP_HH_
#define _MOOF_RUNLOOP_HH_
* Thread timer management class.
*/
-#include <set>
+#include <vector>
#include <boost/noncopyable.hpp>
-#include <moof/backend.hh>
#include <moof/thread.hh>
// forward declarations
class timer;
-
/**
* A runloop is a loop with scheduled timers.
*/
-class runloop
+class runloop : public boost::noncopyable
{
public:
* Construct a runloop.
*/
runloop() :
- stop_(false),
- thread_id_(0) {}
+ stop_(false)
+ {
+#if ENABLE_THREADS
+ thread_id_ = 0;
+#endif
+ }
/**
- * Deconstruct the runloop.
+ * Do one iteration of the runloop.
+ * \return The number of tasks which were given time.
*/
- ~runloop();
-
+ int run_once();
/**
* Start running the runloop.
*/
void stop(int code = 0);
+ bool is_running() const
+ {
+ return !stop_;
+ }
- /** Get the runloop of the current thread.
- * \return The current runloop or 0 if none is running in the current
- * thread.
- */
- static runloop* current();
-
+ void add_timer(timer& timer);
+ void remove_timer(timer& timer);
private:
- friend class timer;
-
- void add_timer(timer* timer);
- void remove_timer(timer* timer);
-
-
bool stop_;
- int code_;
+ int code_;
- typedef std::set<timer*> timer_table;
+ typedef std::vector<timer*> timer_table;
timer_table timers_;
timer_table::iterator timers_it_;
MOOF_DECLARE_MUTEX(timers_mutex_);
uint32_t thread_id_;
#endif
-
-
- backend backend_;
};