os/ossrv/glib/tsrc/BC/tests/asyncqueue-test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/asyncqueue-test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,208 @@
     1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
     1.5 +#undef G_DISABLE_ASSERT
     1.6 +#undef G_LOG_DOMAIN
     1.7 +
     1.8 +#include <time.h>
     1.9 +#include <stdlib.h>
    1.10 +
    1.11 +#include <glib.h>
    1.12 +#include <glib/gthread.h>
    1.13 +
    1.14 +#define DEBUG_MSG(args)
    1.15 +/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  */
    1.16 +#define PRINT_MSG(args)
    1.17 +/* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
    1.18 +
    1.19 +#define MAX_THREADS            50
    1.20 +#define MAX_SORTS              5    /* only applies if
    1.21 +				       ASYC_QUEUE_DO_SORT is set to 1 */ 
    1.22 +#define MAX_TIME               20   /* seconds */
    1.23 +#define MIN_TIME               5    /* seconds */
    1.24 +
    1.25 +#define SORT_QUEUE_AFTER       1
    1.26 +#define SORT_QUEUE_ON_PUSH     1    /* if this is done, the
    1.27 +				       SORT_QUEUE_AFTER is ignored */
    1.28 +#define QUIT_WHEN_DONE         1
    1.29 +
    1.30 +
    1.31 +#if SORT_QUEUE_ON_PUSH == 1
    1.32 +#  undef SORT_QUEUE_AFTER
    1.33 +#  define SORT_QUEUE_AFTER     0
    1.34 +#endif
    1.35 +
    1.36 +#ifdef SYMBIAN
    1.37 +#include "mrt2_glib2_test.h"
    1.38 +#endif /*SYMBIAN*/
    1.39 +
    1.40 +
    1.41 +static GMainLoop   *main_loop = NULL;
    1.42 +static GThreadPool *thread_pool = NULL;
    1.43 +static GAsyncQueue *async_queue = NULL;
    1.44 +
    1.45 +
    1.46 +static gint
    1.47 +sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
    1.48 +{
    1.49 +  gint32 id1;
    1.50 +  gint32 id2;
    1.51 +
    1.52 +  id1 = GPOINTER_TO_INT (p1);
    1.53 +  id2 = GPOINTER_TO_INT (p2);
    1.54 +
    1.55 +  DEBUG_MSG (("comparing #1:%d and #2:%d, returning %d", 
    1.56 +	     id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1)));
    1.57 +
    1.58 +  return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
    1.59 +}
    1.60 +
    1.61 +static gboolean
    1.62 +sort_queue (gpointer user_data)
    1.63 +{
    1.64 +  static gint     sorts = 0;
    1.65 +  static gpointer last_p = NULL;
    1.66 +  gpointer        p;
    1.67 +  gboolean        can_quit = FALSE;
    1.68 +  gint            sort_multiplier;
    1.69 +  gint            len;
    1.70 +  gint            i;
    1.71 +
    1.72 +  sort_multiplier = GPOINTER_TO_INT (user_data);
    1.73 +
    1.74 +  if (SORT_QUEUE_AFTER) {
    1.75 +    PRINT_MSG (("sorting async queue...")); 
    1.76 +    g_async_queue_sort (async_queue, sort_compare, NULL);
    1.77 +
    1.78 +    sorts++;
    1.79 +
    1.80 +    if (sorts >= sort_multiplier) {
    1.81 +      can_quit = TRUE;
    1.82 +    }
    1.83 +    
    1.84 +    g_async_queue_sort (async_queue, sort_compare, NULL);
    1.85 +    len = g_async_queue_length (async_queue);
    1.86 +
    1.87 +    PRINT_MSG (("sorted queue (for %d/%d times, size:%d)...", sorts, MAX_SORTS, len)); 
    1.88 +  } else {
    1.89 +    can_quit = TRUE;
    1.90 +    len = g_async_queue_length (async_queue);
    1.91 +    DEBUG_MSG (("printing queue (size:%d)...", len)); 
    1.92 +  }
    1.93 +
    1.94 +  for (i = 0, last_p = NULL; i < len; i++) {
    1.95 +    p = g_async_queue_pop (async_queue);
    1.96 +    DEBUG_MSG (("item %d ---> %d", i, GPOINTER_TO_INT (p))); 
    1.97 +
    1.98 +    if (last_p) {
    1.99 +      g_assert (GPOINTER_TO_INT (last_p) <= GPOINTER_TO_INT (p));
   1.100 +    }
   1.101 +
   1.102 +    last_p = p;
   1.103 +  }
   1.104 +  
   1.105 +  if (can_quit && QUIT_WHEN_DONE) {
   1.106 +    g_main_loop_quit (main_loop);
   1.107 +  }
   1.108 +
   1.109 +  return !can_quit;
   1.110 +}
   1.111 +
   1.112 +static void
   1.113 +enter_thread (gpointer data, gpointer user_data)
   1.114 +{
   1.115 +  gint   len;
   1.116 +  gint   id;
   1.117 +  gulong ms;
   1.118 +
   1.119 +  id = GPOINTER_TO_INT (data);
   1.120 +  
   1.121 +  ms = g_random_int_range (MIN_TIME * 1000, MAX_TIME * 1000);
   1.122 +  DEBUG_MSG (("entered thread with id:%d, adding to queue in:%ld ms", id, ms));
   1.123 +
   1.124 +  g_usleep (ms * 1000);
   1.125 +
   1.126 +  if (SORT_QUEUE_ON_PUSH) {
   1.127 +    g_async_queue_push_sorted (async_queue, GINT_TO_POINTER (id), sort_compare, NULL);
   1.128 +  } else {
   1.129 +    g_async_queue_push (async_queue, GINT_TO_POINTER (id));
   1.130 +  }
   1.131 +
   1.132 +  len = g_async_queue_length (async_queue);
   1.133 +
   1.134 +  DEBUG_MSG (("thread id:%d added to async queue (size:%d)", 
   1.135 +	     id, len));
   1.136 +}
   1.137 +
   1.138 +int 
   1.139 +main (int argc, char *argv[])
   1.140 +{
   1.141 +
   1.142 +   gint   i;
   1.143 +  gint   max_threads = MAX_THREADS;
   1.144 +  gint   max_unused_threads = MAX_THREADS;
   1.145 +  gint   sort_multiplier = MAX_SORTS;
   1.146 +  gint   sort_interval;
   1.147 +  gchar *msg;
   1.148 +  
   1.149 +  #ifdef SYMBIAN
   1.150 +  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
   1.151 +  #endif /*SYMBIAN*/
   1.152 +  g_thread_init (NULL);
   1.153 +
   1.154 +  PRINT_MSG (("creating async queue..."));
   1.155 +  async_queue = g_async_queue_new ();
   1.156 +  g_assert(async_queue != NULL);
   1.157 +
   1.158 +  g_return_val_if_fail (async_queue != NULL, EXIT_FAILURE);
   1.159 +
   1.160 +  PRINT_MSG (("creating thread pool with max threads:%d, max unused threads:%d...",
   1.161 +	     max_threads, max_unused_threads));
   1.162 +  thread_pool = g_thread_pool_new (enter_thread,
   1.163 +				   async_queue,
   1.164 +				   max_threads,
   1.165 +				   FALSE,
   1.166 +				   NULL);
   1.167 +
   1.168 +  g_return_val_if_fail (thread_pool != NULL, EXIT_FAILURE);
   1.169 +
   1.170 +  g_thread_pool_set_max_unused_threads (max_unused_threads);
   1.171 +
   1.172 +  PRINT_MSG (("creating threads..."));
   1.173 +  for (i = 1; i <= max_threads; i++) {
   1.174 +    GError *error = NULL;
   1.175 +  
   1.176 +    g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
   1.177 +    
   1.178 +    g_assert (error == NULL);
   1.179 +  }
   1.180 +
   1.181 +  if (!SORT_QUEUE_AFTER) {
   1.182 +    sort_multiplier = 1;
   1.183 +  }
   1.184 +  
   1.185 +  sort_interval = ((MAX_TIME / sort_multiplier) + 2)  * 1000;
   1.186 +  g_timeout_add (sort_interval, sort_queue, GINT_TO_POINTER (sort_multiplier));
   1.187 +
   1.188 +  if (SORT_QUEUE_ON_PUSH) {
   1.189 +    msg = "sorting when pushing into the queue, checking queue is sorted";
   1.190 +  } else {
   1.191 +    msg = "sorting";
   1.192 +  }
   1.193 +
   1.194 +  PRINT_MSG (("%s %d %s %d ms",
   1.195 +	      msg,
   1.196 +	      sort_multiplier, 
   1.197 +	      sort_multiplier == 1 ? "time in" : "times, once every",
   1.198 +	      sort_interval));
   1.199 +
   1.200 +  DEBUG_MSG (("entering main event loop"));
   1.201 +
   1.202 +  main_loop = g_main_loop_new (NULL, FALSE);
   1.203 +  g_main_loop_run (main_loop);
   1.204 + 
   1.205 +  
   1.206 +  #if SYMBIAN
   1.207 +  testResultXml("asyncqueue-test");
   1.208 +  #endif /* EMULATOR */
   1.209 +  
   1.210 +  return EXIT_SUCCESS;
   1.211 +}