summaryrefslogtreecommitdiff
path: root/grpc/src/core/lib/iomgr/work_serializer.h
blob: 547b80c71d77c4f7e88f8230d61e3ca271584d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 *
 * Copyright 2019 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <grpc/support/port_platform.h>

#include <functional>

#include "absl/synchronization/mutex.h"

#include "src/core/lib/debug/trace.h"
#include "src/core/lib/gprpp/atomic.h"
#include "src/core/lib/gprpp/debug_location.h"
#include "src/core/lib/gprpp/mpscq.h"
#include "src/core/lib/gprpp/orphanable.h"
#include "src/core/lib/gprpp/ref_counted.h"
#include "src/core/lib/iomgr/exec_ctx.h"

#ifndef GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H
#define GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H

namespace grpc_core {

// WorkSerializer is a mechanism to schedule callbacks in a synchronized manner.
// All callbacks scheduled on a WorkSerializer instance will be executed
// serially in a borrowed thread. The API provides a FIFO guarantee to the
// execution of callbacks scheduled on the thread.
// When a thread calls Run() with a callback, the thread is considered borrowed.
// The callback might run inline, or it might run asynchronously in a different
// thread that is already inside of Run(). If the callback runs directly inline,
// other callbacks from other threads might also be executed before Run()
// returns. Since an arbitrary set of callbacks might be executed when Run() is
// called, generally no locks should be held while calling Run().
class ABSL_LOCKABLE WorkSerializer {
 public:
  WorkSerializer();

  ~WorkSerializer();

  // Runs a given callback.
  //
  // If you want to use clang thread annotation to make sure that callback is
  // called by WorkSerializer only, you need to add the annotation to both the
  // lambda function given to Run and the actual callback function like;
  //
  //   void run_callback() {
  //     work_serializer.Run(
  //         []() ABSL_EXCLUSIVE_LOCKS_REQUIRED(work_serializer) {
  //            callback();
  //         }, DEBUG_LOCATION);
  //   }
  //   void callback() ABSL_EXCLUSIVE_LOCKS_REQUIRED(work_serializer) { ... }
  //
  // TODO(yashkt): Replace grpc_core::DebugLocation with absl::SourceLocation
  // once we can start using it directly.
  void Run(std::function<void()> callback,
           const grpc_core::DebugLocation& location);

 private:
  class WorkSerializerImpl;

  OrphanablePtr<WorkSerializerImpl> impl_;
};

} /* namespace grpc_core */

#endif /* GRPC_CORE_LIB_IOMGR_WORK_SERIALIZER_H */