glib/tests/asyncqueue-test.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #undef G_DISABLE_ASSERT
       
    19 #undef G_LOG_DOMAIN
       
    20 
       
    21 #include <time.h>
       
    22 #include <stdlib.h>
       
    23 
       
    24 #include <glib.h>
       
    25 
       
    26 #define DEBUG_MSG(args)
       
    27 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  */
       
    28 #define PRINT_MSG(args)
       
    29 /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
       
    30 
       
    31 #define MAX_THREADS            50
       
    32 #define MAX_SORTS              5    /* only applies if
       
    33 				       ASYC_QUEUE_DO_SORT is set to 1 */ 
       
    34 #define MAX_TIME               20   /* seconds */
       
    35 #define MIN_TIME               5    /* seconds */
       
    36 
       
    37 #define SORT_QUEUE_AFTER       1
       
    38 #define SORT_QUEUE_ON_PUSH     1    /* if this is done, the
       
    39 				       SORT_QUEUE_AFTER is ignored */
       
    40 #define QUIT_WHEN_DONE         1
       
    41 
       
    42 
       
    43 #if SORT_QUEUE_ON_PUSH == 1
       
    44 #  undef SORT_QUEUE_AFTER
       
    45 #  define SORT_QUEUE_AFTER     0
       
    46 #endif
       
    47 
       
    48 #ifdef __SYMBIAN32__
       
    49 #include "mrt2_glib2_test.h"
       
    50 #endif /*__SYMBIAN32__*/
       
    51 
       
    52 
       
    53 static GMainLoop   *main_loop = NULL;
       
    54 static GThreadPool *thread_pool = NULL;
       
    55 static GAsyncQueue *async_queue = NULL;
       
    56 
       
    57 
       
    58 static gint
       
    59 sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
       
    60 {
       
    61   gint32 id1;
       
    62   gint32 id2;
       
    63 
       
    64   id1 = GPOINTER_TO_INT (p1);
       
    65   id2 = GPOINTER_TO_INT (p2);
       
    66 
       
    67   DEBUG_MSG (("comparing #1:%d and #2:%d, returning %d", 
       
    68 	     id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1)));
       
    69 
       
    70   return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
       
    71 }
       
    72 
       
    73 static gboolean
       
    74 sort_queue (gpointer user_data)
       
    75 {
       
    76   static gint     sorts = 0;
       
    77   static gpointer last_p = NULL;
       
    78   gpointer        p;
       
    79   gboolean        can_quit = FALSE;
       
    80   gint            sort_multiplier;
       
    81   gint            len;
       
    82   gint            i;
       
    83 
       
    84   sort_multiplier = GPOINTER_TO_INT (user_data);
       
    85 
       
    86   if (SORT_QUEUE_AFTER) {
       
    87     PRINT_MSG (("sorting async queue...")); 
       
    88     g_async_queue_sort (async_queue, sort_compare, NULL);
       
    89 
       
    90     sorts++;
       
    91 
       
    92     if (sorts >= sort_multiplier) {
       
    93       can_quit = TRUE;
       
    94     }
       
    95     
       
    96     g_async_queue_sort (async_queue, sort_compare, NULL);
       
    97     len = g_async_queue_length (async_queue);
       
    98 
       
    99     PRINT_MSG (("sorted queue (for %d/%d times, size:%d)...", sorts, MAX_SORTS, len)); 
       
   100   } else {
       
   101     can_quit = TRUE;
       
   102     len = g_async_queue_length (async_queue);
       
   103     DEBUG_MSG (("printing queue (size:%d)...", len)); 
       
   104   }
       
   105 
       
   106   for (i = 0, last_p = NULL; i < len; i++) {
       
   107     p = g_async_queue_pop (async_queue);
       
   108     DEBUG_MSG (("item %d ---> %d", i, GPOINTER_TO_INT (p))); 
       
   109 
       
   110     if (last_p) {
       
   111       g_assert (GPOINTER_TO_INT (last_p) <= GPOINTER_TO_INT (p));
       
   112     }
       
   113 
       
   114     last_p = p;
       
   115   }
       
   116   
       
   117   if (can_quit && QUIT_WHEN_DONE) {
       
   118     g_main_loop_quit (main_loop);
       
   119   }
       
   120 
       
   121   return !can_quit;
       
   122 }
       
   123 
       
   124 static void
       
   125 enter_thread (gpointer data, gpointer user_data)
       
   126 {
       
   127   gint   len;
       
   128   gint   id;
       
   129   gulong ms;
       
   130 
       
   131   id = GPOINTER_TO_INT (data);
       
   132   
       
   133   ms = g_random_int_range (MIN_TIME * 1000, MAX_TIME * 1000);
       
   134   DEBUG_MSG (("entered thread with id:%d, adding to queue in:%ld ms", id, ms));
       
   135 
       
   136   g_usleep (ms * 1000);
       
   137 
       
   138   if (SORT_QUEUE_ON_PUSH) {
       
   139     g_async_queue_push_sorted (async_queue, GINT_TO_POINTER (id), sort_compare, NULL);
       
   140   } else {
       
   141     g_async_queue_push (async_queue, GINT_TO_POINTER (id));
       
   142   }
       
   143 
       
   144   len = g_async_queue_length (async_queue);
       
   145 
       
   146   DEBUG_MSG (("thread id:%d added to async queue (size:%d)", 
       
   147 	     id, len));
       
   148 }
       
   149 
       
   150 int 
       
   151 main (int argc, char *argv[])
       
   152 {
       
   153 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
       
   154   gint   i;
       
   155   gint   max_threads = MAX_THREADS;
       
   156   gint   max_unused_threads = MAX_THREADS;
       
   157   gint   sort_multiplier = MAX_SORTS;
       
   158   gint   sort_interval;
       
   159   gchar *msg;
       
   160   
       
   161   #ifdef __SYMBIAN32__
       
   162   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);
       
   163   g_set_print_handler(mrtPrintHandler);
       
   164   #endif /*__SYMBIAN32__*/
       
   165   g_thread_init (NULL);
       
   166 
       
   167   PRINT_MSG (("creating async queue..."));
       
   168   async_queue = g_async_queue_new ();
       
   169 
       
   170   g_return_val_if_fail (async_queue != NULL, EXIT_FAILURE);
       
   171 
       
   172   PRINT_MSG (("creating thread pool with max threads:%d, max unused threads:%d...",
       
   173 	     max_threads, max_unused_threads));
       
   174   thread_pool = g_thread_pool_new (enter_thread,
       
   175 				   async_queue,
       
   176 				   max_threads,
       
   177 				   FALSE,
       
   178 				   NULL);
       
   179 
       
   180   g_return_val_if_fail (thread_pool != NULL, EXIT_FAILURE);
       
   181 
       
   182   g_thread_pool_set_max_unused_threads (max_unused_threads);
       
   183 
       
   184   PRINT_MSG (("creating threads..."));
       
   185   for (i = 1; i <= max_threads; i++) {
       
   186     GError *error = NULL;
       
   187   
       
   188     g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
       
   189     
       
   190     g_assert_no_error (error);
       
   191   }
       
   192 
       
   193   if (!SORT_QUEUE_AFTER) {
       
   194     sort_multiplier = 1;
       
   195   }
       
   196   
       
   197   sort_interval = ((MAX_TIME / sort_multiplier) + 2)  * 1000;
       
   198   g_timeout_add (sort_interval, sort_queue, GINT_TO_POINTER (sort_multiplier));
       
   199 
       
   200   if (SORT_QUEUE_ON_PUSH) {
       
   201     msg = "sorting when pushing into the queue, checking queue is sorted";
       
   202   } else {
       
   203     msg = "sorting";
       
   204   }
       
   205 
       
   206   PRINT_MSG (("%s %d %s %d ms",
       
   207 	      msg,
       
   208 	      sort_multiplier, 
       
   209 	      sort_multiplier == 1 ? "time in" : "times, once every",
       
   210 	      sort_interval));
       
   211 
       
   212   DEBUG_MSG (("entering main event loop"));
       
   213 
       
   214   main_loop = g_main_loop_new (NULL, FALSE);
       
   215   g_main_loop_run (main_loop);
       
   216  #endif
       
   217   
       
   218   #if __SYMBIAN32__
       
   219   testResultXml("asyncqueue-test");
       
   220   #endif /* EMULATOR */
       
   221   
       
   222   return EXIT_SUCCESS;
       
   223 }