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 | |
| 19 | #include "SynchronizedQueue.h" |
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 | |
| 27 | /* |
| 28 | * A background infinite loop that runs the Tasks push()'ed. |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 29 | * Equivalent to a simple single-threaded Looper. |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 30 | */ |
| 31 | class TaskRunner { |
| 32 | public: |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 33 | using Task = std::function<void(void)>; |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 34 | |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame^] | 35 | /* 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] | 36 | TaskRunner(); |
| 37 | |
| 38 | /* |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 39 | * Notify the background thread to terminate and return immediately. |
| 40 | * Tasks in the queue will continue to be done sequentially in background |
| 41 | * until all tasks are finished. |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 42 | */ |
| 43 | ~TaskRunner(); |
| 44 | |
| 45 | /* |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame^] | 46 | * Sets the queue limit. Fails the push operation once the limit is reached. |
| 47 | * Then kicks off the loop. |
| 48 | */ |
| 49 | void start(size_t limit); |
| 50 | |
| 51 | /* |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 52 | * Add a task. Return true if successful, false if |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 53 | * 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] | 54 | */ |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 55 | inline bool push(const Task &t) { |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame^] | 56 | return (mQueue != nullptr) && (!!t) && this->mQueue->push(t); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 59 | private: |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 60 | std::shared_ptr<SynchronizedQueue<Task>> mQueue; |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 63 | } // namespace details |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 64 | } // namespace hardware |
| 65 | } // namespace android |
| 66 | |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 67 | #endif // ANDROID_HIDL_TASK_RUNNER_H |