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