ArmNN
 20.08
SendThread Class Reference

#include <SendThread.hpp>

Inheritance diagram for SendThread:
ISendThread IConsumer

Public Member Functions

 SendThread (ProfilingStateMachine &profilingStateMachine, IBufferManager &buffer, ISendCounterPacket &sendCounterPacket, int timeout=1000)
 
 ~SendThread ()
 
void Start (IProfilingConnection &profilingConnection) override
 Start the thread. More...
 
void Stop (bool rethrowSendThreadExceptions=true) override
 Stop the thread. More...
 
void SetReadyToRead () override
 Set a "ready to read" flag in the buffer to notify the reading thread to start reading it. More...
 
bool IsRunning ()
 
bool WaitForPacketSent (uint32_t timeout)
 
- Public Member Functions inherited from ISendThread
virtual ~ISendThread ()
 
- Public Member Functions inherited from IConsumer
virtual ~IConsumer ()
 

Detailed Description

Definition at line 29 of file SendThread.hpp.

Constructor & Destructor Documentation

◆ SendThread()

SendThread ( ProfilingStateMachine profilingStateMachine,
IBufferManager buffer,
ISendCounterPacket sendCounterPacket,
int  timeout = 1000 
)

Definition at line 26 of file SendThread.cpp.

References IBufferManager::SetConsumer().

30  : m_StateMachine(profilingStateMachine)
31  , m_BufferManager(buffer)
32  , m_SendCounterPacket(sendCounterPacket)
33  , m_Timeout(timeout)
34  , m_IsRunning(false)
35  , m_KeepRunning(false)
36  , m_SendThreadException(nullptr)
37 {
38  m_BufferManager.SetConsumer(this);
39 }
virtual void SetConsumer(IConsumer *consumer)=0

◆ ~SendThread()

~SendThread ( )
inline

Definition at line 34 of file SendThread.hpp.

References SendThread::SetReadyToRead(), SendThread::Start(), and SendThread::Stop().

35  {
36  // Don't rethrow when destructing the object
37  Stop(false);
38  }
void Stop(bool rethrowSendThreadExceptions=true) override
Stop the thread.
Definition: SendThread.cpp:82

Member Function Documentation

◆ IsRunning()

bool IsRunning ( )
inline

Definition at line 45 of file SendThread.hpp.

References SendThread::WaitForPacketSent().

Referenced by BOOST_AUTO_TEST_CASE().

45 { return m_IsRunning.load(); }

◆ SetReadyToRead()

void SetReadyToRead ( )
overridevirtual

Set a "ready to read" flag in the buffer to notify the reading thread to start reading it.

Implements IConsumer.

Definition at line 41 of file SendThread.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), SendThread::Stop(), and SendThread::~SendThread().

42 {
43  // We need to wait for the send thread to release its mutex
44  {
45  std::lock_guard<std::mutex> lck(m_WaitMutex);
46  m_ReadyToRead = true;
47  }
48  // Signal the send thread that there's something to read in the buffer
49  m_WaitCondition.notify_one();
50 }

◆ Start()

void Start ( IProfilingConnection profilingConnection)
overridevirtual

Start the thread.

Implements ISendThread.

Definition at line 52 of file SendThread.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), ProfilingService::Update(), and SendThread::~SendThread().

53 {
54  // Check if the send thread is already running
55  if (m_IsRunning.load())
56  {
57  // The send thread is already running
58  return;
59  }
60 
61  if (m_SendThread.joinable())
62  {
63  m_SendThread.join();
64  }
65 
66  // Mark the send thread as running
67  m_IsRunning.store(true);
68 
69  // Keep the send procedure going until the send thread is signalled to stop
70  m_KeepRunning.store(true);
71 
72  // Make sure the send thread will not flush the buffer until signaled to do so
73  // no need for a mutex as the send thread can not be running at this point
74  m_ReadyToRead = false;
75 
76  m_PacketSent = false;
77 
78  // Start the send thread
79  m_SendThread = std::thread(&SendThread::Send, this, std::ref(profilingConnection));
80 }

◆ Stop()

void Stop ( bool  rethrowSendThreadExceptions = true)
overridevirtual

Stop the thread.

Implements ISendThread.

Definition at line 82 of file SendThread.cpp.

References armnn::profiling::Active, ProfilingStateMachine::GetCurrentState(), IBufferManager::GetReadableBuffer(), IProfilingConnection::IsOpen(), IBufferManager::MarkRead(), armnn::profiling::NotConnected, ISendCounterPacket::SendStreamMetaDataPacket(), SendThread::SetReadyToRead(), armnn::profiling::Uninitialised, armnn::profiling::WaitingForAck, and IProfilingConnection::WritePacket().

Referenced by BOOST_AUTO_TEST_CASE(), ProfilingService::GetSendTimelinePacket(), ProfilingService::Update(), and SendThread::~SendThread().

83 {
84  // Signal the send thread to stop
85  m_KeepRunning.store(false);
86 
87  // Check that the send thread is running
88  if (m_SendThread.joinable())
89  {
90  // Kick the send thread out of the wait condition
92  // Wait for the send thread to complete operations
93  m_SendThread.join();
94  }
95 
96  // Check if the send thread exception has to be rethrown
97  if (!rethrowSendThreadExceptions)
98  {
99  // No need to rethrow the send thread exception, return immediately
100  return;
101  }
102 
103  // Check if there's an exception to rethrow
104  if (m_SendThreadException)
105  {
106  // Rethrow the send thread exception
107  std::rethrow_exception(m_SendThreadException);
108 
109  // Nullify the exception as it has been rethrown
110  m_SendThreadException = nullptr;
111  }
112 }
void SetReadyToRead() override
Set a "ready to read" flag in the buffer to notify the reading thread to start reading it...
Definition: SendThread.cpp:41

◆ WaitForPacketSent()

bool WaitForPacketSent ( uint32_t  timeout = 1000)

Definition at line 263 of file SendThread.cpp.

Referenced by SendThread::IsRunning(), and ProfilingService::WaitForPacketSent().

264 {
265  std::unique_lock<std::mutex> lock(m_PacketSentWaitMutex);
266  // Blocks until notified that at least a packet has been sent or until timeout expires.
267  bool timedOut = m_PacketSentWaitCondition.wait_for(lock,
268  std::chrono::milliseconds(timeout),
269  [&] { return m_PacketSent; });
270 
271  m_PacketSent = false;
272 
273  return timedOut;
274 }

The documentation for this class was generated from the following files: