Blame gobject/tests/threadtests.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 * Copyright (C) 2008 Imendio AB
Packit ae235b
 * Authors: Tim Janik
Packit ae235b
 *
Packit ae235b
 * This work is provided "as is"; redistribution and modification
Packit ae235b
 * in whole or in part, in any medium, physical or electronic is
Packit ae235b
 * permitted without restriction.
Packit ae235b
 *
Packit ae235b
 * This work is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit ae235b
 *
Packit ae235b
 * In no event shall the authors or contributors be liable for any
Packit ae235b
 * direct, indirect, incidental, special, exemplary, or consequential
Packit ae235b
 * damages (including, but not limited to, procurement of substitute
Packit ae235b
 * goods or services; loss of use, data, or profits; or business
Packit ae235b
 * interruption) however caused and on any theory of liability, whether
Packit ae235b
 * in contract, strict liability, or tort (including negligence or
Packit ae235b
 * otherwise) arising in any way out of the use of this software, even
Packit ae235b
 * if advised of the possibility of such damage.
Packit ae235b
 */
Packit ae235b
#define GLIB_DISABLE_DEPRECATION_WARNINGS
Packit ae235b
#include <glib.h>
Packit ae235b
#include <glib-object.h>
Packit ae235b
Packit ae235b
static volatile int mtsafe_call_counter = 0; /* multi thread safe call counter */
Packit ae235b
static int          unsafe_call_counter = 0; /* single-threaded call counter */
Packit ae235b
static GCond sync_cond;
Packit ae235b
static GMutex sync_mutex;
Packit ae235b
Packit ae235b
#define NUM_COUNTER_INCREMENTS 100000
Packit ae235b
Packit ae235b
static void
Packit ae235b
call_counter_init (gpointer tclass)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  for (i = 0; i < NUM_COUNTER_INCREMENTS; i++)
Packit ae235b
    {
Packit ae235b
      int saved_unsafe_call_counter = unsafe_call_counter;
Packit ae235b
      g_atomic_int_add (&mtsafe_call_counter, 1); /* real call count update */
Packit ae235b
      g_thread_yield(); /* let concurrent threads corrupt the unsafe_call_counter state */
Packit ae235b
      unsafe_call_counter = 1 + saved_unsafe_call_counter; /* non-atomic counter update */
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void interface_per_class_init (void) { call_counter_init (NULL); }
Packit ae235b
Packit ae235b
/* define 3 test interfaces */
Packit ae235b
typedef GTypeInterface MyFace0Interface;
Packit ae235b
static GType my_face0_get_type (void);
Packit ae235b
G_DEFINE_INTERFACE (MyFace0, my_face0, G_TYPE_OBJECT)
Packit ae235b
static void my_face0_default_init (MyFace0Interface *iface) { call_counter_init (iface); }
Packit ae235b
typedef GTypeInterface MyFace1Interface;
Packit ae235b
static GType my_face1_get_type (void);
Packit ae235b
G_DEFINE_INTERFACE (MyFace1, my_face1, G_TYPE_OBJECT)
Packit ae235b
static void my_face1_default_init (MyFace1Interface *iface) { call_counter_init (iface); }
Packit ae235b
Packit ae235b
/* define 3 test objects, adding interfaces 0 & 1, and adding interface 2 after class initialization */
Packit ae235b
typedef GObject         MyTester0;
Packit ae235b
typedef GObjectClass    MyTester0Class;
Packit ae235b
static GType my_tester0_get_type (void);
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (MyTester0, my_tester0, G_TYPE_OBJECT,
Packit ae235b
                         G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init)
Packit ae235b
                         G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init))
Packit ae235b
static void my_tester0_init (MyTester0*t) {}
Packit ae235b
static void my_tester0_class_init (MyTester0Class*c) { call_counter_init (c); }
Packit ae235b
typedef GObject         MyTester1;
Packit ae235b
typedef GObjectClass    MyTester1Class;
Packit ae235b
Packit ae235b
/* Disabled for now (see https://bugzilla.gnome.org/show_bug.cgi?id=687659) */
Packit ae235b
#if 0
Packit ae235b
typedef GTypeInterface MyFace2Interface;
Packit ae235b
static GType my_face2_get_type (void);
Packit ae235b
G_DEFINE_INTERFACE (MyFace2, my_face2, G_TYPE_OBJECT)
Packit ae235b
static void my_face2_default_init (MyFace2Interface *iface) { call_counter_init (iface); }
Packit ae235b
Packit ae235b
static GType my_tester1_get_type (void);
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (MyTester1, my_tester1, G_TYPE_OBJECT,
Packit ae235b
                         G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init)
Packit ae235b
                         G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init))
Packit ae235b
static void my_tester1_init (MyTester1*t) {}
Packit ae235b
static void my_tester1_class_init (MyTester1Class*c) { call_counter_init (c); }
Packit ae235b
typedef GObject         MyTester2;
Packit ae235b
typedef GObjectClass    MyTester2Class;
Packit ae235b
static GType my_tester2_get_type (void);
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (MyTester2, my_tester2, G_TYPE_OBJECT,
Packit ae235b
                         G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init)
Packit ae235b
                         G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init))
Packit ae235b
static void my_tester2_init (MyTester2*t) {}
Packit ae235b
static void my_tester2_class_init (MyTester2Class*c) { call_counter_init (c); }
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
tester_init_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  const GInterfaceInfo face2_interface_info = { (GInterfaceInitFunc) interface_per_class_init, NULL, NULL };
Packit ae235b
  gpointer klass;
Packit ae235b
  /* first, syncronize with other threads,
Packit ae235b
   * then run interface and class initializers,
Packit ae235b
   * using unsafe_call_counter concurrently
Packit ae235b
   */
Packit ae235b
  g_mutex_lock (&sync_mutex);
Packit ae235b
  g_mutex_unlock (&sync_mutex);
Packit ae235b
  /* test default interface initialization for face0 */
Packit ae235b
  g_type_default_interface_unref (g_type_default_interface_ref (my_face0_get_type()));
Packit ae235b
  /* test class initialization, face0 per-class initializer, face1 default and per-class initializer */
Packit ae235b
  klass = g_type_class_ref ((GType) data);
Packit ae235b
  /* test face2 default and per-class initializer, after class_init */
Packit ae235b
  g_type_add_interface_static (G_TYPE_FROM_CLASS (klass), my_face2_get_type(), &face2_interface_info);
Packit ae235b
  /* cleanups */
Packit ae235b
  g_type_class_unref (klass);
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_threaded_class_init (void)
Packit ae235b
{
Packit ae235b
  GThread *t1, *t2, *t3;
Packit ae235b
Packit ae235b
  /* pause newly created threads */
Packit ae235b
  g_mutex_lock (&sync_mutex);
Packit ae235b
Packit ae235b
  /* create threads */
Packit ae235b
  t1 = g_thread_create (tester_init_thread, (gpointer) my_tester0_get_type(), TRUE, NULL);
Packit ae235b
  t2 = g_thread_create (tester_init_thread, (gpointer) my_tester1_get_type(), TRUE, NULL);
Packit ae235b
  t3 = g_thread_create (tester_init_thread, (gpointer) my_tester2_get_type(), TRUE, NULL);
Packit ae235b
Packit ae235b
  /* execute threads */
Packit ae235b
  g_mutex_unlock (&sync_mutex);
Packit ae235b
  while (g_atomic_int_get (&mtsafe_call_counter) < (3 + 3 + 3 * 3) * NUM_COUNTER_INCREMENTS)
Packit ae235b
    {
Packit ae235b
      if (g_test_verbose())
Packit ae235b
        g_printerr ("Initializers counted: %u\n", g_atomic_int_get (&mtsafe_call_counter));
Packit ae235b
      g_usleep (50 * 1000); /* wait for threads to complete */
Packit ae235b
    }
Packit ae235b
  if (g_test_verbose())
Packit ae235b
    g_printerr ("Total initializers: %u\n", g_atomic_int_get (&mtsafe_call_counter));
Packit ae235b
  /* ensure non-corrupted counter updates */
Packit ae235b
  g_assert_cmpint (g_atomic_int_get (&mtsafe_call_counter), ==, unsafe_call_counter);
Packit ae235b
Packit ae235b
  g_thread_join (t1);
Packit ae235b
  g_thread_join (t2);
Packit ae235b
  g_thread_join (t3);
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GObject parent;
Packit ae235b
  char   *name;
Packit ae235b
} PropTester;
Packit ae235b
typedef GObjectClass    PropTesterClass;
Packit ae235b
static GType prop_tester_get_type (void);
Packit ae235b
G_DEFINE_TYPE (PropTester, prop_tester, G_TYPE_OBJECT)
Packit ae235b
#define PROP_NAME 1
Packit ae235b
static void
Packit ae235b
prop_tester_init (PropTester* t)
Packit ae235b
{
Packit ae235b
  if (t->name == NULL)
Packit ae235b
    ; /* neds unit test framework initialization: g_test_bug ("race initializing properties"); */
Packit ae235b
}
Packit ae235b
static void
Packit ae235b
prop_tester_set_property (GObject        *object,
Packit ae235b
                          guint           property_id,
Packit ae235b
                          const GValue   *value,
Packit ae235b
                          GParamSpec     *pspec)
Packit ae235b
{}
Packit ae235b
static void
Packit ae235b
prop_tester_class_init (PropTesterClass *c)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  GParamSpec *param;
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (c);
Packit ae235b
Packit ae235b
  gobject_class->set_property = prop_tester_set_property; /* silence GObject checks */
Packit ae235b
Packit ae235b
  g_mutex_lock (&sync_mutex);
Packit ae235b
  g_cond_signal (&sync_cond);
Packit ae235b
  g_mutex_unlock (&sync_mutex);
Packit ae235b
Packit ae235b
  for (i = 0; i < 100; i++) /* wait a bit. */
Packit ae235b
    g_thread_yield();
Packit ae235b
Packit ae235b
  call_counter_init (c);
Packit ae235b
  param = g_param_spec_string ("name", "name_i18n",
Packit ae235b
			       "yet-more-wasteful-i18n",
Packit ae235b
			       NULL,
Packit ae235b
			       G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
Packit ae235b
			       G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB |
Packit ae235b
			       G_PARAM_STATIC_NICK);
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_NAME, param);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
object_create (gpointer data)
Packit ae235b
{
Packit ae235b
  GObject *obj = g_object_new (prop_tester_get_type(), "name", "fish", NULL);
Packit ae235b
  g_object_unref (obj);
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_threaded_object_init (void)
Packit ae235b
{
Packit ae235b
  GThread *creator;
Packit ae235b
  g_mutex_lock (&sync_mutex);
Packit ae235b
Packit ae235b
  creator = g_thread_create (object_create, NULL, TRUE, NULL);
Packit ae235b
  /* really provoke the race */
Packit ae235b
  g_cond_wait (&sync_cond, &sync_mutex);
Packit ae235b
Packit ae235b
  object_create (NULL);
Packit ae235b
  g_mutex_unlock (&sync_mutex);
Packit ae235b
Packit ae235b
  g_thread_join (creator);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
    MyTester0 *strong;
Packit ae235b
    guint unref_delay;
Packit ae235b
} UnrefInThreadData;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
unref_in_thread (gpointer p)
Packit ae235b
{
Packit ae235b
  UnrefInThreadData *data = p;
Packit ae235b
Packit ae235b
  g_usleep (data->unref_delay);
Packit ae235b
  g_object_unref (data->strong);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* undefine to see this test fail without GWeakRef */
Packit ae235b
#define HAVE_G_WEAK_REF
Packit ae235b
Packit ae235b
#define SLEEP_MIN_USEC 1
Packit ae235b
#define SLEEP_MAX_USEC 10
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_threaded_weak_ref (void)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
  guint get_wins = 0, unref_wins = 0;
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  if (g_test_thorough ())
Packit ae235b
    n = NUM_COUNTER_INCREMENTS;
Packit ae235b
  else
Packit ae235b
    n = NUM_COUNTER_INCREMENTS / 20;
Packit ae235b
Packit ae235b
  for (i = 0; i < n; i++)
Packit ae235b
    {
Packit ae235b
      UnrefInThreadData data;
Packit ae235b
#ifdef HAVE_G_WEAK_REF
Packit ae235b
      /* GWeakRef<MyTester0> in C++ terms */
Packit ae235b
      GWeakRef weak;
Packit ae235b
#else
Packit ae235b
      gpointer weak;
Packit ae235b
#endif
Packit ae235b
      MyTester0 *strengthened;
Packit ae235b
      guint get_delay;
Packit ae235b
      GThread *thread;
Packit ae235b
      GError *error = NULL;
Packit ae235b
Packit ae235b
      if (g_test_verbose () && (i % (n/20)) == 0)
Packit ae235b
        g_printerr ("%u%%\n", ((i * 100) / n));
Packit ae235b
Packit ae235b
      /* Have an object and a weak ref to it */
Packit ae235b
      data.strong = g_object_new (my_tester0_get_type (), NULL);
Packit ae235b
Packit ae235b
#ifdef HAVE_G_WEAK_REF
Packit ae235b
      g_weak_ref_init (&weak, data.strong);
Packit ae235b
#else
Packit ae235b
      weak = data.strong;
Packit ae235b
      g_object_add_weak_pointer ((GObject *) weak, &weak);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      /* Delay for a random time on each side of the race, to perturb the
Packit ae235b
       * timing. Ideally, we want each side to win half the races; on
Packit ae235b
       * smcv's laptop, these timings are about right.
Packit ae235b
       */
Packit ae235b
      data.unref_delay = g_random_int_range (SLEEP_MIN_USEC / 2, SLEEP_MAX_USEC / 2);
Packit ae235b
      get_delay = g_random_int_range (SLEEP_MIN_USEC, SLEEP_MAX_USEC);
Packit ae235b
Packit ae235b
      /* One half of the race is to unref the shared object */
Packit ae235b
      thread = g_thread_create (unref_in_thread, &data, TRUE, &error);
Packit ae235b
      g_assert_no_error (error);
Packit ae235b
Packit ae235b
      /* The other half of the race is to get the object from the "global
Packit ae235b
       * singleton"
Packit ae235b
       */
Packit ae235b
      g_usleep (get_delay);
Packit ae235b
Packit ae235b
#ifdef HAVE_G_WEAK_REF
Packit ae235b
      strengthened = g_weak_ref_get (&weak);
Packit ae235b
#else
Packit ae235b
      /* Spot the unsafe pointer access! In GDBusConnection this is rather
Packit ae235b
       * better-hidden, but ends up with essentially the same thing, albeit
Packit ae235b
       * cleared in dispose() rather than by a traditional weak pointer
Packit ae235b
       */
Packit ae235b
      strengthened = weak;
Packit ae235b
Packit ae235b
      if (strengthened != NULL)
Packit ae235b
        g_object_ref (strengthened);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      if (strengthened != NULL)
Packit ae235b
        g_assert (G_IS_OBJECT (strengthened));
Packit ae235b
Packit ae235b
      /* Wait for the thread to run */
Packit ae235b
      g_thread_join (thread);
Packit ae235b
Packit ae235b
      if (strengthened != NULL)
Packit ae235b
        {
Packit ae235b
          get_wins++;
Packit ae235b
          g_assert (G_IS_OBJECT (strengthened));
Packit ae235b
          g_object_unref (strengthened);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          unref_wins++;
Packit ae235b
        }
Packit ae235b
Packit ae235b
#ifdef HAVE_G_WEAK_REF
Packit ae235b
      g_weak_ref_clear (&weak);
Packit ae235b
#else
Packit ae235b
      if (weak != NULL)
Packit ae235b
        g_object_remove_weak_pointer (weak, &weak);
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("Race won by get %u times, unref %u times\n",
Packit ae235b
             get_wins, unref_wins);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  /* g_test_add_func ("/GObject/threaded-class-init", test_threaded_class_init); */
Packit ae235b
  g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init);
Packit ae235b
  g_test_add_func ("/GObject/threaded-weak-ref", test_threaded_weak_ref);
Packit ae235b
Packit ae235b
  return g_test_run();
Packit ae235b
}