glib/gthread/gthread-solaris.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

247 lines
6.4 KiB
C

/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* gthread.c: solaris thread system implementation
* 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
*/
#include <thread.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#define solaris_print_error( name, num ) \
g_error( "file %s: line %d (%s): error %s during %s", \
__FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
g_strerror((num)), #name )
#define solaris_check_for_error( what ) G_STMT_START{ \
int error = (what); \
if( error ) { solaris_print_error( what, error ); } \
}G_STMT_END
#define HAVE_G_THREAD_IMPL_INIT
static void
g_thread_impl_init()
{
g_thread_min_priority = 0;
g_thread_max_priority = 127;
}
static GMutex *
g_mutex_new_solaris_impl (void)
{
GMutex *result = (GMutex *) g_new (mutex_t, 1);
solaris_check_for_error (mutex_init ((mutex_t *) result, USYNC_PROCESS, 0));
return result;
}
static void
g_mutex_free_solaris_impl (GMutex * mutex)
{
solaris_check_for_error (mutex_destroy ((mutex_t *) mutex));
free (mutex);
}
/* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
functions from gmem.c and gmessages.c; */
/* mutex_lock, mutex_unlock can be taken directly, as
signature and semantic are right, but without error check then!!!!,
we might want to change this therefore. */
static gboolean
g_mutex_trylock_solaris_impl (GMutex * mutex)
{
int result;
result = mutex_trylock ((mutex_t *) mutex);
if (result == EBUSY)
return FALSE;
solaris_check_for_error (result);
return TRUE;
}
static GCond *
g_cond_new_solaris_impl ()
{
GCond *result = (GCond *) g_new (cond_t, 1);
solaris_check_for_error (cond_init ((cond_t *) result, USYNC_THREAD, 0));
return result;
}
/* cond_signal, cond_broadcast and cond_wait
can be taken directly, as signature and semantic are right, but
without error check then!!!!, we might want to change this
therfore. */
#define G_MICROSEC 1000000
#define G_NANOSEC 1000000000
static gboolean
g_cond_timed_wait_solaris_impl (GCond * cond,
GMutex * entered_mutex,
GTimeVal * abs_time)
{
int result;
timestruc_t end_time;
gboolean timed_out;
g_return_val_if_fail (cond != NULL, FALSE);
g_return_val_if_fail (entered_mutex != NULL, FALSE);
if (!abs_time)
{
g_cond_wait (cond, entered_mutex);
return TRUE;
}
end_time.tv_sec = abs_time->tv_sec;
end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
g_assert (end_time.tv_nsec < G_NANOSEC);
result = cond_timedwait ((cond_t *) cond, (mutex_t *) entered_mutex,
&end_time);
timed_out = (result == ETIME);
if (!timed_out)
solaris_check_for_error (result);
return !timed_out;
}
static void
g_cond_free_solaris_impl (GCond * cond)
{
solaris_check_for_error (cond_destroy ((cond_t *) cond));
g_free (cond);
}
static GPrivate *
g_private_new_solaris_impl (GDestroyNotify destructor)
{
GPrivate *result = (GPrivate *) g_new (thread_key_t,1);
solaris_check_for_error (thr_keycreate ((thread_key_t *) result,
destructor));
return result;
}
/* NOTE: the functions g_private_get and g_private_set may not use
functions from gmem.c and gmessages.c */
static void
g_private_set_solaris_impl (GPrivate * private_key, gpointer value)
{
if (!private_key)
return;
thr_setspecific (*(thread_key_t *) private_key, value);
}
static gpointer
g_private_get_solaris_impl (GPrivate * private_key)
{
gpointer result;
if (!private_key)
return NULL;
thr_getspecific (*(thread_key_t *) private_key, &result);
return result;
}
void
g_thread_set_priority_solaris_impl (gpointer thread, GThreadPriority priority)
{
solaris_check_for_error (thr_setprio (GPOINTER_TO_INT (thread),
g_thread_map_priority (priority)));
}
gpointer
g_thread_create_solaris_impl (GThreadFunc thread_func,
gpointer arg,
gulong stack_size,
gboolean joinable,
gboolean bound,
GThreadPriority priority)
{
thread_t thread;
long flags = (bound ? THR_BOUND : 0) | (joinable ? 0: THR_DETACHED);
g_return_val_if_fail (thread_func, NULL);
solaris_check_for_error (thr_create (NULL, stack_size,
(void* (*)(void*))thread_func,
arg, flags, &thread));
g_thread_set_priority_solaris_impl (GINT_TO_POINTER (thread), priority);
return GINT_TO_POINTER (thread);
}
void
g_thread_yield_solaris_impl (void)
{
thr_yield ();
}
void
g_thread_join_solaris_impl (gpointer thread)
{
gpointer ignore;
solaris_check_for_error (thr_join (GPOINTER_TO_INT (thread), NULL, &ignore));
}
void
g_thread_exit_solaris_impl (void)
{
thr_exit (NULL);
}
static GThreadFunctions g_thread_functions_for_glib_use_default =
{
g_mutex_new_solaris_impl,
(void (*)(GMutex *)) mutex_lock,
g_mutex_trylock_solaris_impl,
(void (*)(GMutex *)) mutex_unlock,
g_mutex_free_solaris_impl,
g_cond_new_solaris_impl,
(void (*)(GCond *)) cond_signal,
(void (*)(GCond *)) cond_broadcast,
(void (*)(GCond *, GMutex *)) cond_wait,
g_cond_timed_wait_solaris_impl,
g_cond_free_solaris_impl,
g_private_new_solaris_impl,
g_private_get_solaris_impl,
g_private_set_solaris_impl,
g_thread_create_solaris_impl,
g_thread_yield_solaris_impl,
g_thread_join_solaris_impl,
g_thread_exit_solaris_impl,
g_thread_set_priority_solaris_impl,
(gpointer (*)())thr_self
};