Skip to content

Commit

Permalink
memory pool for x25519 keys
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Nov 2, 2024
1 parent 0d09a8b commit 29d7711
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 6 additions & 6 deletions libi2pd/Transports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace transport
{
template<typename Keys>
EphemeralKeysSupplier<Keys>::EphemeralKeysSupplier (int size):
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr)
m_QueueSize (size), m_IsRunning (false)
{
}

Expand All @@ -39,7 +39,7 @@ namespace transport
void EphemeralKeysSupplier<Keys>::Start ()
{
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&EphemeralKeysSupplier<Keys>::Run, this));
m_Thread.reset (new std::thread (std::bind (&EphemeralKeysSupplier<Keys>::Run, this)));
}

template<typename Keys>
Expand All @@ -53,8 +53,7 @@ namespace transport
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
m_Thread = nullptr;
}
}

Expand All @@ -78,6 +77,7 @@ namespace transport
}
else
{
m_KeysPool.CleanUpMt ();
std::unique_lock<std::mutex> l(m_AcquiredMutex);
if (!m_IsRunning) break;
m_Acquired.wait (l); // wait for element gets acquired
Expand All @@ -92,7 +92,7 @@ namespace transport
{
for (int i = 0; i < num; i++)
{
auto pair = std::make_shared<Keys> ();
auto pair = m_KeysPool.AcquireSharedMt ();
pair->GenerateKeys ();
std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair);
Expand All @@ -114,7 +114,7 @@ namespace transport
}
}
// queue is empty, create new
auto pair = std::make_shared<Keys> ();
auto pair = m_KeysPool.AcquireSharedMt ();
pair->GenerateKeys ();
return pair;
}
Expand Down
4 changes: 3 additions & 1 deletion libi2pd/Transports.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "RouterInfo.h"
#include "I2NPProtocol.h"
#include "Identity.h"
#include "util.h"

namespace i2p
{
Expand Down Expand Up @@ -53,9 +54,10 @@ namespace transport

const int m_QueueSize;
std::queue<std::shared_ptr<Keys> > m_Queue;
i2p::util::MemoryPoolMt<Keys> m_KeysPool;

bool m_IsRunning;
std::thread * m_Thread;
std::unique_ptr<std::thread> m_Thread;
std::condition_variable m_Acquired;
std::mutex m_AcquiredMutex;
};
Expand Down

0 comments on commit 29d7711

Please sign in to comment.