Commit Graph

68 Commits

Author SHA1 Message Date
Avi Kivity
00da6d85c3 future: optimize for compile time
.then()'s return type is a complex template, which needs to be mangled into
the function's name.  Move the return type into a defaulted template type
parameter, so that the entire type expression is eliminated, being replaced
by the result type.

Saves about 1% compile time and 3% object size on futures_test.o.
2015-07-02 12:47:07 +03:00
Nadav Har'El
770e6b7816 future: add handle_exception() method
This patch adds a "class future" method for handling an exception result
of the future. It is impossible to discard a future's exception while passing
through the value of the result (what will we pass in the case of
exception?), so we discard the result as well.

An example of how this can be used, to log an error (but otherwise do
nothing) if removing a file fails:

  remove_file(filename).handle_exception(
    [] (std::exception_ptr eptr) {
      print("Exception when deleting file: %s", eptr);
    });

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2015-06-30 14:57:53 +03:00
Tomasz Grabiec
14a8110d1f future: Avoid copying of the result in get0()
Even though we accept std::tuple<T...>&&, 'x' is an l-value reference
inside get0().
2015-06-23 14:28:01 +03:00
Avi Kivity
a4c711afd4 future: fix future<>::get0()
Empty tuples don't have a first element.
2015-06-15 11:43:38 +03:00
Avi Kivity
ddaaa315c8 future: add a get0() helper to get the first/only member of the result tuple
Reviewed-by: Pekka Enberg <penberg@cloudius-systems.com>
2015-06-15 11:29:02 +03:00
Avi Kivity
27d9446f77 future: fix false-positive when a promise tied to a future that lost its state is destroyed
When a promise that still tracks its future is destroyed, but after
future::get() or future::then() was called, the promise thinks it was
destroyed before generating a value, and fails an assertion.

Fix be detaching the promise from the future as soon as get() or then()
remove the value.
2015-06-06 13:47:44 +03:00
Avi Kivity
ba0e4e45bc future: disable inline continuations in debug mode
Inline continuations can hide bugs where a stack variable is captured by
reference.  Disable them in debug mode.
2015-06-02 15:12:10 +03:00
Avi Kivity
3d4314b7e4 doc: disambigute class future from the future documentation module 2015-05-31 19:08:16 +03:00
Avi Kivity
abaad55a66 future: fix documentation errors
Noted by Nadav.
2015-05-31 18:10:54 +03:00
Avi Kivity
aa519f86a8 future: user-level documentation 2015-05-31 17:29:31 +03:00
Avi Kivity
2877364947 future: separate task class into its own header
tasks are a lower-level concept than future/promise, so move them to their
own header.
2015-05-31 15:49:05 +03:00
Avi Kivity
d115f9cef6 future: modernize lambda_task, make_task
Use inline, override, final, make_unique.
2015-05-25 22:21:49 +03:00
Avi Kivity
d1bc08e9e7 future: merge two variants of make_task()
Using universal references we can unify the two cases.
2015-05-25 22:18:24 +03:00
Avi Kivity
8812504e4b future: sprinke always_inline attributes in strategic places
gcc makes poor inlining decisions sometimes, which cause all the value-
tracking optimizations to be lost.  Forcing it to inline (particularly
around destructors) allows ready futures to be inlined with fewer always-
true tests and data movements.
2015-05-25 21:27:40 +03:00
Asias He
88e7dcfa86 Remove redundant const in static constexpr const
From http://en.cppreference.com/w/cpp/language/constexpr:

   A constexpr specifier used in an object declaration implies const.

However, We can not change from
   static constexpr const char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";
to
   static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";

The compiler complains:

   In file included from json/formatter.cc:22:0:
   json/formatter.hh:132:42: error: deprecated conversion from string
   constant to ‘char*’ [-Werror=write-strings]
        static constexpr char* TIME_FORMAT = "%a %b %d %I:%M:%S %Z %Y";

Since, unlike const, constexpr does not modify a type. It just applies
to an object (or function), and incidentally implies const to the
top-level type.
2015-05-25 11:57:19 +03:00
Avi Kivity
0bd48b37ed future: consolidate task_with_state, task_with_ready_state
The internal structs task_with_state and task_with_ready_state are
identical except for their constructors.  Merge them into a new
struct continuation.
2015-05-23 11:40:39 +03:00
Avi Kivity
cd0ae463b3 core: thread support
Add a thread class that can be used to launch a blockable thread of
execution.  Within a thread, future<>::get() can be called on an
unavailable future, in which case it blocks until the future is made ready.
2015-05-17 15:57:11 +03:00
Gleb Natapov
58c62dd121 core: drop superfluous qualifier from discard_result 2015-04-01 11:33:19 +03:00
Avi Kivity
6852f21463 future: mark state as invalid when moving away from *this
Avoid inconsistency between an future thinking it's an exception, while
the exception pointer is null.

Fixes smp_test.
2015-03-18 12:34:32 +02:00
Avi Kivity
ceb4778ad2 Merge branch 'log-exceptions' 2015-03-16 10:14:23 +02:00
Avi Kivity
b968f80695 future: report abandoned failed futures
Report an abandoned future containing an exception, corresponding to
an uncaught exception in an ordinary program.
2015-03-15 15:44:08 +02:00
Gleb Natapov
c3ba8678e4 core: consolidate finally() implemetations 2015-03-15 14:44:58 +02:00
Gleb Natapov
186e25717e core: consolidate then() and then_wrapped() code 2015-03-15 14:18:43 +02:00
Gleb Natapov
ad63b70d53 core: remove unused structure future_task 2015-03-15 14:11:51 +02:00
Gleb Natapov
c138e31a2a core: check future_avail_count in then_wrapped 2015-03-15 12:41:26 +02:00
Gleb Natapov
ecb72e995a core: drop superfluous qualifier from or_terminate 2015-03-15 12:18:44 +02:00
Gleb Natapov
0838a0ce2f core: drop future<> casting to rvalue before call to then_wrapped()
No longer required.
2015-03-11 17:19:23 +02:00
Gleb Natapov
4d286f0504 core: drop superfluous qualifier from then_wrapped 2015-03-06 12:52:11 +02:00
Tomasz Grabiec
af82e23c75 core: Use futurize<> to unify then_wrapped() specializations 2015-03-06 11:35:56 +01:00
Tomasz Grabiec
a232b7e6c5 core: Use futurize<> to unify future::then() specializations
As a side benefit, returning non-void non-future is now allowed:

 future<int> x() {
     return later().then([] {
         return 3;
     });
 }
2015-03-06 11:35:56 +01:00
Tomasz Grabiec
f25d7ac068 core: Add futurize::apply()
Invokes given function wrapping the result in a future if necessary.
2015-03-06 11:35:56 +01:00
Tomasz Grabiec
422d642cf4 core: Add futurize::primise_type 2015-03-06 11:35:56 +01:00
Tomasz Grabiec
f5485c667d core: Move futurize<> to future.hh 2015-03-06 11:35:56 +01:00
Tomasz Grabiec
83963b23d3 Replace rescue() usages with then_wrapped()
They are pretty much the same. This change removes rescue().
2015-03-04 17:34:59 +01:00
Tomasz Grabiec
e36115e1d4 core: add then_wrapped() overload which works with void-returning callbacks 2015-03-04 17:33:38 +01:00
Calle Wilund
7b5193b80c future: deferring finally() callback
Add "finally" continuation overload for functions returning future type that
awaits finishing the continuation in question before continuing the "present"
one. I.e. a wrapper around "then_wrapped" to remove the need to deal with the
original return value.

Signed-off-by: Calle Wilund <calle@cloudius-systems.com>
2015-03-04 18:09:34 +02:00
Gleb Natapov
590d8da4f1 core: provide "void" promise specialization
Makes it easier to write generic code.
2015-03-01 15:58:36 +02:00
Nadav Har'El
b64f26832e reactor: terminate cleanly in or_terminate()
The ".or_terminate()" continuation is needed when one needs to exit the
application on an unhandled exception, instead of just letting the event
loop continue to spin forever.

or_terminate() currently calls std::terminate() which only incidentally
(as a gcc-specific implementation) prints out the exception's type; It
then calls abort(), which results in a painfully slow core dump. This
abort() is NOT helpful, because at that point the debugger can only find
where abort() was called, not where the original exception was thrown (see
issue #32).

So instead of calling std::terminate(), this patch switches to calling
engine().exit(), to cleanly shut down the application, without dumping
core. It also prints, like gcc's std::terminate(), the exception's type.
This printing requires non-standard gcc extensions.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2015-02-25 12:28:34 +02:00
Avi Kivity
7f8d88371a Add LICENSE, NOTICE, and copyright headers to all source files.
The two files imported from the OSv project retain their original licenses.
2015-02-19 16:52:34 +02:00
Avi Kivity
e746eb7f84 future: fix make_exception_future build error
Add missing std::.
2015-01-15 15:19:02 +02:00
Avi Kivity
311e1c834e future: fix future::then_wrapped() in exception case
Calling state.get() will throw the exception instead of calling the function,
thus denying the called function the chance to deal with the exception.

Fix by constructing the future directly from state.
2015-01-15 15:19:02 +02:00
Avi Kivity
7dfd7de8cd future: optimize data-less future<>
A future that does not carry any data (future<>) and its sibling (promise<>)
are heavily used in the code.  We can optimize them by overlaying the
future's payload, which in this case can only be an std::exception_ptr,
with the future state, as a pointer and an enum have disjoint values.

This of course depends on std::exception_ptr being implemented as a pointer,
but as it happens, it is.

With this, sizeof(future<>) is reduced from 24 bytes to 16 bytes.
2014-12-09 10:08:48 +02:00
Tomasz Grabiec
b6511ce3f4 core: add future::discard_result()
Use when you don't want to care about the result and just want to
return a future<>.

The current implementation may not be the most optimal way to do it
but it can be improved later if there's need.
2014-11-09 16:33:34 +02:00
Gleb Natapov
e5dfd8e863 future: limit number of ready futures that are executed without scheduling a task
Otherwise stack may overflow if a very long chain of ready futures is
executed.
2014-11-04 15:18:32 +02:00
Avi Kivity
3d414111eb future: make .rescue() require an rvalue reference for its future
This makes it harder to misuse.
2014-10-30 14:07:42 +02:00
Avi Kivity
c4bc67414e future: add then_wrapped()
Unlike future::then(), which unwraps the value, then_wrapped() keeps it
wrapped in a future<>, so if it is exceptional, it can still be accessed.

This is similar to the proposed std::future::then(), so we should later
rename it to match (and rename the existing future::then() to future::next().
2014-10-30 13:55:31 +02:00
Tomasz Grabiec
eb84a3b78b core: fix future::forward_to()
It did not handle properly the case when the target promise's future gets dead
without installing a callback or the future was never installed. The
mishanlding of the former case was causing httpd to abort on SMP.
2014-10-27 15:58:57 +02:00
Avi Kivity
0d745abf69 future: sprinke noexcept everywhere
When used correctly, noexcept allows containers to optimize their reallocation
code.
2014-10-26 14:34:56 +02:00
Tomasz Grabiec
74ac65a491 core: optimize future::forward_to()
We can avoid extra allocation and chaining by linking the current
future's promise with the target promise's future, as if the target
promise was moved into the current future's promise.
2014-10-24 19:40:48 +03:00
Tomasz Grabiec
ca077f33ef core: introduce or_terminate()
Calls std::terminate() when future fails. It can be used like this:

  do_something([] {}).or_terminate();
2014-10-15 15:59:41 +02:00