Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
ez_rpc_kj_io.hpp
Go to the documentation of this file.
1/* Flow-IPC: Shared Memory
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
22#include <flow/util/shared_ptr_alias_holder.hpp>
23#include <kj/async-io.h>
24#include <boost/weak_ptr.hpp>
25
27{
28
29// Types.
30
31/**
32 * Internal-use thread-local singleton-style access to a `kj::AsyncIoContext` which, for each given thread,
33 * shall exist only as long as ref-counted handles to it (presumably held by `Ez_rpc_server`s and
34 * `Ez_rpc_client`s) remain outstanding.
35 *
36 * So just call this_thread_obj() and hold on to the returned `shared_ptr` handle, accessing
37 * `->m_kj_io` off it -- from that same thread only -- and let the ref-counted group expire once no longer needed.
38 *
39 * ### Impl notes ###
40 * While the concept of what is happening here I lifted from `capnp::EzRpc*` internals, I (ygoldfel) did it
41 * it in the vanilla C++1x way: use `weak_ptr` (instead of the KJ way: `kj::Own` and `kj::Refcounted`).
42 * Plus I removed a couple wordy things such as the accessors (`public` access to #m_kj_io seems fine, in
43 * this internal-use situation).
44 */
46 public flow::util::Shared_ptr_alias_holder<boost::shared_ptr<Ez_rpc_kj_io>>,
47 private boost::noncopyable
48{
49public:
50 // Methods.
51
52 /**
53 * Returns a handle to a `*this` which shall contain a live `AsyncIoContext` #m_kj_io; it will continue to
54 * be valid, as long as the returned handle is only touched from this thread and is kept alive.
55 *
56 * @return See above. Not null.
57 */
58 static Ptr this_thread_obj();
59
60 // Data.
61
62 /// The thread-local event loop/context object.
63 kj::AsyncIoContext m_kj_io;
64
65private:
66 // Constructors.
67
68 /// Private ctor that sets #m_kj_io to its permanent value, `kj::setupAsyncIo()`.
70
71 // Data.
72
73 /**
74 * Weak-pointer observer to the last-retured in this thread (via this_thread_obj()) `Ptr`; or null.
75 * Effectively it is in one of two states:
76 * - If `s_this_thread_obj_observer.lock() == nullptr`: this_thread_obj() has not been called; or it has been
77 * called, but the returned `Ptr` group has become empty (no references remain).
78 * - Else: The `Ptr` group returned by the last this_thread_obj() is alive.
79 */
80 static thread_local boost::weak_ptr<Ez_rpc_kj_io> s_this_thread_obj_observer;
81}; // class Ez_rpc_kj_io
82
83} // namespace ipc::transport::struc::shm::rpc
Internal-use thread-local singleton-style access to a kj::AsyncIoContext which, for each given thread...
Ez_rpc_kj_io()
Private ctor that sets m_kj_io to its permanent value, kj::setupAsyncIo().
kj::AsyncIoContext m_kj_io
The thread-local event loop/context object.
static Ptr this_thread_obj()
Returns a handle to a *this which shall contain a live AsyncIoContext m_kj_io; it will continue to be...
static thread_local boost::weak_ptr< Ez_rpc_kj_io > s_this_thread_obj_observer
Weak-pointer observer to the last-retured in this thread (via this_thread_obj()) Ptr; or null.
Provides Flow-IPC's integration with Cap'n Proto's Remote Procedure Call (RPC) layer,...