2016-05-02 04:01:00 +00:00
|
|
|
// Handles IO that may hang.
|
2011-12-27 05:21:12 +00:00
|
|
|
#ifndef FISH_IOTHREAD_H
|
|
|
|
#define FISH_IOTHREAD_H
|
|
|
|
|
Introduce topic monitoring
topic_monitor allows for querying changes posted to one or more topics,
initially sigchld. This will eventually replace the waitpid logic in
process_mark_finished_children().
Comment from the new header:
Topic monitoring support. Topics are conceptually "a thing that can
happen." For example, delivery of a SIGINT, a child process exits, etc. It
is possible to post to a topic, which means that that thing happened.
Associated with each topic is a current generation, which is a 64 bit
value. When you query a topic, you get back a generation. If on the next
query the generation has increased, then it indicates someone posted to
the topic.
For example, if you are monitoring a child process, you can query the
sigchld topic. If it has increased since your last query, it is possible
that your child process has exited.
Topic postings may be coalesced. That is there may be two posts to a given
topic, yet the generation only increases by 1. The only guarantee is that
after a topic post, the current generation value is larger than any value
previously queried.
Tying this all together is the topic_monitor_t. This provides the current
topic generations, and also provides the ability to perform a blocking
wait for any topic to change in a particular topic set. This is the real
power of topics: you can wait for a sigchld signal OR a thread exit.
2019-02-02 23:39:04 +00:00
|
|
|
#include <pthread.h>
|
2019-10-13 22:50:48 +00:00
|
|
|
|
2020-02-17 13:12:27 +00:00
|
|
|
#include <cstdint> // for uint64_t
|
2017-01-23 18:37:16 +00:00
|
|
|
#include <functional>
|
2020-03-03 06:57:41 +00:00
|
|
|
#include <memory>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <type_traits>
|
2017-01-23 18:37:16 +00:00
|
|
|
|
2020-03-03 06:57:41 +00:00
|
|
|
#include "maybe.h"
|
|
|
|
|
2020-11-01 23:07:59 +00:00
|
|
|
/// \return the fd on which to listen for completion callbacks.
|
|
|
|
int iothread_port();
|
2011-12-27 05:21:12 +00:00
|
|
|
|
2020-11-02 02:25:09 +00:00
|
|
|
/// Services iothread completion callbacks.
|
2020-11-01 23:07:59 +00:00
|
|
|
void iothread_service_completion();
|
2011-12-27 05:21:12 +00:00
|
|
|
|
2020-11-02 02:25:09 +00:00
|
|
|
/// Services completions, except does not wait more than \p timeout_usec.
|
|
|
|
void iothread_service_completion_with_timeout(long timeout_usec);
|
|
|
|
|
2016-05-02 04:01:00 +00:00
|
|
|
/// Waits for all iothreads to terminate.
|
2019-11-23 21:37:15 +00:00
|
|
|
/// \return the number of threads that were running.
|
2020-11-01 23:07:59 +00:00
|
|
|
int iothread_drain_all();
|
2012-02-28 03:46:15 +00:00
|
|
|
|
2017-01-24 17:30:30 +00:00
|
|
|
// Internal implementation
|
2020-11-01 23:07:59 +00:00
|
|
|
void iothread_perform_impl(std::function<void()> &&func, std::function<void()> &&completion,
|
|
|
|
bool cant_wait = false);
|
2017-01-23 19:35:22 +00:00
|
|
|
|
2020-03-03 06:57:41 +00:00
|
|
|
// This is the glue part of the handler-completion handoff.
|
|
|
|
// Given a Handler and Completion, where the return value of Handler should be passed to Completion,
|
|
|
|
// this generates new void->void functions that wraps that behavior. The type T is the return type
|
|
|
|
// of Handler and the argument to Completion
|
|
|
|
template <typename Handler, typename Completion,
|
|
|
|
typename Result = typename std::result_of<Handler()>::type>
|
|
|
|
struct iothread_trampoline_t {
|
|
|
|
iothread_trampoline_t(const Handler &hand, const Completion &comp) {
|
|
|
|
auto result = std::make_shared<maybe_t<Result>>();
|
|
|
|
this->handler = [=] { *result = hand(); };
|
|
|
|
this->completion = [=] { comp(result->acquire()); };
|
2017-01-24 17:30:30 +00:00
|
|
|
}
|
2020-03-03 06:57:41 +00:00
|
|
|
|
|
|
|
// The generated handler and completion functions.
|
|
|
|
std::function<void()> handler;
|
|
|
|
std::function<void()> completion;
|
2017-01-24 17:30:30 +00:00
|
|
|
};
|
|
|
|
|
2020-03-03 06:57:41 +00:00
|
|
|
// Void specialization.
|
|
|
|
template <typename Handler, typename Completion>
|
|
|
|
struct iothread_trampoline_t<Handler, Completion, void> {
|
|
|
|
iothread_trampoline_t(std::function<void()> hand, std::function<void()> comp)
|
|
|
|
: handler(std::move(hand)), completion(std::move(comp)) {}
|
|
|
|
|
2020-11-01 23:07:59 +00:00
|
|
|
// The handler and completion functions.
|
2020-03-03 06:57:41 +00:00
|
|
|
std::function<void()> handler;
|
|
|
|
std::function<void()> completion;
|
2017-01-24 17:30:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// iothread_perform invokes a handler on a background thread, and then a completion function
|
|
|
|
// on the main thread. The value returned from the handler is passed to the completion.
|
2020-03-03 06:57:41 +00:00
|
|
|
// In other words, this is like Completion(Handler()) except the handler part is invoked
|
2017-01-26 17:33:03 +00:00
|
|
|
// on a background thread.
|
2020-03-03 06:57:41 +00:00
|
|
|
template <typename Handler, typename Completion>
|
2020-11-01 23:07:59 +00:00
|
|
|
void iothread_perform(const Handler &handler, const Completion &completion) {
|
2020-03-03 06:57:41 +00:00
|
|
|
iothread_trampoline_t<Handler, Completion> tramp(handler, completion);
|
2020-11-01 23:07:59 +00:00
|
|
|
iothread_perform_impl(std::move(tramp.handler), std::move(tramp.completion));
|
2017-01-24 17:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// variant of iothread_perform without a completion handler
|
2020-11-01 23:07:59 +00:00
|
|
|
inline void iothread_perform(std::function<void()> &&func) {
|
|
|
|
iothread_perform_impl(std::move(func), {});
|
2017-01-23 19:35:22 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 19:32:44 +00:00
|
|
|
/// Variant of iothread_perform that disrespects the thread limit.
|
|
|
|
/// It does its best to spawn a new thread if all other threads are occupied.
|
|
|
|
/// This is for cases where deferring a new thread might lead to deadlock.
|
2020-11-01 23:07:59 +00:00
|
|
|
inline void iothread_perform_cantwait(std::function<void()> &&func) {
|
|
|
|
iothread_perform_impl(std::move(func), {}, true);
|
2020-01-18 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:37:16 +00:00
|
|
|
/// Performs a function on the main thread, blocking until it completes.
|
2020-11-01 23:07:59 +00:00
|
|
|
void iothread_perform_on_main(std::function<void()> &&func);
|
2017-01-23 18:37:16 +00:00
|
|
|
|
2019-02-01 09:04:14 +00:00
|
|
|
/// Creates a pthread, manipulating the signal mask so that the thread receives no signals.
|
2019-12-16 22:08:28 +00:00
|
|
|
/// The thread is detached.
|
2019-02-01 09:04:14 +00:00
|
|
|
/// The pthread runs \p func.
|
|
|
|
/// \returns true on success, false on failure.
|
2019-12-16 22:08:28 +00:00
|
|
|
bool make_detached_pthread(void *(*func)(void *), void *param);
|
2020-11-01 23:07:59 +00:00
|
|
|
bool make_detached_pthread(std::function<void()> &&func);
|
2019-02-01 09:04:14 +00:00
|
|
|
|
2019-05-29 19:33:44 +00:00
|
|
|
/// \returns a thread ID for this thread.
|
|
|
|
/// Thread IDs are never repeated.
|
|
|
|
uint64_t thread_id();
|
|
|
|
|
2020-03-03 06:57:41 +00:00
|
|
|
/// A Debounce is a simple class which executes one function in a background thread,
|
|
|
|
/// while enqueuing at most one more. New execution requests overwrite the enqueued one.
|
|
|
|
/// It has an optional timeout; if a handler does not finish within the timeout, then
|
|
|
|
/// a new thread is spawned.
|
|
|
|
class debounce_t {
|
|
|
|
public:
|
|
|
|
/// Enqueue \p handler to be performed on a background thread, and \p completion (if any) to be
|
|
|
|
/// performed on the main thread. If a function is already enqueued, this overwrites it; that
|
|
|
|
/// function will not execute.
|
|
|
|
/// This returns the active thread token, which is only of interest to tests.
|
|
|
|
template <typename Handler, typename Completion>
|
2020-11-01 23:07:59 +00:00
|
|
|
void perform(Handler handler, Completion completion) {
|
2020-03-03 06:57:41 +00:00
|
|
|
iothread_trampoline_t<Handler, Completion> tramp(handler, completion);
|
2020-11-01 23:07:59 +00:00
|
|
|
perform_impl(std::move(tramp.handler), std::move(tramp.completion));
|
2020-03-03 06:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// One-argument form with no completion.
|
|
|
|
uint64_t perform(std::function<void()> func) { return perform_impl(std::move(func), {}); }
|
|
|
|
|
|
|
|
explicit debounce_t(long timeout_msec = 0);
|
|
|
|
~debounce_t();
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Implementation of perform().
|
|
|
|
uint64_t perform_impl(std::function<void()> handler, std::function<void()> completion);
|
|
|
|
|
|
|
|
const long timeout_msec_;
|
|
|
|
struct impl_t;
|
|
|
|
const std::shared_ptr<impl_t> impl_;
|
|
|
|
};
|
|
|
|
|
2011-12-27 05:21:12 +00:00
|
|
|
#endif
|