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