os/ossrv/glib/tsrc/BC/src/tthread.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/src/tthread.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,221 @@
     1.4 +#undef G_DISABLE_ASSERT
     1.5 +#undef G_LOG_DOMAIN
     1.6 +
     1.7 +#include <glib.h>
     1.8 +#include <stdio.h>
     1.9 +
    1.10 +#ifdef SYMBIAN
    1.11 +#include <glib_global.h>
    1.12 +#include "mrt2_glib2_test.h"
    1.13 +#endif /*SYMBIAN*/
    1.14 +
    1.15 +#define THREADS 10
    1.16 +
    1.17 +/* GStaticRecMutex */
    1.18 +
    1.19 +GStaticRecMutex test_g_static_rec_mutex_mutex;
    1.20 +static guint test_g_static_rec_mutex_int = 0;
    1.21 +static gboolean test_g_static_rec_mutex_thread_ready;
    1.22 +
    1.23 +static gpointer
    1.24 +test_g_static_rec_mutex_thread (gpointer data)
    1.25 +{
    1.26 +  g_assert (GPOINTER_TO_INT (data) == 42);
    1.27 +  g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) 
    1.28 +	    == FALSE);
    1.29 +  test_g_static_rec_mutex_thread_ready = TRUE;
    1.30 +  
    1.31 +  //Testing g_static_rec_mutex_lock_full
    1.32 +  g_static_rec_mutex_lock_full (&test_g_static_rec_mutex_mutex, 10);
    1.33 +  
    1.34 +  g_assert (test_g_static_rec_mutex_int == 42);
    1.35 +  test_g_static_rec_mutex_thread_ready = FALSE;
    1.36 +  
    1.37 +  //Testing g_static_rec_mutex_unlock_full for lock_full
    1.38 +  g_static_rec_mutex_unlock_full (&test_g_static_rec_mutex_mutex);
    1.39 +  
    1.40 +  g_thread_exit (GINT_TO_POINTER (43));
    1.41 +  
    1.42 +  g_assert_not_reached ();
    1.43 +  return NULL;
    1.44 +}
    1.45 +
    1.46 +static void
    1.47 +test_g_static_rec_mutex (void)
    1.48 +{
    1.49 +  GThread *thread;
    1.50 +
    1.51 +  //Test for g_static_rec_mutex_init	
    1.52 +  g_static_rec_mutex_init(&test_g_static_rec_mutex_mutex);
    1.53 +
    1.54 +  g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
    1.55 +  test_g_static_rec_mutex_thread_ready = FALSE;
    1.56 +  thread = g_thread_create (test_g_static_rec_mutex_thread, 
    1.57 +			    GINT_TO_POINTER (42), TRUE, NULL);
    1.58 +  /* This busy wait is only for testing purposes and not an example of
    1.59 +   * good code!*/
    1.60 +  while (!test_g_static_rec_mutex_thread_ready)
    1.61 +    g_usleep (G_USEC_PER_SEC / 5);
    1.62 +
    1.63 +  g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
    1.64 +  test_g_static_rec_mutex_int = 41;
    1.65 +  g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
    1.66 +  test_g_static_rec_mutex_int = 42;  
    1.67 +  g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
    1.68 +
    1.69 +  /* This busy wait is only for testing purposes and not an example of
    1.70 +   * good code!*/
    1.71 +  while (test_g_static_rec_mutex_thread_ready)
    1.72 +    g_usleep (G_USEC_PER_SEC / 5);
    1.73 +
    1.74 +  g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
    1.75 +  test_g_static_rec_mutex_int = 0;  
    1.76 +  
    1.77 +  //Testing g_static_rec_mutex_unlock_full with normal single lock
    1.78 +  g_static_rec_mutex_unlock_full (&test_g_static_rec_mutex_mutex);
    1.79 +
    1.80 +  g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43);
    1.81 +  g_static_rec_mutex_free (&test_g_static_rec_mutex_mutex);
    1.82 +}
    1.83 +
    1.84 +
    1.85 +/* GStaticRWLock */
    1.86 +
    1.87 +/* -1 = writing; >0 = # of readers */
    1.88 +static gint test_g_static_rw_lock_state = 0; 
    1.89 +G_LOCK_DEFINE (test_g_static_rw_lock_state);
    1.90 +
    1.91 +static gboolean test_g_static_rw_lock_run = TRUE; 
    1.92 +GStaticRWLock test_g_static_rw_lock_lock;// = G_STATIC_RW_LOCK_INIT;
    1.93 +
    1.94 +static gpointer
    1.95 +test_g_static_rw_lock_thread (gpointer data)
    1.96 +{
    1.97 +  while (test_g_static_rw_lock_run)
    1.98 +    {
    1.99 +      if (g_random_double() > .2) /* I'm a reader */
   1.100 +	{
   1.101 +	  
   1.102 +	  if (g_random_double() > .2) /* I'll block */
   1.103 +	    g_static_rw_lock_reader_lock (&test_g_static_rw_lock_lock);
   1.104 +	  else /* I'll only try */
   1.105 +	    if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
   1.106 +	      continue;
   1.107 +	  G_LOCK (test_g_static_rw_lock_state);
   1.108 +	  g_assert (test_g_static_rw_lock_state >= 0);
   1.109 +	  test_g_static_rw_lock_state++;
   1.110 +	  G_UNLOCK (test_g_static_rw_lock_state);
   1.111 +
   1.112 +	  g_usleep (g_random_int_range (20,1000));
   1.113 +
   1.114 +	  G_LOCK (test_g_static_rw_lock_state);
   1.115 +	  test_g_static_rw_lock_state--;
   1.116 +	  G_UNLOCK (test_g_static_rw_lock_state);
   1.117 +
   1.118 +	  g_static_rw_lock_reader_unlock (&test_g_static_rw_lock_lock);
   1.119 +	}
   1.120 +      else /* I'm a writer */
   1.121 +	{
   1.122 +	  
   1.123 +	  if (g_random_double() > .2) /* I'll block */ 
   1.124 +	    g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
   1.125 +	  else /* I'll only try */
   1.126 +	    if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
   1.127 +	      continue;
   1.128 +	  G_LOCK (test_g_static_rw_lock_state);
   1.129 +	  g_assert (test_g_static_rw_lock_state == 0);
   1.130 +	  test_g_static_rw_lock_state = -1;
   1.131 +	  G_UNLOCK (test_g_static_rw_lock_state);
   1.132 +
   1.133 +	  g_usleep (g_random_int_range (20,1000));
   1.134 +
   1.135 +	  G_LOCK (test_g_static_rw_lock_state);
   1.136 +	  test_g_static_rw_lock_state = 0;
   1.137 +	  G_UNLOCK (test_g_static_rw_lock_state);
   1.138 +
   1.139 +	  g_static_rw_lock_writer_unlock (&test_g_static_rw_lock_lock);
   1.140 +	}
   1.141 +    }
   1.142 +  return NULL;
   1.143 +}
   1.144 +
   1.145 +static void
   1.146 +test_g_static_rw_lock ()
   1.147 +{
   1.148 +  GThread *threads[THREADS];
   1.149 +  guint i;
   1.150 +  
   1.151 +  g_static_rw_lock_init(&test_g_static_rw_lock_lock);
   1.152 +  
   1.153 +  for (i = 0; i < THREADS; i++)
   1.154 +    {
   1.155 +      threads[i] = g_thread_create (test_g_static_rw_lock_thread, 
   1.156 +				    NULL, TRUE, NULL);      
   1.157 +    }
   1.158 +  g_usleep (G_USEC_PER_SEC * 5);
   1.159 +  test_g_static_rw_lock_run = FALSE;
   1.160 +  for (i = 0; i < THREADS; i++)
   1.161 +    {
   1.162 +      g_thread_join (threads[i]);
   1.163 +    }
   1.164 +  g_assert (test_g_static_rw_lock_state == 0);
   1.165 +  g_static_rw_lock_free(&test_g_static_rw_lock_lock);
   1.166 +}
   1.167 +
   1.168 +void test_g_thread_error_quark()
   1.169 +{
   1.170 +	GQuark c = g_thread_error_quark();
   1.171 +	g_assert(c == 1);
   1.172 +}
   1.173 +
   1.174 +
   1.175 +static void test_g_thread_set_priority (void)
   1.176 +{
   1.177 +  GThread *thread;
   1.178 +  thread = g_thread_self();
   1.179 +  g_thread_set_priority (thread, G_THREAD_PRIORITY_HIGH);
   1.180 +  g_assert((thread->priority) == G_THREAD_PRIORITY_HIGH);
   1.181 +}
   1.182 +
   1.183 +
   1.184 +/* run all the tests */
   1.185 +void
   1.186 +run_all_thread_tests()
   1.187 +{
   1.188 +  test_g_static_rec_mutex ();
   1.189 +  test_g_static_rw_lock ();
   1.190 +  test_g_thread_error_quark();
   1.191 +  test_g_thread_set_priority();
   1.192 +}
   1.193 +
   1.194 +int 
   1.195 +main (int   argc,
   1.196 +      char *argv[])
   1.197 +{
   1.198 +  #ifdef SYMBIAN
   1.199 +  
   1.200 +  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);
   1.201 +  g_set_print_handler(mrtPrintHandler);
   1.202 +  #endif /*SYMBIAN*/
   1.203 +	  
   1.204 +
   1.205 +  /* Only run the test, if threads are enabled and a default thread
   1.206 +     implementation is available */
   1.207 +#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
   1.208 +  g_thread_init (NULL);
   1.209 +  run_all_thread_tests ();
   1.210 +
   1.211 +  /* Now we rerun all tests, but this time we fool the system into
   1.212 +   * thinking, that the available thread system is not native, but
   1.213 +   * userprovided. */
   1.214 +
   1.215 +  g_thread_use_default_impl = FALSE;
   1.216 +  run_all_thread_tests ();
   1.217 +  
   1.218 +#endif
   1.219 +
   1.220 +#ifdef SYMBIAN
   1.221 +  testResultXml("tthread");
   1.222 +#endif /* EMULATOR */
   1.223 +  return 0;
   1.224 +}