os/ossrv/glib/gthread/gthread-impl.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* GLIB - Library of useful routines for C programming
sl@0
     2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
sl@0
     3
 *
sl@0
     4
 * gthread.c: thread related functions
sl@0
     5
 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
sl@0
     6
 * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * This library is free software; you can redistribute it and/or
sl@0
     9
 * modify it under the terms of the GNU Lesser General Public
sl@0
    10
 * License as published by the Free Software Foundation; either
sl@0
    11
 * version 2 of the License, or (at your option) any later version.
sl@0
    12
 *
sl@0
    13
 * This library is distributed in the hope that it will be useful,
sl@0
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
sl@0
    16
 * Lesser General Public License for more details.
sl@0
    17
 *
sl@0
    18
 * You should have received a copy of the GNU Lesser General Public
sl@0
    19
 * License along with this library; if not, write to the
sl@0
    20
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    21
 * Boston, MA 02111-1307, USA.
sl@0
    22
 */
sl@0
    23
sl@0
    24
/*
sl@0
    25
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
sl@0
    26
 * file for a list of people on the GLib Team.  See the ChangeLog
sl@0
    27
 * files for a list of changes.  These files are distributed with
sl@0
    28
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
sl@0
    29
 */
sl@0
    30
sl@0
    31
/*
sl@0
    32
 * MT safe
sl@0
    33
 */
sl@0
    34
sl@0
    35
#include "config.h"
sl@0
    36
sl@0
    37
#include "glib.h"
sl@0
    38
#include "gthreadprivate.h"
sl@0
    39
sl@0
    40
#ifdef __SYMBIAN32__
sl@0
    41
#include <string.h>
sl@0
    42
#endif//__SYMBIAN32__
sl@0
    43
sl@0
    44
#ifdef __SYMBIAN32__
sl@0
    45
#include <glib_global.h>
sl@0
    46
#include "gthread_wsd.h"
sl@0
    47
#endif /* __SYMBIAN32__ */
sl@0
    48
sl@0
    49
#ifdef G_THREADS_ENABLED
sl@0
    50
#if EMULATOR
sl@0
    51
PLS(zero_thread,gthread_impl,GSystemThread)
sl@0
    52
PLS(thread_system_already_initialized,gthread_impl,gboolean)
sl@0
    53
PLS_ARRAY(g_thread_priority_map,gthread_impl,gint)
sl@0
    54
sl@0
    55
#define zero_thread (*FUNCTION_NAME(zero_thread,gthread_impl)())
sl@0
    56
#define thread_system_already_initialized (*FUNCTION_NAME(thread_system_already_initialized,gthread_impl)())
sl@0
    57
#define g_thread_priority_map (FUNCTION_NAME(g_thread_priority_map,gthread_impl)())
sl@0
    58
#else
sl@0
    59
static GSystemThread zero_thread; /* This is initialized to all zero */
sl@0
    60
static gboolean thread_system_already_initialized = FALSE;
sl@0
    61
static gint g_thread_priority_map [G_THREAD_PRIORITY_URGENT + 1];
sl@0
    62
sl@0
    63
#endif /* EMULATOR */
sl@0
    64
sl@0
    65
#include G_THREAD_SOURCE
sl@0
    66
sl@0
    67
#ifndef PRIORITY_LOW_VALUE
sl@0
    68
# define PRIORITY_LOW_VALUE 0
sl@0
    69
#endif
sl@0
    70
sl@0
    71
#ifndef PRIORITY_URGENT_VALUE
sl@0
    72
# define PRIORITY_URGENT_VALUE 0
sl@0
    73
#endif
sl@0
    74
sl@0
    75
#ifndef PRIORITY_NORMAL_VALUE
sl@0
    76
# define PRIORITY_NORMAL_VALUE						\
sl@0
    77
  ((PRIORITY_LOW_VALUE * 6 + PRIORITY_URGENT_VALUE * 4) / 10)
sl@0
    78
#endif /* PRIORITY_NORMAL_VALUE */
sl@0
    79
sl@0
    80
#ifndef PRIORITY_HIGH_VALUE
sl@0
    81
# define PRIORITY_HIGH_VALUE						\
sl@0
    82
  ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
sl@0
    83
#endif /* PRIORITY_HIGH_VALUE */
sl@0
    84
sl@0
    85
void g_mutex_init (void);
sl@0
    86
void g_mem_init (void);
sl@0
    87
void g_messages_init (void);
sl@0
    88
void g_convert_init (void);
sl@0
    89
void g_rand_init (void);
sl@0
    90
void g_main_thread_init (void);
sl@0
    91
sl@0
    92
typedef struct _GMutexDebugInfo GMutexDebugInfo;
sl@0
    93
struct _GMutexDebugInfo
sl@0
    94
{
sl@0
    95
  gchar *location;
sl@0
    96
  GSystemThread owner;
sl@0
    97
};
sl@0
    98
sl@0
    99
#define G_MUTEX_DEBUG_INFO(mutex) 					\
sl@0
   100
  (((GMutexDebugInfo*)(((char*)mutex)+G_MUTEX_SIZE)))
sl@0
   101
sl@0
   102
static GMutex *
sl@0
   103
g_mutex_new_errorcheck_impl (void)
sl@0
   104
{
sl@0
   105
  GMutex *retval = g_thread_functions_for_glib_use_default.mutex_new ();
sl@0
   106
  GMutexDebugInfo *info;
sl@0
   107
  retval = g_realloc (retval, G_MUTEX_SIZE + sizeof (GMutexDebugInfo));
sl@0
   108
sl@0
   109
  info = G_MUTEX_DEBUG_INFO (retval);
sl@0
   110
  g_system_thread_assign (info->owner, zero_thread);
sl@0
   111
  info->location = "invalid";
sl@0
   112
sl@0
   113
  return retval;
sl@0
   114
}
sl@0
   115
sl@0
   116
static void
sl@0
   117
g_mutex_lock_errorcheck_impl (GMutex *mutex,
sl@0
   118
			      const gulong magic,
sl@0
   119
			      gchar * const location)
sl@0
   120
{
sl@0
   121
  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
sl@0
   122
  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
sl@0
   123
sl@0
   124
  GSystemThread self;
sl@0
   125
  g_thread_functions_for_glib_use.thread_self (&self);
sl@0
   126
sl@0
   127
  if (g_system_thread_equal (info->owner, self))
sl@0
   128
    g_error ("Trying to recursivly lock a mutex at '%s', "
sl@0
   129
	     "previously locked at '%s'",
sl@0
   130
	     loc, info->location);
sl@0
   131
sl@0
   132
  g_thread_functions_for_glib_use_default.mutex_lock (mutex);
sl@0
   133
sl@0
   134
  g_system_thread_assign (info->owner, self);
sl@0
   135
  info->location = loc;
sl@0
   136
}
sl@0
   137
sl@0
   138
static gboolean
sl@0
   139
g_mutex_trylock_errorcheck_impl (GMutex *mutex,
sl@0
   140
				 const gulong magic,
sl@0
   141
				 gchar * const location)
sl@0
   142
{
sl@0
   143
  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
sl@0
   144
  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
sl@0
   145
sl@0
   146
  GSystemThread self;
sl@0
   147
  g_thread_functions_for_glib_use.thread_self (&self);
sl@0
   148
sl@0
   149
  if (g_system_thread_equal (info->owner, self))
sl@0
   150
    g_error ("Trying to recursivly lock a mutex at '%s', "
sl@0
   151
	     "previously locked at '%s'",
sl@0
   152
	     loc, info->location);
sl@0
   153
sl@0
   154
  if (!g_thread_functions_for_glib_use_default.mutex_trylock (mutex))
sl@0
   155
    return FALSE;
sl@0
   156
sl@0
   157
  g_system_thread_assign (info->owner, self);
sl@0
   158
  info->location = loc;
sl@0
   159
sl@0
   160
  return TRUE;
sl@0
   161
}
sl@0
   162
sl@0
   163
static void
sl@0
   164
g_mutex_unlock_errorcheck_impl (GMutex *mutex,
sl@0
   165
				const gulong magic,
sl@0
   166
				gchar * const location)
sl@0
   167
{
sl@0
   168
  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
sl@0
   169
  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
sl@0
   170
sl@0
   171
  GSystemThread self;
sl@0
   172
  g_thread_functions_for_glib_use.thread_self (&self);
sl@0
   173
sl@0
   174
  if (g_system_thread_equal (info->owner, zero_thread))
sl@0
   175
    g_error ("Trying to unlock an unlocked mutex at '%s'", loc);
sl@0
   176
sl@0
   177
  if (!g_system_thread_equal (info->owner, self))
sl@0
   178
    g_warning ("Trying to unlock a mutex at '%s', "
sl@0
   179
	       "previously locked by a different thread at '%s'",
sl@0
   180
	       loc, info->location);
sl@0
   181
sl@0
   182
  g_system_thread_assign (info->owner, zero_thread);
sl@0
   183
  info->location = NULL;
sl@0
   184
sl@0
   185
  g_thread_functions_for_glib_use_default.mutex_unlock (mutex);
sl@0
   186
}
sl@0
   187
sl@0
   188
static void
sl@0
   189
g_mutex_free_errorcheck_impl (GMutex *mutex,
sl@0
   190
			      const gulong magic,
sl@0
   191
			      gchar * const location)
sl@0
   192
{
sl@0
   193
  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
sl@0
   194
  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
sl@0
   195
sl@0
   196
  if (info && !g_system_thread_equal (info->owner, zero_thread))
sl@0
   197
    g_error ("Trying to free a locked mutex at '%s', "
sl@0
   198
	     "which was previously locked at '%s'",
sl@0
   199
	     loc, info->location);
sl@0
   200
sl@0
   201
  g_thread_functions_for_glib_use_default.mutex_free (mutex);
sl@0
   202
}
sl@0
   203
sl@0
   204
static void
sl@0
   205
g_cond_wait_errorcheck_impl (GCond *cond,
sl@0
   206
			     GMutex *mutex,
sl@0
   207
			     const gulong magic,
sl@0
   208
			     gchar * const location)
sl@0
   209
{
sl@0
   210
  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
sl@0
   211
  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
sl@0
   212
sl@0
   213
  GSystemThread self;
sl@0
   214
  g_thread_functions_for_glib_use.thread_self (&self);
sl@0
   215
sl@0
   216
  if (g_system_thread_equal (info->owner, zero_thread))
sl@0
   217
    g_error ("Trying to use an unlocked mutex in g_cond_wait() at '%s'", loc);
sl@0
   218
sl@0
   219
  if (!g_system_thread_equal (info->owner, self))
sl@0
   220
    g_error ("Trying to use a mutex locked by another thread in "
sl@0
   221
	     "g_cond_wait() at '%s'", loc);
sl@0
   222
sl@0
   223
  g_system_thread_assign (info->owner, zero_thread);
sl@0
   224
  loc = info->location;
sl@0
   225
sl@0
   226
  g_thread_functions_for_glib_use_default.cond_wait (cond, mutex);
sl@0
   227
sl@0
   228
  g_system_thread_assign (info->owner, self);
sl@0
   229
  info->location = loc;
sl@0
   230
}
sl@0
   231
sl@0
   232
sl@0
   233
static gboolean
sl@0
   234
g_cond_timed_wait_errorcheck_impl (GCond *cond,
sl@0
   235
                                   GMutex *mutex,
sl@0
   236
                                   GTimeVal *end_time,
sl@0
   237
				   const gulong magic,
sl@0
   238
				   gchar * const location)
sl@0
   239
{
sl@0
   240
  GMutexDebugInfo *info = G_MUTEX_DEBUG_INFO (mutex);
sl@0
   241
  gchar *loc = (magic == G_MUTEX_DEBUG_MAGIC) ? location : "unknown";
sl@0
   242
  gboolean retval;
sl@0
   243
sl@0
   244
  GSystemThread self;
sl@0
   245
  g_thread_functions_for_glib_use.thread_self (&self);
sl@0
   246
sl@0
   247
  if (g_system_thread_equal (info->owner, zero_thread))
sl@0
   248
    g_error ("Trying to use an unlocked mutex in g_cond_timed_wait() at '%s'",
sl@0
   249
	     loc);
sl@0
   250
sl@0
   251
  if (!g_system_thread_equal (info->owner, self))
sl@0
   252
    g_error ("Trying to use a mutex locked by another thread in "
sl@0
   253
	     "g_cond_timed_wait() at '%s'", loc);
sl@0
   254
sl@0
   255
  g_system_thread_assign (info->owner, zero_thread);
sl@0
   256
  loc = info->location;
sl@0
   257
sl@0
   258
  retval = g_thread_functions_for_glib_use_default.cond_timed_wait (cond,
sl@0
   259
								    mutex,
sl@0
   260
								    end_time);
sl@0
   261
sl@0
   262
  g_system_thread_assign (info->owner, self);
sl@0
   263
  info->location = loc;
sl@0
   264
sl@0
   265
  return retval;
sl@0
   266
}
sl@0
   267
sl@0
   268
sl@0
   269
/* unshadow function declaration. See gthread.h */
sl@0
   270
#undef g_thread_init
sl@0
   271
sl@0
   272
EXPORT_C
sl@0
   273
void
sl@0
   274
g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
sl@0
   275
{
sl@0
   276
  GThreadFunctions errorcheck_functions;
sl@0
   277
  if (init)
sl@0
   278
    g_error ("Errorcheck mutexes can only be used for native "
sl@0
   279
	     "thread implementations. Sorry." );
sl@0
   280
sl@0
   281
#ifdef HAVE_G_THREAD_IMPL_INIT
sl@0
   282
  /* This isn't called in g_thread_init, as it doesn't think to get
sl@0
   283
   * the default implementation, so we have to call it on our own.
sl@0
   284
   *
sl@0
   285
   * We must call this before copying
sl@0
   286
   * g_thread_functions_for_glib_use_default as the
sl@0
   287
   * implementation-specific init function might modify the contents
sl@0
   288
   * of g_thread_functions_for_glib_use_default based on operating
sl@0
   289
   * system version, C library version, or whatever. */
sl@0
   290
  g_thread_impl_init();
sl@0
   291
#endif /* HAVE_G_THREAD_IMPL_INIT */
sl@0
   292
sl@0
   293
  errorcheck_functions = g_thread_functions_for_glib_use_default;
sl@0
   294
  errorcheck_functions.mutex_new = g_mutex_new_errorcheck_impl;
sl@0
   295
  errorcheck_functions.mutex_lock =
sl@0
   296
    (void (*)(GMutex *)) g_mutex_lock_errorcheck_impl;
sl@0
   297
  errorcheck_functions.mutex_trylock =
sl@0
   298
    (gboolean (*)(GMutex *)) g_mutex_trylock_errorcheck_impl;
sl@0
   299
  errorcheck_functions.mutex_unlock =
sl@0
   300
    (void (*)(GMutex *)) g_mutex_unlock_errorcheck_impl;
sl@0
   301
  errorcheck_functions.mutex_free =
sl@0
   302
    (void (*)(GMutex *)) g_mutex_free_errorcheck_impl;
sl@0
   303
  errorcheck_functions.cond_wait =
sl@0
   304
    (void (*)(GCond *, GMutex *)) g_cond_wait_errorcheck_impl;
sl@0
   305
  errorcheck_functions.cond_timed_wait =
sl@0
   306
    (gboolean (*)(GCond *, GMutex *, GTimeVal *))
sl@0
   307
    g_cond_timed_wait_errorcheck_impl;
sl@0
   308
sl@0
   309
  g_thread_init (&errorcheck_functions);
sl@0
   310
}
sl@0
   311
sl@0
   312
EXPORT_C
sl@0
   313
void
sl@0
   314
g_thread_init (GThreadFunctions* init)
sl@0
   315
{
sl@0
   316
  gboolean supported;
sl@0
   317
sl@0
   318
  if (thread_system_already_initialized)
sl@0
   319
    g_error ("GThread system may only be initialized once.");
sl@0
   320
sl@0
   321
  thread_system_already_initialized = TRUE;
sl@0
   322
sl@0
   323
  if (init == NULL)
sl@0
   324
    {
sl@0
   325
#ifdef HAVE_G_THREAD_IMPL_INIT
sl@0
   326
      /* now do any initialization stuff required by the
sl@0
   327
       * implementation, but only if called with a NULL argument, of
sl@0
   328
       * course. Otherwise it's up to the user to do so. */
sl@0
   329
      g_thread_impl_init();
sl@0
   330
#endif /* HAVE_G_THREAD_IMPL_INIT */
sl@0
   331
#ifdef __SYMBIAN32__
sl@0
   332
	  memcpy(&g_thread_functions_for_glib_use_default, &g_thread_functions_for_glib_use_default_temp, sizeof(GThreadFunctions));
sl@0
   333
#endif//__SYMBIAN32__
sl@0
   334
      init = &g_thread_functions_for_glib_use_default;
sl@0
   335
    }
sl@0
   336
  else
sl@0
   337
    g_thread_use_default_impl = FALSE;
sl@0
   338
sl@0
   339
  g_thread_functions_for_glib_use = *init;
sl@0
   340
  if (g_thread_gettime_impl)
sl@0
   341
    g_thread_gettime = g_thread_gettime_impl;
sl@0
   342
sl@0
   343
  supported = (init->mutex_new &&
sl@0
   344
	       init->mutex_lock &&
sl@0
   345
	       init->mutex_trylock &&
sl@0
   346
	       init->mutex_unlock &&
sl@0
   347
	       init->mutex_free &&
sl@0
   348
	       init->cond_new &&
sl@0
   349
	       init->cond_signal &&
sl@0
   350
	       init->cond_broadcast &&
sl@0
   351
	       init->cond_wait &&
sl@0
   352
	       init->cond_timed_wait &&
sl@0
   353
	       init->cond_free &&
sl@0
   354
	       init->private_new &&
sl@0
   355
	       init->private_get &&
sl@0
   356
	       init->private_set &&
sl@0
   357
	       init->thread_create &&
sl@0
   358
	       init->thread_yield &&
sl@0
   359
	       init->thread_join &&
sl@0
   360
	       init->thread_exit &&
sl@0
   361
	       init->thread_set_priority &&
sl@0
   362
	       init->thread_self);
sl@0
   363
sl@0
   364
  /* if somebody is calling g_thread_init (), it means that he wants to
sl@0
   365
   * have thread support, so check this
sl@0
   366
   */
sl@0
   367
  if (!supported)
sl@0
   368
    {
sl@0
   369
      if (g_thread_use_default_impl)
sl@0
   370
	g_error ("Threads are not supported on this platform.");
sl@0
   371
      else
sl@0
   372
	g_error ("The supplied thread function vector is invalid.");
sl@0
   373
    }
sl@0
   374
sl@0
   375
  g_thread_priority_map [G_THREAD_PRIORITY_LOW] = PRIORITY_LOW_VALUE;
sl@0
   376
  g_thread_priority_map [G_THREAD_PRIORITY_NORMAL] = PRIORITY_NORMAL_VALUE;
sl@0
   377
  g_thread_priority_map [G_THREAD_PRIORITY_HIGH] = PRIORITY_HIGH_VALUE;
sl@0
   378
  g_thread_priority_map [G_THREAD_PRIORITY_URGENT] = PRIORITY_URGENT_VALUE;
sl@0
   379
sl@0
   380
  g_thread_init_glib ();
sl@0
   381
}
sl@0
   382
sl@0
   383
#else /* !G_THREADS_ENABLED */
sl@0
   384
sl@0
   385
void
sl@0
   386
g_thread_init (GThreadFunctions* init)
sl@0
   387
{
sl@0
   388
  g_error ("GLib thread support is disabled.");
sl@0
   389
}
sl@0
   390
sl@0
   391
void
sl@0
   392
g_thread_init_with_errorcheck_mutexes (GThreadFunctions* init)
sl@0
   393
{
sl@0
   394
  g_error ("GLib thread support is disabled.");
sl@0
   395
}
sl@0
   396
sl@0
   397
#endif /* !G_THREADS_ENABLED */