Blame tests/asyncqueue-test.c

Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
#undef G_DISABLE_DEPRECATED
Packit ae235b
Packit ae235b
#include <time.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
#define DEBUG_MSG(args)
Packit ae235b
/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  */
Packit ae235b
#define PRINT_MSG(args)
Packit ae235b
/* #define PRINT_MSG(args) g_printerr args ; g_printerr ("\n"); */
Packit ae235b
Packit ae235b
#define MAX_THREADS            50
Packit ae235b
#define MAX_SORTS              5    /* only applies if
Packit ae235b
				       ASYC_QUEUE_DO_SORT is set to 1 */ 
Packit ae235b
#define MAX_TIME               20   /* seconds */
Packit ae235b
#define MIN_TIME               5    /* seconds */
Packit ae235b
Packit ae235b
#define SORT_QUEUE_AFTER       1
Packit ae235b
#define SORT_QUEUE_ON_PUSH     1    /* if this is done, the
Packit ae235b
				       SORT_QUEUE_AFTER is ignored */
Packit ae235b
#define QUIT_WHEN_DONE         1
Packit ae235b
Packit ae235b
Packit ae235b
#if SORT_QUEUE_ON_PUSH == 1
Packit ae235b
#  undef SORT_QUEUE_AFTER
Packit ae235b
#  define SORT_QUEUE_AFTER     0
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
static GMainLoop   *main_loop = NULL;
Packit ae235b
static GThreadPool *thread_pool = NULL;
Packit ae235b
static GAsyncQueue *async_queue = NULL;
Packit ae235b
Packit ae235b
Packit ae235b
static gint
Packit ae235b
sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
Packit ae235b
{
Packit ae235b
  gint32 id1;
Packit ae235b
  gint32 id2;
Packit ae235b
Packit ae235b
  id1 = GPOINTER_TO_INT (p1);
Packit ae235b
  id2 = GPOINTER_TO_INT (p2);
Packit ae235b
Packit ae235b
  DEBUG_MSG (("comparing #1:%d and #2:%d, returning %d", 
Packit ae235b
	     id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1)));
Packit ae235b
Packit ae235b
  return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
sort_queue (gpointer user_data)
Packit ae235b
{
Packit ae235b
  static gint     sorts = 0;
Packit ae235b
  static gpointer last_p = NULL;
Packit ae235b
  gpointer        p;
Packit ae235b
  gboolean        can_quit = FALSE;
Packit ae235b
  gint            sort_multiplier;
Packit ae235b
  gint            len;
Packit ae235b
  gint            i;
Packit ae235b
Packit ae235b
  sort_multiplier = GPOINTER_TO_INT (user_data);
Packit ae235b
Packit ae235b
  if (SORT_QUEUE_AFTER) {
Packit ae235b
    PRINT_MSG (("sorting async queue...")); 
Packit ae235b
    g_async_queue_sort (async_queue, sort_compare, NULL);
Packit ae235b
Packit ae235b
    sorts++;
Packit ae235b
Packit ae235b
    if (sorts >= sort_multiplier) {
Packit ae235b
      can_quit = TRUE;
Packit ae235b
    }
Packit ae235b
    
Packit ae235b
    g_async_queue_sort (async_queue, sort_compare, NULL);
Packit ae235b
    len = g_async_queue_length (async_queue);
Packit ae235b
Packit ae235b
    PRINT_MSG (("sorted queue (for %d/%d times, size:%d)...", sorts, MAX_SORTS, len)); 
Packit ae235b
  } else {
Packit ae235b
    can_quit = TRUE;
Packit ae235b
    len = g_async_queue_length (async_queue);
Packit ae235b
    DEBUG_MSG (("printing queue (size:%d)...", len)); 
Packit ae235b
  }
Packit ae235b
Packit ae235b
  for (i = 0, last_p = NULL; i < len; i++) {
Packit ae235b
    p = g_async_queue_pop (async_queue);
Packit ae235b
    DEBUG_MSG (("item %d ---> %d", i, GPOINTER_TO_INT (p))); 
Packit ae235b
Packit ae235b
    if (last_p) {
Packit ae235b
      g_assert (GPOINTER_TO_INT (last_p) <= GPOINTER_TO_INT (p));
Packit ae235b
    }
Packit ae235b
Packit ae235b
    last_p = p;
Packit ae235b
  }
Packit ae235b
  
Packit ae235b
  if (can_quit && QUIT_WHEN_DONE) {
Packit ae235b
    g_main_loop_quit (main_loop);
Packit ae235b
  }
Packit ae235b
Packit ae235b
  return !can_quit;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
enter_thread (gpointer data, gpointer user_data)
Packit ae235b
{
Packit ae235b
  gint   len G_GNUC_UNUSED;
Packit ae235b
  gint   id;
Packit ae235b
  gulong ms;
Packit ae235b
Packit ae235b
  id = GPOINTER_TO_INT (data);
Packit ae235b
  
Packit ae235b
  ms = g_random_int_range (MIN_TIME * 1000, MAX_TIME * 1000);
Packit ae235b
  DEBUG_MSG (("entered thread with id:%d, adding to queue in:%ld ms", id, ms));
Packit ae235b
Packit ae235b
  g_usleep (ms * 1000);
Packit ae235b
Packit ae235b
  if (SORT_QUEUE_ON_PUSH) {
Packit ae235b
    g_async_queue_push_sorted (async_queue, GINT_TO_POINTER (id), sort_compare, NULL);
Packit ae235b
  } else {
Packit ae235b
    g_async_queue_push (async_queue, GINT_TO_POINTER (id));
Packit ae235b
  }
Packit ae235b
Packit ae235b
  len = g_async_queue_length (async_queue);
Packit ae235b
Packit ae235b
  DEBUG_MSG (("thread id:%d added to async queue (size:%d)", 
Packit ae235b
	     id, len));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint destroy_count = 0;
Packit ae235b
Packit ae235b
static void
Packit ae235b
counting_destroy (gpointer item)
Packit ae235b
{
Packit ae235b
  destroy_count++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
basic_tests (void)
Packit ae235b
{
Packit ae235b
  GAsyncQueue *q;
Packit ae235b
  gpointer item;
Packit ae235b
Packit ae235b
  destroy_count = 0;
Packit ae235b
Packit ae235b
  q = g_async_queue_new_full (counting_destroy);
Packit ae235b
  g_async_queue_lock (q);
Packit ae235b
  g_async_queue_ref (q);
Packit ae235b
  g_async_queue_unlock (q);
Packit ae235b
  g_async_queue_lock (q);
Packit ae235b
  g_async_queue_ref_unlocked (q);
Packit ae235b
  g_async_queue_unref_and_unlock (q);
Packit ae235b
Packit ae235b
  item = g_async_queue_try_pop (q);
Packit ae235b
  g_assert (item == NULL);
Packit ae235b
Packit ae235b
  g_async_queue_lock (q);
Packit ae235b
  item = g_async_queue_try_pop_unlocked (q);
Packit ae235b
  g_async_queue_unlock (q);
Packit ae235b
  g_assert (item == NULL);
Packit ae235b
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (1));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (2));
Packit ae235b
  g_async_queue_push (q, GINT_TO_POINTER (3));
Packit ae235b
  g_assert_cmpint (destroy_count, ==, 0);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
  g_assert_cmpint (destroy_count, ==, 0);
Packit ae235b
Packit ae235b
  item = g_async_queue_pop (q);
Packit ae235b
  g_assert_cmpint (GPOINTER_TO_INT (item), ==, 1);
Packit ae235b
  g_assert_cmpint (destroy_count, ==, 0);
Packit ae235b
Packit ae235b
  g_async_queue_unref (q);
Packit ae235b
  g_assert_cmpint (destroy_count, ==, 2);
Packit ae235b
}
Packit ae235b
Packit ae235b
int 
Packit ae235b
main (int argc, char *argv[])
Packit ae235b
{
Packit ae235b
  gint   i;
Packit ae235b
  gint   max_threads = MAX_THREADS;
Packit ae235b
  gint   max_unused_threads = MAX_THREADS;
Packit ae235b
  gint   sort_multiplier = MAX_SORTS;
Packit ae235b
  gint   sort_interval;
Packit ae235b
  gchar *msg G_GNUC_UNUSED;
Packit ae235b
Packit ae235b
  basic_tests ();
Packit ae235b
Packit ae235b
  PRINT_MSG (("creating async queue..."));
Packit ae235b
  async_queue = g_async_queue_new ();
Packit ae235b
Packit ae235b
  g_return_val_if_fail (async_queue != NULL, EXIT_FAILURE);
Packit ae235b
Packit ae235b
  PRINT_MSG (("creating thread pool with max threads:%d, max unused threads:%d...",
Packit ae235b
	     max_threads, max_unused_threads));
Packit ae235b
  thread_pool = g_thread_pool_new (enter_thread,
Packit ae235b
				   async_queue,
Packit ae235b
				   max_threads,
Packit ae235b
				   FALSE,
Packit ae235b
				   NULL);
Packit ae235b
Packit ae235b
  g_return_val_if_fail (thread_pool != NULL, EXIT_FAILURE);
Packit ae235b
Packit ae235b
  g_thread_pool_set_max_unused_threads (max_unused_threads);
Packit ae235b
Packit ae235b
  PRINT_MSG (("creating threads..."));
Packit ae235b
  for (i = 1; i <= max_threads; i++) {
Packit ae235b
    GError *error = NULL;
Packit ae235b
  
Packit ae235b
    g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
Packit ae235b
    
Packit ae235b
    g_assert_no_error (error);
Packit ae235b
  }
Packit ae235b
Packit ae235b
  if (!SORT_QUEUE_AFTER) {
Packit ae235b
    sort_multiplier = 1;
Packit ae235b
  }
Packit ae235b
  
Packit ae235b
  sort_interval = ((MAX_TIME / sort_multiplier) + 2)  * 1000;
Packit ae235b
  g_timeout_add (sort_interval, sort_queue, GINT_TO_POINTER (sort_multiplier));
Packit ae235b
Packit ae235b
  if (SORT_QUEUE_ON_PUSH) {
Packit ae235b
    msg = "sorting when pushing into the queue, checking queue is sorted";
Packit ae235b
  } else {
Packit ae235b
    msg = "sorting";
Packit ae235b
  }
Packit ae235b
Packit ae235b
  PRINT_MSG (("%s %d %s %d ms",
Packit ae235b
	      msg,
Packit ae235b
	      sort_multiplier, 
Packit ae235b
	      sort_multiplier == 1 ? "time in" : "times, once every",
Packit ae235b
	      sort_interval));
Packit ae235b
Packit ae235b
  DEBUG_MSG (("entering main event loop"));
Packit ae235b
Packit ae235b
  main_loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
  g_main_loop_run (main_loop);
Packit ae235b
Packit ae235b
  g_main_loop_unref (main_loop);
Packit ae235b
  g_thread_pool_free (thread_pool, TRUE, TRUE);
Packit ae235b
  g_async_queue_unref (async_queue);
Packit ae235b
Packit ae235b
  return EXIT_SUCCESS;
Packit ae235b
}