glib/gthread/gthread-impl.c
Sebastian Wilhelmi 90f6cc9bf2 Completed the thread support in GLib. Thread creation, prioritizing
1999-06-17  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>

	* configure.in, acglib.m4, acconfig.h, glib.h, gthread.c:
	Completed the thread support in GLib. Thread creation,
	prioritizing threads, yielding, joining threads as well as
	reader/writer locks and recursive mutexes are now in place. Please
	test heavily on your platform. It is so far tested on
	Linux/i386/pthreads, Solaris/Sparc/pthreads and
	Solaris/Sparc/solaristhreads.

	* gtimer.c, glib.h: Implement g_usleep (gulong microseconds) for
	thread safe sleeping. (sleep() is not MT-safe at all!)

	* gutils.c: Avoid compiler warning.

	* tests/Makefile.am, tests/thread-test.c: New program to test some
	aspects of the thread implementation.

	* gthread.c, Makefile.am: Renamed from gmutex.c to reflect the
	change of content.

	* configure.in: Purged all appearances of nspr.

	* gthread/gthread-posix.c, gthread-solaris.c: Added the native
	implementations for the GLib's extended thread support.

	* gthread/gthread-nspr.c: Removed for good. NSPR is nothing we
	would want to build upon.

	* gthread/gthread.c: Renamed to gthread-impl.c to avoid
	confusion with ../gthread.c (Formerly known as the file called
	gmutex.c)

	* gthread/testgthread.c: Removed. The new and much extended
        tests are in ../tests/thread-test.c.

	* gthread/Makefile.am: Changed to reflect the changes above.
1999-06-17 15:39:31 +00:00

144 lines
4.3 KiB
C

/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* gthread.c: thread related functions
* Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-1999. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
/*
* MT safe
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib.h>
static gboolean thread_system_already_initialized = FALSE;
static gint g_thread_map_priority (GThreadPriority priority);
static gint g_thread_min_priority = 0;
static gint g_thread_max_priority = 0;
#include G_THREAD_SOURCE
void g_mutex_init (void);
void g_mem_init (void);
void g_messages_init (void);
void
g_thread_init (GThreadFunctions* init)
{
gboolean supported;
#ifndef G_THREADS_ENABLED
g_error ("GLib thread support is disabled.");
#endif /* !G_THREADS_ENABLED */
if (thread_system_already_initialized)
g_error ("GThread system may only be initialized once.");
thread_system_already_initialized = TRUE;
if (init == NULL)
init = &g_thread_functions_for_glib_use_default;
else
g_thread_use_default_impl = FALSE;
#if defined (WIN32) && defined (__GNUC__)
memcpy(&g_thread_functions_for_glib_use, init, sizeof (*init));
#else
g_thread_functions_for_glib_use = *init;
#endif
/* It is important, that g_threads_got_initialized is not set before the
* thread initialization functions of the different modules are called
*/
supported = (init->mutex_new &&
init->mutex_lock &&
init->mutex_trylock &&
init->mutex_unlock &&
init->mutex_free &&
init->cond_new &&
init->cond_signal &&
init->cond_broadcast &&
init->cond_wait &&
init->cond_timed_wait &&
init->cond_free &&
init->private_new &&
init->private_get &&
init->private_get);
/* if somebody is calling g_thread_init (), it means that he wants to
* have thread support, so check this
*/
if (!supported)
{
if (g_thread_use_default_impl)
g_error ("Threads are not supported on this platform.");
else
g_error ("The supplied thread function vector is invalid.");
}
/* now do any initialization stuff required by the implementation,
but only if called with a NULL argument, of course. Otherwise it's
up to the user to do do. */
#ifdef HAVE_G_THREAD_IMPL_INIT
if (g_thread_use_default_impl)
g_thread_impl_init();
#endif
/* now call the thread initialization functions of the different
* glib modules. order does matter, g_mutex_init MUST come first.
*/
g_mutex_init ();
g_mem_init ();
g_messages_init ();
/* now we can set g_threads_got_initialized and thus enable
* all the thread functions
*/
g_threads_got_initialized = TRUE;
/* we want the main thread to run with normal priority */
g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);
}
static gint
g_thread_map_priority (GThreadPriority priority)
{
guint procent;
switch (priority)
{
case G_THREAD_PRIORITY_LOW: procent = 0; break;
default: case G_THREAD_PRIORITY_NORMAL: procent = 40; break;
case G_THREAD_PRIORITY_HIGH: procent = 80; break;
case G_THREAD_PRIORITY_URGENT: procent = 100; break;
}
return g_thread_min_priority +
(g_thread_max_priority - g_thread_min_priority) * procent / 100;
}