Blame tests/thread-test.c

Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
/* GMutex */
Packit ae235b
Packit ae235b
static GMutex test_g_mutex_mutex;
Packit ae235b
static guint test_g_mutex_int = 0;
Packit ae235b
static gboolean test_g_mutex_thread_ready;
Packit ae235b
G_LOCK_DEFINE_STATIC (test_g_mutex);
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_g_mutex_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  g_assert (GPOINTER_TO_INT (data) == 42);
Packit ae235b
  g_assert (g_mutex_trylock (&test_g_mutex_mutex) == FALSE);
Packit ae235b
  g_assert (G_TRYLOCK (test_g_mutex) == FALSE);
Packit ae235b
  test_g_mutex_thread_ready = TRUE;
Packit ae235b
  g_mutex_lock (&test_g_mutex_mutex);
Packit ae235b
  g_assert (test_g_mutex_int == 42);
Packit ae235b
  g_mutex_unlock (&test_g_mutex_mutex);
Packit ae235b
Packit ae235b
  return GINT_TO_POINTER (41);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_mutex (void)
Packit ae235b
{
Packit ae235b
  GThread *thread;
Packit ae235b
Packit ae235b
  g_assert (g_mutex_trylock (&test_g_mutex_mutex));
Packit ae235b
  g_assert (G_TRYLOCK (test_g_mutex));
Packit ae235b
  test_g_mutex_thread_ready = FALSE;
Packit ae235b
  thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
Packit ae235b
			    TRUE, NULL);
Packit ae235b
  /* This busy wait is only for testing purposes and not an example of
Packit ae235b
   * good code!*/
Packit ae235b
  while (!test_g_mutex_thread_ready)
Packit ae235b
    g_usleep (G_USEC_PER_SEC / 5);
Packit ae235b
  test_g_mutex_int = 42;
Packit ae235b
  G_UNLOCK (test_g_mutex);
Packit ae235b
  g_mutex_unlock (&test_g_mutex_mutex);
Packit ae235b
  g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GStaticRecMutex */
Packit ae235b
Packit ae235b
static GStaticRecMutex test_g_static_rec_mutex_mutex = G_STATIC_REC_MUTEX_INIT;
Packit ae235b
static guint test_g_static_rec_mutex_int = 0;
Packit ae235b
static gboolean test_g_static_rec_mutex_thread_ready;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_g_static_rec_mutex_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  g_assert (GPOINTER_TO_INT (data) == 42);
Packit ae235b
  g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) 
Packit ae235b
	    == FALSE);
Packit ae235b
  test_g_static_rec_mutex_thread_ready = TRUE;
Packit ae235b
  g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
Packit ae235b
  g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
Packit ae235b
  g_assert (test_g_static_rec_mutex_int == 42);
Packit ae235b
  test_g_static_rec_mutex_thread_ready = FALSE;
Packit ae235b
  g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
Packit ae235b
  g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
Packit ae235b
Packit ae235b
  g_thread_exit (GINT_TO_POINTER (43));
Packit ae235b
  
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_static_rec_mutex (void)
Packit ae235b
{
Packit ae235b
  GThread *thread;
Packit ae235b
Packit ae235b
  g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
Packit ae235b
  test_g_static_rec_mutex_thread_ready = FALSE;
Packit ae235b
  thread = g_thread_create (test_g_static_rec_mutex_thread, 
Packit ae235b
			    GINT_TO_POINTER (42), TRUE, NULL);
Packit ae235b
  /* This busy wait is only for testing purposes and not an example of
Packit ae235b
   * good code!*/
Packit ae235b
  while (!test_g_static_rec_mutex_thread_ready)
Packit ae235b
    g_usleep (G_USEC_PER_SEC / 5);
Packit ae235b
Packit ae235b
  g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
Packit ae235b
  test_g_static_rec_mutex_int = 41;
Packit ae235b
  g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
Packit ae235b
  test_g_static_rec_mutex_int = 42;  
Packit ae235b
  g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
Packit ae235b
Packit ae235b
  /* This busy wait is only for testing purposes and not an example of
Packit ae235b
   * good code!*/
Packit ae235b
  while (test_g_static_rec_mutex_thread_ready)
Packit ae235b
    g_usleep (G_USEC_PER_SEC / 5);
Packit ae235b
Packit ae235b
  g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
Packit ae235b
  test_g_static_rec_mutex_int = 0;  
Packit ae235b
  g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
Packit ae235b
Packit ae235b
  g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GStaticPrivate */
Packit ae235b
Packit ae235b
#define THREADS 10
Packit ae235b
Packit ae235b
static GStaticPrivate test_g_static_private_private1 = G_STATIC_PRIVATE_INIT;
Packit ae235b
static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT;
Packit ae235b
static GMutex test_g_static_private_mutex;
Packit ae235b
static guint test_g_static_private_counter = 0;
Packit ae235b
static guint test_g_static_private_ready = 0;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_g_static_private_constructor (void)
Packit ae235b
{
Packit ae235b
  g_mutex_lock (&test_g_static_private_mutex);
Packit ae235b
  test_g_static_private_counter++;
Packit ae235b
  g_mutex_unlock (&test_g_static_private_mutex);  
Packit ae235b
  return g_new (guint,1);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_static_private_destructor (gpointer data)
Packit ae235b
{
Packit ae235b
  g_mutex_lock (&test_g_static_private_mutex);
Packit ae235b
  test_g_static_private_counter--;
Packit ae235b
  g_mutex_unlock (&test_g_static_private_mutex);  
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_g_static_private_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  guint number = GPOINTER_TO_INT (data);
Packit ae235b
  guint i;
Packit ae235b
  guint *private1, *private2;
Packit ae235b
  for (i = 0; i < 10; i++)
Packit ae235b
    {
Packit ae235b
      number = number * 11 + 1; /* A very simple and bad RNG ;-) */
Packit ae235b
      private1 = g_static_private_get (&test_g_static_private_private1);
Packit ae235b
      if (!private1 || number % 7 > 3)
Packit ae235b
	{
Packit ae235b
	  private1 = test_g_static_private_constructor ();
Packit ae235b
	  g_static_private_set (&test_g_static_private_private1, private1,
Packit ae235b
				test_g_static_private_destructor);
Packit ae235b
	}
Packit ae235b
      *private1 = number;
Packit ae235b
      private2 = g_static_private_get (&test_g_static_private_private2);
Packit ae235b
      if (!private2 || number % 13 > 5)
Packit ae235b
	{
Packit ae235b
	  private2 = test_g_static_private_constructor ();
Packit ae235b
	  g_static_private_set (&test_g_static_private_private2, private2,
Packit ae235b
				test_g_static_private_destructor);
Packit ae235b
	}
Packit ae235b
      *private2 = number * 2;
Packit ae235b
      g_usleep (G_USEC_PER_SEC / 5);
Packit ae235b
      g_assert (number == *private1);
Packit ae235b
      g_assert (number * 2 == *private2);      
Packit ae235b
    }
Packit ae235b
  g_mutex_lock (&test_g_static_private_mutex);
Packit ae235b
  test_g_static_private_ready++;
Packit ae235b
  g_mutex_unlock (&test_g_static_private_mutex);  
Packit ae235b
Packit ae235b
  /* Busy wait is not nice but that's just a test */
Packit ae235b
  while (test_g_static_private_ready != 0)
Packit ae235b
    g_usleep (G_USEC_PER_SEC / 5);  
Packit ae235b
Packit ae235b
  for (i = 0; i < 10; i++)
Packit ae235b
    {
Packit ae235b
      private2 = g_static_private_get (&test_g_static_private_private2);
Packit ae235b
      number = number * 11 + 1; /* A very simple and bad RNG ;-) */
Packit ae235b
      if (!private2 || number % 13 > 5)
Packit ae235b
	{
Packit ae235b
	  private2 = test_g_static_private_constructor ();
Packit ae235b
	  g_static_private_set (&test_g_static_private_private2, private2,
Packit ae235b
				test_g_static_private_destructor);
Packit ae235b
	}      
Packit ae235b
      *private2 = number * 2;
Packit ae235b
      g_usleep (G_USEC_PER_SEC / 5);
Packit ae235b
      g_assert (number * 2 == *private2);      
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_static_private (void)
Packit ae235b
{
Packit ae235b
  GThread *threads[THREADS];
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  test_g_static_private_ready = 0;
Packit ae235b
Packit ae235b
  for (i = 0; i < THREADS; i++)
Packit ae235b
    {
Packit ae235b
      threads[i] = g_thread_create (test_g_static_private_thread, 
Packit ae235b
				    GINT_TO_POINTER (i), TRUE, NULL);      
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Busy wait is not nice but that's just a test */
Packit ae235b
  while (test_g_static_private_ready != THREADS)
Packit ae235b
    g_usleep (G_USEC_PER_SEC / 5);
Packit ae235b
Packit ae235b
  /* Reuse the static private */
Packit ae235b
  g_static_private_free (&test_g_static_private_private2);
Packit ae235b
  g_static_private_init (&test_g_static_private_private2);
Packit ae235b
  
Packit ae235b
  test_g_static_private_ready = 0;
Packit ae235b
Packit ae235b
  for (i = 0; i < THREADS; i++)
Packit ae235b
    g_assert (GPOINTER_TO_INT (g_thread_join (threads[i])) == i * 3);
Packit ae235b
    
Packit ae235b
  g_assert (test_g_static_private_counter == 0); 
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GStaticRWLock */
Packit ae235b
Packit ae235b
/* -1 = writing; >0 = # of readers */
Packit ae235b
static gint test_g_static_rw_lock_state = 0; 
Packit ae235b
G_LOCK_DEFINE (test_g_static_rw_lock_state);
Packit ae235b
Packit ae235b
static gboolean test_g_static_rw_lock_run = TRUE; 
Packit ae235b
static GStaticRWLock test_g_static_rw_lock_lock = G_STATIC_RW_LOCK_INIT;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_g_static_rw_lock_thread (gpointer data)
Packit ae235b
{
Packit ae235b
  while (test_g_static_rw_lock_run)
Packit ae235b
    {
Packit ae235b
      if (g_random_double() > .2) /* I'm a reader */
Packit ae235b
	{
Packit ae235b
	  
Packit ae235b
	  if (g_random_double() > .2) /* I'll block */
Packit ae235b
	    g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
Packit ae235b
	  else /* I'll only try */
Packit ae235b
	    if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
Packit ae235b
	      continue;
Packit ae235b
	  G_LOCK (test_g_static_rw_lock_state);
Packit ae235b
	  g_assert (test_g_static_rw_lock_state >= 0);
Packit ae235b
	  test_g_static_rw_lock_state++;
Packit ae235b
	  G_UNLOCK (test_g_static_rw_lock_state);
Packit ae235b
Packit ae235b
	  g_usleep (g_random_int_range (20,1000));
Packit ae235b
Packit ae235b
	  G_LOCK (test_g_static_rw_lock_state);
Packit ae235b
	  test_g_static_rw_lock_state--;
Packit ae235b
	  G_UNLOCK (test_g_static_rw_lock_state);
Packit ae235b
Packit ae235b
	  g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
Packit ae235b
	}
Packit ae235b
      else /* I'm a writer */
Packit ae235b
	{
Packit ae235b
	  
Packit ae235b
	  if (g_random_double() > .2) /* I'll block */ 
Packit ae235b
	    g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
Packit ae235b
	  else /* I'll only try */
Packit ae235b
	    if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
Packit ae235b
	      continue;
Packit ae235b
	  G_LOCK (test_g_static_rw_lock_state);
Packit ae235b
	  g_assert (test_g_static_rw_lock_state == 0);
Packit ae235b
	  test_g_static_rw_lock_state = -1;
Packit ae235b
	  G_UNLOCK (test_g_static_rw_lock_state);
Packit ae235b
Packit ae235b
	  g_usleep (g_random_int_range (20,1000));
Packit ae235b
Packit ae235b
	  G_LOCK (test_g_static_rw_lock_state);
Packit ae235b
	  test_g_static_rw_lock_state = 0;
Packit ae235b
	  G_UNLOCK (test_g_static_rw_lock_state);
Packit ae235b
Packit ae235b
	  g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_static_rw_lock (void)
Packit ae235b
{
Packit ae235b
  GThread *threads[THREADS];
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < THREADS; i++)
Packit ae235b
    {
Packit ae235b
      threads[i] = g_thread_create (test_g_static_rw_lock_thread, 
Packit ae235b
				    NULL, TRUE, NULL);      
Packit ae235b
    }
Packit ae235b
  g_usleep (G_USEC_PER_SEC * 5);
Packit ae235b
  test_g_static_rw_lock_run = FALSE;
Packit ae235b
  for (i = 0; i < THREADS; i++)
Packit ae235b
    {
Packit ae235b
      g_thread_join (threads[i]);
Packit ae235b
    }
Packit ae235b
  g_assert (test_g_static_rw_lock_state == 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
#define G_ONCE_SIZE 100
Packit ae235b
#define G_ONCE_THREADS 10
Packit ae235b
Packit ae235b
G_LOCK_DEFINE (test_g_once);
Packit ae235b
static guint test_g_once_guint_array[G_ONCE_SIZE];
Packit ae235b
static GOnce test_g_once_array[G_ONCE_SIZE];
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_g_once_init_func(gpointer arg)
Packit ae235b
{
Packit ae235b
  guint *count = arg;
Packit ae235b
  g_usleep (g_random_int_range (20,1000));
Packit ae235b
  (*count)++;
Packit ae235b
  g_usleep (g_random_int_range (20,1000));
Packit ae235b
  return arg;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_g_once_thread (gpointer ignore)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
  G_LOCK (test_g_once);
Packit ae235b
  /* Don't start before all threads are created */
Packit ae235b
  G_UNLOCK (test_g_once);
Packit ae235b
  for (i = 0; i < 1000; i++)
Packit ae235b
    {
Packit ae235b
      guint pos = g_random_int_range (0, G_ONCE_SIZE);
Packit ae235b
      gpointer ret = g_once (test_g_once_array + pos, test_g_once_init_func, 
Packit ae235b
			     test_g_once_guint_array + pos);
Packit ae235b
      g_assert (ret == test_g_once_guint_array + pos);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* Make sure, that all counters are touched at least once */
Packit ae235b
  for (i = 0; i < G_ONCE_SIZE; i++)
Packit ae235b
    {
Packit ae235b
      gpointer ret = g_once (test_g_once_array + i, test_g_once_init_func, 
Packit ae235b
			     test_g_once_guint_array + i);
Packit ae235b
      g_assert (ret == test_g_once_guint_array + i);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_thread_once (void)
Packit ae235b
{
Packit ae235b
  static GOnce once_init = G_ONCE_INIT;
Packit ae235b
  GThread *threads[G_ONCE_THREADS];
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < G_ONCE_SIZE; i++) 
Packit ae235b
    {
Packit ae235b
      test_g_once_array[i] = once_init;
Packit ae235b
      test_g_once_guint_array[i] = i;
Packit ae235b
    }
Packit ae235b
  G_LOCK (test_g_once);
Packit ae235b
  for (i = 0; i < G_ONCE_THREADS; i++)
Packit ae235b
    {
Packit ae235b
      threads[i] = g_thread_create (test_g_once_thread, GUINT_TO_POINTER(i%2), 
Packit ae235b
				    TRUE, NULL);
Packit ae235b
    }
Packit ae235b
  G_UNLOCK (test_g_once);
Packit ae235b
  for (i = 0; i < G_ONCE_THREADS; i++)
Packit ae235b
    {
Packit ae235b
      g_thread_join (threads[i]);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  for (i = 0; i < G_ONCE_SIZE; i++) 
Packit ae235b
    {
Packit ae235b
      g_assert (test_g_once_guint_array[i] == i + 1);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* run all the tests */
Packit ae235b
static void
Packit ae235b
run_all_tests (void)
Packit ae235b
{
Packit ae235b
  test_g_mutex ();
Packit ae235b
  test_g_static_rec_mutex ();
Packit ae235b
  test_g_static_private ();
Packit ae235b
  test_g_static_rw_lock ();
Packit ae235b
  test_g_thread_once ();
Packit ae235b
}
Packit ae235b
Packit ae235b
int 
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  run_all_tests ();
Packit ae235b
Packit ae235b
  /* Now we rerun all tests, but this time we fool the system into
Packit ae235b
   * thinking, that the available thread system is not native, but
Packit ae235b
   * userprovided. */
Packit ae235b
Packit ae235b
  g_thread_use_default_impl = FALSE;
Packit ae235b
  run_all_tests ();
Packit ae235b
Packit ae235b
  /* XXX: And this shows how silly the above non-native tests are */
Packit ae235b
  g_static_rw_lock_free (&test_g_static_rw_lock_lock);
Packit ae235b
  g_static_rec_mutex_free (&test_g_static_rec_mutex_mutex);
Packit ae235b
  g_static_private_free (&test_g_static_private_private2);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}