Commit 5455a471 authored by octal's avatar octal

A promise is not attached to its core anymore

parent c9a8e990
...@@ -76,28 +76,12 @@ namespace Async { ...@@ -76,28 +76,12 @@ namespace Async {
struct Core { struct Core {
Core(State state) Core(State state)
: state(state) : state(state)
, promise(nullptr)
{ } { }
State state; State state;
std::exception_ptr exc; std::exception_ptr exc;
PromiseBase* promise;
std::vector<std::shared_ptr<Request>> requests; std::vector<std::shared_ptr<Request>> requests;
void attach(PromiseBase* p) {
if (promise)
throw Error("Trying to double-attach a Promise");
promise = p;
}
void detach() {
if (!promise)
throw Error("Trying to detach a non attached Promise");
promise = nullptr;
}
virtual void* memory() = 0; virtual void* memory() = 0;
}; };
...@@ -113,9 +97,6 @@ namespace Async { ...@@ -113,9 +97,6 @@ namespace Async {
bool operator()(Arg&& arg) { bool operator()(Arg&& arg) {
typedef typename std::remove_reference<Arg>::type Type; typedef typename std::remove_reference<Arg>::type Type;
auto promise = core_->promise;
if (!promise) return false;
if (core_->state != State::Pending) if (core_->state != State::Pending)
throw Error("Attempt to resolve a fulfilled promise"); throw Error("Attempt to resolve a fulfilled promise");
...@@ -142,10 +123,6 @@ namespace Async { ...@@ -142,10 +123,6 @@ namespace Async {
template<typename Exc> template<typename Exc>
bool operator()(Exc exc) { bool operator()(Exc exc) {
auto promise = core_->promise;
if (!promise)
return false;
if (core_->state != State::Pending) if (core_->state != State::Pending)
throw Error("Attempt to reject a fulfilled promise"); throw Error("Attempt to reject a fulfilled promise");
...@@ -196,7 +173,6 @@ namespace Async { ...@@ -196,7 +173,6 @@ namespace Async {
, resolver_(core_) , resolver_(core_)
, rejection_(core_) , rejection_(core_)
{ {
core_->attach(this);
func(resolver_, rejection_); func(resolver_, rejection_);
} }
...@@ -208,7 +184,6 @@ namespace Async { ...@@ -208,7 +184,6 @@ namespace Async {
~Promise() ~Promise()
{ {
core_->detach();
} }
bool isPending() const { return core_->state == State::Pending; } bool isPending() const { return core_->state == State::Pending; }
...@@ -273,7 +248,6 @@ namespace Async { ...@@ -273,7 +248,6 @@ namespace Async {
, resolver_(core) , resolver_(core)
, rejection_(core) , rejection_(core)
{ {
core_->attach(this);
} }
......
...@@ -369,8 +369,7 @@ Response::send(Code code) { ...@@ -369,8 +369,7 @@ Response::send(Code code) {
Async::Promise<ssize_t> Async::Promise<ssize_t>
Response::send(Code code, const std::string& body, const Mime::MediaType& mime) Response::send(Code code, const std::string& body, const Mime::MediaType& mime)
{ {
char buffer[Const::MaxBuffer]; Io::OutArrayBuf obuf(buffer_, Io::Init::ZeroOut);
Io::OutArrayBuf obuf(buffer, Io::Init::ZeroOut);
std::ostream stream(&obuf); std::ostream stream(&obuf);
stream << "HTTP/1.1 "; stream << "HTTP/1.1 ";
...@@ -403,7 +402,7 @@ Response::send(Code code, const std::string& body, const Mime::MediaType& mime) ...@@ -403,7 +402,7 @@ Response::send(Code code, const std::string& body, const Mime::MediaType& mime)
stream << crlf; stream << crlf;
} }
return peer()->send(buffer, obuf.len()); return peer()->send(buffer_, obuf.len());
} }
void void
......
...@@ -107,6 +107,7 @@ private: ...@@ -107,6 +107,7 @@ private:
void associatePeer(const std::shared_ptr<Tcp::Peer>& peer); void associatePeer(const std::shared_ptr<Tcp::Peer>& peer);
std::weak_ptr<Tcp::Peer> peer_; std::weak_ptr<Tcp::Peer> peer_;
char buffer_[Const::MaxBuffer];
}; };
namespace Private { namespace Private {
......
...@@ -30,9 +30,11 @@ TEST(async_test, basic_test) { ...@@ -30,9 +30,11 @@ TEST(async_test, basic_test) {
p1.then([&](int v) { val = v; }, Async::NoExcept); p1.then([&](int v) { val = v; }, Async::NoExcept);
ASSERT_EQ(val, 10); ASSERT_EQ(val, 10);
Async::Promise<int> p2 = doAsync(10); {
p2.then([](int result) { ASSERT_EQ(result, 20); }, Async::Promise<int> p2 = doAsync(10);
Async::NoExcept); p2.then([](int result) { ASSERT_EQ(result, 20); },
Async::NoExcept);
}
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment