2021-11-21 02:27:46 -05:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
2021-11-20 12:38:53 -05:00
|
|
|
#include <cstdlib>
|
2021-11-21 02:27:46 -05:00
|
|
|
|
|
|
|
#include <getopt.h>
|
2021-11-20 12:38:53 -05:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
#include "pbd/event_loop.h"
|
2021-11-20 12:38:53 -05:00
|
|
|
#include "pbd/pbd.h"
|
2021-11-21 02:27:46 -05:00
|
|
|
#include "pbd/pcg_rand.h"
|
2021-11-20 12:38:53 -05:00
|
|
|
#include "pbd/signals.h"
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
class Tx
|
|
|
|
{
|
2021-11-20 12:38:53 -05:00
|
|
|
public:
|
|
|
|
PBD::Signal1<void, int> sig1;
|
|
|
|
};
|
|
|
|
|
2021-11-20 22:11:22 -05:00
|
|
|
/* ****************************************************************************/
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
class Rx1
|
|
|
|
{
|
2021-11-20 12:38:53 -05:00
|
|
|
public:
|
2021-11-21 02:27:46 -05:00
|
|
|
Rx1 (Tx& sender)
|
|
|
|
{
|
2024-11-17 14:19:33 -05:00
|
|
|
sender.sig1.connect_same_thread (_connection, std::bind (&Rx1::cb, this, _1));
|
2021-11-20 12:38:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-11-21 02:27:46 -05:00
|
|
|
void cb (int i)
|
|
|
|
{
|
|
|
|
printf ("Rx1(%d) ", i);
|
2021-11-20 12:38:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
PBD::ScopedConnection _connection;
|
|
|
|
};
|
|
|
|
|
2021-11-20 22:11:22 -05:00
|
|
|
/* ****************************************************************************/
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
class MyEventLoop : public sigc::trackable, public PBD::EventLoop
|
2021-11-20 22:11:22 -05:00
|
|
|
{
|
2021-11-21 02:27:46 -05:00
|
|
|
public:
|
|
|
|
MyEventLoop (std::string const& name)
|
|
|
|
: EventLoop (name)
|
|
|
|
{
|
|
|
|
run_loop_thread = Glib::Threads::Thread::self ();
|
|
|
|
}
|
|
|
|
|
2024-08-16 15:18:31 -04:00
|
|
|
void call_slot (InvalidationRecord* ir, const std::function<void ()>& f)
|
2021-11-21 02:27:46 -05:00
|
|
|
{
|
|
|
|
if (Glib::Threads::Thread::self () == run_loop_thread) {
|
|
|
|
f ();
|
|
|
|
} else {
|
|
|
|
assert (!ir);
|
|
|
|
assert (0);
|
|
|
|
f (); // XXX really queue and process during run ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run ()
|
|
|
|
{
|
|
|
|
; // process Events, if any
|
|
|
|
}
|
|
|
|
|
2023-04-21 14:02:22 -04:00
|
|
|
Glib::Threads::RWlock& slot_invalidation_rwlock ()
|
2021-11-21 02:27:46 -05:00
|
|
|
{
|
|
|
|
return request_buffer_map_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Glib::Threads::Thread* run_loop_thread;
|
2023-04-21 14:02:22 -04:00
|
|
|
Glib::Threads::RWLock request_buffer_map_lock;
|
2021-11-21 02:27:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MyInvalidationRecord : public PBD::EventLoop::InvalidationRecord {
|
|
|
|
~MyInvalidationRecord ()
|
|
|
|
{
|
2021-11-20 22:11:22 -05:00
|
|
|
assert (use_count () == 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
static MyEventLoop event_loop ("foo");
|
|
|
|
static MyInvalidationRecord _ir;
|
2021-11-20 22:11:22 -05:00
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
class Rx2 : public PBD::ScopedConnectionList
|
|
|
|
{
|
2021-11-20 22:11:22 -05:00
|
|
|
public:
|
2021-11-21 02:27:46 -05:00
|
|
|
Rx2 (Tx& sender)
|
|
|
|
{
|
2024-11-17 14:19:33 -05:00
|
|
|
sender.sig1.connect (*this, &_ir, std::bind (&Rx2::cb, this, _1), &event_loop);
|
2021-11-20 22:11:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-11-21 02:27:46 -05:00
|
|
|
void cb (int i)
|
|
|
|
{
|
|
|
|
printf ("Rx2(%d) ", i);
|
2021-11-20 22:11:22 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ****************************************************************************/
|
|
|
|
|
2021-11-20 12:38:53 -05:00
|
|
|
pthread_barrier_t barrier;
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
static void*
|
|
|
|
delete_tx (void* arg)
|
|
|
|
{
|
2021-11-20 22:11:22 -05:00
|
|
|
Tx* tx = static_cast<Tx*> (arg);
|
2021-11-20 12:38:53 -05:00
|
|
|
pthread_barrier_wait (&barrier);
|
2021-11-20 22:11:22 -05:00
|
|
|
delete tx;
|
2021-11-20 12:38:53 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
static void*
|
|
|
|
delete_rx1 (void* arg)
|
|
|
|
{
|
2021-11-20 22:11:22 -05:00
|
|
|
Rx1* rx1 = static_cast<Rx1*> (arg);
|
2021-11-20 12:38:53 -05:00
|
|
|
pthread_barrier_wait (&barrier);
|
2021-11-20 22:11:22 -05:00
|
|
|
delete rx1;
|
2021-11-20 12:38:53 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
static void*
|
|
|
|
delete_rx2 (void* arg)
|
|
|
|
{
|
2021-11-20 22:11:22 -05:00
|
|
|
Rx2* rx2 = static_cast<Rx2*> (arg);
|
|
|
|
pthread_barrier_wait (&barrier);
|
|
|
|
delete rx2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ****************************************************************************/
|
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
static PBD::PCGRand pcg;
|
|
|
|
static bool emit_signal = false;
|
|
|
|
|
|
|
|
static void
|
|
|
|
run_test ()
|
2021-11-20 12:38:53 -05:00
|
|
|
{
|
2021-11-21 02:27:46 -05:00
|
|
|
Tx* tx = new Tx ();
|
2021-11-20 22:11:22 -05:00
|
|
|
Rx1* rx1 = new Rx1 (*tx);
|
|
|
|
Rx2* rx2 = new Rx2 (*tx);
|
2021-11-20 12:38:53 -05:00
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
/* randomize thread start, not that it matters much since
|
|
|
|
* pthread_barrier_wait() leaves it undefined which thread
|
|
|
|
* continues with PTHREAD_BARRIER_SERIAL_THREAD, but some
|
|
|
|
* implementations may special-case the last */
|
|
|
|
static int rnd[3] = { 0, 1, 2 };
|
|
|
|
for (int i = 2; i > 0; --i) {
|
|
|
|
int j = pcg.rand (i + 1);
|
|
|
|
int tmp = rnd[i];
|
|
|
|
rnd[i] = rnd[j];
|
|
|
|
rnd[j] = tmp;
|
|
|
|
}
|
2021-11-20 12:38:53 -05:00
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
if (emit_signal) {
|
|
|
|
tx->sig1 (rnd[0]); /* EMIT SIGNAL */
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_t t[3];
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
switch (rnd[i]) {
|
|
|
|
case 0:
|
|
|
|
pthread_create (&t[0], NULL, delete_tx, (void*)tx);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
pthread_create (&t[1], NULL, delete_rx1, (void*)rx1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
pthread_create (&t[2], NULL, delete_rx2, (void*)rx2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-20 12:38:53 -05:00
|
|
|
|
2021-11-20 22:11:22 -05:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
pthread_join (t[i], NULL);
|
|
|
|
}
|
2021-11-20 12:38:53 -05:00
|
|
|
|
2021-11-21 02:27:46 -05:00
|
|
|
if (emit_signal) {
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char** argv)
|
|
|
|
{
|
|
|
|
int n_iter = 0;
|
|
|
|
const char* optstring = "ei:";
|
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
const struct option longopts[] = {
|
|
|
|
{ "emit", no_argument, 0, 'e' },
|
|
|
|
{ "iterations", required_argument, 0, 'i' },
|
|
|
|
};
|
|
|
|
/* clang-format on */
|
|
|
|
|
|
|
|
int c = 0;
|
|
|
|
while (EOF != (c = getopt_long (argc, argv, optstring, longopts, (int*)0))) {
|
|
|
|
switch (c) {
|
|
|
|
case 'e':
|
|
|
|
emit_signal = true;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
n_iter = atoi (optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "Error: unrecognized option.\n");
|
|
|
|
::exit (EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != argc) {
|
|
|
|
fprintf (stderr, "Error: unrecognized option.\n");
|
|
|
|
::exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_iter <= 0 || n_iter > 1000000) {
|
|
|
|
n_iter = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
PBD::init ();
|
|
|
|
pthread_barrier_init (&barrier, NULL, 3);
|
|
|
|
|
|
|
|
for (int i = 0; i < n_iter; ++i) {
|
|
|
|
run_test ();
|
|
|
|
}
|
|
|
|
|
2021-11-20 12:38:53 -05:00
|
|
|
pthread_barrier_destroy (&barrier);
|
|
|
|
PBD::cleanup ();
|
|
|
|
return 0;
|
|
|
|
}
|