All template params for PriorityMPMCQueue
[folly.git] / folly / python / NotificationQueueExecutor.h
1 /*
2  * Copyright 2017-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
18 #include <folly/ExceptionString.h>
19 #include <folly/Function.h>
20 #include <folly/futures/DrivableExecutor.h>
21 #include <folly/io/async/NotificationQueue.h>
22
23 namespace folly {
24 namespace python {
25
26 class NotificationQueueExecutor : public folly::DrivableExecutor {
27  public:
28   using Func = folly::Func;
29
30   void add(Func func) override {
31     queue_.putMessage(std::move(func));
32   }
33
34   int fileno() const {
35     return consumer_.getFd();
36   }
37
38   void drive() noexcept override {
39     Func func;
40     while (queue_.tryConsume(func)) {
41       try {
42         func();
43       } catch (const std::exception& ex) {
44         LOG(ERROR) << "Exception thrown by NotificationQueueExecutor task."
45                    << "Exception message: " << folly::exceptionStr(ex);
46       } catch (...) {
47         LOG(ERROR) << "Unknown Exception thrown "
48                    << "by NotificationQueueExecutor task.";
49       }
50     }
51   }
52
53  private:
54   folly::NotificationQueue<Func> queue_;
55   folly::NotificationQueue<Func>::SimpleConsumer consumer_{queue_};
56 }; // NotificationQueueExecutor
57
58 } // python
59 } // folly