work_serializer.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2019 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_WORK_SERIALIZER_H
20 #define GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H
22 
23 #include <atomic>
24 #include <functional>
25 
26 #include "absl/synchronization/mutex.h"
27 
34 
35 namespace grpc_core {
36 
37 // WorkSerializer is a mechanism to schedule callbacks in a synchronized manner.
38 // All callbacks scheduled on a WorkSerializer instance will be executed
39 // serially in a borrowed thread. The API provides a FIFO guarantee to the
40 // execution of callbacks scheduled on the thread.
41 // When a thread calls Run() with a callback, the thread is considered borrowed.
42 // The callback might run inline, or it might run asynchronously in a different
43 // thread that is already inside of Run(). If the callback runs directly inline,
44 // other callbacks from other threads might also be executed before Run()
45 // returns. Since an arbitrary set of callbacks might be executed when Run() is
46 // called, generally no locks should be held while calling Run().
47 // If a thread wants to preclude the possibility of the callback being invoked
48 // inline in Run() (for example, if a mutex lock is held and executing callbacks
49 // inline would cause a deadlock), it should use Schedule() instead and then
50 // invoke DrainQueue() when it is safe to invoke the callback.
52  public:
54 
55  ~WorkSerializer();
56 
57  // Runs a given callback on the work serializer. If there is no other thread
58  // currently executing the WorkSerializer, the callback is run immediately. In
59  // this case, the current thread is also borrowed for draining the queue for
60  // any callbacks that get added in the meantime.
61  //
62  // If you want to use clang thread annotation to make sure that callback is
63  // called by WorkSerializer only, you need to add the annotation to both the
64  // lambda function given to Run and the actual callback function like;
65  //
66  // void run_callback() {
67  // work_serializer.Run(
68  // []() ABSL_EXCLUSIVE_LOCKS_REQUIRED(work_serializer) {
69  // callback();
70  // }, DEBUG_LOCATION);
71  // }
72  // void callback() ABSL_EXCLUSIVE_LOCKS_REQUIRED(work_serializer) { ... }
73  //
74  // TODO(yashkt): Replace DebugLocation with absl::SourceLocation
75  // once we can start using it directly.
76  void Run(std::function<void()> callback, const DebugLocation& location);
77 
78  // Schedule \a callback to be run later when the queue of callbacks is
79  // drained.
80  void Schedule(std::function<void()> callback, const DebugLocation& location);
81  // Drains the queue of callbacks.
82  void DrainQueue();
83 
84  private:
86 
88 };
89 
90 } /* namespace grpc_core */
91 
92 #endif /* GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H */
trace.h
orphanable.h
mpscq.h
grpc_core::DebugLocation
Definition: debug_location.h:31
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::WorkSerializer
Definition: work_serializer.h:51
grpc_core::WorkSerializer::impl_
OrphanablePtr< WorkSerializerImpl > impl_
Definition: work_serializer.h:85
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
debug_location.h
ref_counted.h
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
exec_ctx.h
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: bloaty/third_party/googletest/googletest/test/googletest-break-on-failure-unittest.py:76
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_core::WorkSerializer::WorkSerializerImpl
Definition: work_serializer.cc:27
ABSL_LOCKABLE
#define ABSL_LOCKABLE
Definition: abseil-cpp/absl/base/thread_annotations.h:183
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:52