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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
| ...
#define SIGSLOT_DEFAULT_MT_POLICY single_threaded
#if defined(SIGSLOT_PURE_ISO) || \ (!defined(WEBRTC_WIN) && !defined(__GNUG__) && \ !defined(SIGSLOT_USE_POSIX_THREADS)) #define _SIGSLOT_SINGLE_THREADED #elif defined(WEBRTC_WIN) #define _SIGSLOT_HAS_WIN32_THREADS #if !defined(WIN32_LEAN_AND_MEAN) #define WIN32_LEAN_AND_MEAN #endif #include "webrtc/rtc_base/win32.h" #elif defined(__GNUG__) || defined(SIGSLOT_USE_POSIX_THREADS) #define _SIGSLOT_HAS_POSIX_THREADS #include <pthread.h> #else #define _SIGSLOT_SINGLE_THREADED #endif
#ifndef SIGSLOT_DEFAULT_MT_POLICY #ifdef _SIGSLOT_SINGLE_THREADED #define SIGSLOT_DEFAULT_MT_POLICY single_threaded #else #define SIGSLOT_DEFAULT_MT_POLICY multi_threaded_local #endif #endif
namespace sigslot {
...
#ifdef _SIGSLOT_HAS_WIN32_THREADS
class multi_threaded_global { public: multi_threaded_global() { static bool isinitialised = false;
if (!isinitialised) { InitializeCriticalSection(get_critsec()); isinitialised = true; } }
void lock() { EnterCriticalSection(get_critsec()); }
void unlock() { LeaveCriticalSection(get_critsec()); }
private: CRITICAL_SECTION* get_critsec() { static CRITICAL_SECTION g_critsec; return &g_critsec; } };
class multi_threaded_local { public: multi_threaded_local() { InitializeCriticalSection(&m_critsec); }
multi_threaded_local(const multi_threaded_local&) { InitializeCriticalSection(&m_critsec); }
~multi_threaded_local() { DeleteCriticalSection(&m_critsec); }
void lock() { EnterCriticalSection(&m_critsec); }
void unlock() { LeaveCriticalSection(&m_critsec); }
private: CRITICAL_SECTION m_critsec; }; #endif
#ifdef _SIGSLOT_HAS_POSIX_THREADS
class multi_threaded_global { public: void lock() { pthread_mutex_lock(get_mutex()); } void unlock() { pthread_mutex_unlock(get_mutex()); }
private: static pthread_mutex_t* get_mutex(); };
class multi_threaded_local { public: multi_threaded_local() { pthread_mutex_init(&m_mutex, nullptr); } multi_threaded_local(const multi_threaded_local&) { pthread_mutex_init(&m_mutex, nullptr); } ~multi_threaded_local() { pthread_mutex_destroy(&m_mutex); } void lock() { pthread_mutex_lock(&m_mutex); } void unlock() { pthread_mutex_unlock(&m_mutex); }
private: pthread_mutex_t m_mutex; }; #endif
template <class mt_policy> class lock_block { public: mt_policy* m_mutex;
lock_block(mt_policy* mtx) : m_mutex(mtx) { m_mutex->lock(); }
~lock_block() { m_mutex->unlock(); } };
class _signal_base_interface;
class has_slots_interface {
...
public: void signal_connect(_signal_base_interface* sender) { ... }
void signal_disconnect(_signal_base_interface* sender) { ... }
void disconnect_all() { ... } };
class _signal_base_interface { ...
public: void slot_disconnect(has_slots_interface* pslot) { ... }
void slot_duplicate(const has_slots_interface* poldslot, has_slots_interface* pnewslot) { ... } };
class _opaque_connection { private: typedef void (*emit_t)(const _opaque_connection*);
template <typename FromT, typename ToT> union union_caster { FromT from; ToT to; };
emit_t pemit; has_slots_interface* pdest; unsigned char pmethod[16];
public: template <typename DestT, typename... Args> _opaque_connection(DestT* pd, void (DestT::*pm)(Args...)) : pdest(pd) { typedef void (DestT::*pm_t)(Args...); static_assert(sizeof(pm_t) <= sizeof(pmethod), "Size of slot function pointer too large.");
std::memcpy(pmethod, &pm, sizeof(pm_t));
typedef void (*em_t)(const _opaque_connection* self, Args...);
union_caster<em_t, emit_t> caster2; caster2.from = &_opaque_connection::emitter<DestT, Args...>; pemit = caster2.to; }
has_slots_interface* getdest() const { return pdest; }
...
template <typename... Args> void emit(Args... args) const { typedef void (*em_t)(const _opaque_connection*, Args...); union_caster<emit_t, em_t> caster; caster.from = pemit; (caster.to)(this, args...); }
private: template <typename DestT, typename... Args> static void emitter(const _opaque_connection* self, Args... args) { typedef void (DestT::*pm_t)(Args...); pm_t pm; std::memcpy(&pm, self->pmethod, sizeof(pm_t)); (static_cast<DestT*>(self->pdest)->*(pm))(args...); } };
template <class mt_policy> class _signal_base : public _signal_base_interface, public mt_policy { protected: typedef std::list<_opaque_connection> connections_list;
public: ...
protected: connections_list m_connected_slots; ...
};
template <class mt_policy = SIGSLOT_DEFAULT_MT_POLICY> class has_slots : public has_slots_interface, public mt_policy { private: typedef std::set<_signal_base_interface*> sender_set; typedef sender_set::const_iterator const_iterator;
public: has_slots() : has_slots_interface(&has_slots::do_signal_connect, &has_slots::do_signal_disconnect, &has_slots::do_disconnect_all) {}
...
private: has_slots& operator=(has_slots const&);
static void do_signal_connect(has_slots_interface* p, _signal_base_interface* sender) { has_slots* const self = static_cast<has_slots*>(p); lock_block<mt_policy> lock(self); self->m_senders.insert(sender); }
static void do_signal_disconnect(has_slots_interface* p, _signal_base_interface* sender) { has_slots* const self = static_cast<has_slots*>(p); lock_block<mt_policy> lock(self); self->m_senders.erase(sender); }
...
private: sender_set m_senders; };
template <class mt_policy, typename... Args> class signal_with_thread_policy : public _signal_base<mt_policy> { public:
...
template <class desttype> void connect(desttype* pclass, void (desttype::*pmemfun)(Args...)) { lock_block<mt_policy> lock(this); this->m_connected_slots.push_back(_opaque_connection(pclass, pmemfun));
pclass->signal_connect(static_cast<_signal_base_interface*>(this)); }
void emit(Args... args) { lock_block<mt_policy> lock(this); this->m_current_iterator = this->m_connected_slots.begin(); while (this->m_current_iterator != this->m_connected_slots.end()) { _opaque_connection const& conn = *this->m_current_iterator; ++(this->m_current_iterator);
conn.emit<Args...>(args...); } }
void operator()(Args... args) { emit(args...); } };
template <typename... Args> using signal = signal_with_thread_policy<SIGSLOT_DEFAULT_MT_POLICY, Args...>;
template <typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> using signal0 = signal_with_thread_policy<mt_policy>;
template <typename A1, typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> using signal1 = signal_with_thread_policy<mt_policy, A1>;
template <typename A1, typename A2, typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> using signal2 = signal_with_thread_policy<mt_policy, A1, A2>;
...
}
|