grpc
src
core
lib
event_engine
event_engine/promise.h
Go to the documentation of this file.
1
// Copyright 2021 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
#ifndef GRPC_CORE_LIB_EVENT_ENGINE_PROMISE_H
15
#define GRPC_CORE_LIB_EVENT_ENGINE_PROMISE_H
16
#include <
grpc/support/port_platform.h
>
17
18
#include <
grpc/support/log.h
>
19
20
#include "
src/core/lib/gprpp/sync.h
"
21
22
namespace
grpc_event_engine
{
23
namespace
experimental {
24
30
template
<
typename
T>
31
class
Promise
{
32
public
:
33
// The getter will wait until the setter has been called, and will return the
34
// value passed during Set.
35
T
&
Get
() {
36
grpc_core::MutexLock
lock(&
mu_
);
37
if
(!
set_
) {
38
cv_
.
Wait
(&
mu_
);
39
}
40
return
val_
;
41
}
42
// This setter can only be called exactly once without a Reset.
43
// Will automatically unblock getters.
44
void
Set
(
T
&& val) {
45
grpc_core::MutexLock
lock(&
mu_
);
46
GPR_ASSERT
(!
set_
);
47
val_
=
std::move
(val);
48
set_
=
true
;
49
cv_
.
SignalAll
();
50
}
51
52
// Can only be called after a set operation.
53
void
Reset
() {
54
grpc_core::MutexLock
lock(&
mu_
);
55
GPR_ASSERT
(
set_
);
56
set_
=
false
;
57
}
58
59
private
:
60
grpc_core::Mutex
mu_
;
61
grpc_core::CondVar
cv_
;
62
T
val_
;
63
bool
set_
=
false
;
64
};
65
66
}
// namespace experimental
67
}
// namespace grpc_event_engine
68
69
#endif // GRPC_CORE_LIB_EVENT_ENGINE_PROMISE_H
grpc_core::CondVar
Definition:
src/core/lib/gprpp/sync.h:126
log.h
grpc_core::MutexLock
Definition:
src/core/lib/gprpp/sync.h:88
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_event_engine::experimental::Promise::mu_
grpc_core::Mutex mu_
Definition:
event_engine/promise.h:60
grpc_event_engine::experimental::Promise::set_
bool set_
Definition:
event_engine/promise.h:63
grpc_core::CondVar::SignalAll
void SignalAll()
Definition:
src/core/lib/gprpp/sync.h:135
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition:
abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition:
include/grpc/impl/codegen/log.h:94
grpc_event_engine::experimental::Promise
Definition:
event_engine/promise.h:31
grpc_core::Mutex
Definition:
src/core/lib/gprpp/sync.h:61
grpc_event_engine::experimental::Promise::Get
T & Get()
Definition:
event_engine/promise.h:35
grpc_core::CondVar::Wait
void Wait(Mutex *mu)
Definition:
src/core/lib/gprpp/sync.h:137
grpc_event_engine
Definition:
endpoint_config.h:24
grpc_event_engine::experimental::Promise::Set
void Set(T &&val)
Definition:
event_engine/promise.h:44
grpc_event_engine::experimental::Promise::Reset
void Reset()
Definition:
event_engine/promise.h:53
grpc_event_engine::experimental::Promise::val_
T val_
Definition:
event_engine/promise.h:62
grpc_event_engine::experimental::Promise::cv_
grpc_core::CondVar cv_
Definition:
event_engine/promise.h:61
sync.h
port_platform.h
grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:46