1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmutex.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
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.
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.
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.
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/.
37 typedef struct _GStaticPrivateNode GStaticPrivateNode
;
39 struct _GStaticPrivateNode
42 GDestroyNotify destroy
;
45 static void g_static_private_free_data (gpointer data
);
46 static void g_thread_fail (void);
48 /* Global variables */
50 gboolean g_thread_use_default_impl
= TRUE
;
51 gboolean g_threads_got_initialized
= FALSE
;
53 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 */
63 NULL
, /* cond_timed_wait */
65 (GPrivate
*(*)(GDestroyNotify
))g_thread_fail
, /* private_new */
66 NULL
, /* private_get */
67 NULL
, /* private_set */
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
;
76 /* This must be called only once, before any threads are created.
77 * It will only be called from g_thread_init() in -lgthread.
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
85 gpointer private_old
= g_thread_specific_private
;
87 g_thread_specific_private
= g_private_new (g_static_private_free_data
);
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.
92 g_thread_functions_for_glib_use
.private_set (g_thread_specific_private
,
95 g_mutex_protect_static_mutex_allocation
= g_mutex_new();
96 g_thread_specific_mutex
= g_mutex_new();
100 g_static_mutex_get_mutex_impl (GMutex
** mutex
)
102 if (!g_thread_supported ())
105 g_assert (g_mutex_protect_static_mutex_allocation
);
107 g_mutex_lock (g_mutex_protect_static_mutex_allocation
);
110 *mutex
= g_mutex_new();
112 g_mutex_unlock (g_mutex_protect_static_mutex_allocation
);
118 g_static_private_get (GStaticPrivate
*private_key
)
122 array
= g_private_get (g_thread_specific_private
);
126 if (!private_key
->index
)
128 else if (private_key
->index
<= array
->len
)
129 return g_array_index (array
, GStaticPrivateNode
, private_key
->index
- 1).data
;
135 g_static_private_set (GStaticPrivate
*private_key
,
137 GDestroyNotify notify
)
140 static guint next_index
= 0;
141 GStaticPrivateNode
*node
;
143 array
= g_private_get (g_thread_specific_private
);
146 array
= g_array_new (FALSE
, TRUE
, sizeof (GStaticPrivateNode
));
147 g_private_set (g_thread_specific_private
, array
);
150 if (!private_key
->index
)
152 g_mutex_lock (g_thread_specific_mutex
);
154 if (!private_key
->index
)
155 private_key
->index
= ++next_index
;
157 g_mutex_unlock (g_thread_specific_mutex
);
160 if (private_key
->index
> array
->len
)
161 g_array_set_size (array
, private_key
->index
);
163 node
= &g_array_index (array
, GStaticPrivateNode
, private_key
->index
- 1);
166 gpointer ddata
= node
->data
;
167 GDestroyNotify ddestroy
= node
->destroy
;
170 node
->destroy
= notify
;
177 node
->destroy
= notify
;
182 g_static_private_free_data (gpointer data
)
186 GArray
* array
= data
;
189 for (i
= 0; i
< array
->len
; i
++ )
191 GStaticPrivateNode
*node
= &g_array_index (array
, GStaticPrivateNode
, i
);
193 node
->destroy (node
->data
);
201 g_error ("The thread system is not yet initialized.");