Skip to content

Commit

Permalink
Ch-ch-ch-changes and improvements
Browse files Browse the repository at this point in the history
* co_thread is much cleaner now.
* Also it handles exceptions inside the thread and propagates them outside.
* Added signal::connect overload that returns a std::unique_ptr<has_slots> for convenient RAII-based lifetime.
* Wrote some words. Actual words.
  • Loading branch information
dwd committed Jul 24, 2024
1 parent 1e78eaa commit b518d82
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 94 deletions.
16 changes: 0 additions & 16 deletions README

This file was deleted.

56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# sigslot - C++11 Signal/Slot library

Originally written by Sarah Thompson.

Various patches and fixes applied by Cat Nap Games:

To make this compile under Xcode 4.3 with Clang 3.0 I made some changes myself and also used some diffs published in the original project's Sourceforge forum.
I don't remember which ones though.

C++11-erization (and C++2x-erixation, and mini coroutine library) by Dave Cridland:

See example.cc and co_example.cc for some documentation and a walk-through example, or read the tests.

This is public domain; no copyright is claimed or asserted.

No warranty is implied or offered either.

## Tagging and version

Until recently, I'd say just use HEAD. But some people are really keen on tags, so I'll do some semantic version tagging on this.

## Promising, yet oddly vague and sometimes outright misleading documentation

This library is a pure header library, and consists of four header files:

<sigslot/siglot.h>

This contains a sigslot::signal<T...> class, and a sigslot::has_slots class.

Signals can be connected to arbitrary functions, but in order to handle disconnect on lifetime termination, there's a "has_slots" base class to make it simpler.

Loosely, calling "emit(...)" on the signal will then call all the connected "slots", which are just arbitrary functions.

If a class is derived (publicly) from has_slots, you can pass in the instance of the class you want to control the lifetime. For calling a specific member directly, that's an easy decision; but if you pass in a lambda or some other arbitrary function, it might not be.

If there's nothing obvious to hand, something still needs to control the scope - leaving out the has_slots argument therefore returns you a (deliberately undocumented) placeholder class, which acts in lieu of a has_slots derived class of your choice.

<sigslot/tasklet.h>

This has a somewhat integrated coroutine library. Tasklets are coroutines, and like most coroutines they can be started, resumed, etc. There's no generator defined, just simple coroutines.

Tasklets expose co_await, so can be awaited by other coroutines. Signals can also be awaited upon, and will resolve to nothing (ie, void), or the single type, or a std::tuple of the types.

<sigslot/resume.h>

Coroutine resumption can be tricky, and is usually best integrated into some kind of event loop. Failure to do so will make it very hard to do anything that you couldn't do as well (or better!) without.

You can define your own resume function which will be called when a coroutine should be resumed, a trivial (and rather poor) example is at the beginning of the co_thread tests.

If you don't, then std::coroutine_handle<>::resume() will be called directly (which works for trivial cases, but not for anything useful).

<sigslot/cothread.h>

sigslot::co_thread is a convenient (but very simple) wrapper to run a non-coroutine in a std::jthread, but outwardly behave as a coroutine. Construct once, and it can be treated as a coroutine definition thereafter, and called multiple times.

This will not work with the built-in resumption, you'll need to implement *some* kind of event loop.
210 changes: 156 additions & 54 deletions sigslot/cothread.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,91 +10,193 @@
#include "sigslot/tasklet.h"

namespace sigslot {
template<typename Result, class... Args>
class co_thread {
public:
// Single argument version uses a bare T
namespace cothread_internal {
template<typename Result>
struct awaitable {
std::coroutine_handle<> awaiting = nullptr;
co_thread & wait_for;

explicit awaitable(co_thread & t) : wait_for(t) {}
awaitable() = default;
awaitable(awaitable && other) = delete;
awaitable(awaitable const &) = delete;

bool await_ready() {
return wait_for.has_payload();
return has_payload();
}

void await_suspend(std::coroutine_handle<> h) {
// The awaiting coroutine is already suspended.
awaiting = h;
wait_for.await(this);
await();
}

auto await_resume() {
return wait_for.payload();
return payload();
}

void resolve() {
std::coroutine_handle<> a = nullptr;
std::swap(a, awaiting);
if (a) sigslot::resume_switch(a);
}

template<typename Fn, typename ...Args>
void run(Fn & fn, Args&&... args) {
auto wrapped_fn = [this, &fn](Args... a) {
try {
auto result = fn(a...);
{
std::lock_guard l_(m_mutex);
m_payload.emplace(result);
resolve();
}
} catch(...) {
std::lock_guard l_(m_mutex);
m_eptr = std::current_exception();
resolve();
}
};
m_thread.emplace(wrapped_fn, args...);
}

void check_await() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
}

bool has_payload() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
std::lock_guard l_(m_mutex);
return m_eptr || m_payload.has_value();
}

auto payload() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
m_thread->join();
m_thread.reset();
if (m_eptr) std::rethrow_exception(m_eptr);
return *m_payload;
}

void await() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
std::lock_guard l_(m_mutex);
if (m_eptr || m_payload.has_value()) {
resolve();
}
}

private:
std::optional<std::jthread> m_thread;
std::optional<Result> m_payload;
std::recursive_mutex m_mutex;
std::exception_ptr m_eptr;
};
private:
std::function<Result(Args...)> m_fn;
std::optional<std::jthread> m_thread;
std::optional<Result> m_payload;
std::recursive_mutex m_mutex;
awaitable * m_awaitable = nullptr;
template<>
struct awaitable<void> {
std::coroutine_handle<> awaiting = nullptr;

public:
awaitable() = default;
awaitable(awaitable && other) = delete;
awaitable(awaitable const &) = delete;

bool await_ready() {
return is_done();
}

void await_suspend(std::coroutine_handle<> h) {
// The awaiting coroutine is already suspended.
awaiting = h;
await();
}

explicit co_thread(std::function<Result(Args...)> && fn) : m_fn(std::move(fn)) {}
void await_resume() {
done();
}

co_thread & run(Args&&... args) {
auto wrapped_fn = [this](Args... a) {
auto result = m_fn(a...);
{
std::lock_guard l_(m_mutex);
m_payload.emplace(result);
if (m_awaitable) {
m_awaitable->resolve();
void resolve() {
std::coroutine_handle<> a = nullptr;
std::swap(a, awaiting);
if (a) sigslot::resume_switch(a);
}

template<typename Fn, typename ...Args>
void run(Fn & fn, Args&&... args) {
auto wrapped_fn = [this, &fn](Args... a) {
try {
fn(a...);
{
std::lock_guard l_(m_mutex);
m_done = true;
resolve();
}
} catch(...) {
std::lock_guard l_(m_mutex);
m_eptr = std::current_exception();
resolve();
}
}
};
m_thread.emplace(wrapped_fn, args...);
return *this;
}
auto & operator() (Args&&... args) {
return this->run(args...);
}
};
m_thread.emplace(wrapped_fn, args...);
}

awaitable operator co_await() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
return awaitable(*this);
}
void check_await() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
}

bool has_payload() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
std::lock_guard l_(m_mutex);
return m_payload.has_value();
}
bool is_done() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
std::lock_guard l_(m_mutex);
return m_eptr || m_done;
}

auto payload() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
m_thread->join();
m_thread.reset();
return *m_payload;
}
void done() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
m_thread->join();
m_thread.reset();
if (m_eptr) std::rethrow_exception(m_eptr);
}

void await(awaitable * a) {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
std::lock_guard l_(m_mutex);
m_awaitable = a;
if (m_payload.has_value()) {
a->resolve();
void await() {
if (!m_thread.has_value()) throw std::logic_error("No thread started");
std::lock_guard l_(m_mutex);
if (m_eptr || m_done) {
resolve();
}
}

private:
std::optional<std::jthread> m_thread;
bool m_done = false;
std::exception_ptr m_eptr;
std::recursive_mutex m_mutex;
};
template<typename T>
struct awaitable_ptr {
std::unique_ptr<awaitable<T>> m_guts;

awaitable_ptr() : m_guts(std::make_unique<awaitable<T>>()) {}
awaitable_ptr(awaitable_ptr &&) = default;

awaitable<T> & operator co_await() {
m_guts->check_await();
return *m_guts;
}
};
}

template<typename Callable>
class co_thread {
public:
private:
Callable m_fn;
public:

template<typename ...Args>
[[nodiscard]] auto operator() (Args && ...args) {
cothread_internal::awaitable_ptr<decltype(m_fn(args...))> awaitable;
awaitable.m_guts->run(m_fn, args...);
return std::move(awaitable);
}

explicit co_thread(Callable && fn) : m_fn(std::move(fn)) {}
};
}

Expand Down
8 changes: 8 additions & 0 deletions sigslot/sigslot.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,19 @@ namespace sigslot {

// Helper for ptr-to-member; call the member function "normally".
template<class desttype>
requires std::derived_from<desttype, has_slots>
void connect(desttype *pclass, void (desttype::* memfn)(args...), bool one_shot = false)
{
this->connect(pclass, [pclass, memfn](args... a) { (pclass->*memfn)(a...); }, one_shot);
}

[[nodiscard]] std::unique_ptr<has_slots> connect(std::function<void(args...)> && fn, bool one_shot=false)
{
auto raii = std::make_unique<has_slots>();
this->connect(raii.get(), std::move(fn), one_shot);
return raii;
}

// This code uses the long-hand because it assumes it may mutate the list.
void emit(args... a)
{
Expand Down
Loading

0 comments on commit b518d82

Please sign in to comment.