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