Add TaskRunner to manage the thread for Bs*.

Bug: 31819198 (partly fixes it)

Test: libhidl-test (repeated 1000 times)
Test: hidl_test

Change-Id: Ia68adbab9dd40c09dfd2861ae03f65028d4694c7
diff --git a/include/hidl/SynchronizedQueue.h b/include/hidl/SynchronizedQueue.h
index 7241411..80123ba 100644
--- a/include/hidl/SynchronizedQueue.h
+++ b/include/hidl/SynchronizedQueue.h
@@ -14,11 +14,16 @@
  * limitations under the License.
  */
 
+#ifndef ANDROID_SYNCHRONIZED_QUEUE_H
+#define ANDROID_SYNCHRONIZED_QUEUE_H
+
 #include <condition_variable>
 #include <mutex>
 #include <queue>
 #include <thread>
 
+namespace android {
+namespace hardware {
 /* Threadsafe queue.
  */
 template <typename T>
@@ -32,16 +37,22 @@
 
     /* Puts an item onto the end of the queue.
      */
-    void push(const T& item);
+    bool push(const T& item);
 
     /* Gets the size of the array.
      */
     size_t size();
 
+    /* Sets the limit to the queue. Will fail
+     * the push operation if the limit is reached.
+     */
+    void setLimit(size_t limit);
+
 private:
     std::condition_variable mCondition;
     std::mutex mMutex;
     std::queue<T> mQueue;
+    size_t mQueueLimit = SIZE_MAX;
 };
 
 template <typename T>
@@ -59,13 +70,20 @@
 }
 
 template <typename T>
-void SynchronizedQueue<T>::push(const T &item) {
+bool SynchronizedQueue<T>::push(const T &item) {
+    bool success;
     {
         std::unique_lock<std::mutex> lock(mMutex);
-        mQueue.push(item);
+        if (mQueue.size() < mQueueLimit) {
+            mQueue.push(item);
+            success = true;
+        } else {
+            success = false;
+        }
     }
 
     mCondition.notify_one();
+    return success;
 }
 
 template <typename T>
@@ -74,3 +92,15 @@
 
     return mQueue.size();
 }
+
+template <typename T>
+void SynchronizedQueue<T>::setLimit(size_t limit) {
+    std::unique_lock<std::mutex> lock(mMutex);
+
+    mQueueLimit = limit;
+}
+
+} // namespace hardware
+} // namespace android
+
+#endif
diff --git a/include/hidl/TaskRunner.h b/include/hidl/TaskRunner.h
new file mode 100644
index 0000000..bc9087f
--- /dev/null
+++ b/include/hidl/TaskRunner.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef ANDROID_TASK_RUNNER_H
+#define ANDROID_TASK_RUNNER_H
+
+#include "SynchronizedQueue.h"
+#include <thread>
+
+namespace android {
+namespace hardware {
+
+/*
+ * A background infinite loop that runs the Tasks push()'ed.
+ * Just a simple single-threaded thread pool.
+ */
+class TaskRunner {
+public:
+
+    /* Kicks off the loop immediately. */
+    TaskRunner();
+
+    /*
+     * Detaches the background thread and return immediately.
+     * Tasks in the queue will continue to be done sequentially, and _after_
+     * all tasks are done, the background thread releases the resources
+     * (the queue, the std::thread object, etc.)
+     */
+    ~TaskRunner();
+
+    /*
+     * Add a task. Return true if successful, false if
+     * the queue's size exceeds limit.
+     */
+    inline bool push(const std::function<void(void)> &t) {
+        return this->mQueue->push(t);
+    }
+
+    /*
+     * Sets the queue limit. Fails the push operation once the limit is reached.
+     */
+    inline void setLimit(size_t limit) {
+        this->mQueue->setLimit(limit);
+    }
+private:
+
+    // resources managed by the background thread.
+    bool *mRunning;
+    SynchronizedQueue<std::function<void(void)>> *mQueue;
+    std::thread *mThread;
+};
+
+} // namespace hardware
+} // namespace android
+
+#endif // ANDROID_TASK_RUNNER_H