From fdbddaa4873fda6e83b8894dd58f166ebde795c3 Mon Sep 17 00:00:00 2001 From: Nicolas Noble Date: Fri, 2 Aug 2013 10:36:31 -0700 Subject: Refining a bit more the Future class. --- includes/Task.h | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'includes/Task.h') diff --git a/includes/Task.h b/includes/Task.h index dcbdf91..4334ad2 100644 --- a/includes/Task.h +++ b/includes/Task.h @@ -262,22 +262,48 @@ class QueueBase { }; template -struct Future { +class Future { + public: typedef std::function func_t; + Future(func_t func) : m_func(func) { } R get(); - func_t m_run; + void run(); + private: + func_t m_func; + Events::BaseEvent * m_evt = NULL; }; template R Future::get() { R r; for (;;) { + if (m_evt && !m_evt->gotSignal()) + continue; + m_evt = NULL; try { - r = m_run(); + r = m_func(); return r; } catch (EAgain & e) { - Task::operationYield(e.getEvent(), Task::INTERRUPTIBLE); + m_evt = e.getEvent(); + Task::operationYield(m_evt, Task::INTERRUPTIBLE); + } + } +} + +template +void Future::run() { + for (;;) { + if (m_evt && !m_evt->gotSignal()) + continue; + m_evt = NULL; + try { + m_func(); + return; + } + catch (EAgain & e) { + m_evt = e.getEvent(); + Task::operationYield(m_evt, Task::INTERRUPTIBLE); } } } -- cgit v1.2.3