EventBaseManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #pragma once
  17. #include <list>
  18. #include <set>
  19. #include <folly/ThreadLocal.h>
  20. #include <folly/io/async/EventBase.h>
  21. namespace folly {
  22. /**
  23. * Manager for per-thread EventBase objects.
  24. * This class will find or create a EventBase for the current
  25. * thread, associated with thread-specific storage for that thread.
  26. * Although a typical application will generally only have one
  27. * EventBaseManager, there is no restriction on multiple instances;
  28. * the EventBases belong to one instance are isolated from those of
  29. * another.
  30. */
  31. class EventBaseManager {
  32. public:
  33. // XXX Constructing a EventBaseManager directly is DEPRECATED and not
  34. // encouraged. You should instead use the global singleton if possible.
  35. EventBaseManager() {}
  36. ~EventBaseManager() {}
  37. explicit EventBaseManager(const std::shared_ptr<EventBaseObserver>& observer)
  38. : observer_(observer) {}
  39. /**
  40. * Get the global EventBaseManager for this program. Ideally all users
  41. * of EventBaseManager go through this interface and do not construct
  42. * EventBaseManager directly.
  43. */
  44. static EventBaseManager* get();
  45. /**
  46. * Get the EventBase for this thread, or create one if none exists yet.
  47. *
  48. * If no EventBase exists for this thread yet, a new one will be created and
  49. * returned. May throw std::bad_alloc if allocation fails.
  50. */
  51. EventBase* getEventBase() const;
  52. /**
  53. * Get the EventBase for this thread.
  54. *
  55. * Returns nullptr if no EventBase has been created for this thread yet.
  56. */
  57. EventBase* getExistingEventBase() const {
  58. EventBaseInfo* info = localStore_.get();
  59. if (info == nullptr) {
  60. return nullptr;
  61. }
  62. return info->eventBase;
  63. }
  64. /**
  65. * Set the EventBase to be used by this thread.
  66. *
  67. * This may only be called if no EventBase has been defined for this thread
  68. * yet. If a EventBase is already defined for this thread, a
  69. * std::runtime_error is thrown. std::bad_alloc may also be thrown if
  70. * allocation fails while setting the EventBase.
  71. *
  72. * This should typically be invoked by the code that will call loop() on the
  73. * EventBase, to make sure the EventBaseManager points to the correct
  74. * EventBase that is actually running in this thread.
  75. */
  76. void setEventBase(EventBase* eventBase, bool takeOwnership);
  77. /**
  78. * Clear the EventBase for this thread.
  79. *
  80. * This can be used if the code driving the EventBase loop() has finished
  81. * the loop and new events should no longer be added to the EventBase.
  82. */
  83. void clearEventBase();
  84. /**
  85. * Gives the caller all references to all assigned EventBase instances at
  86. * this moment in time. Locks a mutex so that these EventBase set cannot
  87. * be changed, and also the caller can rely on no instances being destructed.
  88. */
  89. template <typename FunctionType>
  90. void withEventBaseSet(const FunctionType& runnable) {
  91. // grab the mutex for the caller
  92. std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
  93. // give them only a const set to work with
  94. const std::set<EventBase*>& constSet = eventBaseSet_;
  95. runnable(constSet);
  96. }
  97. private:
  98. struct EventBaseInfo {
  99. EventBaseInfo(EventBase* evb, bool owned) : eventBase(evb), owned_(owned) {}
  100. EventBaseInfo() : eventBase(new EventBase), owned_(true) {}
  101. EventBase* eventBase;
  102. bool owned_;
  103. ~EventBaseInfo() {
  104. if (owned_) {
  105. delete eventBase;
  106. }
  107. }
  108. };
  109. // Forbidden copy constructor and assignment opererator
  110. EventBaseManager(EventBaseManager const&);
  111. EventBaseManager& operator=(EventBaseManager const&);
  112. void trackEventBase(EventBase* evb) {
  113. std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
  114. eventBaseSet_.insert(evb);
  115. }
  116. void untrackEventBase(EventBase* evb) {
  117. std::lock_guard<std::mutex> g(*&eventBaseSetMutex_);
  118. eventBaseSet_.erase(evb);
  119. }
  120. mutable folly::ThreadLocalPtr<EventBaseInfo> localStore_;
  121. // set of "active" EventBase instances
  122. // (also see the mutex "eventBaseSetMutex_" below
  123. // which governs access to this).
  124. mutable std::set<EventBase*> eventBaseSet_;
  125. // a mutex to use as a guard for the above set
  126. std::mutex eventBaseSetMutex_;
  127. std::shared_ptr<folly::EventBaseObserver> observer_;
  128. };
  129. } // namespace folly