From beddcf1a0142d7f21d48851d9f63c3aa6d8fd883 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sun, 21 Nov 2021 04:11:22 +0100 Subject: [PATCH] Test cross thread invalidation-record This also tests 2 receivers connected to the same signal --- tools/signal-test/signal-test.cc | 82 ++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/tools/signal-test/signal-test.cc b/tools/signal-test/signal-test.cc index 6f4ae58241..aa4add19ca 100644 --- a/tools/signal-test/signal-test.cc +++ b/tools/signal-test/signal-test.cc @@ -4,60 +4,100 @@ #include "pbd/pbd.h" #include "pbd/signals.h" +#include "pbd/event_loop.h" class Tx { public: PBD::Signal1 sig1; }; -class Rx { +/* ****************************************************************************/ + +class Rx1 { public: - Rx (Tx& sender) { - sender.sig1.connect_same_thread (_connection, boost::bind (&Rx::cb, this, _1)); + Rx1 (Tx& sender) { + sender.sig1.connect_same_thread (_connection, boost::bind (&Rx1::cb, this, _1)); + } + +private: + void cb (int i) { + printf ("Rx1 %d\n", i); + } + + PBD::ScopedConnection _connection; +}; + +/* ****************************************************************************/ + +struct MyInvalidationRecord : public PBD::EventLoop::InvalidationRecord +{ + ~MyInvalidationRecord () { + assert (use_count () == 0); + } +}; + +MyInvalidationRecord _ir; + +class Rx2 : public PBD::ScopedConnectionList { +public: + Rx2 (Tx& sender) { + sender.sig1.connect (*this, &_ir, boost::bind (&Rx2::cb, this, _1), /* PBD::EventLoop */ 0); } private: void cb (int i) { printf ("CB %d\n", i); } - - PBD::ScopedConnection _connection; }; +/* ****************************************************************************/ + pthread_barrier_t barrier; -static void* delete_t (void* arg) { - Tx* t = static_cast (arg); +static void* delete_tx (void* arg) { + Tx* tx = static_cast (arg); pthread_barrier_wait (&barrier); - delete t; + delete tx; //printf ("Deleted tx\n"); return 0; } -static void* delete_r (void* arg) { - Rx* r = static_cast (arg); +static void* delete_rx1 (void* arg) { + Rx1* rx1 = static_cast (arg); pthread_barrier_wait (&barrier); - delete r; - //printf ("Deleted rx\n"); + delete rx1; + //printf ("Deleted rx1\n"); return 0; } +static void* delete_rx2 (void* arg) { + Rx2* rx2 = static_cast (arg); + pthread_barrier_wait (&barrier); + delete rx2; + //printf ("Deleted rx2\n"); + return 0; +} + +/* ****************************************************************************/ + int main (int argc, char** argv) { PBD::init (); - Tx* t = new Tx (); - Rx* r = new Rx (*t); + Tx* tx = new Tx (); + Rx1* rx1 = new Rx1 (*tx); + Rx2* rx2 = new Rx2 (*tx); - //t->sig1 (11); /* EMIT SIGNAL */ + pthread_barrier_init (&barrier, NULL, 3); + pthread_t t[3]; - pthread_barrier_init (&barrier, NULL, 2); - pthread_t dt, dr; - pthread_create (&dt, NULL, delete_t, (void*)t); - pthread_create (&dr, NULL, delete_r, (void*)r); + pthread_create (&t[0], NULL, delete_tx, (void*)tx); + pthread_create (&t[1], NULL, delete_rx1, (void*)rx1); + pthread_create (&t[2], NULL, delete_rx2, (void*)rx2); - pthread_join (dt, NULL); - pthread_join (dr, NULL); + for (int i = 0; i < 3; ++i) { + pthread_join (t[i], NULL); + } pthread_barrier_destroy (&barrier); PBD::cleanup ();