blob: 0983793e52f9f3ec29082c3aa694b8e82fcc99a9 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// See LICENSE file in the project root for full license information.
4//
5
6#pragma once
7
8#include <queue>
9#include <thread>
10#include <mutex>
11#include <condition_variable>
12
13#include "CpuExecutor.h"
14#include "HalInterfaces.h"
15#include <armnn/ArmNN.hpp>
16
17namespace armnn_driver
18{
19
20class ArmnnPreparedModel;
21
22class RequestThread
23{
24public:
25 /// Constructor creates the thread
26 RequestThread();
27
28 /// Destructor terminates the thread
29 ~RequestThread();
30
31 /// Add a message to the thread queue.
32 /// @param[in] model pointer to the prepared model handling the request
33 /// @param[in] memPools pointer to the memory pools vector for the tensors
34 /// @param[in] inputTensors pointer to the input tensors for the request
35 /// @param[in] outputTensors pointer to the output tensors for the request
36 /// @param[in] callback the android notification callback
37 void PostMsg(armnn_driver::ArmnnPreparedModel* model,
38 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& memPools,
39 std::shared_ptr<armnn::InputTensors>& inputTensors,
40 std::shared_ptr<armnn::OutputTensors>& outputTensors,
41 const ::android::sp<IExecutionCallback>& callback);
42
43private:
44 RequestThread(const RequestThread&) = delete;
45 RequestThread& operator=(const RequestThread&) = delete;
46
47 /// storage for a prepared model and args for the asyncExecute call
48 struct AsyncExecuteData
49 {
50 AsyncExecuteData(ArmnnPreparedModel* model,
51 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& memPools,
52 std::shared_ptr<armnn::InputTensors>& inputTensors,
53 std::shared_ptr<armnn::OutputTensors>& outputTensors,
54 const ::android::sp<IExecutionCallback>& cb)
55 : m_Model(model)
56 , m_MemPools(memPools)
57 , m_InputTensors(inputTensors)
58 , m_OutputTensors(outputTensors)
59 , m_callback(cb)
60 {
61 }
62
63 armnn_driver::ArmnnPreparedModel* m_Model;
64 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools;
65 std::shared_ptr<armnn::InputTensors> m_InputTensors;
66 std::shared_ptr<armnn::OutputTensors> m_OutputTensors;
67 const ::android::sp<IExecutionCallback> m_callback;
68 };
69
70 enum class ThreadMsgType
71 {
72 EXIT, // exit the thread
73 REQUEST // user request to process
74 };
75
76 /// storage for the thread message type and data
77 struct ThreadMsg
78 {
79 ThreadMsg(ThreadMsgType msgType,
80 std::shared_ptr<AsyncExecuteData>& msgData)
81 : type(msgType)
82 , data(msgData)
83 {
84 }
85
86 ThreadMsgType type;
87 std::shared_ptr<AsyncExecuteData> data;
88 };
89
90 /// Add a prepared thread message to the thread queue.
91 /// @param[in] threadMsg the message to add to the queue
92 void PostMsg(std::shared_ptr<ThreadMsg>& pThreadMsg);
93
94 /// Entry point for the request thread
95 void Process();
96
97 std::unique_ptr<std::thread> m_Thread;
98 std::queue<std::shared_ptr<ThreadMsg>> m_Queue;
99 std::mutex m_Mutex;
100 std::condition_variable m_Cv;
101};
102
103} // namespace armnn_driver
104