os/ossrv/glib/tests/refcount/objects.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
     3 */
     4 #include <unistd.h>
     5 #include <glib.h>
     6 #include <glib-object.h>
     7 
     8 #ifdef __SYMBIAN32__
     9 #include <glib_global.h>
    10 #include "mrt2_glib2_test.h"
    11 #endif /*__SYMBIAN32__*/
    12 #define G_TYPE_TEST               (my_test_get_type ())
    13 #define MY_TEST(test)              (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
    14 #define MY_IS_TEST(test)           (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
    15 #define MY_TEST_CLASS(tclass)      (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
    16 #define MY_IS_TEST_CLASS(tclass)   (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
    17 #define MY_TEST_GET_CLASS(test)    (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
    18 
    19 typedef struct _GTest GTest;
    20 typedef struct _GTestClass GTestClass;
    21 
    22 struct _GTest
    23 {
    24   GObject object;
    25 };
    26 
    27 struct _GTestClass
    28 {
    29   GObjectClass parent_class;
    30 };
    31 
    32 static GType my_test_get_type (void);
    33 static volatile gboolean stopping;
    34 
    35 static void my_test_class_init (GTestClass * klass);
    36 static void my_test_init (GTest * test);
    37 static void my_test_dispose (GObject * object);
    38 
    39 static GObjectClass *parent_class = NULL;
    40 
    41 static GType
    42 my_test_get_type (void)
    43 {
    44   static GType test_type = 0;
    45 
    46   if (!test_type) {
    47     static const GTypeInfo test_info = {
    48       sizeof (GTestClass),
    49       NULL,
    50       NULL,
    51       (GClassInitFunc) my_test_class_init,
    52       NULL,
    53       NULL,
    54       sizeof (GTest),
    55       0,
    56       (GInstanceInitFunc) my_test_init,
    57       NULL
    58     };
    59 
    60     test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
    61         &test_info, 0);
    62   }
    63   return test_type;
    64 }
    65 
    66 static void
    67 my_test_class_init (GTestClass * klass)
    68 {
    69   GObjectClass *gobject_class;
    70 
    71   gobject_class = (GObjectClass *) klass;
    72 
    73   parent_class = g_type_class_ref (G_TYPE_OBJECT);
    74 
    75   gobject_class->dispose = my_test_dispose;
    76 }
    77 
    78 static void
    79 my_test_init (GTest * test)
    80 {
    81   g_print ("init %p\n", test);
    82 }
    83 
    84 static void
    85 my_test_dispose (GObject * object)
    86 {
    87   GTest *test;
    88 
    89   test = MY_TEST (object);
    90 
    91   g_print ("dispose %p!\n", object);
    92 
    93   G_OBJECT_CLASS (parent_class)->dispose (object);
    94 }
    95 
    96 static void
    97 my_test_do_refcount (GTest * test)
    98 {
    99   g_object_ref (test); 
   100   g_object_unref (test); 
   101 }
   102 
   103 static gpointer
   104 run_thread (GTest * test)
   105 {
   106   gint i = 1;
   107 
   108   while (!stopping) {
   109     my_test_do_refcount (test);
   110     if ((i++ % 10000) == 0) {
   111       g_print (".");
   112       g_thread_yield(); /* force context switch */
   113     }
   114   }
   115 
   116   return NULL;
   117 }
   118 
   119 int
   120 main (int argc, char **argv)
   121 {
   122   gint i;
   123   GTest *test1, *test2;
   124   GArray *test_threads;
   125   const guint n_threads = 5;
   126 
   127   #ifdef __SYMBIAN32__
   128   
   129   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);
   130   g_set_print_handler(mrtPrintHandler);
   131   #endif /*__SYMBIAN32__*/
   132   g_thread_init (NULL);
   133   g_print ("START: %s\n", argv[0]);
   134   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
   135   g_type_init ();
   136 
   137   test1 = g_object_new (G_TYPE_TEST, NULL);
   138   test2 = g_object_new (G_TYPE_TEST, NULL);
   139 
   140   test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
   141 
   142   stopping = FALSE;
   143 
   144   for (i = 0; i < n_threads; i++) {
   145     GThread *thread;
   146 
   147     thread = g_thread_create ((GThreadFunc) run_thread, test1, TRUE, NULL);
   148     g_array_append_val (test_threads, thread);
   149 
   150     thread = g_thread_create ((GThreadFunc) run_thread, test2, TRUE, NULL);
   151     g_array_append_val (test_threads, thread);
   152   }
   153   g_usleep (5000000);
   154 
   155   stopping = TRUE;
   156 
   157   g_print ("\nstopping\n");
   158 
   159   /* join all threads */
   160   for (i = 0; i < 2 * n_threads; i++) {
   161     GThread *thread;
   162 
   163     thread = g_array_index (test_threads, GThread *, i);
   164     g_thread_join (thread);
   165   }
   166 
   167   g_print ("stopped\n");
   168 #ifdef __SYMBIAN32__
   169   testResultXml("objects");
   170 #endif /* EMULATOR */
   171 
   172   return 0;
   173 }