glib/tests/errorcheck-mutex-test.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /* Portions copyright (c) 2009 Nokia Corporation.  All rights reserved. */
       
     2 #undef G_DISABLE_ASSERT
       
     3 #undef G_LOG_DOMAIN
       
     4 #define G_ERRORCHECK_MUTEXES
       
     5 
       
     6 #include <glib.h>
       
     7 #include <stdio.h>
       
     8 #include <string.h>
       
     9 #ifdef __SYMBIAN32__
       
    10 #include <glib_global.h>
       
    11 #include "mrt2_glib2_test.h"
       
    12 #endif /*__SYMBIAN32__*/
       
    13 static gpointer
       
    14 locking_thread (gpointer mutex)
       
    15 {
       
    16   g_mutex_lock ((GMutex*)mutex);
       
    17 
       
    18   return NULL;
       
    19 }
       
    20 
       
    21 static void
       
    22 lock_locked_mutex (void)
       
    23 {
       
    24   GMutex* mutex = g_mutex_new ();
       
    25   g_mutex_lock (mutex);
       
    26   g_mutex_lock (mutex);
       
    27 }
       
    28 
       
    29 static void
       
    30 trylock_locked_mutex (void)
       
    31 {
       
    32   GMutex* mutex = g_mutex_new ();
       
    33   g_mutex_lock (mutex);
       
    34   g_mutex_trylock (mutex);
       
    35 }
       
    36 
       
    37 static void
       
    38 unlock_unlocked_mutex (void)
       
    39 {
       
    40   GMutex* mutex = g_mutex_new ();
       
    41   g_mutex_lock (mutex);
       
    42   g_mutex_unlock (mutex);
       
    43   g_mutex_unlock (mutex);
       
    44 }
       
    45 
       
    46 static void
       
    47 free_locked_mutex (void)
       
    48 {
       
    49   GMutex* mutex = g_mutex_new ();
       
    50   g_mutex_lock (mutex);
       
    51   g_mutex_free (mutex);
       
    52 }
       
    53 
       
    54 static void
       
    55 wait_on_unlocked_mutex (void)
       
    56 {
       
    57   GMutex* mutex = g_mutex_new ();
       
    58   GCond* cond = g_cond_new ();
       
    59   g_cond_wait (cond, mutex);
       
    60 }
       
    61 
       
    62 static void
       
    63 wait_on_otherwise_locked_mutex (void)
       
    64 {
       
    65   GMutex* mutex = g_mutex_new ();
       
    66   GCond* cond = g_cond_new ();
       
    67   GThread* thread = g_thread_create (locking_thread, mutex, TRUE, NULL);
       
    68   g_assert (thread != NULL);
       
    69   g_usleep (G_USEC_PER_SEC);
       
    70   g_cond_wait (cond, mutex);
       
    71 }
       
    72 
       
    73 static void
       
    74 timed_wait_on_unlocked_mutex (void)
       
    75 {
       
    76   GMutex* mutex = g_mutex_new ();
       
    77   GCond* cond = g_cond_new ();
       
    78   g_cond_timed_wait (cond, mutex, NULL);
       
    79 }
       
    80 
       
    81 static void
       
    82 timed_wait_on_otherwise_locked_mutex (void)
       
    83 {
       
    84   GMutex* mutex = g_mutex_new ();
       
    85   GCond* cond = g_cond_new ();
       
    86   GThread* thread = g_thread_create (locking_thread, mutex, TRUE, NULL);
       
    87   g_assert (thread != NULL);
       
    88   g_usleep (G_USEC_PER_SEC);
       
    89   g_cond_timed_wait (cond, mutex, NULL);
       
    90 }
       
    91 
       
    92 struct
       
    93 {
       
    94   char *name;
       
    95   void (*func)();
       
    96 } func_table[] =
       
    97 {
       
    98   {"lock_locked_mutex", lock_locked_mutex},
       
    99   {"trylock_locked_mutex", trylock_locked_mutex},
       
   100   {"unlock_unlocked_mutex", unlock_unlocked_mutex},
       
   101   {"free_locked_mutex", free_locked_mutex},
       
   102   {"wait_on_unlocked_mutex", wait_on_unlocked_mutex},
       
   103   {"wait_on_otherwise_locked_mutex", wait_on_otherwise_locked_mutex},
       
   104   {"timed_wait_on_unlocked_mutex", timed_wait_on_unlocked_mutex},
       
   105   {"timed_wait_on_otherwise_locked_mutex",
       
   106    timed_wait_on_otherwise_locked_mutex}
       
   107 };
       
   108 
       
   109 int
       
   110 main (int argc, char* argv[])
       
   111 {
       
   112   int i;
       
   113 #ifdef __SYMBIAN32__
       
   114  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);
       
   115  g_set_print_handler(mrtPrintHandler);
       
   116  #endif /*__SYMBIAN32__*/
       
   117   if (argc == 2)
       
   118     {
       
   119       for (i = 0; i < G_N_ELEMENTS (func_table); i++)
       
   120         {
       
   121           if (strcmp (func_table[i].name, argv[1]) == 0)
       
   122             {
       
   123               g_thread_init (NULL);
       
   124               func_table[i].func ();
       
   125               g_assert_not_reached ();
       
   126             }
       
   127         }
       
   128     }
       
   129 
       
   130   fprintf (stderr, "Usage: errorcheck-mutex-test [TEST]\n\n");
       
   131   fprintf (stderr, "   where TEST can be one of:\n\n");
       
   132   for (i = 0; i < G_N_ELEMENTS (func_table); i++)
       
   133     {
       
   134       fprintf (stderr, "      %s\n", func_table[i].name);
       
   135     }
       
   136 #ifdef __SYMBIAN32__
       
   137   testResultXml("errorcheck-mutex-test");
       
   138   #endif /* EMULATOR */
       
   139   return 0;
       
   140 }