Lely core libraries  2.2.5
ctx.hpp
Go to the documentation of this file.
1 
24 #ifndef LELY_IO2_CTX_HPP_
25 #define LELY_IO2_CTX_HPP_
26 
27 #include <lely/io2/ctx.h>
28 #include <lely/util/error.hpp>
29 
30 #include <utility>
31 
32 namespace lely {
33 namespace io {
34 
39 enum class ForkEvent {
41  prepare = IO_FORK_PREPARE,
43  parent = IO_FORK_PARENT,
45  child = IO_FORK_CHILD
46 };
47 
49 class ContextBase {
50  public:
51  explicit ContextBase(io_ctx_t* ctx_) noexcept : ctx(ctx_) {}
52 
53  operator io_ctx_t*() const noexcept { return ctx; }
54 
56  void
57  insert(io_svc& svc) noexcept {
58  io_ctx_insert(*this, &svc);
59  }
60 
62  void
63  remove(io_svc& svc) noexcept {
64  io_ctx_remove(*this, &svc);
65  }
66 
68  void
69  notify_fork(ForkEvent e, ::std::error_code& ec) noexcept {
70  int errsv = get_errc();
71  set_errc(0);
72  if (!io_ctx_notify_fork(*this, static_cast<io_fork_event>(e)))
73  ec.clear();
74  else
75  ec = util::make_error_code();
76  set_errc(errsv);
77  }
78 
80  void
82  ::std::error_code ec;
83  notify_fork(e, ec);
84  if (ec) throw ::std::system_error(ec, "notify_fork");
85  }
86 
88  void
89  shutdown() noexcept {
90  io_ctx_shutdown(*this);
91  }
92 
93  protected:
94  io_ctx_t* ctx{nullptr};
95 };
96 
98 class Context : public ContextBase {
99  public:
102  if (!ctx) util::throw_errc("Context");
103  }
104 
105  Context(const Context&) = delete;
106 
107  Context(Context&& other) noexcept : ContextBase(other.ctx) {
108  other.ctx = nullptr;
109  }
110 
111  Context& operator=(const Context&) = delete;
112 
113  Context&
114  operator=(Context&& other) noexcept {
115  using ::std::swap;
116  swap(ctx, other.ctx);
117  return *this;
118  }
119 
121  ~Context() { io_ctx_destroy(*this); }
122 };
123 
124 } // namespace io
125 } // namespace lely
126 
127 #endif // !LELY_IO2_CTX_HPP_
void insert(io_svc &svc) noexcept
Definition: ctx.hpp:57
This header file is part of the I/O library; it contains the I/O context and service declarations...
A refence to an I/O context. This class is a wrapper around io_ctx_t*.
Definition: ctx.hpp:49
The event generated after the fork in the child process.
Definition: ctx.h:43
An I/O context.
Definition: ctx.hpp:98
Definition: ctx.c:35
void io_ctx_insert(io_ctx_t *ctx, struct io_svc *svc)
Registers an I/O service with an I/O context.
Definition: ctx.c:121
void io_ctx_shutdown(io_ctx_t *ctx)
Shuts down all registered I/O services in reverse order of registration.
Definition: ctx.c:195
void notify_fork(ForkEvent e)
Definition: ctx.hpp:81
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
Definition: errnum.c:957
void io_ctx_destroy(io_ctx_t *ctx)
Destroys an I/O context.
Definition: ctx.c:112
int io_ctx_notify_fork(io_ctx_t *ctx, enum io_fork_event e)
Notifies all registered I/O services of the specified fork event.
Definition: ctx.c:151
An I/O service.
Definition: ctx.h:49
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function...
Definition: errnum.c:947
void shutdown() noexcept
Definition: ctx.hpp:89
void io_ctx_remove(io_ctx_t *ctx, struct io_svc *svc)
Unregisters an I/O service with an I/O context.
Definition: ctx.c:136
This header file is part of the utilities library; it contains C++ convenience functions for creating...
void notify_fork(ForkEvent e, ::std::error_code &ec) noexcept
Definition: ctx.hpp:69
The event generated after the fork in the parent process.
Definition: ctx.h:41
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
ForkEvent
The type of event generated by an I/O context before and after a process fork.
Definition: ctx.hpp:39
Definition: buf.hpp:32
io_ctx_t * io_ctx_create(void)
Creates a new I/O context.
Definition: ctx.c:85
The event generated before the fork.
Definition: ctx.h:39