Fix decompression of truncated data
[folly.git] / folly / Executor.h
1 /*
2  * Copyright 2017 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 <climits>
20
21 #include <folly/Function.h>
22
23 namespace folly {
24
25 using Func = Function<void()>;
26
27 /// An Executor accepts units of work with add(), which should be
28 /// threadsafe.
29 class Executor {
30  public:
31   virtual ~Executor() = default;
32
33   /// Enqueue a function to executed by this executor. This and all
34   /// variants must be threadsafe.
35   virtual void add(Func) = 0;
36
37   /// Enqueue a function with a given priority, where 0 is the medium priority
38   /// This is up to the implementation to enforce
39   virtual void addWithPriority(Func, int8_t priority);
40
41   virtual uint8_t getNumPriorities() const {
42     return 1;
43   }
44
45   static const int8_t LO_PRI  = SCHAR_MIN;
46   static const int8_t MID_PRI = 0;
47   static const int8_t HI_PRI  = SCHAR_MAX;
48
49   /// A convenience function for shared_ptr to legacy functors.
50   ///
51   /// Sometimes you have a functor that is move-only, and therefore can't be
52   /// converted to a std::function (e.g. std::packaged_task). In that case,
53   /// wrap it in a shared_ptr (or maybe folly::MoveWrapper) and use this.
54   template <class P>
55   void addPtr(P fn) {
56     this->add([fn]() mutable { (*fn)(); });
57   }
58
59   class KeepAlive {
60    public:
61     KeepAlive() {}
62
63     void reset() {
64       executor_.reset();
65     }
66
67     explicit operator bool() const {
68       return executor_ != nullptr;
69     }
70
71    private:
72     friend class Executor;
73     explicit KeepAlive(folly::Executor* executor) : executor_(executor) {}
74
75     struct Deleter {
76       void operator()(folly::Executor* executor) {
77         executor->keepAliveRelease();
78       }
79     };
80     std::unique_ptr<folly::Executor, Deleter> executor_;
81   };
82
83   /// Returns a keep-alive token which guarantees that Executor will keep
84   /// processing tasks until the token is released. keep-alive token can only
85   /// be destroyed from within the task, scheduled to be run on an executor.
86   ///
87   /// If executor does not support keep-alive functionality - dummy token will
88   /// be returned.
89   virtual KeepAlive getKeepAliveToken() {
90     return {};
91   }
92
93  protected:
94   virtual void keepAliveRelease();
95
96   KeepAlive makeKeepAlive() {
97     return KeepAlive{this};
98   }
99 };
100
101 } // folly