2017
[folly.git] / folly / experimental / observer / detail / Core.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 #pragma once
17
18 #include <folly/Function.h>
19 #include <folly/Synchronized.h>
20 #include <folly/futures/Future.h>
21
22 #include <atomic>
23 #include <memory>
24 #include <mutex>
25 #include <unordered_set>
26 #include <utility>
27 #include <vector>
28
29 namespace folly {
30 namespace observer_detail {
31
32 class ObserverManager;
33
34 /**
35  * Core stores the current version of the object held by Observer. It also keeps
36  * all dependencies and dependents of the Observer.
37  */
38 class Core : public std::enable_shared_from_this<Core> {
39  public:
40   using Ptr = std::shared_ptr<Core>;
41   using WeakPtr = std::weak_ptr<Core>;
42
43   /**
44    * Blocks until creator is successfully run by ObserverManager
45    */
46   static Ptr create(folly::Function<std::shared_ptr<const void>()> creator);
47
48   /**
49    * View of the observed object and its version
50    */
51   struct VersionedData {
52     VersionedData() {}
53
54     VersionedData(std::shared_ptr<const void> data_, size_t version_)
55         : data(std::move(data_)), version(version_) {}
56
57     std::shared_ptr<const void> data;
58     size_t version{0};
59   };
60
61   /**
62    * Gets current view of the observed object.
63    * This is safe to call from any thread. If this is called from other Observer
64    * functor then that Observer is marked as dependent on current Observer.
65    */
66   VersionedData getData();
67
68   /**
69    * Gets the version of the observed object.
70    */
71   size_t getVersion() const {
72     return version_;
73   }
74
75   /**
76    * Get the last version at which the observed object was actually changed.
77    */
78   size_t getVersionLastChange() {
79     return versionLastChange_;
80   }
81
82   /**
83    * Check if the observed object needs to be re-computed. Returns the version
84    * of last change. If force is true, re-computes the observed object, even if
85    * dependencies didn't change.
86    *
87    * This should be only called from ObserverManager thread.
88    */
89   size_t refresh(size_t version, bool force = false);
90
91   ~Core();
92
93  private:
94   explicit Core(folly::Function<std::shared_ptr<const void>()> creator);
95
96   void addDependent(Core::WeakPtr dependent);
97   void removeStaleDependents();
98
99   using Dependents = std::vector<WeakPtr>;
100   using Dependencies = std::unordered_set<Ptr>;
101
102   folly::Synchronized<Dependents> dependents_;
103   folly::Synchronized<Dependencies> dependencies_;
104
105   std::atomic<size_t> version_{0};
106   std::atomic<size_t> versionLastChange_{0};
107
108   folly::Synchronized<VersionedData> data_;
109
110   folly::Function<std::shared_ptr<const void>()> creator_;
111
112   std::mutex refreshMutex_;
113 };
114 }
115 }