proxygen
EventBaseManager.cpp
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 
18 
19 namespace folly {
20 
21 std::atomic<EventBaseManager*> globalManager(nullptr);
22 
25  if (mgr) {
26  return mgr;
27  }
28 
29  EventBaseManager* new_mgr = new EventBaseManager;
30  bool exchanged = globalManager.compare_exchange_strong(mgr, new_mgr);
31  if (!exchanged) {
32  delete new_mgr;
33  return mgr;
34  } else {
35  return new_mgr;
36  }
37 }
38 
39 /*
40  * EventBaseManager methods
41  */
42 
43 void EventBaseManager::setEventBase(EventBase* eventBase, bool takeOwnership) {
45  if (info != nullptr) {
46  throw std::runtime_error(
47  "EventBaseManager: cannot set a new EventBase "
48  "for this thread when one already exists");
49  }
50 
51  info = new EventBaseInfo(eventBase, takeOwnership);
52  localStore_.reset(info);
53  this->trackEventBase(eventBase);
54 }
55 
58  if (info != nullptr) {
59  this->untrackEventBase(info->eventBase);
60  this->localStore_.reset(nullptr);
61  }
62 }
63 
64 // XXX should this really be "const"?
66  // have one?
67  auto* info = localStore_.get();
68  if (!info) {
69  info = new EventBaseInfo();
70  localStore_.reset(info);
71 
72  if (observer_) {
73  info->eventBase->setObserver(observer_);
74  }
75 
76  // start tracking the event base
77  // XXX
78  // note: ugly cast because this does something mutable
79  // even though this method is defined as "const".
80  // Simply removing the const causes trouble all over fbcode;
81  // lots of services build a const EventBaseManager and errors
82  // abound when we make this non-const.
83  (const_cast<EventBaseManager*>(this))->trackEventBase(info->eventBase);
84  }
85 
86  return info->eventBase;
87 }
88 
89 } // namespace folly
def info()
Definition: deadlock.py:447
EventBase * getEventBase() const
void setEventBase(EventBase *eventBase, bool takeOwnership)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void untrackEventBase(EventBase *evb)
static EventBaseManager * get()
std::shared_ptr< folly::EventBaseObserver > observer_
std::atomic< EventBaseManager * > globalManager(nullptr)
folly::ThreadLocalPtr< EventBaseInfo > localStore_
void trackEventBase(EventBase *evb)