1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: thread related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
40 static gboolean thread_system_already_initialized = FALSE;
41 static gint g_thread_map_priority (GThreadPriority priority);
42 static gint g_thread_min_priority = 0;
43 static gint g_thread_max_priority = 0;
45 #include G_THREAD_SOURCE
47 void g_mutex_init (void);
48 void g_mem_init (void);
49 void g_messages_init (void);
52 g_thread_init (GThreadFunctions* init)
56 #ifndef G_THREADS_ENABLED
57 g_error ("GLib thread support is disabled.");
58 #endif /* !G_THREADS_ENABLED */
60 if (thread_system_already_initialized)
61 g_error ("GThread system may only be initialized once.");
63 thread_system_already_initialized = TRUE;
66 init = &g_thread_functions_for_glib_use_default;
68 g_thread_use_default_impl = FALSE;
70 #if defined (G_OS_WIN32) && defined (__GNUC__)
71 memcpy(&g_thread_functions_for_glib_use, init, sizeof (*init));
73 g_thread_functions_for_glib_use = *init;
75 /* It is important, that g_threads_got_initialized is not set before the
76 * thread initialization functions of the different modules are called
79 supported = (init->mutex_new &&
81 init->mutex_trylock &&
86 init->cond_broadcast &&
88 init->cond_timed_wait &&
94 /* if somebody is calling g_thread_init (), it means that he wants to
95 * have thread support, so check this
99 if (g_thread_use_default_impl)
100 g_error ("Threads are not supported on this platform.");
102 g_error ("The supplied thread function vector is invalid.");
105 /* now do any initialization stuff required by the implementation,
106 but only if called with a NULL argument, of course. Otherwise it's
107 up to the user to do do. */
109 #ifdef HAVE_G_THREAD_IMPL_INIT
110 if (g_thread_use_default_impl)
111 g_thread_impl_init();
114 /* now call the thread initialization functions of the different
115 * glib modules. order does matter, g_mutex_init MUST come first.
121 /* now we can set g_threads_got_initialized and thus enable
122 * all the thread functions
124 g_threads_got_initialized = TRUE;
126 /* we want the main thread to run with normal priority */
127 g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);
131 g_thread_map_priority (GThreadPriority priority)
136 case G_THREAD_PRIORITY_LOW: procent = 0; break;
137 default: case G_THREAD_PRIORITY_NORMAL: procent = 40; break;
138 case G_THREAD_PRIORITY_HIGH: procent = 80; break;
139 case G_THREAD_PRIORITY_URGENT: procent = 100; break;
141 return g_thread_min_priority +
142 (g_thread_max_priority - g_thread_min_priority) * procent / 100;