applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[platform/upstream/glib.git] / gthread / gthread-impl.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gthread.c: thread related functions
5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser 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.
11  *
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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser 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.
21  */
22
23 /*
24  * Modified by the GLib Team and others 1997-2000.  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/. 
28  */
29
30 /* 
31  * MT safe
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <glib.h>
39
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;
44
45 #include G_THREAD_SOURCE
46
47 void g_mutex_init (void);
48 void g_mem_init (void);
49 void g_messages_init (void);
50
51 void
52 g_thread_init (GThreadFunctions* init)
53 {
54   gboolean supported;
55
56 #ifndef G_THREADS_ENABLED
57   g_error ("GLib thread support is disabled.");
58 #endif  /* !G_THREADS_ENABLED */
59
60   if (thread_system_already_initialized)
61     g_error ("GThread system may only be initialized once.");
62     
63   thread_system_already_initialized = TRUE;
64
65   if (init == NULL)
66     init = &g_thread_functions_for_glib_use_default;
67   else
68     g_thread_use_default_impl = FALSE;
69
70 #if defined (G_OS_WIN32) && defined (__GNUC__)
71   memcpy(&g_thread_functions_for_glib_use, init, sizeof (*init));
72 #else
73   g_thread_functions_for_glib_use = *init;
74 #endif
75   /* It is important, that g_threads_got_initialized is not set before the
76    * thread initialization functions of the different modules are called
77    */
78
79   supported = (init->mutex_new &&  
80                init->mutex_lock && 
81                init->mutex_trylock && 
82                init->mutex_unlock && 
83                init->mutex_free && 
84                init->cond_new && 
85                init->cond_signal && 
86                init->cond_broadcast && 
87                init->cond_wait && 
88                init->cond_timed_wait &&
89                init->cond_free &&
90                init->private_new &&
91                init->private_get &&
92                init->private_get);
93
94   /* if somebody is calling g_thread_init (), it means that he wants to
95    * have thread support, so check this
96    */
97   if (!supported)
98     {
99       if (g_thread_use_default_impl)
100         g_error ("Threads are not supported on this platform.");
101       else
102         g_error ("The supplied thread function vector is invalid.");
103     }
104
105   /* now do any initialization stuff required by the implementation,
106    * but only if called with a NULL argument, of course. Otherwise
107    * it's up to the user to do so. */
108
109 #ifdef HAVE_G_THREAD_IMPL_INIT
110   if (g_thread_use_default_impl)
111     g_thread_impl_init();
112 #endif
113
114   /* now call the thread initialization functions of the different
115    * glib modules. order does matter, g_mutex_init MUST come first.
116    */
117   g_mutex_init ();
118   g_mem_init ();
119   g_messages_init ();
120
121   /* now we can set g_threads_got_initialized and thus enable
122    * all the thread functions
123    */
124   g_threads_got_initialized = TRUE;
125
126   /* we want the main thread to run with normal priority */
127   g_thread_set_priority (g_thread_self(), G_THREAD_PRIORITY_NORMAL);
128 }
129
130 static gint 
131 g_thread_map_priority (GThreadPriority priority)
132 {
133   guint procent;
134   switch (priority)
135     {
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;
140     }
141   return g_thread_min_priority + 
142     (g_thread_max_priority - g_thread_min_priority) * procent / 100;
143 }