mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Remove unused or obsolete classes and files
This commit is contained in:
294
src/ripple/core/impl/Workers.cpp
Normal file
294
src/ripple/core/impl/Workers.cpp
Normal file
@@ -0,0 +1,294 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/core/impl/Workers.h>
|
||||
#include <beast/unit_test/suite.h>
|
||||
#include <cassert>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
Workers::Workers (
|
||||
Callback& callback,
|
||||
std::string const& threadNames,
|
||||
int numberOfThreads)
|
||||
: m_callback (callback)
|
||||
, m_threadNames (threadNames)
|
||||
, m_allPaused (true, true)
|
||||
, m_semaphore (0)
|
||||
, m_numberOfThreads (0)
|
||||
, m_activeCount (0)
|
||||
, m_pauseCount (0)
|
||||
, m_runningTaskCount (0)
|
||||
{
|
||||
setNumberOfThreads (numberOfThreads);
|
||||
}
|
||||
|
||||
Workers::~Workers ()
|
||||
{
|
||||
pauseAllThreadsAndWait ();
|
||||
|
||||
deleteWorkers (m_everyone);
|
||||
}
|
||||
|
||||
int Workers::getNumberOfThreads () const noexcept
|
||||
{
|
||||
return m_numberOfThreads;
|
||||
}
|
||||
|
||||
// VFALCO NOTE if this function is called quickly to reduce then
|
||||
// increase the number of threads, it could result in
|
||||
// more paused threads being created than expected.
|
||||
//
|
||||
void Workers::setNumberOfThreads (int numberOfThreads)
|
||||
{
|
||||
if (m_numberOfThreads != numberOfThreads)
|
||||
{
|
||||
if (numberOfThreads > m_numberOfThreads)
|
||||
{
|
||||
// Increasing the number of working threads
|
||||
|
||||
int const amount = numberOfThreads - m_numberOfThreads;
|
||||
|
||||
for (int i = 0; i < amount; ++i)
|
||||
{
|
||||
// See if we can reuse a paused worker
|
||||
Worker* worker = m_paused.pop_front ();
|
||||
|
||||
if (worker != nullptr)
|
||||
{
|
||||
// If we got here then the worker thread is at [1]
|
||||
// This will unblock their call to wait()
|
||||
//
|
||||
worker->notify ();
|
||||
}
|
||||
else
|
||||
{
|
||||
worker = new Worker (*this, m_threadNames);
|
||||
}
|
||||
|
||||
m_everyone.push_front (worker);
|
||||
}
|
||||
}
|
||||
else if (numberOfThreads < m_numberOfThreads)
|
||||
{
|
||||
// Decreasing the number of working threads
|
||||
|
||||
int const amount = m_numberOfThreads - numberOfThreads;
|
||||
|
||||
for (int i = 0; i < amount; ++i)
|
||||
{
|
||||
++m_pauseCount;
|
||||
|
||||
// Pausing a thread counts as one "internal task"
|
||||
m_semaphore.signal ();
|
||||
}
|
||||
}
|
||||
|
||||
m_numberOfThreads = numberOfThreads;
|
||||
}
|
||||
}
|
||||
|
||||
void Workers::pauseAllThreadsAndWait ()
|
||||
{
|
||||
setNumberOfThreads (0);
|
||||
|
||||
m_allPaused.wait ();
|
||||
|
||||
assert (numberOfCurrentlyRunningTasks () == 0);
|
||||
}
|
||||
|
||||
void Workers::addTask ()
|
||||
{
|
||||
m_semaphore.signal ();
|
||||
}
|
||||
|
||||
int Workers::numberOfCurrentlyRunningTasks () const noexcept
|
||||
{
|
||||
return m_runningTaskCount.load ();
|
||||
}
|
||||
|
||||
void Workers::deleteWorkers (beast::LockFreeStack <Worker>& stack)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
Worker* const worker = stack.pop_front ();
|
||||
|
||||
if (worker != nullptr)
|
||||
{
|
||||
// This call blocks until the thread orderly exits
|
||||
delete worker;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
Workers::Worker::Worker (Workers& workers, std::string const& threadName)
|
||||
: Thread (threadName)
|
||||
, m_workers (workers)
|
||||
{
|
||||
startThread ();
|
||||
}
|
||||
|
||||
Workers::Worker::~Worker ()
|
||||
{
|
||||
stopThread ();
|
||||
}
|
||||
|
||||
void Workers::Worker::run ()
|
||||
{
|
||||
while (! threadShouldExit ())
|
||||
{
|
||||
// Increment the count of active workers, and if
|
||||
// we are the first one then reset the "all paused" event
|
||||
//
|
||||
if (++m_workers.m_activeCount == 1)
|
||||
m_workers.m_allPaused.reset ();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
// Acquire a task or "internal task."
|
||||
//
|
||||
m_workers.m_semaphore.wait ();
|
||||
|
||||
// See if there's a pause request. This
|
||||
// counts as an "internal task."
|
||||
//
|
||||
int pauseCount = m_workers.m_pauseCount.load ();
|
||||
|
||||
if (pauseCount > 0)
|
||||
{
|
||||
// Try to decrement
|
||||
pauseCount = --m_workers.m_pauseCount;
|
||||
|
||||
if (pauseCount >= 0)
|
||||
{
|
||||
// We got paused
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Undo our decrement
|
||||
++m_workers.m_pauseCount;
|
||||
}
|
||||
}
|
||||
|
||||
// We couldn't pause so we must have gotten
|
||||
// unblocked in order to process a task.
|
||||
//
|
||||
++m_workers.m_runningTaskCount;
|
||||
m_workers.m_callback.processTask ();
|
||||
--m_workers.m_runningTaskCount;
|
||||
|
||||
// Put the name back in case the callback changed it
|
||||
Thread::setCurrentThreadName (Thread::getThreadName());
|
||||
}
|
||||
|
||||
// Any worker that goes into the paused list must
|
||||
// guarantee that it will eventually block on its
|
||||
// event object.
|
||||
//
|
||||
m_workers.m_paused.push_front (this);
|
||||
|
||||
// Decrement the count of active workers, and if we
|
||||
// are the last one then signal the "all paused" event.
|
||||
//
|
||||
if (--m_workers.m_activeCount == 0)
|
||||
m_workers.m_allPaused.signal ();
|
||||
|
||||
Thread::setCurrentThreadName ("(" + getThreadName() + ")");
|
||||
|
||||
// [1] We will be here when the paused list is popped
|
||||
//
|
||||
// We block on our event object, a requirement of being
|
||||
// put into the paused list.
|
||||
//
|
||||
// This will get signaled on either a reactivate or a stopThread()
|
||||
//
|
||||
wait ();
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class Workers_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
struct TestCallback : Workers::Callback
|
||||
{
|
||||
explicit TestCallback (int count_)
|
||||
: finished (false, count_ == 0)
|
||||
, count (count_)
|
||||
{
|
||||
}
|
||||
|
||||
void processTask ()
|
||||
{
|
||||
if (--count == 0)
|
||||
finished.signal ();
|
||||
}
|
||||
|
||||
beast::WaitableEvent finished;
|
||||
std::atomic <int> count;
|
||||
};
|
||||
|
||||
void testThreads (int const threadCount)
|
||||
{
|
||||
testcase ("threadCount = " + std::to_string (threadCount));
|
||||
|
||||
TestCallback cb (threadCount);
|
||||
|
||||
Workers w (cb, "Test", 0);
|
||||
expect (w.getNumberOfThreads () == 0);
|
||||
|
||||
w.setNumberOfThreads (threadCount);
|
||||
expect (w.getNumberOfThreads () == threadCount);
|
||||
|
||||
for (int i = 0; i < threadCount; ++i)
|
||||
w.addTask ();
|
||||
|
||||
// 10 seconds should be enough to finish on any system
|
||||
//
|
||||
bool signaled = cb.finished.wait (10 * 1000);
|
||||
expect (signaled, "timed out");
|
||||
|
||||
w.pauseAllThreadsAndWait ();
|
||||
|
||||
// We had better finished all our work!
|
||||
expect (cb.count.load () == 0, "Did not complete task!");
|
||||
}
|
||||
|
||||
void run ()
|
||||
{
|
||||
testThreads (0);
|
||||
testThreads (1);
|
||||
testThreads (2);
|
||||
testThreads (4);
|
||||
testThreads (16);
|
||||
testThreads (64);
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(Workers,core,ripple);
|
||||
|
||||
} // beast
|
||||
Reference in New Issue
Block a user