2 * Copyright 2016-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/executors/DrivableExecutor.h>
21 #include <folly/io/async/NotificationQueue.h>
25 class NotificationQueueExecutor : public folly::DrivableExecutor {
27 using Func = folly::Func;
29 void add(Func func) override {
30 queue_.putMessage(std::move(func));
34 return consumer_.getFd();
37 void drive() noexcept override {
39 while (queue_.tryConsume(func)) {
42 } catch (const std::exception& ex) {
43 LOG(ERROR) << "Exception thrown by NotificationQueueExecutor task."
44 << "Exception message: " << folly::exceptionStr(ex);
46 LOG(ERROR) << "Unknown Exception thrown "
47 << "by NotificationQueueExecutor task.";
53 folly::NotificationQueue<Func> queue_;
54 folly::NotificationQueue<Func>::SimpleConsumer consumer_{queue_};
55 }; // NotificationQueueExecutor