From 11e1cea6667467b2fa5b5791c86b64444420a16a Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Fri, 2 Aug 2013 10:28:03 +0200 Subject: Introducing the concept of Future. --- includes/Handle.h | 3 ++- includes/Task.h | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/includes/Handle.h b/includes/Handle.h index f501ab1..e72507c 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -57,7 +58,7 @@ class Handle { ssize_t forceWrite(const void * buf, size_t count, Events::BaseEvent * evt = NULL) throw (GeneralException); ssize_t write(const String & str) { return write(str.to_charp(), str.strlen()); } ssize_t forceWrite(const String & str) { return forceWrite(str.to_charp(), str.strlen()); } - uint8_t readU8() { uint8_t r; read(&r, 1); return r; } + Future readU8(); protected: Handle() { } private: diff --git a/includes/Task.h b/includes/Task.h index e8aea70..dcbdf91 100644 --- a/includes/Task.h +++ b/includes/Task.h @@ -1,6 +1,7 @@ #pragma once #include +#include #ifndef _WIN32 #include #endif @@ -260,6 +261,27 @@ class QueueBase { pthread_cond_t m_cond; }; +template +struct Future { + typedef std::function func_t; + R get(); + func_t m_run; +}; + +template +R Future::get() { + R r; + for (;;) { + try { + r = m_run(); + return r; + } + catch (EAgain & e) { + Task::operationYield(e.getEvent(), Task::INTERRUPTIBLE); + } + } +} + template class Queue : public QueueBase { public: -- cgit v1.2.3