• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_CLOSURE_H
16 #define GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_CLOSURE_H
17 #include <grpc/event_engine/event_engine.h>
18 #include <grpc/support/port_platform.h>
19 
20 #include <utility>
21 
22 #include "absl/functional/any_invocable.h"
23 #include "absl/status/status.h"
24 
25 namespace grpc_event_engine {
26 namespace experimental {
27 
28 // The callbacks for Endpoint read and write take an absl::Status as
29 // argument - this is important for the tcp code to function correctly. We need
30 // a custom closure type because the default EventEngine::Closure type doesn't
31 // provide a way to pass a status when the callback is run.
32 class PosixEngineClosure final
33     : public grpc_event_engine::experimental::EventEngine::Closure {
34  public:
35   PosixEngineClosure() = default;
PosixEngineClosure(absl::AnyInvocable<void (absl::Status)> cb,bool is_permanent)36   PosixEngineClosure(absl::AnyInvocable<void(absl::Status)> cb,
37                      bool is_permanent)
38       : cb_(std::move(cb)),
39         is_permanent_(is_permanent),
40         status_(absl::OkStatus()) {}
41   ~PosixEngineClosure() final = default;
SetStatus(absl::Status status)42   void SetStatus(absl::Status status) { status_ = status; }
Run()43   void Run() override {
44     // We need to read the is_permanent_ variable before executing the
45     // enclosed callback. This is because a permanent closure may delete this
46     // object within the callback itself and thus reading this variable after
47     // the callback execution is not safe.
48     if (!is_permanent_) {
49       cb_(std::exchange(status_, absl::OkStatus()));
50       delete this;
51     } else {
52       cb_(std::exchange(status_, absl::OkStatus()));
53     }
54   }
55 
56   // This closure clean doesn't itself up after execution by default. The caller
57   // should take care if its lifetime.
ToPermanentClosure(absl::AnyInvocable<void (absl::Status)> cb)58   static PosixEngineClosure* ToPermanentClosure(
59       absl::AnyInvocable<void(absl::Status)> cb) {
60     return new PosixEngineClosure(std::move(cb), true);
61   }
62 
63   // This closure clean's itself up after execution. It is expected to be
64   // used only in tests.
TestOnlyToClosure(absl::AnyInvocable<void (absl::Status)> cb)65   static PosixEngineClosure* TestOnlyToClosure(
66       absl::AnyInvocable<void(absl::Status)> cb) {
67     return new PosixEngineClosure(std::move(cb), false);
68   }
69 
70  private:
71   absl::AnyInvocable<void(absl::Status)> cb_;
72   bool is_permanent_ = false;
73   absl::Status status_;
74 };
75 
76 }  // namespace experimental
77 }  // namespace grpc_event_engine
78 
79 #endif  // GRPC_SRC_CORE_LIB_EVENT_ENGINE_POSIX_ENGINE_POSIX_ENGINE_CLOSURE_H
80