proxygen
SerialExecutor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
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 #pragma once
18 
19 #include <atomic>
20 #include <memory>
21 #include <mutex>
22 
26 
27 namespace folly {
28 
53  public:
54  SerialExecutor(SerialExecutor const&) = delete;
55  SerialExecutor& operator=(SerialExecutor const&) = delete;
56  SerialExecutor(SerialExecutor&&) = delete;
58 
61 
62  class Deleter {
63  public:
64  Deleter() {}
65 
67  executor->keepAliveRelease();
68  }
69 
70  private:
71  friend class SerialExecutor;
72  explicit Deleter(std::shared_ptr<Executor> parent)
73  : parent_(std::move(parent)) {}
74 
75  std::shared_ptr<Executor> parent_;
76  };
77 
78  using UniquePtr = std::unique_ptr<SerialExecutor, Deleter>;
79  [[deprecated("Replaced by create")]] static UniquePtr createUnique(
80  std::shared_ptr<Executor> parent = getCPUExecutor());
81 
85  void add(Func func) override;
86 
97  void addWithPriority(Func func, int8_t priority) override;
98  uint8_t getNumPriorities() const override {
99  return parent_->getNumPriorities();
100  }
101 
102  protected:
103  bool keepAliveAcquire() override;
104 
105  void keepAliveRelease() override;
106 
107  private:
109  ~SerialExecutor() override;
110 
111  void run();
112 
114  std::atomic<std::size_t> scheduled_{0};
120 
121  std::atomic<ssize_t> keepAliveCounter_{1};
122 };
123 
124 } // namespace folly
void keepAliveRelease() override
std::atomic< ssize_t > keepAliveCounter_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
static KeepAlive< SerialExecutor > create(KeepAlive< Executor > parent=getKeepAliveToken(getCPUExecutor().get()))
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
void operator()(SerialExecutor *executor)
bool keepAliveAcquire() override
folly::UnboundedQueue< Func, false, true, false > queue_
static KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:138
SerialExecutor(SerialExecutor const &)=delete
std::shared_ptr< Executor > getCPUExecutor()
void addWithPriority(Func func, int8_t priority) override
Executor that guarantees serial non-concurrent execution of added tasks.
std::shared_ptr< Executor > parent_
void add(Func func) override
Deleter(std::shared_ptr< Executor > parent)
std::atomic< std::size_t > scheduled_
static UniquePtr createUnique(std::shared_ptr< Executor > parent=getCPUExecutor())
uint8_t getNumPriorities() const override
std::unique_ptr< SerialExecutor, Deleter > UniquePtr
SerialExecutor & operator=(SerialExecutor const &)=delete
KeepAlive< Executor > parent_
folly::Function< void()> parent
Definition: AtFork.cpp:34