2 * Copyright 2017-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <folly/ExceptionString.h>
19 #include <folly/Function.h>
20 #include <folly/futures/DrivableExecutor.h>
21 #include <folly/io/async/NotificationQueue.h>
26 class NotificationQueueExecutor : public folly::DrivableExecutor {
28 using Func = folly::Func;
30 void add(Func func) override {
31 queue_.putMessage(std::move(func));
35 return consumer_.getFd();
38 void drive() noexcept override {
40 while (queue_.tryConsume(func)) {
43 } catch (const std::exception& ex) {
44 LOG(ERROR) << "Exception thrown by NotificationQueueExecutor task."
45 << "Exception message: " << folly::exceptionStr(ex);
47 LOG(ERROR) << "Unknown Exception thrown "
48 << "by NotificationQueueExecutor task.";
54 folly::NotificationQueue<Func> queue_;
55 folly::NotificationQueue<Func>::SimpleConsumer consumer_{queue_};
56 }; // NotificationQueueExecutor