summaryrefslogtreecommitdiff
path: root/src/lib/signal.h
blob: 80c6811d554774e1188198cc4796444de8f28c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
    Copyright (C) 2025 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#ifndef DCPOMATIC_SIGNAL_H
#define DCPOMATIC_SIGNAL_H


#include "cross.h"
#include <boost/thread.hpp>
#include <iostream>


class SignalBase
{
public:
	virtual ~SignalBase() = default;
	virtual void disconnect(int id) = 0;
};


/** @class Connection
 *  @brief Record of a connection to a signal, so it can be disconnected.
 */
class Connection
{
public:
	Connection(SignalBase& signal, int id)
		: _signal(signal)
		, _id(id)
	{}

	Connection& operator=(Connection const& other)
	{
		_signal = other._signal;
		_id = other._id;
		return *this;
	}

	void disconnect()
	{
		_signal.disconnect(_id);
	}

private:
	SignalBase& _signal;
	int _id;
};


/** @class ScopedConnection
 *  @brief Connection that disconnects itself on destruction.
 */
class ScopedConnection
{
public:
	ScopedConnection() {}

	ScopedConnection(Connection connection)
		: _connection(std::move(connection))
	{}

	ScopedConnection(ScopedConnection const& other) = delete;

	ScopedConnection(ScopedConnection&& other)
		: _connection(std::move(other._connection))
	{}

	ScopedConnection& operator=(ScopedConnection const& other) = delete;

	ScopedConnection& operator=(ScopedConnection&& other)
	{
		if (this != &other) {
			_connection = other._connection;
			other._connection.reset();
		}
		return *this;
	}

	~ScopedConnection()
	{
		if (_connection) {
			_connection->disconnect();
		}
	}

private:
	boost::optional<Connection> _connection;
};


namespace pending {

extern boost::mutex mutex;
extern std::map<boost::thread::id, std::list<std::function<void ()>>> callbacks;
extern void process();

};


class ThreadWaker
{
public:
	virtual void wake() {}
};


extern ThreadWaker* thread_waker;


template <typename Parameters>
class Signal : public SignalBase
{
private:
	struct Callback
	{
		int id;
		boost::thread::id thread;
		/* XXX */
		std::string thread_name;
		std::function<Parameters> function;
	};

public:
	Connection connect(std::function<Parameters> callback)
	{
		boost::mutex::scoped_lock lm(_mutex);
		auto const id = _id++;
		auto const thread_id = boost::this_thread::get_id();
		_callbacks.push_back(Callback{id, thread_id, thread_name(), callback});
		return Connection(*this, id);
	}

	void disconnect(int id) override
	{
		boost::mutex::scoped_lock lm(_mutex);
		auto iter = std::find_if(_callbacks.begin(), _callbacks.end(), [id](Callback const& callback) { return callback.id == id; });
		if (iter != _callbacks.end()) {
			_callbacks.erase(iter);
		}
	}

	template <typename... Args>
	void operator()(Args...args)
	{
		emit(args...);
	}

	template <typename... Args>
	void emit(Args... args)
	{
                boost::mutex::scoped_lock lm(_mutex);
                auto thread_id = boost::this_thread::get_id();
		std::cout << "emit with " << _callbacks.size() << "\n";
                for (auto const& callback: _callbacks) {
                        if (thread_id == callback.thread) {
				std::cout << "simple emit.\n";
                                callback.function(args...);
                        } else {
				std::cout << "x-thread emit from " << thread_name() << " to " << callback.thread_name << "\n";
                                boost::mutex::scoped_lock lm2(pending::mutex);
				pending::callbacks[callback.thread].push_back(boost::bind(callback.function, args...));
				if (thread_waker) {
					std::cout << "x-thread emit wakes shit.\n";
					thread_waker->wake();
				}
                        }
                }
	}

private:
	boost::mutex _mutex;
	int _id = 0;
        std::list<Callback> _callbacks;
};


#endif