version bump to 1.1.8, binary age 0, interface age 0.
[platform/upstream/glib.git] / gthread / gthread.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 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.
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  * Library General Public License for more details.
16  *
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.
21  */
22
23 /* 
24  * MT safe
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <glib.h>
32
33 static const char *g_log_domain_gthread = "GThread";
34 static gboolean thread_system_already_initialized = FALSE;
35
36 #include G_THREAD_SOURCE
37
38 void g_mutex_init (void);
39 void g_mem_init (void);
40 void g_messages_init (void);
41
42 void
43 g_thread_init (GThreadFunctions* init)
44 {
45   gboolean supported;
46
47 #ifndef G_THREADS_ENABLED
48   g_error ("GLib thread support is disabled.");
49 #endif  /* !G_THREADS_ENABLED */
50
51   if (thread_system_already_initialized)
52     g_error ("GThread system may only be initialized once.");
53     
54   thread_system_already_initialized = TRUE;
55
56   if (init == NULL)
57     init = &g_thread_functions_for_glib_use_default;
58   else
59     g_thread_use_default_impl = FALSE;
60
61   g_thread_functions_for_glib_use = *init;
62
63   /* It is important, that g_threads_got_initialized is not set before the
64    * thread initialization functions of the different modules are called
65    */
66
67   supported = (init->mutex_new &&  
68                init->mutex_lock && 
69                init->mutex_trylock && 
70                init->mutex_unlock && 
71                init->mutex_free && 
72                init->cond_new && 
73                init->cond_signal && 
74                init->cond_broadcast && 
75                init->cond_wait && 
76                init->cond_timed_wait &&
77                init->cond_free &&
78                init->private_new &&
79                init->private_get &&
80                init->private_get);
81
82   /* if somebody is calling g_thread_init (), it means that he wants to
83    * have thread support, so check this
84    */
85   if (!supported)
86     {
87       if (g_thread_use_default_impl)
88         g_error ("Threads are not supported on this platform.");
89       else
90         g_error ("The supplied thread function vector is invalid.");
91     }
92
93   /* now call the thread initialization functions of the different
94    * glib modules. order does matter, g_mutex_init MUST come first.
95    */
96   g_mutex_init ();
97   g_mem_init ();
98   g_messages_init ();
99
100   /* now we can set g_threads_got_initialized and thus enable
101    * all the thread functions
102    */
103   g_threads_got_initialized = TRUE;
104 }