15e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong/* 25e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * Copyright (C) 2016 The Android Open Source Project 35e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * 45e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * Licensed under the Apache License, Version 2.0 (the "License"); 55e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * you may not use this file except in compliance with the License. 65e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * You may obtain a copy of the License at 75e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * 85e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * http://www.apache.org/licenses/LICENSE-2.0 95e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * 105e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * Unless required by applicable law or agreed to in writing, software 115e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * distributed under the License is distributed on an "AS IS" BASIS, 125e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * See the License for the specific language governing permissions and 145e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * limitations under the License. 155e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong */ 160a35139d24ae0071475adeae3e1370b8f8617f8fYifan Hong#ifndef ANDROID_HIDL_TASK_RUNNER_H 170a35139d24ae0071475adeae3e1370b8f8617f8fYifan Hong#define ANDROID_HIDL_TASK_RUNNER_H 185e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 198184c1220e120414ebd968930737daa563788fcaYifan Hong#include <memory> 205e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong#include <thread> 215e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 225e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hongnamespace android { 235e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hongnamespace hardware { 240a35139d24ae0071475adeae3e1370b8f8617f8fYifan Hongnamespace details { 255e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 26c03f943790b273c261e613099b7916e9c52f64e2Steven Morelandusing Task = std::function<void(void)>; 27c03f943790b273c261e613099b7916e9c52f64e2Steven Moreland 28c03f943790b273c261e613099b7916e9c52f64e2Steven Morelandtemplate <typename T> 29c03f943790b273c261e613099b7916e9c52f64e2Steven Morelandstruct SynchronizedQueue; 30c03f943790b273c261e613099b7916e9c52f64e2Steven Moreland 315e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong/* 325e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * A background infinite loop that runs the Tasks push()'ed. 338184c1220e120414ebd968930737daa563788fcaYifan Hong * Equivalent to a simple single-threaded Looper. 345e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong */ 355e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hongclass TaskRunner { 365e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hongpublic: 375e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 386f6675464c82fc796d72458d7268e28d759b997fYifan Hong /* Create an empty task runner. Nothing will be done until start() is called. */ 395e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong TaskRunner(); 405e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 415e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong /* 428184c1220e120414ebd968930737daa563788fcaYifan Hong * Notify the background thread to terminate and return immediately. 438184c1220e120414ebd968930737daa563788fcaYifan Hong * Tasks in the queue will continue to be done sequentially in background 448184c1220e120414ebd968930737daa563788fcaYifan Hong * until all tasks are finished. 455e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong */ 465e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong ~TaskRunner(); 475e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 485e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong /* 496f6675464c82fc796d72458d7268e28d759b997fYifan Hong * Sets the queue limit. Fails the push operation once the limit is reached. 50c03f943790b273c261e613099b7916e9c52f64e2Steven Moreland * This function is named start for legacy reasons and to maintain ABI 51c03f943790b273c261e613099b7916e9c52f64e2Steven Moreland * stability, but the underlying thread running tasks isn't started until 52c03f943790b273c261e613099b7916e9c52f64e2Steven Moreland * the first task is pushed. 536f6675464c82fc796d72458d7268e28d759b997fYifan Hong */ 546f6675464c82fc796d72458d7268e28d759b997fYifan Hong void start(size_t limit); 556f6675464c82fc796d72458d7268e28d759b997fYifan Hong 566f6675464c82fc796d72458d7268e28d759b997fYifan Hong /* 575e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong * Add a task. Return true if successful, false if 588184c1220e120414ebd968930737daa563788fcaYifan Hong * the queue's size exceeds limit or t doesn't contain a callable target. 595e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong */ 600540d280036757c36fd52992c9986a7864cc794fSteven Moreland bool push(const Task &t); 615e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 625e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hongprivate: 638184c1220e120414ebd968930737daa563788fcaYifan Hong std::shared_ptr<SynchronizedQueue<Task>> mQueue; 645e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong}; 655e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 660a35139d24ae0071475adeae3e1370b8f8617f8fYifan Hong} // namespace details 675e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong} // namespace hardware 685e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong} // namespace android 695e2318c9d54c3fe9454f2d79bc80ead3782658dbYifan Hong 700a35139d24ae0071475adeae3e1370b8f8617f8fYifan Hong#endif // ANDROID_HIDL_TASK_RUNNER_H 71