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