blob: 6a79ebf544f35d19f47e2d04e04277992bb3b733 [file] [log] [blame]
Yifan Hong5e2318c2016-10-27 17:19:21 -07001/*
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 Hong0a351392017-03-20 17:17:52 -070016#ifndef ANDROID_HIDL_TASK_RUNNER_H
17#define ANDROID_HIDL_TASK_RUNNER_H
Yifan Hong5e2318c2016-10-27 17:19:21 -070018
Yifan Hong8184c122017-03-20 18:26:43 -070019#include <memory>
Yifan Hong5e2318c2016-10-27 17:19:21 -070020#include <thread>
21
22namespace android {
23namespace hardware {
Yifan Hong0a351392017-03-20 17:17:52 -070024namespace details {
Yifan Hong5e2318c2016-10-27 17:19:21 -070025
Steven Morelandc03f9432017-08-16 14:17:49 -070026using Task = std::function<void(void)>;
27
28template <typename T>
29struct SynchronizedQueue;
30
Yifan Hong5e2318c2016-10-27 17:19:21 -070031/*
32 * A background infinite loop that runs the Tasks push()'ed.
Yifan Hong8184c122017-03-20 18:26:43 -070033 * Equivalent to a simple single-threaded Looper.
Yifan Hong5e2318c2016-10-27 17:19:21 -070034 */
35class TaskRunner {
36public:
37
Yifan Hong6f667542017-03-20 19:04:05 -070038 /* Create an empty task runner. Nothing will be done until start() is called. */
Yifan Hong5e2318c2016-10-27 17:19:21 -070039 TaskRunner();
40
41 /*
Yifan Hong8184c122017-03-20 18:26:43 -070042 * 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 Hong5e2318c2016-10-27 17:19:21 -070045 */
46 ~TaskRunner();
47
48 /*
Yifan Hong6f667542017-03-20 19:04:05 -070049 * Sets the queue limit. Fails the push operation once the limit is reached.
Steven Morelandc03f9432017-08-16 14:17:49 -070050 * 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 Hong6f667542017-03-20 19:04:05 -070053 */
54 void start(size_t limit);
55
56 /*
Yifan Hong5e2318c2016-10-27 17:19:21 -070057 * Add a task. Return true if successful, false if
Yifan Hong8184c122017-03-20 18:26:43 -070058 * the queue's size exceeds limit or t doesn't contain a callable target.
Yifan Hong5e2318c2016-10-27 17:19:21 -070059 */
Steven Moreland0540d282017-05-08 16:03:11 -070060 bool push(const Task &t);
Yifan Hong5e2318c2016-10-27 17:19:21 -070061
Yifan Hong5e2318c2016-10-27 17:19:21 -070062private:
Yifan Hong8184c122017-03-20 18:26:43 -070063 std::shared_ptr<SynchronizedQueue<Task>> mQueue;
Yifan Hong5e2318c2016-10-27 17:19:21 -070064};
65
Yifan Hong0a351392017-03-20 17:17:52 -070066} // namespace details
Yifan Hong5e2318c2016-10-27 17:19:21 -070067} // namespace hardware
68} // namespace android
69
Yifan Hong0a351392017-03-20 17:17:52 -070070#endif // ANDROID_HIDL_TASK_RUNNER_H