L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
errand.h
1/*
2 * Copyright (C) 2014, 2020, 2024 Kernkonzept GmbH.
3 * Author(s): Sarah Hoffmann <sarah.hoffmann@kernkonzept.com>
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7#pragma once
8
9#include <l4/re/env.h>
10#include <l4/re/util/object_registry>
11#include <l4/re/util/br_manager>
12#include <l4/cxx/ipc_timeout_queue>
13#include <l4/cxx/ref_ptr>
14#include <l4/cxx/exceptions>
15#include <l4/libblock-device/debug.h>
16
17#include <functional>
18
19namespace Block_device { namespace Errand {
20
22extern L4::Ipc_svr::Server_iface *_sif;
23
27typedef std::function<void()> Callback;
28
34 public cxx::Ref_obj
35{
36public:
37 void expired() final
38 {
39 // Recapture the reference pointer from the timeout queue.
40 cxx::Ref_ptr<Poll_errand> p(this, false);
41
42 try
43 {
44 if (_poll())
45 _callback(true);
46 else
47 if (--_retries <= 0)
48 _callback(false);
49 else
50 reschedule();
51 }
52 catch (L4::Runtime_error const &e)
53 {
54 Err().printf("Polling task failed: %s\n", e.str());
55 }
56 }
57
58
59 void reschedule()
60 {
61 // create a place holder reference pointer for the timeout queue
63
64 _sif->add_timeout(p.release(), l4_kip_clock(l4re_kip()) + _interval);
65 }
66
67 // Class can only be instantiated as a reference counting object.
68 template< typename T, typename... Args >
69 friend
70 cxx::Ref_ptr<T> cxx::make_ref_obj(Args &&... args);
71
72private:
73 Poll_errand(int retries, int interval,
74 std::function<bool()> const &poll_func,
75 std::function<void(bool)> const &callback)
76 : _retries(retries),
77 _interval(interval),
78 _poll(poll_func),
79 _callback(callback)
80 {}
81
82 int _retries;
83 int _interval;
84 std::function<bool()> _poll;
85 std::function<void(bool)> _callback;
86};
87
98class Errand
100 public cxx::Ref_obj
101{
102public:
103 void expired() final
104 {
105 // Recapture the reference pointer from the timeout queue.
106 cxx::Ref_ptr<Errand> p(this, false);
107
108 if (_callback)
109 {
110 try
111 {
112 _callback();
113 }
114 catch (L4::Runtime_error const &e)
115 {
116 Err().printf("Asynchronous task failed: %s\n", e.str());
117 }
118 }
119 }
120
121 void reschedule(unsigned interval = 0)
122 {
123 // create a placeholder reference pointer for the timeout queue
124 cxx::Ref_ptr<Errand> p(this);
125
126 _sif->add_timeout(p.release(), l4_kip_clock(l4re_kip()) + interval);
127 }
128
129 // Class can only be instantiated as a reference counting object.
130 template< typename T, typename... Args >
131 friend
132 cxx::Ref_ptr<T> cxx::make_ref_obj(Args &&... args);
133
134private:
135 Errand(Callback const &callback) : _callback(callback) {}
136
137 Callback _callback;
138};
139
140struct Loop_hooks
141: L4::Ipc_svr::Timeout_queue_hooks<Loop_hooks, L4Re::Util::Br_manager>,
143{
144 l4_kernel_clock_t now() { return l4_kip_clock(l4re_kip()); }
145};
146
147using Errand_server = L4Re::Util::Registry_server<Loop_hooks>;
148
154inline void set_server_iface(L4::Ipc_svr::Server_iface *sif) { _sif = sif; }
155
166inline void schedule(Callback const &callback, int interval)
167{
168 cxx::make_ref_obj<Errand>(callback)->reschedule(interval);
169}
170
191inline void poll(int retries, int interval,
192 std::function<bool()> const &poll_func,
193 std::function<void(bool)> const &callback)
194{
195 if (poll_func())
196 callback(true);
197 else
198 cxx::make_ref_obj<Poll_errand>(retries, interval, poll_func,
199 callback)->reschedule();
200}
201
202
203} } // name space
Wrapper for a small task executed asynchronously in the server loop.
Definition errand.h:101
void expired() final
callback function to be called when timeout happened
Definition errand.h:103
Wrapper for a regularly repeated task.
Definition errand.h:35
void expired() final
callback function to be called when timeout happened
Definition errand.h:37
A server loop object which has a Object_registry included.
Interface for server-loop related functions.
Definition ipc_epiface:37
virtual int add_timeout(Timeout *timeout, l4_kernel_clock_t time)=0
Add a timeout to the server internal timeout queue.
Loop hooks mixin for integrating a timeout queue into the server loop.
Callback interface for Timeout_queue.
Exception for an abstract runtime error.
Definition exceptions:129
char const * str() const noexcept override
Return a human readable string for the exception.
Definition exceptions:154
A reference-counting pointer with automatic cleanup.
Definition ref_ptr:71
Environment interface.
Base exceptions.
l4_kernel_info_t const * l4re_kip(void) L4_NOTHROW
Get Kernel Info Page.
Definition env.h:184
l4_uint64_t l4_kernel_clock_t
Kernel clock type.
Definition l4int.h:53
l4_cpu_time_t l4_kip_clock(l4_kernel_info_t const *kip) L4_NOTHROW
Return clock value from the KIP.
Definition kip.h:200
Mix in for LOOP_HOOKS to ignore IPC errors.