aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/AsyncExecutionCallback.cpp
blob: 5b87927af2e8f33facaab2eb4220b1fc97aec014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <AsyncExecutionCallback.hpp>

namespace armnn
{

namespace experimental
{

void AsyncExecutionCallback::Notify(armnn::Status status, InferenceTimingPair timeTaken)
{
    {
#if !defined(ARMNN_DISABLE_THREADS)
        std::lock_guard<std::mutex> hold(m_Mutex);
#endif
        // store results and mark as notified
        m_Status    = status;
        m_StartTime = timeTaken.first;
        m_EndTime   = timeTaken.second;
        m_NotificationQueue.push(m_InferenceId);
    }
#if !defined(ARMNN_DISABLE_THREADS)
    m_Condition.notify_all();
#endif
}

armnn::Status AsyncExecutionCallback::GetStatus() const
{
    return m_Status;
}

HighResolutionClock AsyncExecutionCallback::GetStartTime() const
{
    return m_StartTime;
}

HighResolutionClock AsyncExecutionCallback::GetEndTime() const
{
    return m_EndTime;
}

std::shared_ptr<AsyncExecutionCallback> AsyncCallbackManager::GetNewCallback()
{
    auto cb = std::make_unique<AsyncExecutionCallback>(m_NotificationQueue
#if !defined(ARMNN_DISABLE_THREADS)
                                                       , m_Mutex
                                                       , m_Condition
#endif
        );
    InferenceId id = cb->GetInferenceId();
    m_Callbacks.insert({id, std::move(cb)});

    return m_Callbacks.at(id);
}

std::shared_ptr<AsyncExecutionCallback> AsyncCallbackManager::GetNotifiedCallback()
{
#if !defined(ARMNN_DISABLE_THREADS)
    std::unique_lock<std::mutex> lock(m_Mutex);

    m_Condition.wait(lock, [this] { return !m_NotificationQueue.empty(); });
#endif
    InferenceId id = m_NotificationQueue.front();
    m_NotificationQueue.pop();

    std::shared_ptr<AsyncExecutionCallback> callback = m_Callbacks.at(id);
    m_Callbacks.erase(id);
    return callback;
}

} // namespace experimental

} // namespace armnn