proxygen
EventBaseManager.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-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 <list>
20 #include <set>
21 
22 #include <folly/ThreadLocal.h>
24 
25 namespace folly {
26 
37  public:
38  // XXX Constructing a EventBaseManager directly is DEPRECATED and not
39  // encouraged. You should instead use the global singleton if possible.
41 
43 
44  explicit EventBaseManager(const std::shared_ptr<EventBaseObserver>& observer)
45  : observer_(observer) {}
46 
52  static EventBaseManager* get();
53 
60  EventBase* getEventBase() const;
61 
69  if (info == nullptr) {
70  return nullptr;
71  }
72  return info->eventBase;
73  }
74 
87  void setEventBase(EventBase* eventBase, bool takeOwnership);
88 
95  void clearEventBase();
96 
102  template <typename FunctionType>
103  void withEventBaseSet(const FunctionType& runnable) {
104  // grab the mutex for the caller
105  std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
106  // give them only a const set to work with
107  const std::set<EventBase*>& constSet = eventBaseSet_;
108  runnable(constSet);
109  }
110 
111  private:
112  struct EventBaseInfo {
113  EventBaseInfo(EventBase* evb, bool owned) : eventBase(evb), owned_(owned) {}
114 
115  EventBaseInfo() : eventBase(new EventBase), owned_(true) {}
116 
118  bool owned_;
120  if (owned_) {
121  delete eventBase;
122  }
123  }
124  };
125 
126  // Forbidden copy constructor and assignment opererator
129 
131  std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
132  eventBaseSet_.insert(evb);
133  }
134 
136  std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
137  eventBaseSet_.erase(evb);
138  }
139 
141 
142  // set of "active" EventBase instances
143  // (also see the mutex "eventBaseSetMutex_" below
144  // which governs access to this).
145  mutable std::set<EventBase*> eventBaseSet_;
146 
147  // a mutex to use as a guard for the above set
149 
150  std::shared_ptr<folly::EventBaseObserver> observer_;
151 };
152 
153 } // namespace folly
def info()
Definition: deadlock.py:447
EventBase * getEventBase() const
EventBaseInfo(EventBase *evb, bool owned)
EventBase * getExistingEventBase() const
void setEventBase(EventBase *eventBase, bool takeOwnership)
void withEventBaseSet(const FunctionType &runnable)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void untrackEventBase(EventBase *evb)
std::shared_ptr< folly::EventBaseObserver > observer_
EventBaseManager & operator=(EventBaseManager const &)
folly::ThreadLocalPtr< EventBaseInfo > localStore_
EventBaseManager(const std::shared_ptr< EventBaseObserver > &observer)
std::mutex mutex
g_t g(f_t)
std::set< EventBase * > eventBaseSet_
void trackEventBase(EventBase *evb)