Debian Codegraph @

src/core/lib/gprpp/work_serializer.h

raw
1 //
2 // Copyright 2019 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #ifndef GRPC_CORE_LIB_GPRPP_WORK_SERIALIZER_H
18 #define GRPC_CORE_LIB_GPRPP_WORK_SERIALIZER_H
19
20 #include <grpc/support/port_platform.h>
21
22 #include <functional>
23
24 #include "absl/base/thread_annotations.h"
25
26 #include "src/core/lib/gprpp/debug_location.h"
27 #include "src/core/lib/gprpp/orphanable.h"
28
29 namespace grpc_core {
30
31 // WorkSerializer is a mechanism to schedule callbacks in a synchronized manner.
32 // All callbacks scheduled on a WorkSerializer instance will be executed
33 // serially in a borrowed thread. The API provides a FIFO guarantee to the
34 // execution of callbacks scheduled on the thread.
35 // When a thread calls Run() with a callback, the thread is considered borrowed.
36 // The callback might run inline, or it might run asynchronously in a different
37 // thread that is already inside of Run(). If the callback runs directly inline,
38 // other callbacks from other threads might also be executed before Run()
39 // returns. Since an arbitrary set of callbacks might be executed when Run() is
40 // called, generally no locks should be held while calling Run().
41 // If a thread wants to preclude the possibility of the callback being invoked
42 // inline in Run() (for example, if a mutex lock is held and executing callbacks
43 // inline would cause a deadlock), it should use Schedule() instead and then
44 // invoke DrainQueue() when it is safe to invoke the callback.
45 class ABSL_LOCKABLE WorkSerializer {
46 public:
47 WorkSerializer();
48
49 ~WorkSerializer();
50
51 // Runs a given callback on the work serializer. If there is no other thread
52 // currently executing the WorkSerializer, the callback is run immediately. In
53 // this case, the current thread is also borrowed for draining the queue for
54 // any callbacks that get added in the meantime.
55 //
56 // If you want to use clang thread annotation to make sure that callback is
57 // called by WorkSerializer only, you need to add the annotation to both the
58 // lambda function given to Run and the actual callback function like;
59 //
60 // void run_callback() {
61 // work_serializer.Run(
62 // []() ABSL_EXCLUSIVE_LOCKS_REQUIRED(work_serializer) {
63 // callback();
64 // }, DEBUG_LOCATION);
65 // }
66 // void callback() ABSL_EXCLUSIVE_LOCKS_REQUIRED(work_serializer) { ... }
67 //
68 // TODO(yashkt): Replace DebugLocation with absl::SourceLocation
69 // once we can start using it directly.
70 void Run(std::function<void()> callback, const DebugLocation& location);
71
72 // Schedule \a callback to be run later when the queue of callbacks is
73 // drained.
74 void Schedule(std::function<void()> callback, const DebugLocation& location);
75 // Drains the queue of callbacks.
76 void DrainQueue();
77
78 private:
79 class WorkSerializerImpl;
80
81 OrphanablePtr<WorkSerializerImpl> impl_;
82 };
83
84 } // namespace grpc_core
85
86 #endif // GRPC_CORE_LIB_GPRPP_WORK_SERIALIZER_H