glib/tests/refcount/properties.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     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 enum {
       
    20   PROP_0,
       
    21   PROP_DUMMY
       
    22 };
       
    23 
       
    24 typedef struct _GTest GTest;
       
    25 typedef struct _GTestClass GTestClass;
       
    26 
       
    27 struct _GTest
       
    28 {
       
    29   GObject object;
       
    30   gint id;
       
    31   gint dummy;
       
    32 
       
    33   gint count;
       
    34 };
       
    35 
       
    36 struct _GTestClass
       
    37 {
       
    38   GObjectClass parent_class;
       
    39 };
       
    40 
       
    41 static GType my_test_get_type (void);
       
    42 static volatile gboolean stopping;
       
    43 
       
    44 static void my_test_class_init (GTestClass * klass);
       
    45 static void my_test_init (GTest * test);
       
    46 static void my_test_dispose (GObject * object);
       
    47 static void my_test_get_property (GObject    *object,
       
    48                                   guint       prop_id,
       
    49                                   GValue     *value,
       
    50                                   GParamSpec *pspec);
       
    51 static void my_test_set_property (GObject      *object,
       
    52                                   guint         prop_id,
       
    53                                   const GValue *value,
       
    54                                   GParamSpec   *pspec);
       
    55 
       
    56 static GObjectClass *parent_class = NULL;
       
    57 
       
    58 static GType
       
    59 my_test_get_type (void)
       
    60 {
       
    61   static GType test_type = 0;
       
    62 
       
    63   if (!test_type) {
       
    64     static const GTypeInfo test_info = {
       
    65       sizeof (GTestClass),
       
    66       NULL,
       
    67       NULL,
       
    68       (GClassInitFunc) my_test_class_init,
       
    69       NULL,
       
    70       NULL,
       
    71       sizeof (GTest),
       
    72       0,
       
    73       (GInstanceInitFunc) my_test_init,
       
    74       NULL
       
    75     };
       
    76 
       
    77     test_type = g_type_register_static (G_TYPE_OBJECT, "GTest", &test_info, 0);
       
    78   }
       
    79   return test_type;
       
    80 }
       
    81 
       
    82 static void
       
    83 my_test_class_init (GTestClass * klass)
       
    84 {
       
    85   GObjectClass *gobject_class;
       
    86 
       
    87   gobject_class = (GObjectClass *) klass;
       
    88 
       
    89   parent_class = g_type_class_ref (G_TYPE_OBJECT);
       
    90 
       
    91   gobject_class->dispose = my_test_dispose;
       
    92   gobject_class->get_property = my_test_get_property;
       
    93   gobject_class->set_property = my_test_set_property;
       
    94 
       
    95   g_object_class_install_property (gobject_class,
       
    96 				   PROP_DUMMY,
       
    97 				   g_param_spec_int ("dummy",
       
    98 						     NULL, 
       
    99 						     NULL,
       
   100 						     0, G_MAXINT, 0,
       
   101 						     G_PARAM_READWRITE));
       
   102 }
       
   103 
       
   104 static void
       
   105 my_test_init (GTest * test)
       
   106 {
       
   107   static guint static_id = 1;
       
   108   test->id = static_id++;
       
   109 }
       
   110 
       
   111 static void
       
   112 my_test_dispose (GObject * object)
       
   113 {
       
   114   GTest *test;
       
   115 
       
   116   test = MY_TEST (object);
       
   117 
       
   118   G_OBJECT_CLASS (parent_class)->dispose (object);
       
   119 }
       
   120 
       
   121 static void 
       
   122 my_test_get_property (GObject    *object,
       
   123                       guint       prop_id,
       
   124                       GValue     *value,
       
   125                       GParamSpec *pspec)
       
   126 {
       
   127   GTest *test;
       
   128 
       
   129   test = MY_TEST (object);
       
   130 
       
   131   switch (prop_id)
       
   132     {
       
   133     case PROP_DUMMY:
       
   134       g_value_set_int (value, test->dummy);
       
   135       break;
       
   136     default:
       
   137       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   138       break;
       
   139     }
       
   140 }
       
   141 
       
   142 static void 
       
   143 my_test_set_property (GObject      *object,
       
   144                       guint         prop_id,
       
   145                       const GValue *value,
       
   146                       GParamSpec   *pspec)
       
   147 {
       
   148   GTest *test;
       
   149 
       
   150   test = MY_TEST (object);
       
   151 
       
   152   switch (prop_id)
       
   153     {
       
   154     case PROP_DUMMY:
       
   155       test->dummy = g_value_get_int (value);
       
   156       break;
       
   157     default:
       
   158       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       
   159       break;
       
   160     }
       
   161 }
       
   162 
       
   163 static void
       
   164 dummy_notify (GObject    *object,
       
   165               GParamSpec *pspec)
       
   166 {
       
   167   GTest *test;
       
   168 
       
   169   test = MY_TEST (object);
       
   170 
       
   171   test->count++;  
       
   172 }
       
   173 
       
   174 static void
       
   175 my_test_do_property (GTest * test)
       
   176 {
       
   177   gint dummy;
       
   178 
       
   179   g_object_get (test, "dummy", &dummy, NULL);
       
   180   g_object_set (test, "dummy", dummy + 1, NULL);
       
   181 }
       
   182 
       
   183 static gpointer
       
   184 run_thread (GTest * test)
       
   185 {
       
   186   gint i = 1;
       
   187   
       
   188   while (!stopping) {
       
   189     my_test_do_property (test);
       
   190     if ((i++ % 10000) == 0)
       
   191       {
       
   192         g_print (".%c", 'a' + test->id);
       
   193         g_thread_yield(); /* force context switch */
       
   194       }
       
   195   }
       
   196 
       
   197   return NULL;
       
   198 }
       
   199 
       
   200 int
       
   201 main (int argc, char **argv)
       
   202 {
       
   203   gint i;
       
   204   GArray *test_objects;
       
   205   GArray *test_threads;
       
   206   const gint n_threads = 5;
       
   207 
       
   208   #ifdef __SYMBIAN32__
       
   209   
       
   210   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);
       
   211   g_set_print_handler(mrtPrintHandler);
       
   212   #endif /*__SYMBIAN32__*/
       
   213   g_thread_init (NULL);
       
   214   g_print ("START: %s\n", argv[0]);
       
   215   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
       
   216   g_type_init ();
       
   217 
       
   218   test_objects = g_array_new (FALSE, FALSE, sizeof (GTest *));
       
   219 
       
   220   for (i = 0; i < n_threads; i++) {
       
   221     GTest *test;
       
   222     
       
   223     test = g_object_new (G_TYPE_TEST, NULL);
       
   224     g_array_append_val (test_objects, test);
       
   225 
       
   226     g_assert (test->count == test->dummy);
       
   227     g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
       
   228   }
       
   229     
       
   230   test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
       
   231 
       
   232   stopping = FALSE;
       
   233 
       
   234   for (i = 0; i < n_threads; i++) {
       
   235     GThread *thread;
       
   236     GTest *test;
       
   237 
       
   238     test = g_array_index (test_objects, GTest *, i);
       
   239 
       
   240     thread = g_thread_create ((GThreadFunc) run_thread, test, TRUE, NULL);
       
   241     g_array_append_val (test_threads, thread);
       
   242   }
       
   243   g_usleep (3000000);
       
   244 
       
   245   stopping = TRUE;
       
   246   g_print ("\nstopping\n");
       
   247 
       
   248   /* join all threads */
       
   249   for (i = 0; i < n_threads; i++) {
       
   250     GThread *thread;
       
   251 
       
   252     thread = g_array_index (test_threads, GThread *, i);
       
   253     g_thread_join (thread);
       
   254   }
       
   255 
       
   256   g_print ("stopped\n");
       
   257 
       
   258   for (i = 0; i < n_threads; i++) {
       
   259     GTest *test;
       
   260 
       
   261     test = g_array_index (test_objects, GTest *, i);
       
   262 
       
   263     g_assert (test->count == test->dummy);
       
   264   }
       
   265 #ifdef __SYMBIAN32__
       
   266   testResultXml("properties");
       
   267 #endif /* EMULATOR */
       
   268   return 0;
       
   269 }