Add PPTP runtime and GUI
[tomato.git] / release / src / router / glib / gmutex.c
bloba6e7934f5e2569691ee266731a5a6c8e150fdd59
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.
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/.
31 /*
32 * MT safe
35 #include "glib.h"
37 typedef struct _GStaticPrivateNode GStaticPrivateNode;
39 struct _GStaticPrivateNode
41 gpointer data;
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 */
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 };
70 /* Local data */
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.
79 void
80 g_mutex_init (void)
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,
93 private_old);
95 g_mutex_protect_static_mutex_allocation = g_mutex_new();
96 g_thread_specific_mutex = g_mutex_new();
99 GMutex *
100 g_static_mutex_get_mutex_impl (GMutex** mutex)
102 if (!g_thread_supported ())
103 return NULL;
105 g_assert (g_mutex_protect_static_mutex_allocation);
107 g_mutex_lock (g_mutex_protect_static_mutex_allocation);
109 if (!(*mutex))
110 *mutex = g_mutex_new();
112 g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
114 return *mutex;
117 gpointer
118 g_static_private_get (GStaticPrivate *private_key)
120 GArray *array;
122 array = g_private_get (g_thread_specific_private);
123 if (!array)
124 return NULL;
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;
134 void
135 g_static_private_set (GStaticPrivate *private_key,
136 gpointer data,
137 GDestroyNotify notify)
139 GArray *array;
140 static guint next_index = 0;
141 GStaticPrivateNode *node;
143 array = g_private_get (g_thread_specific_private);
144 if (!array)
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);
164 if (node->destroy)
166 gpointer ddata = node->data;
167 GDestroyNotify ddestroy = node->destroy;
169 node->data = data;
170 node->destroy = notify;
172 ddestroy (ddata);
174 else
176 node->data = data;
177 node->destroy = notify;
181 static void
182 g_static_private_free_data (gpointer data)
184 if (data)
186 GArray* array = data;
187 guint i;
189 for (i = 0; i < array->len; i++ )
191 GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i);
192 if (node->destroy)
193 node->destroy (node->data);
198 static void
199 g_thread_fail (void)
201 g_error ("The thread system is not yet initialized.");