diff -r e01b68222e60 src/devices/emu/emu-net-device.cc --- a/src/devices/emu/emu-net-device.cc Thu May 06 14:21:20 2010 +0100 +++ b/src/devices/emu/emu-net-device.cc Wed Jun 09 15:04:49 2010 +0100 @@ -48,6 +48,7 @@ #include #include #include +#include NS_LOG_COMPONENT_DEFINE ("EmuNetDevice"); @@ -56,6 +57,8 @@ NS_OBJECT_ENSURE_REGISTERED (EmuNetDevice); #define EMU_MAGIC 65867 +#define MAX_PENDING_READS 1 + TypeId EmuNetDevice::GetTypeId (void) @@ -184,7 +187,8 @@ m_ifIndex (std::numeric_limits::max ()), // absurdly large value m_sll_ifindex (-1), m_isBroadcast (true), - m_isMulticast (false) + m_isMulticast (false), + m_pendingReadCount (0) { NS_LOG_FUNCTION (this); m_packetBuffer = new uint8_t[65536]; @@ -626,6 +630,12 @@ free (buf); buf = 0; + { + CriticalSection cs (m_pendingReadMutex); + //std::cerr << std::endl << "EmuNetDevice main thread: m_pendingReadCount is " << m_pendingReadCount << std::endl; + --m_pendingReadCount; + } + // // Trace sinks will expect complete packets, not packets without some of the // headers. @@ -755,6 +765,31 @@ for (;;) { // + // Too many pending reads at the same time leads to excessive memory allocations. This counter prevents it. + // + bool skip = false; + + { + CriticalSection cs (m_pendingReadMutex); + //std::cerr << std::endl << "EmuNetDevice read thread: m_pendingReadCount is " << m_pendingReadCount << std::endl; + if (m_pendingReadCount >= MAX_PENDING_READS) + { + skip = true; + } + else + { + ++m_pendingReadCount; + } + } + + if (skip) + { + struct timespec time = { 0, 100000000L }; // 100 ms + nanosleep (&time, NULL); + continue; + } + + // // to avoid any issues with a shared reference counted packet, we allocate a buffer on the heap and pass that // buffer into the ns-3 context thread where it will create the packet, copy the buffer and then free it. // diff -r e01b68222e60 src/devices/emu/emu-net-device.h --- a/src/devices/emu/emu-net-device.h Thu May 06 14:21:20 2010 +0100 +++ b/src/devices/emu/emu-net-device.h Wed Jun 09 15:04:49 2010 +0100 @@ -32,6 +32,7 @@ #include "ns3/ptr.h" #include "ns3/mac48-address.h" #include "ns3/system-thread.h" +#include "ns3/system-mutex.h" #include "ns3/realtime-simulator-impl.h" namespace ns3 { @@ -520,6 +521,9 @@ * multithreaded apps is not a good thing. */ uint32_t m_nodeId; + + uint32_t m_pendingReadCount; + SystemMutex m_pendingReadMutex; }; } // namespace ns3