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