summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/Handle.h3
-rw-r--r--includes/Task.h22
2 files changed, 24 insertions, 1 deletions
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 <Task.h>
#include <Exceptions.h>
#include <Printer.h>
#include <BString.h>
@@ -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<uint8_t> 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 <stdlib.h>
+#include <functional>
#ifndef _WIN32
#include <coro.h>
#endif
@@ -260,6 +261,27 @@ class QueueBase {
pthread_cond_t m_cond;
};
+template<class R>
+struct Future {
+ typedef std::function<R()> func_t;
+ R get();
+ func_t m_run;
+};
+
+template<class R>
+R Future<R>::get() {
+ R r;
+ for (;;) {
+ try {
+ r = m_run();
+ return r;
+ }
+ catch (EAgain & e) {
+ Task::operationYield(e.getEvent(), Task::INTERRUPTIBLE);
+ }
+ }
+}
+
template<class T>
class Queue : public QueueBase {
public: