proxygen
folly::EventBaseManager Class Reference

#include <EventBaseManager.h>

Classes

struct  EventBaseInfo
 

Public Member Functions

 EventBaseManager ()
 
 ~EventBaseManager ()
 
 EventBaseManager (const std::shared_ptr< EventBaseObserver > &observer)
 
EventBasegetEventBase () const
 
EventBasegetExistingEventBase () const
 
void setEventBase (EventBase *eventBase, bool takeOwnership)
 
void clearEventBase ()
 
template<typename FunctionType >
void withEventBaseSet (const FunctionType &runnable)
 

Static Public Member Functions

static EventBaseManagerget ()
 

Private Member Functions

 EventBaseManager (EventBaseManager const &)
 
EventBaseManageroperator= (EventBaseManager const &)
 
void trackEventBase (EventBase *evb)
 
void untrackEventBase (EventBase *evb)
 

Private Attributes

folly::ThreadLocalPtr< EventBaseInfolocalStore_
 
std::set< EventBase * > eventBaseSet_
 
std::mutex eventBaseSetMutex_
 
std::shared_ptr< folly::EventBaseObserverobserver_
 

Detailed Description

Manager for per-thread EventBase objects. This class will find or create a EventBase for the current thread, associated with thread-specific storage for that thread. Although a typical application will generally only have one EventBaseManager, there is no restriction on multiple instances; the EventBases belong to one instance are isolated from those of another.

Definition at line 36 of file EventBaseManager.h.

Constructor & Destructor Documentation

folly::EventBaseManager::EventBaseManager ( )
inline

Definition at line 40 of file EventBaseManager.h.

Referenced by get(), and folly::EventBaseManager::EventBaseInfo::~EventBaseInfo().

40 {}
folly::EventBaseManager::~EventBaseManager ( )
inline

Definition at line 42 of file EventBaseManager.h.

42 {}
folly::EventBaseManager::EventBaseManager ( const std::shared_ptr< EventBaseObserver > &  observer)
inlineexplicit

Definition at line 44 of file EventBaseManager.h.

References getEventBase().

45  : observer_(observer) {}
std::shared_ptr< folly::EventBaseObserver > observer_
folly::EventBaseManager::EventBaseManager ( EventBaseManager const &  )
private

Member Function Documentation

void folly::EventBaseManager::clearEventBase ( )

Clear the EventBase for this thread.

This can be used if the code driving the EventBase loop() has finished the loop and new events should no longer be added to the EventBase.

Definition at line 56 of file EventBaseManager.cpp.

References folly::EventBaseManager::EventBaseInfo::eventBase, deadlock::info(), localStore_, and untrackEventBase().

Referenced by proxygen::WorkerThread::cleanup(), getExistingEventBase(), folly::run(), HTTPDownstreamTest< SPDY3_1CodecPair >::SetUp(), and folly::IOThreadPoolExecutor::threadRun().

56  {
57  EventBaseInfo* info = localStore_.get();
58  if (info != nullptr) {
59  this->untrackEventBase(info->eventBase);
60  this->localStore_.reset(nullptr);
61  }
62 }
def info()
Definition: deadlock.py:447
void untrackEventBase(EventBase *evb)
folly::ThreadLocalPtr< EventBaseInfo > localStore_
EventBaseManager * folly::EventBaseManager::get ( )
static

Get the global EventBaseManager for this program. Ideally all users of EventBaseManager go through this interface and do not construct EventBaseManager directly.

Definition at line 23 of file EventBaseManager.cpp.

References EventBaseManager(), and folly::globalManager().

Referenced by wangle::ServerBootstrap< DefaultPipeline >::bind(), wangle::ClientBootstrap< DefaultPipeline >::connect(), TestClientPipelineFactory::newPipeline(), wangle::AsyncServerSocketFactory::newSocket(), wangle::AsyncUDPServerSocketFactory::newSocket(), StaticService::StaticHandler::onEgressResumed(), StaticService::StaticHandler::onRequest(), ProxyService::ProxyHandler::onRequest(), HTTPDownstreamTest< SPDY3_1CodecPair >::SetUp(), TEST(), wangle::TEST(), TEST_F(), and proxygen::WheelTimerInstance::WheelTimerInstance().

23  {
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 }
std::atomic< EventBaseManager * > globalManager(nullptr)
EventBase * folly::EventBaseManager::getEventBase ( ) const

Get the EventBase for this thread, or create one if none exists yet.

If no EventBase exists for this thread yet, a new one will be created and returned. May throw std::bad_alloc if allocation fails.

Definition at line 65 of file EventBaseManager.cpp.

References deadlock::info(), localStore_, and observer_.

Referenced by wangle::ClientBootstrap< DefaultPipeline >::connect(), EventBaseManager(), TestClientPipelineFactory::newPipeline(), wangle::AsyncServerSocketFactory::newSocket(), wangle::AsyncUDPServerSocketFactory::newSocket(), ProxyService::ProxyHandler::onRequest(), TEST(), wangle::TEST(), TEST_F(), folly::IOThreadPoolExecutor::threadRun(), and proxygen::WheelTimerInstance::WheelTimerInstance().

65  {
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 }
def info()
Definition: deadlock.py:447
std::shared_ptr< folly::EventBaseObserver > observer_
folly::ThreadLocalPtr< EventBaseInfo > localStore_
EventBase* folly::EventBaseManager::getExistingEventBase ( ) const
inline

Get the EventBase for this thread.

Returns nullptr if no EventBase has been created for this thread yet.

Definition at line 67 of file EventBaseManager.h.

References clearEventBase(), folly::EventBaseManager::EventBaseInfo::eventBase, deadlock::info(), localStore_, and setEventBase().

67  {
68  EventBaseInfo* info = localStore_.get();
69  if (info == nullptr) {
70  return nullptr;
71  }
72  return info->eventBase;
73  }
def info()
Definition: deadlock.py:447
folly::ThreadLocalPtr< EventBaseInfo > localStore_
EventBaseManager& folly::EventBaseManager::operator= ( EventBaseManager const &  )
private
void folly::EventBaseManager::setEventBase ( EventBase eventBase,
bool  takeOwnership 
)

Set the EventBase to be used by this thread.

This may only be called if no EventBase has been defined for this thread yet. If a EventBase is already defined for this thread, a std::runtime_error is thrown. std::bad_alloc may also be thrown if allocation fails while setting the EventBase.

This should typically be invoked by the code that will call loop() on the EventBase, to make sure the EventBaseManager points to the correct EventBase that is actually running in this thread.

Definition at line 43 of file EventBaseManager.cpp.

References deadlock::info(), localStore_, and trackEventBase().

Referenced by getExistingEventBase(), folly::run(), proxygen::WorkerThread::setup(), and TEST_F().

43  {
44  EventBaseInfo* info = localStore_.get();
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 }
def info()
Definition: deadlock.py:447
folly::ThreadLocalPtr< EventBaseInfo > localStore_
void trackEventBase(EventBase *evb)
void folly::EventBaseManager::trackEventBase ( EventBase evb)
inlineprivate

Definition at line 130 of file EventBaseManager.h.

References eventBaseSet_, eventBaseSetMutex_, and g().

Referenced by setEventBase().

130  {
131  std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
132  eventBaseSet_.insert(evb);
133  }
g_t g(f_t)
std::set< EventBase * > eventBaseSet_
void folly::EventBaseManager::untrackEventBase ( EventBase evb)
inlineprivate

Definition at line 135 of file EventBaseManager.h.

References eventBaseSet_, eventBaseSetMutex_, and g().

Referenced by clearEventBase().

135  {
136  std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
137  eventBaseSet_.erase(evb);
138  }
g_t g(f_t)
std::set< EventBase * > eventBaseSet_
template<typename FunctionType >
void folly::EventBaseManager::withEventBaseSet ( const FunctionType &  runnable)
inline

Gives the caller all references to all assigned EventBase instances at this moment in time. Locks a mutex so that these EventBase set cannot be changed, and also the caller can rely on no instances being destructed.

Definition at line 103 of file EventBaseManager.h.

References eventBaseSet_, eventBaseSetMutex_, and g().

103  {
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  }
g_t g(f_t)
std::set< EventBase * > eventBaseSet_

Member Data Documentation

std::set<EventBase*> folly::EventBaseManager::eventBaseSet_
mutableprivate

Definition at line 145 of file EventBaseManager.h.

Referenced by trackEventBase(), untrackEventBase(), and withEventBaseSet().

std::mutex folly::EventBaseManager::eventBaseSetMutex_
private

Definition at line 148 of file EventBaseManager.h.

Referenced by trackEventBase(), untrackEventBase(), and withEventBaseSet().

folly::ThreadLocalPtr<EventBaseInfo> folly::EventBaseManager::localStore_
mutableprivate
std::shared_ptr<folly::EventBaseObserver> folly::EventBaseManager::observer_
private

Definition at line 150 of file EventBaseManager.h.

Referenced by getEventBase().


The documentation for this class was generated from the following files: