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