blob: 53f145b4441c8d07f3cef97c66972c43ef7384c7 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa015307bc12018-03-09 13:51:08 +00004//
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"
Matteo Martincighe48bdff2018-09-03 13:50:50 +010014#include "ArmnnDriverImpl.hpp"
telsoa01ce3e84a2018-08-31 09:31:35 +010015
16#include <CpuExecutor.h>
telsoa015307bc12018-03-09 13:51:08 +000017#include <armnn/ArmNN.hpp>
18
19namespace armnn_driver
20{
21
Matteo Martincighe48bdff2018-09-03 13:50:50 +010022template<typename HalVersion>
telsoa015307bc12018-03-09 13:51:08 +000023class ArmnnPreparedModel;
24
Matteo Martincighe48bdff2018-09-03 13:50:50 +010025template<typename HalVersion>
telsoa015307bc12018-03-09 13:51:08 +000026class RequestThread
27{
28public:
29 /// Constructor creates the thread
30 RequestThread();
31
32 /// Destructor terminates the thread
33 ~RequestThread();
34
35 /// Add a message to the thread queue.
36 /// @param[in] model pointer to the prepared model handling the request
37 /// @param[in] memPools pointer to the memory pools vector for the tensors
38 /// @param[in] inputTensors pointer to the input tensors for the request
39 /// @param[in] outputTensors pointer to the output tensors for the request
40 /// @param[in] callback the android notification callback
Matteo Martincighe48bdff2018-09-03 13:50:50 +010041 void PostMsg(armnn_driver::ArmnnPreparedModel<HalVersion>* model,
telsoa015307bc12018-03-09 13:51:08 +000042 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& memPools,
43 std::shared_ptr<armnn::InputTensors>& inputTensors,
44 std::shared_ptr<armnn::OutputTensors>& outputTensors,
45 const ::android::sp<IExecutionCallback>& callback);
46
47private:
48 RequestThread(const RequestThread&) = delete;
49 RequestThread& operator=(const RequestThread&) = delete;
50
51 /// storage for a prepared model and args for the asyncExecute call
52 struct AsyncExecuteData
53 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +010054 AsyncExecuteData(ArmnnPreparedModel<HalVersion>* model,
telsoa015307bc12018-03-09 13:51:08 +000055 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& memPools,
56 std::shared_ptr<armnn::InputTensors>& inputTensors,
57 std::shared_ptr<armnn::OutputTensors>& outputTensors,
58 const ::android::sp<IExecutionCallback>& cb)
59 : m_Model(model)
60 , m_MemPools(memPools)
61 , m_InputTensors(inputTensors)
62 , m_OutputTensors(outputTensors)
63 , m_callback(cb)
64 {
65 }
66
Matteo Martincighe48bdff2018-09-03 13:50:50 +010067 armnn_driver::ArmnnPreparedModel<HalVersion>* m_Model;
telsoa015307bc12018-03-09 13:51:08 +000068 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools;
69 std::shared_ptr<armnn::InputTensors> m_InputTensors;
70 std::shared_ptr<armnn::OutputTensors> m_OutputTensors;
71 const ::android::sp<IExecutionCallback> m_callback;
72 };
73
74 enum class ThreadMsgType
75 {
76 EXIT, // exit the thread
77 REQUEST // user request to process
78 };
79
80 /// storage for the thread message type and data
81 struct ThreadMsg
82 {
83 ThreadMsg(ThreadMsgType msgType,
84 std::shared_ptr<AsyncExecuteData>& msgData)
85 : type(msgType)
86 , data(msgData)
87 {
88 }
89
90 ThreadMsgType type;
91 std::shared_ptr<AsyncExecuteData> data;
92 };
93
94 /// Add a prepared thread message to the thread queue.
95 /// @param[in] threadMsg the message to add to the queue
96 void PostMsg(std::shared_ptr<ThreadMsg>& pThreadMsg);
97
98 /// Entry point for the request thread
99 void Process();
100
101 std::unique_ptr<std::thread> m_Thread;
102 std::queue<std::shared_ptr<ThreadMsg>> m_Queue;
103 std::mutex m_Mutex;
104 std::condition_variable m_Cv;
105};
106
arovir01b0717b52018-09-05 17:03:25 +0100107} // namespace armnn_driver