f644f7044267cae851834a02caa62492f374cbcd
[platform/upstream/glib.git] / 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  * 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 Lesser 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  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser 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-2000.  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 "config.h"
36 #include "glib.h"
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #if GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P
43 # define g_system_thread_equal(thread1, thread2)                        \
44    (thread1.dummy_pointer == thread2.dummy_pointer)
45 # define g_system_thread_assign(dest, src)                              \
46    (dest.dummy_pointer = src.dummy_pointer)
47 #else /* GLIB_SIZEOF_SYSTEM_THREAD != SIZEOF_VOID_P */
48 # define g_system_thread_equal(thread1, thread2)                        \
49    (memcmp (&thread1, &thread2, GLIB_SIZEOF_SYSTEM_THREAD) == 0)
50 # define g_system_thread_assign(dest, src)                              \
51    (memcpy (&dest, &src, GLIB_SIZEOF_SYSTEM_THREAD))
52 #endif /* GLIB_SIZEOF_SYSTEM_THREAD == SIZEOF_VOID_P */
53
54 typedef struct _GRealThread GRealThread;
55 struct  _GRealThread
56 {
57   GThread thread;
58   GThreadFunc func;
59   gpointer arg;
60   gpointer private_data;
61   GSystemThread system_thread;
62 };
63
64 typedef struct _GStaticPrivateNode GStaticPrivateNode;
65 struct _GStaticPrivateNode
66 {
67   gpointer       data;
68   GDestroyNotify destroy;
69 };
70
71 static void g_thread_cleanup (gpointer data);
72 static void g_thread_fail (void);
73
74 /* Global variables */
75
76 static GSystemThread zero_thread; /* This is initialized to all zero */
77 gboolean g_thread_use_default_impl = TRUE;
78 gboolean g_threads_got_initialized = FALSE;
79
80 #if defined(G_OS_WIN32) && defined(__GNUC__)
81 __declspec(dllexport)
82 #endif
83 GThreadFunctions g_thread_functions_for_glib_use = {
84   (GMutex*(*)())g_thread_fail,                 /* mutex_new */
85   NULL,                                        /* mutex_lock */
86   NULL,                                        /* mutex_trylock */
87   NULL,                                        /* mutex_unlock */
88   NULL,                                        /* mutex_free */
89   (GCond*(*)())g_thread_fail,                  /* cond_new */
90   NULL,                                        /* cond_signal */
91   NULL,                                        /* cond_broadcast */
92   NULL,                                        /* cond_wait */
93   NULL,                                        /* cond_timed_wait  */
94   NULL,                                        /* cond_free */
95   (GPrivate*(*)(GDestroyNotify))g_thread_fail, /* private_new */
96   NULL,                                        /* private_get */
97   NULL,                                        /* private_set */
98   (void(*)(GThreadFunc, gpointer, gulong, 
99            gboolean, gboolean, GThreadPriority, 
100            gpointer))g_thread_fail,            /* thread_create */
101   NULL,                                        /* thread_yield */
102   NULL,                                        /* thread_join */
103   NULL,                                        /* thread_exit */
104   NULL,                                        /* thread_set_priority */
105   NULL                                         /* thread_self */
106 }; 
107
108 /* Local data */
109
110 static GMutex   *g_mutex_protect_static_mutex_allocation = NULL;
111 static GMutex   *g_thread_specific_mutex = NULL;
112 static GPrivate *g_thread_specific_private = NULL;
113
114 /* This must be called only once, before any threads are created.
115  * It will only be called from g_thread_init() in -lgthread.
116  */
117 void
118 g_mutex_init (void)
119 {
120   GRealThread* main_thread;
121  
122   /* We let the main thread (the one that calls g_thread_init) inherit
123    * the data, that it set before calling g_thread_init
124    */
125   main_thread = (GRealThread*) g_thread_self ();
126
127   g_thread_specific_private = g_private_new (g_thread_cleanup);
128   G_THREAD_UF (private_set, (g_thread_specific_private, main_thread));
129   G_THREAD_UF (thread_self, (&main_thread->system_thread));
130
131   g_mutex_protect_static_mutex_allocation = g_mutex_new();
132   g_thread_specific_mutex = g_mutex_new();
133 }
134
135 GMutex *
136 g_static_mutex_get_mutex_impl (GMutex** mutex)
137 {
138   if (!g_thread_supported ())
139     return NULL;
140
141   g_assert (g_mutex_protect_static_mutex_allocation);
142
143   g_mutex_lock (g_mutex_protect_static_mutex_allocation);
144
145   if (!(*mutex)) 
146     *mutex = g_mutex_new(); 
147
148   g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
149   
150   return *mutex;
151 }
152
153 void
154 g_static_rec_mutex_lock (GStaticRecMutex* mutex)
155 {
156   GSystemThread self;
157
158   g_return_if_fail (mutex);
159
160   G_THREAD_UF (thread_self, (&self));
161
162   if (g_system_thread_equal (self, mutex->owner))
163     {
164       mutex->depth++;
165       return;
166     }
167   g_static_mutex_lock (&mutex->mutex);
168   g_system_thread_assign (mutex->owner, self);
169   mutex->depth = 1;
170 }
171
172 gboolean
173 g_static_rec_mutex_trylock (GStaticRecMutex* mutex)
174 {
175   GSystemThread self;
176
177   g_return_val_if_fail (mutex, FALSE);
178
179   G_THREAD_UF (thread_self, (&self));
180
181   if (g_system_thread_equal (self, mutex->owner))
182     {
183       mutex->depth++;
184       return TRUE;
185     }
186
187   if (!g_static_mutex_trylock (&mutex->mutex))
188     return FALSE;
189
190   g_system_thread_assign (mutex->owner, self);
191   mutex->depth = 1;
192   return TRUE;
193 }
194
195 void
196 g_static_rec_mutex_unlock (GStaticRecMutex* mutex)
197 {
198   g_return_if_fail (mutex);
199
200   if (mutex->depth > 1)
201     {
202       mutex->depth--;
203       return;
204     }
205   g_system_thread_assign (mutex->owner, zero_thread);
206   g_static_mutex_unlock (&mutex->mutex);  
207 }
208
209 void
210 g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
211                                 guint            depth)
212 {
213   g_return_if_fail (mutex);
214
215   g_static_mutex_lock (&mutex->mutex);
216   G_THREAD_UF (thread_self, (&mutex->owner));
217   mutex->depth = depth;
218 }
219
220 guint    
221 g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex)
222 {
223   gint depth = mutex->depth;
224
225   g_return_val_if_fail (mutex, 0);
226
227   g_system_thread_assign (mutex->owner, zero_thread);
228   mutex->depth = 0;
229   g_static_mutex_unlock (&mutex->mutex);
230
231   return depth;
232 }
233
234
235 gpointer
236 g_static_private_get (GStaticPrivate *private_key)
237 {
238   return g_static_private_get_for_thread (private_key, g_thread_self ());
239 }
240
241 gpointer
242 g_static_private_get_for_thread (GStaticPrivate *private_key,
243                                  GThread        *thread)
244 {
245   GArray *array;
246   GRealThread *self = (GRealThread*) thread;
247
248   g_return_val_if_fail (thread, NULL);
249
250   array = self->private_data;
251   if (!array)
252     return NULL;
253
254   if (!private_key->index)
255     return NULL;
256   else if (private_key->index <= array->len)
257     return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data;
258   else
259     return NULL;
260 }
261
262 void
263 g_static_private_set (GStaticPrivate *private_key, 
264                       gpointer        data,
265                       GDestroyNotify  notify)
266 {
267   g_static_private_set_for_thread (private_key, g_thread_self (), 
268                                    data, notify);
269 }
270
271 void
272 g_static_private_set_for_thread (GStaticPrivate *private_key, 
273                                  GThread        *thread,
274                                  gpointer        data,
275                                  GDestroyNotify  notify)
276 {
277   GArray *array;
278   GRealThread *self =(GRealThread*) thread;
279   static guint next_index = 0;
280   GStaticPrivateNode *node;
281
282   g_return_if_fail (thread);
283   
284   array = self->private_data;
285   if (!array)
286     {
287       array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode));
288       self->private_data = array;
289     }
290
291   if (!private_key->index)
292     {
293       g_mutex_lock (g_thread_specific_mutex);
294
295       if (!private_key->index)
296         private_key->index = ++next_index;
297
298       g_mutex_unlock (g_thread_specific_mutex);
299     }
300
301   if (private_key->index > array->len)
302     g_array_set_size (array, private_key->index);
303
304   node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1);
305   if (node->destroy)
306     {
307       gpointer ddata = node->data;
308       GDestroyNotify ddestroy = node->destroy;
309
310       node->data = data;
311       node->destroy = notify;
312
313       ddestroy (ddata);
314     }
315   else
316     {
317       node->data = data;
318       node->destroy = notify;
319     }
320 }
321
322 static void
323 g_thread_cleanup (gpointer data)
324 {
325   if (data)
326     {
327       GRealThread* thread = data;
328       if (thread->private_data)
329         {
330           GArray* array = thread->private_data;
331           guint i;
332           
333           for (i = 0; i < array->len; i++ )
334             {
335               GStaticPrivateNode *node = 
336                 &g_array_index (array, GStaticPrivateNode, i);
337               if (node->destroy)
338                 node->destroy (node->data);
339             }
340           g_array_free (array, TRUE);
341         }
342       /* We only free the thread structure, if it isn't joinable. If
343          it is, the structure is freed in g_thread_join */
344       if (!thread->thread.joinable)
345         {
346           /* Just to make sure, this isn't used any more */
347           g_system_thread_assign (thread->system_thread, zero_thread);
348           g_free (thread);
349         }
350     }
351 }
352
353 static void
354 g_thread_fail (void)
355 {
356   g_error ("The thread system is not yet initialized.");
357 }
358
359 G_LOCK_DEFINE_STATIC (g_thread_create);
360
361 static void 
362 g_thread_create_proxy (gpointer data)
363 {
364   GRealThread* thread = data;
365
366   g_assert (data);
367
368   /* the lock makes sure, that thread->system_thread is written,
369      before thread->func is called. See g_thread_create */
370
371   G_LOCK (g_thread_create);
372   g_private_set (g_thread_specific_private, data);
373   G_UNLOCK (g_thread_create);
374
375   thread->func (thread->arg);
376 }
377
378 GThread* 
379 g_thread_create (GThreadFunc             thread_func,
380                  gpointer                arg,
381                  gulong                  stack_size,
382                  gboolean                joinable,
383                  gboolean                bound,
384                  GThreadPriority         priority)
385 {
386   GRealThread* result = g_new (GRealThread, 1);
387
388   g_return_val_if_fail (thread_func, NULL);
389   
390   result->thread.joinable = joinable;
391   result->thread.bound = bound;
392   result->thread.priority = priority;
393   result->func = thread_func;
394   result->arg = arg;
395   result->private_data = NULL; 
396   G_LOCK (g_thread_create);
397   G_THREAD_UF (thread_create, (g_thread_create_proxy, result, stack_size, 
398                                joinable, bound, priority,
399                                &result->system_thread));
400   G_UNLOCK (g_thread_create);
401   return (GThread*) result;
402 }
403
404 void 
405 g_thread_join (GThread* thread)
406 {
407   GRealThread* real = (GRealThread*) thread;
408   
409
410   g_return_if_fail (thread);
411   g_return_if_fail (thread->joinable);
412   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
413
414   G_THREAD_UF (thread_join, (&real->system_thread));
415
416   /* Just to make sure, this isn't used any more */
417   thread->joinable = 0;
418   g_system_thread_assign (real->system_thread, zero_thread);
419
420   /* the thread structure for non-joinable threads is freed upon
421      thread end. We free the memory here. This will leave loose end,
422      if a joinable thread is not joined. */
423
424   g_free (thread);
425 }
426
427 void
428 g_thread_set_priority (GThread* thread, 
429                        GThreadPriority priority)
430 {
431   GRealThread* real = (GRealThread*) thread;
432
433   g_return_if_fail (thread);
434   g_return_if_fail (!g_system_thread_equal (real->system_thread, zero_thread));
435
436   thread->priority = priority;
437   G_THREAD_CF (thread_set_priority, (void)0, (&real->system_thread, priority));
438 }
439
440 GThread*
441 g_thread_self()
442 {
443   GRealThread* thread = g_private_get (g_thread_specific_private);
444
445   if (!thread)
446     {  
447       /* If no thread data is available, provide and set one.  This
448          can happen for the main thread and for threads, that are not
449          created by GLib. */
450       thread = g_new (GRealThread, 1);
451       thread->thread.joinable = FALSE; /* This is a save guess */
452       thread->thread.bound = TRUE; /* This isn't important at all */
453       thread->thread.priority = G_THREAD_PRIORITY_NORMAL; /* This is
454                                                              just a guess */
455       thread->func = NULL;
456       thread->arg = NULL;
457       thread->private_data = NULL;
458
459       if (g_thread_supported ())
460         G_THREAD_UF (thread_self, (&thread->system_thread));
461
462       g_private_set (g_thread_specific_private, thread);
463     }
464   
465   return (GThread*)thread;
466 }
467
468 static void inline g_static_rw_lock_wait (GCond** cond, GStaticMutex* mutex)
469 {
470   if (!*cond)
471       *cond = g_cond_new ();
472   g_cond_wait (*cond, g_static_mutex_get_mutex (mutex));
473 }
474
475 static void inline g_static_rw_lock_signal (GStaticRWLock* lock)
476 {
477   if (lock->want_to_write && lock->write_cond)
478     g_cond_signal (lock->write_cond);
479   else if (lock->read_cond)
480     g_cond_signal (lock->read_cond);
481 }
482
483 void g_static_rw_lock_reader_lock (GStaticRWLock* lock)
484 {
485   g_return_if_fail (lock);
486
487   if (!g_threads_got_initialized)
488     return;
489
490   g_static_mutex_lock (&lock->mutex);
491   while (lock->write || lock->want_to_write) 
492     g_static_rw_lock_wait (&lock->read_cond, &lock->mutex);
493   lock->read_counter++;
494   g_static_mutex_unlock (&lock->mutex);
495 }
496
497 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock)
498 {
499   gboolean ret_val = FALSE;
500
501   g_return_val_if_fail (lock, FALSE);
502
503   if (!g_threads_got_initialized)
504     return TRUE;
505
506   g_static_mutex_lock (&lock->mutex);
507   if (!lock->write && !lock->want_to_write)
508     {
509       lock->read_counter++;
510       ret_val = TRUE;
511     }
512   g_static_mutex_unlock (&lock->mutex);
513   return ret_val;
514 }
515
516 void g_static_rw_lock_reader_unlock  (GStaticRWLock* lock)
517 {
518   g_return_if_fail (lock);
519
520   if (!g_threads_got_initialized)
521     return;
522
523   g_static_mutex_lock (&lock->mutex);
524   lock->read_counter--;
525   g_static_rw_lock_signal (lock);
526   g_static_mutex_unlock (&lock->mutex);
527 }
528
529 void g_static_rw_lock_writer_lock (GStaticRWLock* lock)
530 {
531   g_return_if_fail (lock);
532
533   if (!g_threads_got_initialized)
534     return;
535
536   g_static_mutex_lock (&lock->mutex);
537   lock->want_to_write++;
538   while (lock->write || lock->read_counter)
539     g_static_rw_lock_wait (&lock->write_cond, &lock->mutex);
540   lock->want_to_write--;
541   lock->write = TRUE;
542   g_static_mutex_unlock (&lock->mutex);
543 }
544
545 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock)
546 {
547   gboolean ret_val = FALSE;
548
549   g_return_val_if_fail (lock, FALSE);
550   
551   if (!g_threads_got_initialized)
552     return TRUE;
553
554   g_static_mutex_lock (&lock->mutex);
555   if (!lock->write && !lock->read_counter)
556     {
557       lock->write = TRUE;
558       ret_val = TRUE;
559     }
560   g_static_mutex_unlock (&lock->mutex);
561   return ret_val;
562 }
563
564 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock)
565 {
566   g_return_if_fail (lock);
567   
568   if (!g_threads_got_initialized)
569     return;
570
571   g_static_mutex_lock (&lock->mutex);
572   lock->write = FALSE; 
573   g_static_rw_lock_signal (lock);
574   g_static_mutex_unlock (&lock->mutex);
575 }
576
577 void g_static_rw_lock_free (GStaticRWLock* lock)
578 {
579   g_return_if_fail (lock);
580   
581   if (lock->read_cond)
582     g_cond_free (lock->read_cond);
583   if (lock->write_cond)
584     g_cond_free (lock->write_cond);
585   
586 }
587