libfilezilla
aio.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_AIO_HEADER
2 #define LIBFILEZILLA_AIO_HEADER
3 
8 #include "../event.hpp"
9 #include "../mutex.hpp"
10 #include "../nonowning_buffer.hpp"
11 
12 #include <tuple>
13 
14 namespace fz {
15 
16 class aio_buffer_pool;
17 
24 class FZ_PUBLIC_SYMBOL buffer_lease final
25 {
26 public:
27  constexpr buffer_lease() noexcept = default;
28  ~buffer_lease() noexcept
29  {
30  release();
31  }
32 
33  buffer_lease(buffer_lease && op) noexcept;
34  buffer_lease& operator=(buffer_lease && op) noexcept;
35 
36  buffer_lease(buffer_lease const&) = delete;
37  buffer_lease& operator=(buffer_lease const&) = delete;
38 
39  explicit operator bool() const { return pool_ != nullptr; }
40 
41  // operator. would be nice
42  nonowning_buffer const* operator->() const { return &buffer_; }
43  nonowning_buffer* operator->() { return &buffer_; }
44  nonowning_buffer const& operator*() const { return buffer_; }
45  nonowning_buffer & operator*() { return buffer_; }
46 
47  void release();
48 
49  nonowning_buffer buffer_;
50 private:
51  friend class aio_buffer_pool;
52  FZ_PRIVATE_SYMBOL buffer_lease(nonowning_buffer b, aio_buffer_pool* pool)
53  : buffer_(b)
54  , pool_(pool)
55  {
56  }
57 
58  aio_buffer_pool* pool_{};
59 };
60 
61 class aio_waitable;
62 
68 class FZ_PUBLIC_SYMBOL aio_waiter
69 {
70 public:
71  virtual ~aio_waiter() = default;
72 
73 protected:
78  virtual void on_buffer_availability(aio_waitable const* w) = 0;
79 
80  friend class aio_waitable;
81 };
82 
83 class event_handler;
84 
88 class FZ_PUBLIC_SYMBOL aio_waitable
89 {
90 public:
91  virtual ~aio_waitable() = default;
92 
97  void remove_waiter(event_handler & h);
98 
99 protected:
100 
103 
104  void add_waiter(aio_waiter & h);
105  void add_waiter(event_handler & h);
106 
108  void signal_availibility();
109 
110 private:
111  bool FZ_PRIVATE_SYMBOL is_signalling(aio_waiter* w) const;
112 
113  mutex m_;
114  std::vector<aio_waiter*> waiting_;
115  std::vector<event_handler*> waiting_handlers_;
116  std::vector<aio_waiter*> active_signalling_;
117 };
118 
120 struct aio_buffer_event_type{};
121 
122 typedef simple_event<aio_buffer_event_type, aio_waitable const*> aio_buffer_event;
123 
124 class logger_interface;
125 
131 class FZ_PUBLIC_SYMBOL aio_buffer_pool final : public aio_waitable
132 {
133 public:
134  // If buffer_size is 0, it picks a suitable default
135 #if FZ_MAC
136  // On macOS, if using sandboxing, you need to pass an application group identifier.
137  aio_buffer_pool(logger_interface & logger, size_t buffer_count = 1, size_t buffer_size = 0, bool use_shm = false, std::string_view application_group_id = {});
138 #else
139  aio_buffer_pool(logger_interface & logger, size_t buffer_count = 1, size_t buffer_size = 0, bool use_shm = false);
140 #endif
141  ~aio_buffer_pool() noexcept;
142 
143  operator bool() const {
144  return memory_ != nullptr;
145  }
146 
156  buffer_lease get_buffer(event_handler & h);
157 
158  logger_interface & logger() const { return logger_; }
159 
160 #if FZ_WINDOWS
161  // A HANDLE
162  typedef void* shm_handle;
163  static shm_handle const shm_handle_default;
164 #else
165  // A file descriptor
166  typedef int shm_handle;
167  static shm_handle constexpr shm_handle_default{-1};
168 #endif
169 
189  std::tuple<shm_handle, uint8_t const*, size_t> shared_memory_info() const;
190 
191  size_t buffer_count() const { return buffer_count_; }
192 
193 private:
194  friend class buffer_lease;
195  void FZ_PRIVATE_SYMBOL release(nonowning_buffer && b);
196 
197  logger_interface & logger_;
198 
199  mutable mutex mtx_;
200 
201  uint64_t memory_size_{};
202  uint8_t* memory_{};
203 
204  std::vector<nonowning_buffer> buffers_;
205 
206  shm_handle shm_{shm_handle_default};
207 
208  size_t const buffer_count_{};
209 };
210 
212 enum class aio_result
213 {
215  ok,
216 
219  wait,
220 
222  error
223 };
224 
225 class FZ_PUBLIC_SYMBOL aio_base
226 {
227 public:
228  virtual ~aio_base() noexcept = default;
229 
230  using size_type = uint64_t;
231  static constexpr auto nosize = static_cast<size_type>(-1);
232 };
233 
234 }
235 
236 #endif
Definition: aio.hpp:226
A buffer pool for use with async readers/writers.
Definition: aio.hpp:132
std::tuple< shm_handle, uint8_t const *, size_t > shared_memory_info() const
buffer_lease get_buffer(aio_waiter &h)
Definition: aio.hpp:89
void remove_waiters()
Call in destructor of most-derived class.
void remove_waiter(aio_waiter &h)
Definition: aio.hpp:69
virtual void on_buffer_availability(aio_waitable const *w)=0
Definition: aio.hpp:25
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
Abstract interface for logging strings.
Definition: logger.hpp:51
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:52
Similar to fz::buffer, but does not own memory.
Definition: nonowning_buffer.hpp:23
The namespace used by libfilezilla.
Definition: apply.hpp:17
aio_result
Result of aio operations.
Definition: aio.hpp:213
@ ok
Success, proceed.
@ error
Operationf failed.