lokinet/llarp/util/thread/logic.cpp

148 lines
2.8 KiB
C++
Raw Normal View History

2019-09-01 13:26:16 +00:00
#include <util/thread/logic.hpp>
2019-09-01 12:10:49 +00:00
#include <util/logging/logger.hpp>
#include <util/mem.h>
2018-04-30 18:18:18 +00:00
#include <future>
2018-12-10 14:14:55 +00:00
namespace llarp
{
Logic::Logic(size_t sz) : m_Thread(llarp_init_threadpool(1, "llarp-logic", sz))
{
llarp_threadpool_start(m_Thread);
/// set thread id
std::promise<ID_t> result;
// queue setting id and try to get the result back
llarp_threadpool_queue_job(m_Thread, [&]() {
m_ID.emplace(std::this_thread::get_id());
result.set_value(m_ID.value());
});
// get the result back
ID_t spawned = result.get_future().get();
LogDebug("logic thread spawned on ", spawned);
}
2019-09-05 21:28:50 +00:00
Logic::~Logic()
{
delete m_Thread;
2018-04-30 18:18:18 +00:00
}
size_t
Logic::numPendingJobs() const
{
return m_Thread->pendingJobs();
}
bool
2018-12-10 14:14:55 +00:00
Logic::queue_job(struct llarp_thread_job job)
2018-05-28 20:51:15 +00:00
{
return job.user && job.work && LogicCall(this, std::bind(job.work, job.user));
2018-05-28 20:51:15 +00:00
}
2018-12-10 14:14:55 +00:00
void
Logic::stop()
{
llarp::LogDebug("logic thread stop");
// stop all operations on threadpool
llarp_threadpool_stop(m_Thread);
2018-12-10 14:14:55 +00:00
}
2018-05-18 20:08:57 +00:00
2019-05-15 15:54:26 +00:00
bool
Logic::_traceLogicCall(std::function<void(void)> func, const char* tag, int line)
{
// wrap the function so that we ensure that it's always calling stuff one at
// a time
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
auto f = [self = this, func]() {
if (self->m_Queue)
2019-12-10 15:43:27 +00:00
{
func();
}
else
{
self->m_Killer.TryAccess(func);
}
};
if (can_flush())
2019-11-29 01:08:57 +00:00
{
f();
return true;
}
if (m_Queue)
{
m_Queue(f);
return true;
}
if (m_Thread->LooksFull(5))
{
LogErrorExplicit(
tag ? tag : LOG_TAG,
line ? line : __LINE__,
"holy crap, we are trying to queue a job "
"onto the logic thread but it looks full");
std::abort();
}
auto ret = llarp_threadpool_queue_job(m_Thread, f);
if (not ret)
{
}
return ret;
}
void
Logic::SetQueuer(std::function<void(std::function<void(void)>)> q)
{
m_Queue = q;
m_Queue([self = this]() { self->m_ID = std::this_thread::get_id(); });
}
uint32_t
Logic::call_later(llarp_time_t timeout, std::function<void(void)> func)
{
auto loop = m_Loop;
if (loop != nullptr)
{
return loop->call_after_delay(timeout, func);
}
return 0;
}
2018-12-10 14:14:55 +00:00
void
Logic::cancel_call(uint32_t id)
{
auto loop = m_Loop;
if (loop != nullptr)
{
loop->cancel_delayed_call(id);
}
2018-12-10 14:14:55 +00:00
}
2018-12-10 14:14:55 +00:00
void
Logic::remove_call(uint32_t id)
{
auto loop = m_Loop;
if (loop != nullptr)
{
loop->cancel_delayed_call(id);
}
2018-12-10 14:14:55 +00:00
}
bool
Logic::can_flush() const
{
return m_ID.value() == std::this_thread::get_id();
}
void
Logic::set_event_loop(llarp_ev_loop* loop)
{
m_Loop = loop;
}
void
Logic::clear_event_loop()
{
m_Loop = nullptr;
}
2018-12-10 14:14:55 +00:00
} // namespace llarp