blob: 28ea01c17295e9520ed0889d7384e06656f8aa66 [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
19#include "SynchronizedQueue.h"
Yifan Hong8184c122017-03-20 18:26:43 -070020#include <memory>
Yifan Hong5e2318c2016-10-27 17:19:21 -070021#include <thread>
22
23namespace android {
24namespace hardware {
Yifan Hong0a351392017-03-20 17:17:52 -070025namespace details {
Yifan Hong5e2318c2016-10-27 17:19:21 -070026
27/*
28 * A background infinite loop that runs the Tasks push()'ed.
Yifan Hong8184c122017-03-20 18:26:43 -070029 * Equivalent to a simple single-threaded Looper.
Yifan Hong5e2318c2016-10-27 17:19:21 -070030 */
31class TaskRunner {
32public:
Yifan Hong8184c122017-03-20 18:26:43 -070033 using Task = std::function<void(void)>;
Yifan Hong5e2318c2016-10-27 17:19:21 -070034
Yifan Hong6f667542017-03-20 19:04:05 -070035 /* Create an empty task runner. Nothing will be done until start() is called. */
Yifan Hong5e2318c2016-10-27 17:19:21 -070036 TaskRunner();
37
38 /*
Yifan Hong8184c122017-03-20 18:26:43 -070039 * 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 Hong5e2318c2016-10-27 17:19:21 -070042 */
43 ~TaskRunner();
44
45 /*
Yifan Hong6f667542017-03-20 19:04:05 -070046 * 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 Hong5e2318c2016-10-27 17:19:21 -070052 * Add a task. Return true if successful, false if
Yifan Hong8184c122017-03-20 18:26:43 -070053 * the queue's size exceeds limit or t doesn't contain a callable target.
Yifan Hong5e2318c2016-10-27 17:19:21 -070054 */
Steven Moreland0540d282017-05-08 16:03:11 -070055 bool push(const Task &t);
Yifan Hong5e2318c2016-10-27 17:19:21 -070056
Yifan Hong5e2318c2016-10-27 17:19:21 -070057private:
Yifan Hong8184c122017-03-20 18:26:43 -070058 std::shared_ptr<SynchronizedQueue<Task>> mQueue;
Yifan Hong5e2318c2016-10-27 17:19:21 -070059};
60
Yifan Hong0a351392017-03-20 17:17:52 -070061} // namespace details
Yifan Hong5e2318c2016-10-27 17:19:21 -070062} // namespace hardware
63} // namespace android
64
Yifan Hong0a351392017-03-20 17:17:52 -070065#endif // ANDROID_HIDL_TASK_RUNNER_H