iomgr/timer.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_IOMGR_TIMER_H
20 #define GRPC_CORE_LIB_IOMGR_TIMER_H
21 
23 
24 #include <cstdint>
25 
27 #include <grpc/support/time.h>
28 
32 
33 typedef struct grpc_timer {
35  // Uninitialized if not using heap, or INVALID_HEAP_INDEX if not in heap.
37  bool pending;
38  struct grpc_timer* next;
39  struct grpc_timer* prev;
41 #ifndef NDEBUG
43 #endif
44 
45  // Optional field used by custom timers
46  union {
47  void* custom_timer;
49  };
50 } grpc_timer;
51 
53  "grpc_timer is expected to be a trivial type");
54 
55 typedef enum {
60 
61 typedef struct grpc_timer_vtable {
63  void (*cancel)(grpc_timer* timer);
64 
65  /* Internal API */
67  void (*list_init)();
68  void (*list_shutdown)(void);
69  void (*consume_kick)(void);
71 
72 /* Initialize *timer. When expired or canceled, closure will be called with
73  error set to indicate if it expired (GRPC_ERROR_NONE) or was canceled
74  (GRPC_ERROR_CANCELLED). *closure is guaranteed to be called exactly once, and
75  application code should check the error to determine how it was invoked. The
76  application callback is also responsible for maintaining information about
77  when to free up any user-level state. Behavior is undefined for a deadline of
78  grpc_core::Timestamp::InfFuture(). */
81 
82 /* Initialize *timer without setting it. This can later be passed through
83  the regular init or cancel */
85 
86 /* Note that there is no timer destroy function. This is because the
87  timer is a one-time occurrence with a guarantee that the callback will
88  be called exactly once, either at expiration or cancellation. Thus, all
89  the internal timer event management state is destroyed just before
90  that callback is invoked. If the user has additional state associated with
91  the timer, the user is responsible for determining when it is safe to
92  destroy that state. */
93 
94 /* Cancel an *timer.
95  There are three cases:
96  1. We normally cancel the timer
97  2. The timer has already run
98  3. We can't cancel the timer because it is "in flight".
99 
100  In all of these cases, the cancellation is still considered successful.
101  They are essentially distinguished in that the timer_cb will be run
102  exactly once from either the cancellation (with error GRPC_ERROR_CANCELLED)
103  or from the activation (with error GRPC_ERROR_NONE).
104 
105  Note carefully that the callback function MAY occur in the same callstack
106  as grpc_timer_cancel. It's expected that most timers will be cancelled (their
107  primary use is to implement deadlines), and so this code is optimized such
108  that cancellation costs as little as possible. Making callbacks run inline
109  matches this aim.
110 
111  Requires: cancel() must happen after init() on a given timer */
113 
114 /* iomgr internal api for dealing with timers */
115 
116 /* Check for timers to be run, and run them.
117  Return true if timer callbacks were executed.
118  If next is non-null, TRY to update *next with the next running timer
119  IF that timer occurs before *next current value.
120  *next is never guaranteed to be updated on any given execution; however,
121  with high probability at least one thread in the system will see an update
122  at any time slice. */
124 void grpc_timer_list_init();
126 
127 /* Consume a kick issued by grpc_kick_poller */
128 void grpc_timer_consume_kick(void);
129 
130 /* the following must be implemented by each iomgr implementation */
131 void grpc_kick_poller(void);
132 
133 /* Sets the timer implementation */
135 
136 #endif /* GRPC_CORE_LIB_IOMGR_TIMER_H */
grpc_set_timer_impl
void grpc_set_timer_impl(grpc_timer_vtable *vtable)
Definition: iomgr/timer.cc:27
iomgr.h
grpc_timer::next
struct grpc_timer * next
Definition: iomgr/timer.h:38
vtable
static const grpc_transport_vtable vtable
Definition: binder_transport.cc:680
grpc_timer::custom_timer
void * custom_timer
Definition: iomgr/timer.h:47
GRPC_TIMERS_NOT_CHECKED
@ GRPC_TIMERS_NOT_CHECKED
Definition: iomgr/timer.h:56
event_engine.h
grpc_timer_vtable
Definition: iomgr/timer.h:61
grpc_timer_list_shutdown
void grpc_timer_list_shutdown()
Definition: iomgr/timer.cc:44
grpc_timer_vtable::list_init
void(* list_init)()
Definition: iomgr/timer.h:67
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_timer::heap_index
uint32_t heap_index
Definition: iomgr/timer.h:36
grpc_timer_init
void grpc_timer_init(grpc_timer *timer, grpc_core::Timestamp deadline, grpc_closure *closure)
Definition: iomgr/timer.cc:31
time.h
grpc_timer
Definition: iomgr/timer.h:33
grpc_event_engine::experimental::EventEngine::TaskHandle
Definition: event_engine.h:102
grpc_timer_vtable::list_shutdown
void(* list_shutdown)(void)
Definition: iomgr/timer.h:68
GRPC_TIMERS_FIRED
@ GRPC_TIMERS_FIRED
Definition: iomgr/timer.h:58
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_timer_init_unset
void grpc_timer_init_unset(grpc_timer *timer)
Definition: timer_generic.cc:330
grpc_timer::deadline
int64_t deadline
Definition: iomgr/timer.h:34
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_timer_vtable::cancel
void(* cancel)(grpc_timer *timer)
Definition: iomgr/timer.h:63
grpc_timer::prev
struct grpc_timer * prev
Definition: iomgr/timer.h:39
Timestamp
struct Timestamp Timestamp
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:672
closure
grpc_closure closure
Definition: src/core/lib/surface/server.cc:466
grpc_timer_list_init
void grpc_timer_list_init()
Definition: iomgr/timer.cc:42
grpc_timer_vtable
struct grpc_timer_vtable grpc_timer_vtable
grpc_timer
struct grpc_timer grpc_timer
grpc_kick_poller
void grpc_kick_poller(void)
Definition: iomgr/timer_manager.cc:354
grpc_timer_vtable::init
void(* init)(grpc_timer *timer, grpc_core::Timestamp, grpc_closure *closure)
Definition: iomgr/timer.h:62
GRPC_TIMERS_CHECKED_AND_EMPTY
@ GRPC_TIMERS_CHECKED_AND_EMPTY
Definition: iomgr/timer.h:57
grpc_timer::pending
bool pending
Definition: iomgr/timer.h:37
grpc_timer_check
grpc_timer_check_result grpc_timer_check(grpc_core::Timestamp *next)
Definition: iomgr/timer.cc:38
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_timer::ee_task_handle
grpc_event_engine::experimental::EventEngine::TaskHandle ee_task_handle
Definition: iomgr/timer.h:48
port.h
grpc_timer::hash_table_next
struct grpc_timer * hash_table_next
Definition: iomgr/timer.h:42
grpc_timer_check_result
grpc_timer_check_result
Definition: iomgr/timer.h:55
grpc_timer::closure
grpc_closure * closure
Definition: iomgr/timer.h:40
grpc_timer_vtable::consume_kick
void(* consume_kick)(void)
Definition: iomgr/timer.h:69
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
exec_ctx.h
closure
Definition: proxy.cc:59
grpc_timer_cancel
void grpc_timer_cancel(grpc_timer *timer)
Definition: iomgr/timer.cc:36
grpc_timer_consume_kick
void grpc_timer_consume_kick(void)
Definition: iomgr/timer.cc:46
grpc_closure
Definition: closure.h:56
grpc_timer_vtable::check
grpc_timer_check_result(* check)(grpc_core::Timestamp *next)
Definition: iomgr/timer.h:66
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:38