Use lambda as trampoline to simplify thread functions

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-10-06 13:15:12 +02:00
parent 8732b1df5b
commit 47e63b1f32
2 changed files with 31 additions and 31 deletions

View File

@ -1168,32 +1168,32 @@ namespace stdex
length(0), length(0),
num_written(0) num_written(0)
{ {
*static_cast<std::thread*>(this) = std::thread(process_op, std::ref(*this)); *static_cast<std::thread*>(this) = std::thread([](_Inout_ worker& w) { w.process_op(); }, std::ref(*this));
} }
protected: protected:
static void process_op(_Inout_ worker& w) void process_op()
{ {
for (;;) { for (;;) {
std::unique_lock<std::mutex> lk(w.mutex); std::unique_lock<std::mutex> lk(mutex);
w.cv.wait(lk, [&] {return w.op != op_t::noop; }); cv.wait(lk, [&] {return op != op_t::noop; });
switch (w.op) { switch (op) {
case op_t::quit: case op_t::quit:
return; return;
case op_t::write: case op_t::write:
w.num_written = w.source->write(w.data, w.length); num_written = source->write(data, length);
break; break;
case op_t::close: case op_t::close:
w.source->close(); source->close();
break; break;
case op_t::flush: case op_t::flush:
w.source->flush(); source->flush();
break; break;
case op_t::noop:; case op_t::noop:;
} }
w.op = op_t::noop; op = op_t::noop;
lk.unlock(); lk.unlock();
w.cv.notify_one(); cv.notify_one();
} }
} }
@ -1249,7 +1249,7 @@ namespace stdex
public: public:
async_reader(_Inout_ basic& source) : async_reader(_Inout_ basic& source) :
converter(source), converter(source),
m_worker(process, std::ref(*this)) m_worker([](_Inout_ async_reader& w) { w.process(); }, std::ref(*this))
{} {}
virtual ~async_reader() virtual ~async_reader()
@ -1284,17 +1284,17 @@ namespace stdex
} }
protected: protected:
static void process(_Inout_ async_reader& w) void process()
{ {
for (;;) { for (;;) {
uint8_t* ptr; size_t num_write; uint8_t* ptr; size_t num_write;
std::tie(ptr, num_write) = w.m_ring.back(); std::tie(ptr, num_write) = m_ring.back();
if (!ptr) _Unlikely_ if (!ptr) _Unlikely_
break; break;
num_write = w.m_source->read(ptr, num_write); num_write = m_source->read(ptr, num_write);
w.m_ring.push(num_write); m_ring.push(num_write);
if (!w.m_source->ok()) { if (!m_source->ok()) {
w.m_ring.quit(); m_ring.quit();
break; break;
} }
} }
@ -1316,7 +1316,7 @@ namespace stdex
public: public:
async_writer(_Inout_ basic& source) : async_writer(_Inout_ basic& source) :
converter(source), converter(source),
m_worker(process, std::ref(*this)) m_worker([](_Inout_ async_writer& w) { w.process(); }, std::ref(*this))
{} {}
virtual ~async_writer() virtual ~async_writer()
@ -1356,17 +1356,17 @@ namespace stdex
} }
protected: protected:
static void process(_Inout_ async_writer& w) void process()
{ {
for (;;) { for (;;) {
uint8_t* ptr; size_t num_read; uint8_t* ptr; size_t num_read;
std::tie(ptr, num_read) = w.m_ring.front(); std::tie(ptr, num_read) = m_ring.front();
if (!ptr) if (!ptr)
break; break;
num_read = w.m_source->write(ptr, num_read); num_read = m_source->write(ptr, num_read);
w.m_ring.pop(num_read); m_ring.pop(num_read);
if (!w.m_source->ok()) { if (!m_source->ok()) {
w.m_ring.quit(); m_ring.quit();
break; break;
} }
} }

View File

@ -32,7 +32,7 @@ namespace stdex
m_quit(false), m_quit(false),
m_timeout(timeout), m_timeout(timeout),
m_callback(callback), m_callback(callback),
m_thread(run, std::ref(*this)) m_thread([](_Inout_ watchdog& wd) { wd.run(); }, std::ref(*this))
{} {}
/// ///
@ -64,17 +64,17 @@ namespace stdex
} }
protected: protected:
static void run(_Inout_ watchdog& wd) void run()
{ {
for (;;) { for (;;) {
std::unique_lock<std::mutex> lk(wd.m_mutex); std::unique_lock<std::mutex> lk(m_mutex);
auto phase = wd.m_phase; auto phase = m_phase;
if (wd.m_cv.wait_for(lk, wd.m_timeout, [&] {return wd.m_quit || phase != wd.m_phase; })) { if (m_cv.wait_for(lk, m_timeout, [&] {return m_quit || phase != m_phase; })) {
if (wd.m_quit) if (m_quit)
break; break;
} }
else { else {
wd.m_callback(); m_callback();
break; break;
} }
} }