summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Noble <pixel@nobis-crew.org>2013-08-02 10:36:31 -0700
committerNicolas Noble <pixel@nobis-crew.org>2013-08-02 10:36:31 -0700
commitfdbddaa4873fda6e83b8894dd58f166ebde795c3 (patch)
treea5a203fd98e5b533d75dfbcc1db74f96aaac1b9b
parent07f5a2245d7c15dfb6d716e307d2618a498886d9 (diff)
Refining a bit more the Future class.
-rw-r--r--includes/Task.h34
-rw-r--r--src/Handle.cc13
2 files changed, 35 insertions, 12 deletions
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<class R>
-struct Future {
+class Future {
+ public:
typedef std::function<R()> 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<class R>
R Future<R>::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<class R>
+void Future<R>::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);
}
}
}
diff --git a/src/Handle.cc b/src/Handle.cc
index 361561a..cc39f41 100644
--- a/src/Handle.cc
+++ b/src/Handle.cc
@@ -96,14 +96,11 @@ ssize_t Balau::Handle::forceWrite(const void * _buf, size_t count, Events::BaseE
Balau::Future<uint8_t> Balau::Handle::readU8() {
IO<Handle> t(this);
- auto func = [t]() mutable -> uint8_t {
- uint8_t r;
- t->read(&r, 1);
- return r;
- };
- Future<uint8_t> r;
- r.m_run = func;
- return r;
+ return Future<uint8_t>([t]() mutable {
+ uint8_t b;
+ t->read(&b, 1);
+ return b;
+ });
}
void Balau::Handle::rseek(off_t offset, int whence) throw (GeneralException) {