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
|
|
|
|
|
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>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <utility>
|
2020-03-03 06:57:41 +00:00
|
|
|
|
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
|
|
|
|
2021-02-07 02:43:43 +00:00
|
|
|
/// Services iothread main thread completions and requests.
|
|
|
|
/// This does not block.
|
|
|
|
void iothread_service_main();
|
2011-12-27 05:21:12 +00:00
|
|
|
|
2021-02-07 02:43:43 +00:00
|
|
|
// Services any main thread requests. Does not wait more than \p timeout_usec.
|
2021-04-10 23:45:26 +00:00
|
|
|
void iothread_service_main_with_timeout(uint64_t timeout_usec);
|
2020-11-02 02:25:09 +00:00
|
|
|
|
2016-05-02 04:01:00 +00:00
|
|
|
/// Waits for all iothreads to terminate.
|
2022-06-19 22:06:49 +00:00
|
|
|
/// This is a hacky function only used in the test suite.
|
|
|
|
void iothread_drain_all();
|
2012-02-28 03:46:15 +00:00
|
|
|
|
2017-01-24 17:30:30 +00:00
|
|
|
// Internal implementation
|
2021-02-26 23:27:48 +00:00
|
|
|
void iothread_perform_impl(std::function<void()> &&, bool cant_wait = false);
|
2017-01-24 17:30:30 +00:00
|
|
|
|
2021-02-26 23:27:48 +00:00
|
|
|
// iothread_perform invokes a handler on a background thread.
|
2020-11-01 23:07:59 +00:00
|
|
|
inline void iothread_perform(std::function<void()> &&func) {
|
2021-02-26 23:27:48 +00:00
|
|
|
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) {
|
2021-02-26 23:27:48 +00:00
|
|
|
iothread_perform_impl(std::move(func), true);
|
2020-01-18 19:32:44 +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.
|
2021-02-26 23:27:48 +00:00
|
|
|
/// If the function executes, then \p completion will be invoked on the main thread, with the
|
|
|
|
/// result of the handler.
|
|
|
|
/// The result is a token which is only of interest to the tests.
|
2020-03-03 06:57:41 +00:00
|
|
|
template <typename Handler, typename Completion>
|
2021-02-26 23:27:48 +00:00
|
|
|
uint64_t perform(const Handler &handler, const Completion &completion) {
|
|
|
|
// Make a trampoline function which calls the handler, puts the result into a shared
|
|
|
|
// pointer, and then enqueues a completion.
|
|
|
|
auto trampoline = [=] {
|
|
|
|
using result_type_t = decltype(handler());
|
|
|
|
auto result = std::make_shared<result_type_t>(handler());
|
|
|
|
enqueue_main_thread_result([=] { completion(std::move(*result)); });
|
|
|
|
};
|
|
|
|
return perform(std::move(trampoline));
|
2020-03-03 06:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// One-argument form with no completion.
|
2021-02-26 23:27:48 +00:00
|
|
|
/// The result is a token which is only of interest to the tests.
|
2021-08-17 22:45:42 +00:00
|
|
|
uint64_t perform(std::function<void()> handler);
|
2020-03-03 06:57:41 +00:00
|
|
|
|
|
|
|
explicit debounce_t(long timeout_msec = 0);
|
|
|
|
~debounce_t();
|
|
|
|
|
|
|
|
private:
|
2021-02-26 23:27:48 +00:00
|
|
|
/// Helper to enqueue a function to run on the main thread.
|
|
|
|
static void enqueue_main_thread_result(std::function<void()> func);
|
2020-03-03 06:57:41 +00:00
|
|
|
|
|
|
|
const long timeout_msec_;
|
|
|
|
struct impl_t;
|
|
|
|
const std::shared_ptr<impl_t> impl_;
|
|
|
|
};
|
|
|
|
|
2011-12-27 05:21:12 +00:00
|
|
|
#endif
|