Blame glib/tests/cond.c

Packit ae235b
/* Unit tests for GCond
Packit ae235b
 * Copyright (C) 2011 Red Hat, Inc
Packit ae235b
 * Author: Matthias Clasen
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
Packit ae235b
/* We are testing some deprecated APIs here */
Packit ae235b
#define GLIB_DISABLE_DEPRECATION_WARNINGS
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
static GCond cond;
Packit ae235b
static GMutex mutex;
Packit ae235b
static volatile gint next;
Packit ae235b
Packit ae235b
static void
Packit ae235b
push_value (gint value)
Packit ae235b
{
Packit ae235b
  g_mutex_lock (&mutex);
Packit ae235b
  while (next != 0)
Packit ae235b
    g_cond_wait (&cond, &mutex);
Packit ae235b
  next = value;
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("Thread %p producing next value: %d\n", g_thread_self (), value);
Packit ae235b
  if (value % 10 == 0)
Packit ae235b
    g_cond_broadcast (&cond;;
Packit ae235b
  else
Packit ae235b
    g_cond_signal (&cond;;
Packit ae235b
  g_mutex_unlock (&mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
pop_value (void)
Packit ae235b
{
Packit ae235b
  gint value;
Packit ae235b
Packit ae235b
  g_mutex_lock (&mutex);
Packit ae235b
  while (next == 0)
Packit ae235b
    {
Packit ae235b
      if (g_test_verbose ())
Packit ae235b
        g_printerr ("Thread %p waiting for cond\n", g_thread_self ());
Packit ae235b
      g_cond_wait (&cond, &mutex);
Packit ae235b
    }
Packit ae235b
  value = next;
Packit ae235b
  next = 0;
Packit ae235b
  g_cond_broadcast (&cond;;
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("Thread %p consuming value %d\n", g_thread_self (), value);
Packit ae235b
  g_mutex_unlock (&mutex);
Packit ae235b
Packit ae235b
  return value;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
produce_values (gpointer data)
Packit ae235b
{
Packit ae235b
  gint total;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  total = 0;
Packit ae235b
Packit ae235b
  for (i = 1; i < 100; i++)
Packit ae235b
    {
Packit ae235b
      total += i;
Packit ae235b
      push_value (i);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  push_value (-1);
Packit ae235b
  push_value (-1);
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("Thread %p produced %d altogether\n", g_thread_self (), total);
Packit ae235b
Packit ae235b
  return GINT_TO_POINTER (total);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
consume_values (gpointer data)
Packit ae235b
{
Packit ae235b
  gint accum = 0;
Packit ae235b
  gint value;
Packit ae235b
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      value = pop_value ();
Packit ae235b
      if (value == -1)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      accum += value;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("Thread %p accumulated %d\n", g_thread_self (), accum);
Packit ae235b
Packit ae235b
  return GINT_TO_POINTER (accum);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GThread *producer, *consumer1, *consumer2;
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_cond1 (void)
Packit ae235b
{
Packit ae235b
  gint total, acc1, acc2;
Packit ae235b
Packit ae235b
  producer = g_thread_create (produce_values, NULL, TRUE, NULL);
Packit ae235b
  consumer1 = g_thread_create (consume_values, NULL, TRUE, NULL);
Packit ae235b
  consumer2 = g_thread_create (consume_values, NULL, TRUE, NULL);
Packit ae235b
Packit ae235b
  total = GPOINTER_TO_INT (g_thread_join (producer));
Packit ae235b
  acc1 = GPOINTER_TO_INT (g_thread_join (consumer1));
Packit ae235b
  acc2 = GPOINTER_TO_INT (g_thread_join (consumer2));
Packit ae235b
Packit ae235b
  g_assert_cmpint (total, ==, acc1 + acc2);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GMutex mutex;
Packit ae235b
  GCond  cond;
Packit ae235b
  gint   limit;
Packit ae235b
  gint   count;
Packit ae235b
} Barrier;
Packit ae235b
Packit ae235b
static void
Packit ae235b
barrier_init (Barrier *barrier,
Packit ae235b
              gint     limit)
Packit ae235b
{
Packit ae235b
  g_mutex_init (&barrier->mutex);
Packit ae235b
  g_cond_init (&barrier->cond);
Packit ae235b
  barrier->limit = limit;
Packit ae235b
  barrier->count = limit;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
barrier_wait (Barrier *barrier)
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
  g_mutex_lock (&barrier->mutex);
Packit ae235b
  barrier->count--;
Packit ae235b
  if (barrier->count == 0)
Packit ae235b
    {
Packit ae235b
      ret = -1;
Packit ae235b
      barrier->count = barrier->limit;
Packit ae235b
      g_cond_broadcast (&barrier->cond);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      ret = 0;
Packit ae235b
      while (barrier->count != barrier->limit)
Packit ae235b
        g_cond_wait (&barrier->cond, &barrier->mutex);
Packit ae235b
    }
Packit ae235b
  g_mutex_unlock (&barrier->mutex);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
barrier_clear (Barrier *barrier)
Packit ae235b
{
Packit ae235b
  g_mutex_clear (&barrier->mutex);
Packit ae235b
  g_cond_clear (&barrier->cond);
Packit ae235b
}
Packit ae235b
Packit ae235b
static Barrier b;
Packit ae235b
static gint check;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
cond2_func (gpointer data)
Packit ae235b
{
Packit ae235b
  gint value = GPOINTER_TO_INT (data);
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&check);
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("thread %d starting, check %d\n", value, g_atomic_int_get (&check);;
Packit ae235b
Packit ae235b
  g_usleep (10000 * value);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&check);
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("thread %d reaching barrier, check %d\n", value, g_atomic_int_get (&check);;
Packit ae235b
Packit ae235b
  ret = barrier_wait (&b);
Packit ae235b
Packit ae235b
  g_assert_cmpint (g_atomic_int_get (&check), ==, 10);
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_printerr ("thread %d leaving barrier (%d), check %d\n", value, ret, g_atomic_int_get (&check);;
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* this test demonstrates how to use a condition variable
Packit ae235b
 * to implement a barrier
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
test_cond2 (void)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
  GThread *threads[5];
Packit ae235b
Packit ae235b
  g_atomic_int_set (&check, 0);
Packit ae235b
Packit ae235b
  barrier_init (&b, 5);
Packit ae235b
  for (i = 0; i < 5; i++)
Packit ae235b
    threads[i] = g_thread_create (cond2_func, GINT_TO_POINTER (i), TRUE, NULL);
Packit ae235b
Packit ae235b
  for (i = 0; i < 5; i++)
Packit ae235b
    g_thread_join (threads[i]);
Packit ae235b
Packit ae235b
  g_assert_cmpint (g_atomic_int_get (&check), ==, 10);
Packit ae235b
Packit ae235b
  barrier_clear (&b);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_wait_until (void)
Packit ae235b
{
Packit ae235b
  gint64 until;
Packit ae235b
  GMutex lock;
Packit ae235b
  GCond cond;
Packit ae235b
Packit ae235b
  /* This test will make sure we don't wait too much or too little.
Packit ae235b
   *
Packit ae235b
   * We check the 'too long' with a timeout of 60 seconds.
Packit ae235b
   *
Packit ae235b
   * We check the 'too short' by verifying a guarantee of the API: we
Packit ae235b
   * should not wake up until the specified time has passed.
Packit ae235b
   */
Packit ae235b
  g_mutex_init (&lock);
Packit ae235b
  g_cond_init (&cond;;
Packit ae235b
Packit ae235b
  until = g_get_monotonic_time () + G_TIME_SPAN_SECOND;
Packit ae235b
Packit ae235b
  /* Could still have spurious wakeups, so we must loop... */
Packit ae235b
  g_mutex_lock (&lock);
Packit ae235b
  while (g_cond_wait_until (&cond, &lock, until))
Packit ae235b
    ;
Packit ae235b
  g_mutex_unlock (&lock);
Packit ae235b
Packit ae235b
  /* Make sure it's after the until time */
Packit ae235b
  g_assert_cmpint (until, <=, g_get_monotonic_time ());
Packit ae235b
Packit ae235b
  /* Make sure it returns FALSE on timeout */
Packit ae235b
  until = g_get_monotonic_time () + G_TIME_SPAN_SECOND / 50;
Packit ae235b
  g_mutex_lock (&lock);
Packit ae235b
  g_assert (g_cond_wait_until (&cond, &lock, until) == FALSE);
Packit ae235b
  g_mutex_unlock (&lock);
Packit ae235b
Packit ae235b
  g_mutex_clear (&lock);
Packit ae235b
  g_cond_clear (&cond;;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/thread/cond1", test_cond1);
Packit ae235b
  g_test_add_func ("/thread/cond2", test_cond2);
Packit ae235b
  g_test_add_func ("/thread/cond/wait-until", test_wait_until);
Packit ae235b
Packit ae235b
  return g_test_run ();
Packit ae235b
}