Correct rules for making the win32-related files that are made from
[platform/upstream/glib.git] / gmutex.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gmutex.c: MT safety related functions
5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6  *                Owen Taylor
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
26  * file for a list of people on the GLib Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
29  */
30
31 /* 
32  * MT safe
33  */
34
35 #include "glib.h"
36
37 typedef struct _GStaticPrivateNode GStaticPrivateNode;
38
39 struct _GStaticPrivateNode
40 {
41   gpointer       data;
42   GDestroyNotify destroy;
43 };
44
45 static void g_static_private_free_data (gpointer data);
46 static void g_thread_fail (void);
47
48 /* Global variables */
49
50 gboolean g_thread_use_default_impl = TRUE;
51 gboolean g_threads_got_initialized = FALSE;
52
53 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use = {
54   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
55   NULL,                                        /* mutex_lock */
56   NULL,                                        /* mutex_trylock */
57   NULL,                                        /* mutex_unlock */
58   NULL,                                        /* mutex_free */
59   (GCond*(*)())g_thread_fail,                  /* cond_new */
60   NULL,                                        /* cond_signal */
61   NULL,                                        /* cond_broadcast */
62   NULL,                                        /* cond_wait */
63   NULL,                                        /* cond_timed_wait  */
64   NULL,                                        /* cond_free */
65   (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
66   NULL,                                        /* private_get */
67   NULL,                                        /* private_set */
68 }; 
69
70 /* Local data */
71
72 static GMutex   *g_mutex_protect_static_mutex_allocation = NULL;
73 static GMutex   *g_thread_specific_mutex = NULL;
74 static GPrivate *g_thread_specific_private = NULL;
75
76 /* This must be called only once, before any threads are created.
77  * It will only be called from g_thread_init() in -lgthread.
78  */
79 void
80 g_mutex_init (void)
81 {
82   /* We let the main thread (the one that calls g_thread_init) inherit
83    * the data, that it set before calling g_thread_init
84    */
85   gpointer private_old = g_thread_specific_private;
86
87   g_thread_specific_private = g_private_new (g_static_private_free_data);
88
89   /* we can not use g_private_set here, as g_threads_got_initialized is not
90    * yet set TRUE, whereas the private_set function is already set.
91    */
92   g_thread_functions_for_glib_use.private_set (g_thread_specific_private, 
93                                                private_old);
94
95   g_mutex_protect_static_mutex_allocation = g_mutex_new();
96   g_thread_specific_mutex = g_mutex_new();
97 }
98
99 GMutex *
100 g_static_mutex_get_mutex_impl (GMutex** mutex)
101 {
102   if (!g_thread_supported ())
103     return NULL;
104
105   g_assert (g_mutex_protect_static_mutex_allocation);
106
107   g_mutex_lock (g_mutex_protect_static_mutex_allocation);
108
109   if (!(*mutex)) 
110     *mutex = g_mutex_new(); 
111
112   g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
113   
114   return *mutex;
115 }
116
117 gpointer
118 g_static_private_get (GStaticPrivate *private_key)
119 {
120   GArray *array;
121
122   array = g_private_get (g_thread_specific_private);
123   if (!array)
124     return NULL;
125
126   if (!private_key->index)
127     return NULL;
128   else if (private_key->index <= array->len)
129     return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
130   else
131     return NULL;
132 }
133
134 void
135 g_static_private_set (GStaticPrivate *private_key, 
136                       gpointer        data,
137                       GDestroyNotify  notify)
138 {
139   GArray *array;
140   static guint next_index = 0;
141   GStaticPrivateNode *node;
142   
143   array = g_private_get (g_thread_specific_private);
144   if (!array)
145     {
146       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
147       g_private_set (g_thread_specific_private, array);
148     }
149
150   if (!private_key->index)
151     {
152       g_mutex_lock (g_thread_specific_mutex);
153
154       if (!private_key->index)
155         private_key->index = ++next_index;
156
157       g_mutex_unlock (g_thread_specific_mutex);
158     }
159
160   if (private_key->index > array->len)
161     g_array_set_size (array, private_key->index);
162
163   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
164   if (node->destroy)
165     {
166       gpointer ddata = node->data;
167       GDestroyNotify ddestroy = node->destroy;
168
169       node->data = data;
170       node->destroy = notify;
171
172       ddestroy (ddata);
173     }
174   else
175     {
176       node->data = data;
177       node->destroy = notify;
178     }
179 }
180
181 static void
182 g_static_private_free_data (gpointer data)
183 {
184   if (data)
185     {
186       GArray* array = data;
187       guint i;
188       
189       for (i = 0; i < array->len; i++ )
190         {
191           GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
192           if (node->destroy)
193             node->destroy (node->data);
194         }
195     }
196 }
197
198 static void
199 g_thread_fail (void)
200 {
201   g_error ("The thread system is not yet initialized.");
202 }