Seastar applications obviously cannot use the Posix sleep() function, or
the entire thread will block. This patch implements future<> sleep(duration)
where as expected, the future becomes ready when the given duration passes.
For example, one can do:
sleep(1s).then([] { std::cout << "Done.\n"; });
This can be useful as an example of futures and continuations in the
Seastar tutorial, and people might find other uses for it.
This sleep() is implemented in terms of timer<>. sleep() is easier to use
and more aligned with the rest of Seastar (it uses then() for the
continuation instead of a set_callback() method). The downside of sleep()
compared to a timer is that it cannot be canceled once started.
In this version, sleep() is implemented without shared_ptr, making the
implementation a tiny bit more efficient. There is still a heap allocation
(this is unavoidable because std::function requires a copyable type)
but no reference counting. Unfortunately, this requires us to use bare
"new" and "delete".
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
engine_exit() without a parameter is meant to do engine().exit(0).
We do this, but forget to return from this function, so after calling
engine().exit(0), it continued on and crashed.
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
This patch adds a new header file, "core/unaligned.hh", which defines a
new operation, unaligned_cast<T*>(p). This works exctly like
reinterpret_cast<T*>(p) except it is safe to use even when the address
of p is not a multiple of alignof(T).
A long comment in the header file explains why this unaligned_cast<>
is necessary on some esoteric architectures and to quiet gcc's
-fsanitize=alignment.
The header file also defines a new template for holding an unaligned
version of type - unaligned<T>. unaligned<T> is almost identical to our
existing net::packed<>, so the next patch will implement net::packed
in terms of unaligned<>.
The unaligned_cast<> and unaligned<> templates are of course generally
useful outside the network code, which is why I wanted them out of the
networking header files and namespace.
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Makes invoking non-copyable lambdas possible:
distributed<T> t;
non_copyable_type x;
t.invoke_on(0, [x = std::move(x)] (auto& t) {});
As a side effect this could save some copyable lambdas from being
needlesly copied.
One file, seastar.hh, containing all the main entry points as free functions.
Most classes are forward-declared to avoid the need to #include the entire
world.
Add a when_all() variant that waits for a runtime variable number of
futures, all of the same type. The function returns a future containing
a vector of all input futures, each in ready state, containing either
a result or an exception.
Currently we have one version of when_all(), that accepts a compile
type variable number of independently typed futures. We wish to add more
overloads, but for that, we must ensure that this variant doesn't match them.
Accomplish this by requiring that the first argument be a future, and don't
accept any other type.
allocate_buffer was added to data_sink as a virtual method, so that
a class extending it can override its implementation.
output_stream will also start using allocate_buffer whenever it
needs a temporary buffer.
In fstream.cc, use the convenient new temporary_buffer<char>::aligned()
function for creating an aligned temporary buffer - instead of repeating
its implementation.
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
The file_input_stream interface was messy: it was not fiber safe (e.g., code
doing seek() in the middle of an ongoing read_exactly()), and went against
the PIMPL philosophy.
So this patch removes the file_input_stream class, and replaces it with a
completely different design:
We now have in fstream.hh a global function:
input_stream<char>
make_file_input_stream(
lw_shared_ptr<file> file, uint64_t offset = 0,
uint64_t buffer_size = 8192);
In other words, instead of "seeking" in an input stream, we just open a new
input stream object at a particular offset of the given file. Multiple input
streams might be concurrently active on the same file.
Note how make_file_input_stream now returns a regular "input_stream", not a
subtype, and it can be used just like any normal input_stream to read the stream
starting at the given position.
This patch makes "input_stream" a "final" type: we no longer subclass it in our
code, and we shouldn't in the future because it goes against the PIMPL design
(the subclass should be of the inner workings, like the data_source_impl, not
of input_stream).
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
We had an include loop, which can cause problems in some cases:
1. iostream.hh #includes scattered_message.hh #includes reactor.hh
2. reactor.hh #includes iostream.hh
This patch fixes the loop be removing an unnecessary include.
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
reactor.hh includes forward declarations for input_stream, output_stream
template classes. These are unnecessary, because it #include's iostream.hh,
which contains the full definition of these classes.
These unnecessary forward declarations are harmless in the current code, but
they will become harmful if we change the definitions in iostream.hh (e.g., I
wanted to make input_stream a "final" class).
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
core/reactor.hh had two #include lines in the middle of the file, which
duplicate previous #include lines in the top of the file. So remove them.
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
make_shared() has a special case for detecting a created class deriving
from enable_shared_from_this<>, so it can point the refcount pointer into
the object's data area instead of creating a shared_ptr_count_base for it.
The code, however, fails to detect a creating class deriving indirectly
from enable_shared_from_this:
struct base : enable_shared_from_this<base> {};
struct derived : base {};
make_shared<derived>(); // <- allocates independent refcount
The result is that the object reference counter lives in two locations.
Fix by detecting the derived class case as well.
This patch adds some of the common functionalities from std:string to
sstring.
It adds length (implement by size() )
It define the constant npos to indicate no possition.
It adds the at (reference and const reference)
It define the find char and find sstring methods
and the substr method
Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
need merge sstring
1. Moved all signal handler functions/members into a wrapping class
2. Block all signals on desctuctor call (resetting of signal handler is
not enough since some signals will arive even if we reset signal handler
to SIG_DFL e.g. timer)
Fixes use-after-free when a signal is caught after the reactor is
destroyed.
Signed-off-by: Shlomi Livne <shlomi@cloudius-systems.com>