Copy all elements of the allocations[] array, including the last. (Pointed
[glib.git] / gmutex.c
blobdfcad9d2f6a87d369af7c4dcd8ea705579c107a2
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
6 * Owen Taylor
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.
24 /*
25 * MT safe
28 #include "glib.h"
30 typedef struct _GStaticPrivateNode GStaticPrivateNode;
32 struct _GStaticPrivateNode {
33 gpointer data;
34 GDestroyNotify destroy;
37 static void g_static_private_free_data (gpointer data);
38 static void g_thread_fail (void);
40 /* Global variables */
42 gboolean g_thread_use_default_impl = TRUE;
43 gboolean g_threads_got_initialized = FALSE;
45 GThreadFunctions g_thread_functions_for_glib_use = {
46 (GMutex*(*)())g_thread_fail, /* mutex_new */
47 NULL, /* mutex_lock */
48 NULL, /* mutex_trylock */
49 NULL, /* mutex_unlock */
50 NULL, /* mutex_free */
51 (GCond*(*)())g_thread_fail, /* cond_new */
52 NULL, /* cond_signal */
53 NULL, /* cond_broadcast */
54 NULL, /* cond_wait */
55 NULL, /* cond_timed_wait */
56 NULL, /* cond_free */
57 (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
58 NULL, /* private_get */
59 NULL, /* private_set */
60 };
62 /* Local data */
64 static GMutex *g_mutex_protect_static_mutex_allocation = NULL;
65 static GMutex *g_thread_specific_mutex = NULL;
66 static GPrivate *g_thread_specific_private = NULL;
68 /* This must be called only once, before any threads are created.
69 * It will only be called from g_thread_init() in -lgthread.
71 void
72 g_mutex_init (void)
74 /* We let the main thread (the one that calls g_thread_init) inherit
75 the data, that it set before calling g_thread_init */
76 gpointer private_old = g_thread_specific_private;
77 g_thread_specific_private = g_private_new (g_static_private_free_data);
79 /* we can not use g_private_set here, as g_threads_got_initialized is not
80 yet set TRUE, whereas the private_set function is already set. */
81 g_thread_functions_for_glib_use.private_set (g_thread_specific_private,
82 private_old);
84 g_mutex_protect_static_mutex_allocation = g_mutex_new();
85 g_thread_specific_mutex = g_mutex_new();
88 GMutex *
89 g_static_mutex_get_mutex_impl (GMutex** mutex)
91 if (!g_thread_supported ())
92 return NULL;
94 g_assert (g_mutex_protect_static_mutex_allocation);
96 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
98 if (!(*mutex))
99 *mutex = g_mutex_new();
101 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
103 return *mutex;
106 gpointer
107 g_static_private_get (GStaticPrivate *private_key)
109 GArray *array;
111 array = g_private_get (g_thread_specific_private);
112 if (!array)
113 return NULL;
115 if (!private_key->index)
116 return NULL;
117 else if (private_key->index <= array->len)
118 return g_array_index (array, GStaticPrivateNode, (private_key->index - 1)).data;
119 else
120 return NULL;
123 void
124 g_static_private_set (GStaticPrivate *private_key,
125 gpointer data,
126 GDestroyNotify notify)
128 GArray *array;
129 static guint next_index = 0;
131 array = g_private_get (g_thread_specific_private);
132 if (!array)
134 array = g_array_new (FALSE, FALSE, sizeof(GStaticPrivateNode));
135 g_private_set (g_thread_specific_private, array);
138 if (!private_key->index)
140 g_mutex_lock (g_thread_specific_mutex);
142 if (!private_key->index)
143 private_key->index = ++next_index;
145 g_mutex_unlock (g_thread_specific_mutex);
148 if (private_key->index > array->len)
149 g_array_set_size (array, private_key->index);
151 g_array_index (array, GStaticPrivateNode, (private_key->index - 1)).data = data;
152 g_array_index (array, GStaticPrivateNode, (private_key->index - 1)).destroy = notify;
155 static void
156 g_static_private_free_data (gpointer data)
158 if (data)
160 GArray* array = data;
161 guint i;
163 for (i = 0; i < array->len; i++ )
165 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
166 if (node->data && node->destroy)
167 node->destroy (node->data);
172 static void
173 g_thread_fail (void)
175 g_error ("The thread system is not yet initialized.");