os/ossrv/glib/tsrc/BC/src/tthread.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 #undef G_DISABLE_ASSERT
     2 #undef G_LOG_DOMAIN
     3 
     4 #include <glib.h>
     5 #include <stdio.h>
     6 
     7 #ifdef SYMBIAN
     8 #include <glib_global.h>
     9 #include "mrt2_glib2_test.h"
    10 #endif /*SYMBIAN*/
    11 
    12 #define THREADS 10
    13 
    14 /* GStaticRecMutex */
    15 
    16 GStaticRecMutex test_g_static_rec_mutex_mutex;
    17 static guint test_g_static_rec_mutex_int = 0;
    18 static gboolean test_g_static_rec_mutex_thread_ready;
    19 
    20 static gpointer
    21 test_g_static_rec_mutex_thread (gpointer data)
    22 {
    23   g_assert (GPOINTER_TO_INT (data) == 42);
    24   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) 
    25 	    == FALSE);
    26   test_g_static_rec_mutex_thread_ready = TRUE;
    27   
    28   //Testing g_static_rec_mutex_lock_full
    29   g_static_rec_mutex_lock_full (&test_g_static_rec_mutex_mutex, 10);
    30   
    31   g_assert (test_g_static_rec_mutex_int == 42);
    32   test_g_static_rec_mutex_thread_ready = FALSE;
    33   
    34   //Testing g_static_rec_mutex_unlock_full for lock_full
    35   g_static_rec_mutex_unlock_full (&test_g_static_rec_mutex_mutex);
    36   
    37   g_thread_exit (GINT_TO_POINTER (43));
    38   
    39   g_assert_not_reached ();
    40   return NULL;
    41 }
    42 
    43 static void
    44 test_g_static_rec_mutex (void)
    45 {
    46   GThread *thread;
    47 
    48   //Test for g_static_rec_mutex_init	
    49   g_static_rec_mutex_init(&test_g_static_rec_mutex_mutex);
    50 
    51   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
    52   test_g_static_rec_mutex_thread_ready = FALSE;
    53   thread = g_thread_create (test_g_static_rec_mutex_thread, 
    54 			    GINT_TO_POINTER (42), TRUE, NULL);
    55   /* This busy wait is only for testing purposes and not an example of
    56    * good code!*/
    57   while (!test_g_static_rec_mutex_thread_ready)
    58     g_usleep (G_USEC_PER_SEC / 5);
    59 
    60   g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
    61   test_g_static_rec_mutex_int = 41;
    62   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
    63   test_g_static_rec_mutex_int = 42;  
    64   g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
    65 
    66   /* This busy wait is only for testing purposes and not an example of
    67    * good code!*/
    68   while (test_g_static_rec_mutex_thread_ready)
    69     g_usleep (G_USEC_PER_SEC / 5);
    70 
    71   g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
    72   test_g_static_rec_mutex_int = 0;  
    73   
    74   //Testing g_static_rec_mutex_unlock_full with normal single lock
    75   g_static_rec_mutex_unlock_full (&test_g_static_rec_mutex_mutex);
    76 
    77   g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
    78   g_static_rec_mutex_free (&test_g_static_rec_mutex_mutex);
    79 }
    80 
    81 
    82 /* GStaticRWLock */
    83 
    84 /* -1 = writing; >0 = # of readers */
    85 static gint test_g_static_rw_lock_state = 0; 
    86 G_LOCK_DEFINE (test_g_static_rw_lock_state);
    87 
    88 static gboolean test_g_static_rw_lock_run = TRUE; 
    89 GStaticRWLock test_g_static_rw_lock_lock;// = G_STATIC_RW_LOCK_INIT;
    90 
    91 static gpointer
    92 test_g_static_rw_lock_thread (gpointer data)
    93 {
    94   while (test_g_static_rw_lock_run)
    95     {
    96       if (g_random_double() > .2) /* I'm a reader */
    97 	{
    98 	  
    99 	  if (g_random_double() > .2) /* I'll block */
   100 	    g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
   101 	  else /* I'll only try */
   102 	    if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
   103 	      continue;
   104 	  G_LOCK (test_g_static_rw_lock_state);
   105 	  g_assert (test_g_static_rw_lock_state >= 0);
   106 	  test_g_static_rw_lock_state++;
   107 	  G_UNLOCK (test_g_static_rw_lock_state);
   108 
   109 	  g_usleep (g_random_int_range (20,1000));
   110 
   111 	  G_LOCK (test_g_static_rw_lock_state);
   112 	  test_g_static_rw_lock_state--;
   113 	  G_UNLOCK (test_g_static_rw_lock_state);
   114 
   115 	  g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
   116 	}
   117       else /* I'm a writer */
   118 	{
   119 	  
   120 	  if (g_random_double() > .2) /* I'll block */ 
   121 	    g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
   122 	  else /* I'll only try */
   123 	    if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
   124 	      continue;
   125 	  G_LOCK (test_g_static_rw_lock_state);
   126 	  g_assert (test_g_static_rw_lock_state == 0);
   127 	  test_g_static_rw_lock_state = -1;
   128 	  G_UNLOCK (test_g_static_rw_lock_state);
   129 
   130 	  g_usleep (g_random_int_range (20,1000));
   131 
   132 	  G_LOCK (test_g_static_rw_lock_state);
   133 	  test_g_static_rw_lock_state = 0;
   134 	  G_UNLOCK (test_g_static_rw_lock_state);
   135 
   136 	  g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
   137 	}
   138     }
   139   return NULL;
   140 }
   141 
   142 static void
   143 test_g_static_rw_lock ()
   144 {
   145   GThread *threads[THREADS];
   146   guint i;
   147   
   148   g_static_rw_lock_init(&test_g_static_rw_lock_lock);
   149   
   150   for (i = 0; i < THREADS; i++)
   151     {
   152       threads[i] = g_thread_create (test_g_static_rw_lock_thread, 
   153 				    NULL, TRUE, NULL);      
   154     }
   155   g_usleep (G_USEC_PER_SEC * 5);
   156   test_g_static_rw_lock_run = FALSE;
   157   for (i = 0; i < THREADS; i++)
   158     {
   159       g_thread_join (threads[i]);
   160     }
   161   g_assert (test_g_static_rw_lock_state == 0);
   162   g_static_rw_lock_free(&test_g_static_rw_lock_lock);
   163 }
   164 
   165 void test_g_thread_error_quark()
   166 {
   167 	GQuark c = g_thread_error_quark();
   168 	g_assert(c == 1);
   169 }
   170 
   171 
   172 static void test_g_thread_set_priority (void)
   173 {
   174   GThread *thread;
   175   thread = g_thread_self();
   176   g_thread_set_priority (thread, G_THREAD_PRIORITY_HIGH);
   177   g_assert((thread->priority) == G_THREAD_PRIORITY_HIGH);
   178 }
   179 
   180 
   181 /* run all the tests */
   182 void
   183 run_all_thread_tests()
   184 {
   185   test_g_static_rec_mutex ();
   186   test_g_static_rw_lock ();
   187   test_g_thread_error_quark();
   188   test_g_thread_set_priority();
   189 }
   190 
   191 int 
   192 main (int   argc,
   193       char *argv[])
   194 {
   195   #ifdef SYMBIAN
   196   
   197   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);
   198   g_set_print_handler(mrtPrintHandler);
   199   #endif /*SYMBIAN*/
   200 	  
   201 
   202   /* Only run the test, if threads are enabled and a default thread
   203      implementation is available */
   204 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
   205   g_thread_init (NULL);
   206   run_all_thread_tests ();
   207 
   208   /* Now we rerun all tests, but this time we fool the system into
   209    * thinking, that the available thread system is not native, but
   210    * userprovided. */
   211 
   212   g_thread_use_default_impl = FALSE;
   213   run_all_thread_tests ();
   214   
   215 #endif
   216 
   217 #ifdef SYMBIAN
   218   testResultXml("tthread");
   219 #endif /* EMULATOR */
   220   return 0;
   221 }