EventBaseThread.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2016-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. #include <folly/io/async/EventBaseThread.h>
  17. #include <folly/Memory.h>
  18. #include <folly/io/async/ScopedEventBaseThread.h>
  19. namespace folly {
  20. EventBaseThread::EventBaseThread() : EventBaseThread(true) {}
  21. EventBaseThread::EventBaseThread(
  22. bool autostart,
  23. EventBaseManager* ebm,
  24. folly::StringPiece threadName)
  25. : ebm_(ebm) {
  26. if (autostart) {
  27. start(threadName);
  28. }
  29. }
  30. EventBaseThread::EventBaseThread(EventBaseManager* ebm)
  31. : EventBaseThread(true, ebm) {}
  32. EventBaseThread::~EventBaseThread() = default;
  33. EventBaseThread::EventBaseThread(EventBaseThread&&) noexcept = default;
  34. EventBaseThread& EventBaseThread::operator=(EventBaseThread&&) noexcept =
  35. default;
  36. EventBase* EventBaseThread::getEventBase() const {
  37. return th_ ? th_->getEventBase() : nullptr;
  38. }
  39. bool EventBaseThread::running() const {
  40. return !!th_;
  41. }
  42. void EventBaseThread::start(folly::StringPiece threadName) {
  43. if (th_) {
  44. return;
  45. }
  46. th_ = std::make_unique<ScopedEventBaseThread>(ebm_, threadName);
  47. }
  48. void EventBaseThread::stop() {
  49. th_ = nullptr;
  50. }
  51. } // namespace folly