0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-27 07:54:05 +01:00

ircd::ctx: Eliminate the std future_status and simplify our real-use interfaces.

This commit is contained in:
Jason Volk 2018-04-05 21:19:16 -07:00
parent 022bf95f7f
commit 8d0681e7b1
7 changed files with 42 additions and 71 deletions

View file

@ -18,16 +18,8 @@ namespace ircd::ctx
template<class T = void> class future; template<class T = void> class future;
template<> class future<void>; template<> class future<void>;
template<class... T> struct scoped_future; template<class... T> struct scoped_future;
enum class future_status;
} }
enum class ircd::ctx::future_status
{
ready,
timeout,
deferred,
};
template<class T> template<class T>
struct ircd::ctx::future struct ircd::ctx::future
:private shared_state<T> :private shared_state<T>
@ -43,12 +35,12 @@ struct ircd::ctx::future
bool operator!() const { return !valid(); } bool operator!() const { return !valid(); }
operator bool() const { return valid(); } operator bool() const { return valid(); }
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &, std::nothrow_t); template<class U, class time_point> friend bool wait_until(const future<U> &, const time_point &, std::nothrow_t);
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &); template<class U, class time_point> friend void wait_until(const future<U> &, const time_point &);
template<class time_point> future_status wait_until(const time_point &, std::nothrow_t) const; template<class time_point> bool wait_until(const time_point &, std::nothrow_t) const;
template<class time_point> future_status wait_until(const time_point &) const; template<class time_point> void wait_until(const time_point &) const;
template<class duration> future_status wait(const duration &d, std::nothrow_t) const; template<class duration> bool wait(const duration &d, std::nothrow_t) const;
template<class duration> future_status wait(const duration &d) const; template<class duration> void wait(const duration &d) const;
void wait() const; void wait() const;
T get(); T get();
@ -76,12 +68,12 @@ struct ircd::ctx::future<void>
bool operator!() const { return !valid(); } bool operator!() const { return !valid(); }
operator bool() const { return valid(); } operator bool() const { return valid(); }
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &, std::nothrow_t); template<class U, class time_point> friend bool wait_until(const future<U> &, const time_point &, std::nothrow_t);
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &); template<class U, class time_point> friend void wait_until(const future<U> &, const time_point &);
template<class time_point> future_status wait_until(const time_point &, std::nothrow_t) const; template<class time_point> bool wait_until(const time_point &, std::nothrow_t) const;
template<class time_point> future_status wait_until(const time_point &) const; template<class time_point> void wait_until(const time_point &) const;
template<class duration> future_status wait(const duration &d, std::nothrow_t) const; template<class duration> bool wait(const duration &d, std::nothrow_t) const;
template<class duration> future_status wait(const duration &d) const; template<class duration> void wait(const duration &d) const;
void wait() const; void wait() const;
IRCD_OVERLOAD(already) IRCD_OVERLOAD(already)
@ -100,7 +92,7 @@ namespace ircd::ctx
{ {
template<class T, template<class T,
class time_point> class time_point>
future_status wait_until(const future<T> &, const time_point &, std::nothrow_t); bool wait_until(const future<T> &, const time_point &, std::nothrow_t);
} }
template<class... T> template<class... T>
@ -240,24 +232,24 @@ const
template<class T> template<class T>
template<class duration> template<class duration>
ircd::ctx::future_status void
ircd::ctx::future<T>::wait(const duration &d) ircd::ctx::future<T>::wait(const duration &d)
const const
{ {
return this->wait_until(steady_clock::now() + d); this->wait_until(steady_clock::now() + d);
} }
template<class duration> template<class duration>
ircd::ctx::future_status void
ircd::ctx::future<void>::wait(const duration &d) ircd::ctx::future<void>::wait(const duration &d)
const const
{ {
return this->wait_until(steady_clock::now() + d); this->wait_until(steady_clock::now() + d);
} }
template<class T> template<class T>
template<class duration> template<class duration>
ircd::ctx::future_status bool
ircd::ctx::future<T>::wait(const duration &d, ircd::ctx::future<T>::wait(const duration &d,
std::nothrow_t) std::nothrow_t)
const const
@ -266,7 +258,7 @@ const
} }
template<class duration> template<class duration>
ircd::ctx::future_status bool
ircd::ctx::future<void>::wait(const duration &d, ircd::ctx::future<void>::wait(const duration &d,
std::nothrow_t) std::nothrow_t)
const const
@ -276,32 +268,25 @@ const
template<class T> template<class T>
template<class time_point> template<class time_point>
ircd::ctx::future_status void
ircd::ctx::future<T>::wait_until(const time_point &tp) ircd::ctx::future<T>::wait_until(const time_point &tp)
const const
{ {
return ircd::ctx::wait_until(*this, tp); ircd::ctx::wait_until(*this, tp);
} }
template<class time_point> template<class time_point>
ircd::ctx::future_status void
ircd::ctx::future<void>::wait_until(const time_point &tp) ircd::ctx::future<void>::wait_until(const time_point &tp)
const const
{ {
const auto status if(!this->wait_until(tp, std::nothrow))
{
this->wait_until(tp, std::nothrow)
};
if(status == future_status::timeout)
throw timeout{}; throw timeout{};
return status;
} }
template<class T> template<class T>
template<class time_point> template<class time_point>
ircd::ctx::future_status bool
ircd::ctx::future<T>::wait_until(const time_point &tp, ircd::ctx::future<T>::wait_until(const time_point &tp,
std::nothrow_t) std::nothrow_t)
const const
@ -310,17 +295,12 @@ const
} }
template<class time_point> template<class time_point>
ircd::ctx::future_status bool
ircd::ctx::future<void>::wait_until(const time_point &tp, ircd::ctx::future<void>::wait_until(const time_point &tp,
std::nothrow_t) std::nothrow_t)
const const
{ {
const auto status if(ircd::ctx::wait_until(*this, tp, std::nothrow))
{
ircd::ctx::wait_until(*this, tp, std::nothrow)
};
if(status == future_status::ready)
{ {
auto &state auto &state
{ {
@ -330,31 +310,25 @@ const
set(state, future_state::RETRIEVED); set(state, future_state::RETRIEVED);
if(bool(state.eptr)) if(bool(state.eptr))
std::rethrow_exception(state.eptr); std::rethrow_exception(state.eptr);
}
return status; return true;
}
else return false;
} }
template<class T, template<class T,
class time_point> class time_point>
ircd::ctx::future_status void
ircd::ctx::wait_until(const future<T> &f, ircd::ctx::wait_until(const future<T> &f,
const time_point &tp) const time_point &tp)
{ {
const auto ret if(!wait_until(f, tp, std::nothrow))
{
wait_until(f, tp, std::nothrow)
};
if(ret == future_status::timeout)
throw timeout{}; throw timeout{};
return ret;
} }
template<class T, template<class T,
class time_point> class time_point>
ircd::ctx::future_status bool
ircd::ctx::wait_until(const future<T> &f, ircd::ctx::wait_until(const future<T> &f,
const time_point &tp, const time_point &tp,
std::nothrow_t) std::nothrow_t)
@ -373,9 +347,7 @@ ircd::ctx::wait_until(const future<T> &f,
throw no_state{}; throw no_state{};
if(unlikely(!state.cond.wait_until(tp, wfun))) if(unlikely(!state.cond.wait_until(tp, wfun)))
return future_status::timeout; return false;
return likely(wfun())? return wfun();
future_status::ready:
future_status::deferred;
} }

View file

@ -222,7 +222,7 @@ room_alias_fetch(const mutable_buffer &out,
}; };
//TODO: conf //TODO: conf
if(federation_request.wait(seconds(8)) == ctx::future_status::timeout) if(!federation_request.wait(seconds(8), std::nothrow))
throw http::error throw http::error
{ {
http::REQUEST_TIMEOUT http::REQUEST_TIMEOUT

View file

@ -163,7 +163,7 @@ get__profile_remote(client &client,
}; };
//TODO: conf //TODO: conf
if(federation_request.wait(seconds(8)) == ctx::future_status::timeout) if(!federation_request.wait(seconds(8), std::nothrow))
throw http::error throw http::error
{ {
http::REQUEST_TIMEOUT http::REQUEST_TIMEOUT

View file

@ -2036,10 +2036,9 @@ console_cmd__fed__groups(opt &out, const string_view &line)
node, vector_view<const m::user::id>(ids, count), buf, std::move(opts) node, vector_view<const m::user::id>(ids, count), buf, std::move(opts)
}; };
if(request.wait(seconds(10)) == ctx::future_status::timeout) request.wait(seconds(10));
throw http::error{http::REQUEST_TIMEOUT};
request.get(); request.get();
const json::object response const json::object response
{ {
request.in.content request.in.content

View file

@ -240,7 +240,7 @@ try
ctx::when_any(begin(txns), end(txns)) ctx::when_any(begin(txns), end(txns))
}; };
if(next.wait(seconds(2), std::nothrow) == ctx::future_status::timeout) //TODO: conf if(!next.wait(seconds(2), std::nothrow)) //TODO: conf
return; return;
const auto it const auto it

View file

@ -77,7 +77,7 @@ get__keys(const string_view &server_name,
}; };
//TODO: conf //TODO: conf
if(tag.wait(seconds(20)) == ctx::future_status::timeout) if(!tag.wait(seconds(20), std::nothrow))
{ {
cancel(tag); cancel(tag);
throw m::error throw m::error
@ -170,7 +170,7 @@ try
server_name, { head }, { in } server_name, { head }, { in }
}; };
if(tag.wait(seconds(30)) == ctx::future_status::timeout) if(!tag.wait(seconds(30), std::nothrow))
throw m::error throw m::error
{ {
http::REQUEST_TIMEOUT, "M_TIMEOUT", http::REQUEST_TIMEOUT, "M_TIMEOUT",

View file

@ -161,7 +161,7 @@ get__thumbnail_remote(client &client,
remote, { out_head }, { in_head, in_content }, &opts remote, { out_head }, { in_head, in_content }, &opts
}; };
if(remote_request.wait(seconds(10)) == ctx::future_status::timeout) if(!remote_request.wait(seconds(10), std::nothrow))
throw http::error throw http::error
{ {
http::REQUEST_TIMEOUT http::REQUEST_TIMEOUT