Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | */ |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 16 | #ifndef ANDROID_HIDL_TASK_RUNNER_H |
| 17 | #define ANDROID_HIDL_TASK_RUNNER_H |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 18 | |
Benjamin Lerman | b8180e8 | 2023-03-03 17:51:49 +0100 | [diff] [blame] | 19 | #include <functional> |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 20 | #include <memory> |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 21 | #include <thread> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 25 | namespace details { |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 26 | |
Steven Moreland | c03f943 | 2017-08-16 14:17:49 -0700 | [diff] [blame] | 27 | using Task = std::function<void(void)>; |
| 28 | |
| 29 | template <typename T> |
| 30 | struct SynchronizedQueue; |
| 31 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 32 | /* |
| 33 | * A background infinite loop that runs the Tasks push()'ed. |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 34 | * Equivalent to a simple single-threaded Looper. |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 35 | */ |
| 36 | class TaskRunner { |
| 37 | public: |
| 38 | |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame] | 39 | /* Create an empty task runner. Nothing will be done until start() is called. */ |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 40 | TaskRunner(); |
| 41 | |
| 42 | /* |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 43 | * Notify the background thread to terminate and return immediately. |
| 44 | * Tasks in the queue will continue to be done sequentially in background |
| 45 | * until all tasks are finished. |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 46 | */ |
| 47 | ~TaskRunner(); |
| 48 | |
| 49 | /* |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame] | 50 | * Sets the queue limit. Fails the push operation once the limit is reached. |
Steven Moreland | c03f943 | 2017-08-16 14:17:49 -0700 | [diff] [blame] | 51 | * This function is named start for legacy reasons and to maintain ABI |
| 52 | * stability, but the underlying thread running tasks isn't started until |
| 53 | * the first task is pushed. |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame] | 54 | */ |
| 55 | void start(size_t limit); |
| 56 | |
| 57 | /* |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 58 | * Add a task. Return true if successful, false if |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 59 | * the queue's size exceeds limit or t doesn't contain a callable target. |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 60 | */ |
Steven Moreland | 0540d28 | 2017-05-08 16:03:11 -0700 | [diff] [blame] | 61 | bool push(const Task &t); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 62 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 63 | private: |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 64 | std::shared_ptr<SynchronizedQueue<Task>> mQueue; |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 67 | } // namespace details |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 68 | } // namespace hardware |
| 69 | } // namespace android |
| 70 | |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 71 | #endif // ANDROID_HIDL_TASK_RUNNER_H |