ArmNN
 20.11
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 23 of file SendThread.cpp.

References IBufferManager::SetConsumer().

27  : m_StateMachine(profilingStateMachine)
28  , m_BufferManager(buffer)
29  , m_SendCounterPacket(sendCounterPacket)
30  , m_Timeout(timeout)
31  , m_IsRunning(false)
32  , m_KeepRunning(false)
33  , m_SendThreadException(nullptr)
34 {
35  m_BufferManager.SetConsumer(this);
36 }
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:79

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 38 of file SendThread.cpp.

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

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

◆ Start()

void Start ( IProfilingConnection profilingConnection)
overridevirtual

Start the thread.

Implements ISendThread.

Definition at line 49 of file SendThread.cpp.

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

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

◆ Stop()

void Stop ( bool  rethrowSendThreadExceptions = true)
overridevirtual

Stop the thread.

Implements ISendThread.

Definition at line 79 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().

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

◆ WaitForPacketSent()

bool WaitForPacketSent ( uint32_t  timeout = 1000)

Definition at line 260 of file SendThread.cpp.

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

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

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