|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 <folly/fibers/FiberManager.h> |
| 20 | +#include <folly/fibers/FiberManagerMap.h> |
| 21 | +#include <folly/io/async/EventBase.h> |
| 22 | +#include <folly/io/async/ScopedEventBaseThread.h> |
| 23 | + |
| 24 | +#include <memory> |
| 25 | + |
| 26 | +namespace facebook { |
| 27 | +namespace cachelib { |
| 28 | +namespace navy { |
| 29 | + |
| 30 | +/** |
| 31 | + * NavyThread is a wrapper class that wraps folly::ScopedEventBaseThread and |
| 32 | + * FiberManager. The purpose of NavyThread is to start a new thread running |
| 33 | + * an EventBase loop and FiberManager loop along with providing an deligate |
| 34 | + * interface to add tasks to the FiberManager. |
| 35 | + * |
| 36 | + * NavyThread is not CopyConstructible nor CopyAssignable nor |
| 37 | + * MoveConstructible nor MoveAssignable. |
| 38 | + */ |
| 39 | +class NavyThread { |
| 40 | + public: |
| 41 | + /** |
| 42 | + * Initializes with current EventBaseManager and passed-in thread name. |
| 43 | + */ |
| 44 | + explicit NavyThread(folly::StringPiece name) |
| 45 | + : th_(name), fm_(&folly::fibers::getFiberManager(*th_.getEventBase())) {} |
| 46 | + |
| 47 | + ~NavyThread() = default; |
| 48 | + |
| 49 | + /** |
| 50 | + * Add the passed-in task to the FiberManager. |
| 51 | + * |
| 52 | + * @param func Task functor; must have a signature of `void func()`. |
| 53 | + * The object will be destroyed once task execution is complete. |
| 54 | + */ |
| 55 | + void addTaskRemote(folly::Func func) { fm_->addTaskRemote(std::move(func)); } |
| 56 | + |
| 57 | + /** |
| 58 | + * Add the passed-in task to the FiberManager. |
| 59 | + * Must be called from FiberManager's thread. |
| 60 | + * |
| 61 | + * @param func Task functor; must have a signature of `void func()`. |
| 62 | + * The object will be destroyed once task execution is complete. |
| 63 | + */ |
| 64 | + void addTask(folly::Func func) { fm_->addTask(std::move(func)); } |
| 65 | + |
| 66 | + private: |
| 67 | + NavyThread(NavyThread&& other) = delete; |
| 68 | + NavyThread& operator=(NavyThread&& other) = delete; |
| 69 | + |
| 70 | + NavyThread(const NavyThread& other) = delete; |
| 71 | + NavyThread& operator=(const NavyThread& other) = delete; |
| 72 | + |
| 73 | + // Actual worker thread running EventBase and FiberManager loop |
| 74 | + folly::ScopedEventBaseThread th_; |
| 75 | + |
| 76 | + // FiberManager which are driven by the thread |
| 77 | + folly::fibers::FiberManager* fm_; |
| 78 | +}; |
| 79 | + |
| 80 | +} // namespace navy |
| 81 | +} // namespace cachelib |
| 82 | +} // namespace facebook |
0 commit comments