2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/thread_pool.hpp>
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/threading.hpp>
|
2019-07-09 00:06:22 +00:00
|
|
|
|
2018-11-17 21:07:04 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace thread
|
|
|
|
{
|
|
|
|
void
|
|
|
|
ThreadPool::join()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& t : m_threads)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (t.joinable())
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_createdThreads = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::runJobs()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
while (m_status.load(std::memory_order_relaxed) == Status::Run)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
auto functor = m_queue.tryPopFront();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (functor.has_value())
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
functor.value()();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_idleThreads++;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_status == Status::Run && m_queue.empty())
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_semaphore.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_idleThreads.fetch_sub(1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::drainQueue()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
while (m_status.load(std::memory_order_relaxed) == Status::Drain)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
auto functor = m_queue.tryPopFront();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!functor)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
functor.value()();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::waitThreads()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unique_lock<std::mutex> lock(m_gateMutex);
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
m_numThreadsCV.wait(lock, [this] { return allThreadsReady(); });
|
2018-11-17 21:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::releaseThreads()
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_gateMutex);
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
m_numThreadsReady = 0;
|
|
|
|
++m_gateCount;
|
|
|
|
}
|
|
|
|
m_gateCV.notify_all();
|
2018-11-17 21:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::interrupt()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_gateMutex);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
size_t count = m_idleThreads;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_semaphore.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::worker()
|
|
|
|
{
|
2019-03-03 20:51:47 +00:00
|
|
|
// Lock will be valid until the end of the statement
|
2020-04-07 18:38:56 +00:00
|
|
|
size_t gateCount = (std::lock_guard<std::mutex>(m_gateMutex), m_gateCount);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2019-07-09 00:06:22 +00:00
|
|
|
util::SetThreadName(m_name);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (;;)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unique_lock<std::mutex> lock(m_gateMutex);
|
2018-11-17 21:07:04 +00:00
|
|
|
++m_numThreadsReady;
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
m_numThreadsCV.notify_one();
|
2018-11-17 21:07:04 +00:00
|
|
|
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
m_gateCV.wait(lock, [&] { return gateCount != m_gateCount; });
|
2018-11-17 21:07:04 +00:00
|
|
|
|
|
|
|
gateCount = m_gateCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status status = m_status.load(std::memory_order_relaxed);
|
|
|
|
|
|
|
|
// Can't use a switch here as we want to load and fall through.
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (status == Status::Run)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
runJobs();
|
|
|
|
status = m_status;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (status == Status::Drain)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
drainQueue();
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (status == Status::Suspend)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(status == Status::Stop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThreadPool::spawn()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
m_threads.at(m_createdThreads) = std::thread(std::bind(&ThreadPool::worker, this));
|
2018-11-17 21:07:04 +00:00
|
|
|
++m_createdThreads;
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
catch (const std::system_error&)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
ThreadPool::ThreadPool(size_t numThreads, size_t maxJobs, std::string_view name)
|
2018-11-17 21:07:04 +00:00
|
|
|
: m_queue(maxJobs)
|
|
|
|
, m_semaphore(0)
|
|
|
|
, m_idleThreads(0)
|
|
|
|
, m_status(Status::Stop)
|
|
|
|
, m_gateCount(0)
|
|
|
|
, m_numThreadsReady(0)
|
2019-07-09 00:06:22 +00:00
|
|
|
, m_name(name)
|
2018-11-17 21:07:04 +00:00
|
|
|
, m_threads(numThreads)
|
|
|
|
, m_createdThreads(0)
|
|
|
|
{
|
|
|
|
assert(numThreads != 0);
|
|
|
|
assert(maxJobs != 0);
|
|
|
|
disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPool::~ThreadPool()
|
|
|
|
{
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThreadPool::addJob(const Job& job)
|
|
|
|
{
|
|
|
|
assert(job);
|
|
|
|
|
|
|
|
QueueReturn ret = m_queue.pushBack(job);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ret == QueueReturn::Success && m_idleThreads > 0)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_semaphore.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == QueueReturn::Success;
|
|
|
|
}
|
|
|
|
bool
|
|
|
|
ThreadPool::addJob(Job&& job)
|
|
|
|
{
|
|
|
|
assert(job);
|
|
|
|
QueueReturn ret = m_queue.pushBack(std::move(job));
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ret == QueueReturn::Success && m_idleThreads > 0)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_semaphore.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == QueueReturn::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThreadPool::tryAddJob(const Job& job)
|
|
|
|
{
|
|
|
|
assert(job);
|
|
|
|
QueueReturn ret = m_queue.tryPushBack(job);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ret == QueueReturn::Success && m_idleThreads > 0)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_semaphore.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == QueueReturn::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThreadPool::tryAddJob(Job&& job)
|
|
|
|
{
|
|
|
|
assert(job);
|
|
|
|
QueueReturn ret = m_queue.tryPushBack(std::move(job));
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ret == QueueReturn::Success && m_idleThreads > 0)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_semaphore.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == QueueReturn::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::drain()
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock lock(m_mutex);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_status.load(std::memory_order_relaxed) == Status::Run)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_status = Status::Drain;
|
|
|
|
|
|
|
|
interrupt();
|
|
|
|
waitThreads();
|
|
|
|
|
|
|
|
m_status = Status::Run;
|
|
|
|
|
|
|
|
releaseThreads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::shutdown()
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock lock(m_mutex);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_status.load(std::memory_order_relaxed) == Status::Run)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_queue.disable();
|
|
|
|
m_status = Status::Stop;
|
|
|
|
|
|
|
|
interrupt();
|
|
|
|
m_queue.removeAll();
|
|
|
|
|
|
|
|
join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThreadPool::start()
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock lock(m_mutex);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_status.load(std::memory_order_relaxed) != Status::Stop)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto it = (m_threads.begin() + m_createdThreads); it != m_threads.end(); ++it)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!spawn())
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
releaseThreads();
|
|
|
|
|
|
|
|
join();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
waitThreads();
|
|
|
|
|
|
|
|
m_queue.enable();
|
|
|
|
m_status = Status::Run;
|
|
|
|
|
|
|
|
// `releaseThreads` has a release barrier so workers don't return from
|
|
|
|
// wait and not see the above store.
|
|
|
|
|
|
|
|
releaseThreads();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ThreadPool::stop()
|
|
|
|
{
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
util::Lock lock(m_mutex);
|
2018-11-17 21:07:04 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_status.load(std::memory_order_relaxed) == Status::Run)
|
2018-11-17 21:07:04 +00:00
|
|
|
{
|
|
|
|
m_queue.disable();
|
|
|
|
m_status = Status::Drain;
|
|
|
|
|
|
|
|
// `interrupt` has an acquire barrier (locks a mutex), so nothing will
|
|
|
|
// be executed before the above store to `status`.
|
|
|
|
interrupt();
|
|
|
|
|
|
|
|
waitThreads();
|
|
|
|
|
|
|
|
m_status = Status::Stop;
|
|
|
|
|
|
|
|
// `releaseThreads` has a release barrier so workers don't return from
|
|
|
|
// wait and not see the above store.
|
|
|
|
|
|
|
|
releaseThreads();
|
|
|
|
|
|
|
|
join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace thread
|
|
|
|
} // namespace llarp
|