Ops, forgot this.
[platform/upstream/glib.git] / docs / reference / glib / tmpl / threads.sgml
1 <!-- ##### SECTION Title ##### -->
2
3 Threads
4
5 <!-- ##### SECTION Short_Description ##### -->
6
7 thread abstraction; including threads, different mutexes, conditions
8 and thread private data.
9
10 <!-- ##### SECTION Long_Description ##### -->
11
12 <para>
13 Threads act almost like processes, but unlike processes all threads of
14 one process share the same memory. This is good, as it provides easy
15 communication between the involved threads via this shared memory, and
16 it is bad, because strange things (so called Heisenbugs) might happen,
17 when the program is not carefully designed. Especially bad is, that due
18 to the concurrent nature of threads no assumptions on the order of
19 execution of different threads can be done unless explicitly forced by
20 the programmer through synchronization primitives.
21 </para>
22
23 <para>
24 The aim of the thread related functions in GLib is to provide a
25 portable means for writing multi-threaded software. There are
26 primitives for mutexes to protect the access to portions of memory
27 (#GMutex, #GStaticMutex, #G_LOCK_DEFINE, #GStaticRecMutex and
28 #GStaticRWLock), there are primitives for condition variables to allow
29 synchronization of threads (#GCond) and finally there are primitives
30 for thread-private data, that every thread has a private instance of
31 (#GPrivate, #GStaticPrivate). Last but definitely not least there are
32 primitives to portably create and manage threads (#GThread).
33 </para>
34
35 <!-- ##### SECTION See_Also ##### -->
36 <para>
37 <variablelist>
38
39 <varlistentry>
40 <term>#GThreadPool</term>
41 <listitem><para>Thread pools.</para></listitem>
42 </varlistentry>
43
44 <varlistentry>
45 <term>#GAsyncQueue</term>
46 <listitem><para>Send asynchronous messages between threads.</para></listitem>
47 </varlistentry>
48
49 </variablelist>
50 </para>
51
52 <!-- ##### MACRO G_THREADS_ENABLED ##### -->
53
54 <para>
55 This macro is defined, if GLib was compiled with thread support. This
56 does not necessarily mean, that there is a thread implementation
57 available, but the infrastructure is in place and once you provide a
58 thread implementation to g_thread_init(), GLib will be multi-thread
59 safe. It isn't and cannot be, if #G_THREADS_ENABLED is not defined.
60 </para>
61
62
63
64 <!-- ##### MACRO G_THREADS_IMPL_POSIX ##### -->
65
66 <para>
67 This macro is defined, if POSIX style threads are used.
68 </para>
69
70
71
72 <!-- ##### MACRO G_THREADS_IMPL_SOLARIS ##### -->
73
74 <para>
75 This macro is defined, if the Solaris thread system is used.
76 </para>
77
78
79
80 <!-- ##### MACRO G_THREADS_IMPL_NONE ##### -->
81
82 <para>
83 This macro is defined, if no thread implementation is used. You can
84 however provide one to g_thread_init() to make GLib multi-thread safe.
85 </para>
86
87
88
89 <!-- ##### MACRO G_THREAD_ERROR ##### -->
90 <para>
91 The error domain of the GLib thread subsystem.
92 </para>
93
94
95
96 <!-- ##### ENUM GThreadError ##### -->
97 <para>
98 Possible errors of thread related functions.
99 </para>
100
101 @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
102 shortage. Try again later.
103
104 <!-- ##### STRUCT GThreadFunctions ##### -->
105
106 <para>
107 This function table is used by g_thread_init() to initialize the
108 thread system. The functions in that table are directly used by their
109 g_* prepended counterparts, that are described here, e.g. if you call
110 g_mutex_new() then mutex_new() from the table provided to
111 g_thread_init() will be called.
112 </para>
113
114 <note>
115 <para>
116 This struct should only be used, if you know, what you are doing.
117 </para>
118 </note>
119
120 @mutex_new: 
121 @mutex_lock: 
122 @mutex_trylock: 
123 @mutex_unlock: 
124 @mutex_free: 
125 @cond_new: 
126 @cond_signal: 
127 @cond_broadcast: 
128 @cond_wait: 
129 @cond_timed_wait: 
130 @cond_free: 
131 @private_new: 
132 @private_get: 
133 @private_set: 
134 @thread_create: 
135 @thread_yield: 
136 @thread_join: 
137 @thread_exit: 
138 @thread_set_priority: 
139 @thread_self: 
140
141 <!-- ##### FUNCTION g_thread_init ##### -->
142
143 <para>
144 Before you use a thread related function in GLib, you should
145 initialize the thread system. This is done by calling
146 g_thread_init(). Most of the time you will only have to call
147 g_thread_init(NULL). 
148 </para>
149
150 <note>
151 <para>
152 You should only call g_thread_init() with a non-NULL parameter, if you
153 really know, what you are doing.
154 </para>
155 </note>
156
157 <note>
158 <para>
159 g_thread_init() must not be called directly or indirectly as a
160 call-back from GLib. Also no mutexes may be currently locked, while
161 calling g_thread_init().
162 </para>
163 </note>
164
165 <para>
166 g_thread_init() might only be called once. On the second call
167 it will abort with an error. If you want to make sure, that the thread
168 system is initialized, you can do that too:
169 </para>
170
171 <para>
172 <informalexample>
173 <programlisting>
174 if (!g_thread_supported ()) g_thread_init (NULL);
175 </programlisting>
176 </informalexample>
177 </para>
178
179 <para>
180 After that line either the thread system is initialized or the program
181 will abort, if no thread system is available in GLib, i.e. either
182 #G_THREADS_ENABLED is not defined or #G_THREADS_IMPL_NONE is defined.
183 </para>
184
185 <para>
186 If no thread system is available and @vtable is NULL or if not all
187 elements of @vtable are non-NULL, then g_thread_init() will abort.
188 </para>
189
190 <note>
191 <para>
192 To use g_thread_init() in your program, you have to link with the
193 libraries, that the command "glib-config --libs gthread" outputs. This
194 is not the case for all the other thread related functions of
195 GLib. Those can be used without having to link with the thread
196 libraries.
197 </para>
198 </note>
199
200 @vtable: a function table of type #GThreadFunctions, that provides the
201 entry points to the thread system to be used
202
203
204 <!-- ##### FUNCTION g_thread_supported ##### -->
205 <para>
206 This function returns, whether the thread system is initialized or
207 not.
208 </para>
209
210 <note>
211 <para>
212 This function is actually a macro. Apart from taking the address of it
213 you can however use it as if it was a function.
214 </para>
215 </note>
216
217 @Returns: TRUE, if the thread system is initialized
218
219
220 <!-- ##### USER_FUNCTION GThreadFunc ##### -->
221 <para>
222 Specifies the type of the @func functions passed to
223 g_thread_create().
224 </para>
225
226 @data: data passed to the thread
227 @Returns: the return value of the thread, which will be returned by
228 g_thread_join()
229
230
231 <!-- ##### ENUM GThreadPriority ##### -->
232 <para>
233 Specifies the priority of a thread. 
234 </para>
235
236 <note>
237 <para>
238 It is not guaranteed, that threads with different priorities really
239 behave accordingly. On some systems (e.g. Linux) only root can increase
240 priorities. On other systems (e.g. Solaris) there doesn't seem to be
241 different scheduling for different priorities. All in all try to avoid
242 being dependent on priorities.
243 </para>
244 </note>
245
246 @G_THREAD_PRIORITY_LOW: a priority lower than normal
247 @G_THREAD_PRIORITY_NORMAL: the default priority
248 @G_THREAD_PRIORITY_HIGH: a priority higher than normal
249 @G_THREAD_PRIORITY_URGENT: the highest priority
250
251 <!-- ##### STRUCT GThread ##### -->
252 <para>
253 The #GThread struct represents a running thread. It has three public
254 read-only members, but the underlying struct is bigger, so you must
255 not copy this struct.
256 </para>
257
258 <note>
259 <para>
260 Resources for a joinable thread are not fully released until
261 g_thread_join() is called for that thread.
262 </para>
263 </note>
264
265 @func: the function executing in that thread
266 @data: the argument to the function
267 @joinable: is this thread joinable?
268 @bound: is this thread bound to a system thread?
269 @priority: the priority of the thread
270
271 <!-- ##### FUNCTION g_thread_create ##### -->
272 <para>
273 This function creates a new thread with the priority @priority. The
274 stack gets the size @stack_size or the default value for the current
275 platform, if @stack_size is 0.
276 </para>
277
278 <para>
279 If @joinable is #TRUE, you can wait for this threads termination
280 calling g_thread_wait(). Otherwise the thread will just disappear, when
281 ready. If @bound is #TRUE, this thread will be scheduled in the system
282 scope, otherwise the implementation is free to do scheduling in the
283 process scope. The first variant is more expensive resource-wise, but
284 generally faster. On some systems (e.g. Linux) all threads are bound.
285 </para>
286
287 <para>
288 The new thread executes the function @func with the argument
289 @data. If the thread was created successfully, it is returned.
290 </para>
291
292 <para>
293 @error can be NULL to ignore errors, or non-NULL to report errors. The
294 error is set, if and only if the function returns #NULL.
295 </para>
296
297 <note>
298 <para>
299 It is not guaranteed, that threads with different priorities really
300 behave accordingly. On some systems (e.g. Linux) only root can increase
301 priorities. On other systems (e.g. Solaris) there doesn't seem to be
302 different scheduling for different priorities. All in all try to avoid
303 being dependent on priorities. Use %G_THREAD_PRIORITY_NORMAL here as a
304 default.
305 </para>
306 </note>
307
308 @func: a function to execute in the new thread
309 @data: an argument to supply to the new thread
310 @stack_size: a stack size for the new thread
311 @joinable: should this thread be joinable?
312 @bound: should this thread be bound to a system thread?
313 @priority: a priority for the thread
314 @error: return location for error.
315 @Returns: the new #GThread on success
316
317
318 <!-- ##### FUNCTION g_thread_self ##### -->
319 <para>
320 This functions returns the #GThread corresponding to the calling thread.
321 </para>
322
323 @Returns: the current thread
324
325
326 <!-- ##### FUNCTION g_thread_join ##### -->
327 <para>
328 Waits until @thread finishes, i.e. the function @func, as given
329 to g_thread_create, returns or g_thread_exit() is called by
330 @thread. All resources of @thread including the #GThread struct are
331 released. @thread must have been created with @joinable=#TRUE in
332 g_thread_create(). The value returned by @func or given to
333 g_thread_exit() by @thread is returned by this function.
334 </para>
335
336 @thread: a #GThread to be waited for
337 @Returns: the return value of the thread
338
339
340 <!-- ##### FUNCTION g_thread_set_priority ##### -->
341 <para>
342 Change the priority of @thread to @priority.
343 </para>
344
345 <note>
346 <para>
347 It is not guaranteed, that threads with different priorities really
348 behave accordingly. On some systems (e.g. Linux) only root can increase
349 priorities. On other systems (e.g. Solaris) there doesn't seem to be
350 different scheduling for different priorities. All in all try to avoid
351 being dependent on priorities.
352 </para>
353 </note>
354
355 @thread: a #GThread
356 @priority: a new priority for @thread
357
358
359 <!-- ##### FUNCTION g_thread_yield ##### -->
360 <para>
361 Give way to other threads waiting to be scheduled. 
362 </para>
363
364 <para>
365 This function is often used as a method to make busy wait less
366 evil. But in most cases, you will encounter, there are better methods
367 to do that. So in general you shouldn't use that function.
368 </para>
369
370
371
372 <!-- ##### FUNCTION g_thread_exit ##### -->
373 <para>
374 Exit the current thread. If another thread is waiting for that thread
375 using g_thread_join() and the current thread is joinable, the waiting
376 thread will be woken up and getting @retval as the return value of
377 g_thread_join(). If the current thread is not joinable, @retval is
378 ignored. Calling
379 </para>
380
381 <para>
382 <informalexample>
383 <programlisting>
384 g_thread_join (retval);
385 </programlisting>
386 </informalexample>
387 </para>
388
389 <para>
390 is equivalent to calling 
391 </para>
392
393 <para>
394 <informalexample>
395 <programlisting>
396 return retval;
397 </programlisting>
398 </informalexample>
399 </para>
400
401 <para>
402 in the function @func, as given to g_thread_create().
403 </para>
404
405 <note>
406 <para>
407 Never call g_thread_exit from within a thread of a #GThreadPool, as
408 that will mess up the bookkeeping and lead to funny and unwanted
409 results.
410 </para>
411 </note>
412
413 @retval: the return value of this thread
414
415
416 <!-- ##### STRUCT GMutex ##### -->
417
418 <para>
419 The #GMutex struct is an opaque data structure to represent a mutex
420 (mutual exclusion). It can be used to protect data against shared
421 access. Take for example the following function:
422
423 <example>
424 <title>A function which will not work in a threaded environment</title>
425 <programlisting>
426   int give_me_next_number ()
427   {
428     static int current_number = 0;
429
430     /* now do a very complicated calculation to calculate the new number,
431        this might for example be a random number generator */
432     current_number = calc_next_number (current_number); 
433     return current_number;
434   }
435 </programlisting>
436 </example>
437 </para>
438
439 <para>
440 It is easy to see, that this won't work in a multi-threaded
441 application. There current_number must be protected against shared
442 access. A first naive implementation would be:
443 </para>
444
445 <para>
446 <example>
447 <title>The wrong way to write a thread-safe function</title>
448 <programlisting>
449   int give_me_next_number ()
450   {
451     static int current_number = 0;
452     int ret_val;
453     static GMutex * mutex = NULL;
454
455     if (!mutex)
456       mutex = g_mutex_new ();
457     g_mutex_lock (mutex);
458     ret_val = current_number = calc_next_number (current_number); 
459     g_mutex_unlock (mutex);
460     return ret_val;
461   }
462 </programlisting>
463 </example>
464 </para>
465
466 <para>
467 This looks like it would work, but there is a race condition while
468 constructing the mutex and this code cannot work reliable. So please do
469 not use such constructs in your own programs. One working solution is:
470 </para>
471
472 <para>
473 <example>
474 <title>A correct thread-safe function</title>
475 <programlisting>
476   static GMutex *give_me_next_number_mutex = NULL;
477
478   /* this function must be called before any call to give_me_next_number ()
479      it must be called exactly once. */
480   void init_give_me_next_number () 
481   {
482     g_assert (give_me_next_number_mutex == NULL);
483     give_me_next_number_mutex = g_mutex_new ();
484   }
485
486   int give_me_next_number ()
487   {
488     static int current_number = 0;
489     int ret_val;
490
491     g_mutex_lock (give_me_next_number_mutex);
492     ret_val = current_number = calc_next_number (current_number); 
493     g_mutex_unlock (give_me_next_number_mutex);
494     return ret_val;
495   }
496 </programlisting>
497 </example>
498 </para>
499
500 <para>
501 #GStaticMutex provides a simpler and safer way of doing this.
502 </para>
503
504 <para>
505 If you want to use a mutex, but your code should also work without
506 calling g_thread_init() first, you can not use a #GMutex, as
507 g_mutex_new() requires that. Use a #GStaticMutex instead.
508 </para>
509
510 <para>
511 A #GMutex should only be accessed via the following functions.
512 </para>
513
514 <note>
515 <para>
516 All of the g_mutex_* functions are actually macros. Apart from taking
517 the addresses of them, you can however use them as if they were functions.
518 </para>
519 </note>
520
521
522 <!-- ##### FUNCTION g_mutex_new ##### -->
523
524 <para>
525 Creates a new #GMutex. 
526 </para>
527
528 <note>
529 <para>
530 This function will abort, if g_thread_init() has not been called yet.
531 </para>
532 </note>
533
534 @Returns: a new #GMutex
535
536
537 <!-- ##### FUNCTION g_mutex_lock ##### -->
538
539 <para>
540 Locks @mutex. If @mutex is already locked by another thread, the
541 current thread will block until @mutex is unlocked by the other
542 thread.
543 </para>
544
545 <para>
546 This function can also be used, if g_thread_init() has not yet been
547 called and will do nothing then.
548 </para>
549
550 <note>
551 <para>
552 #GMutex is not recursive, i.e. a thread will deadlock, if it already
553 has locked @mutex while calling g_mutex_lock(). Use
554 #GStaticRecMutex instead, if you need recursive mutexes.
555 </para>
556 </note>
557
558 @mutex: a #GMutex
559
560
561 <!-- ##### FUNCTION g_mutex_trylock ##### -->
562
563 <para>
564 Tries to lock @mutex. If @mutex is already locked by another
565 thread, it immediately returns FALSE. Otherwise it locks @mutex
566 and returns TRUE.
567 </para>
568
569 <para>
570 This function can also be used, if g_thread_init() has not yet been
571 called and will immediately return TRUE then.
572 </para>
573
574 <note>
575 <para>
576 #GMutex is not recursive, i.e. g_mutex_trylock() will return FALSE,
577 if the current thread already has locked @mutex. Use
578 #GStaticRecMutex instead, if you need recursive mutexes.
579 </para>
580 </note>
581
582 @mutex: a #GMutex
583 @Returns: TRUE, if @mutex could be locked
584
585
586 <!-- ##### FUNCTION g_mutex_unlock ##### -->
587
588 <para>
589 Unlocks @mutex. If another thread is blocked in a g_mutex_lock() call
590 for @mutex, it will be woken and can lock @mutex itself.
591 </para>
592
593 <para>
594 This function can also be used, if g_thread_init() has not yet been
595 called and will do nothing then.
596 </para>
597
598 @mutex: a #GMutex
599
600
601 <!-- ##### FUNCTION g_mutex_free ##### -->
602
603 <para>
604 Destroys @mutex.
605 </para>
606
607 @mutex: a #GMutex
608
609
610 <!-- ##### STRUCT GStaticMutex ##### -->
611
612 <para>
613 A #GStaticMutex works like a #GMutex, but it has one significant
614 advantage. It doesn't need to be created at run-time like a #GMutex,
615 but can be defined at compile-time. Here is a shorter, easier and
616 safer version of our give_me_next_number() example:
617 </para>
618
619 <para>
620 <example>
621 <title>Using GStaticMutex to simplify thread-safe programming</title>
622 <programlisting>
623   int give_me_next_number ()
624   {
625     static int current_number = 0;
626     int ret_val;
627     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
628
629     g_static_mutex_lock (&amp;mutex);
630     ret_val = current_number = calc_next_number (current_number); 
631     g_static_mutex_unlock (&amp;mutex);
632     return ret_val;
633   }
634 </programlisting>
635 </example>
636 </para>
637
638 <para>
639 Sometimes you would like to dynamically create a mutex. If you don't
640 want to require prior calling to g_thread_init(), because your code
641 should also be usable in non-threaded programs, you are not able to
642 use g_mutex_new() and thus #GMutex, as that requires a prior call to
643 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
644 must be initialized with g_static_mutex_init() before using it and
645 freed with with g_static_mutex_free() when not needed anymore to free
646 up any allocated recourses.
647 </para>
648
649 <para>
650 Even though #GStaticMutex is not opaque, it should only be used with
651 the following functions, as it is defined differently on different
652 platforms.
653 </para>
654
655 <para>
656 All of the g_static_mutex_* functions can also be used, if
657 g_thread_init() has not yet been called.
658 </para>
659
660 <note>
661 <para>
662 All of the g_static_mutex_* functions are actually macros. Apart from
663 taking the addresses of them, you can however use them as if they were
664 functions.
665 </para>
666 </note>
667
668
669 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
670
671 <para>
672 A #GStaticMutex must be initialized with this macro, before it can be
673 used. This macro can used be to initialize a variable, but it cannot
674 be assigned to a variable. In that case you have to use
675 g_static_mutex_init().
676 </para>
677
678 <para>
679 <informalexample>
680 <programlisting>
681 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
682 </programlisting>
683 </informalexample>
684 </para>
685
686
687
688 <!-- ##### FUNCTION g_static_mutex_init ##### -->
689 <para>
690 Initializes @mutex. Alternatively you can initialize it with
691 #G_STATIC_MUTEX_INIT.
692 </para>
693
694 @mutex: a #GStaticMutex to be initialized
695
696
697 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
698 <para>
699 Works like g_mutex_lock(), but for a #GStaticMutex.
700 </para>
701
702 @mutex: a #GStaticMutex
703
704
705 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
706
707 <para>
708 Works like g_mutex_trylock(), but for a #GStaticMutex.
709 </para>
710
711 @mutex: a #GStaticMutex
712 @Returns: TRUE, if the #GStaticMutex could be locked
713
714
715 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
716
717 <para>
718 Works like g_mutex_unlock(), but for a #GStaticMutex.
719 </para>
720
721 @mutex: a #GStaticMutex
722
723
724 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
725
726 <para>
727 For some operations (like g_cond_wait()) you must have a #GMutex
728 instead of a #GStaticMutex. This function will return the
729 corresponding #GMutex for @mutex.
730 </para>
731
732 @mutex: a #GStaticMutex
733 @Returns: the #GMutex corresponding to @mutex
734
735
736 <!-- ##### FUNCTION g_static_mutex_free ##### -->
737 <para>
738 Releases all resources allocated to @mutex. 
739 </para>
740
741 <para>
742 You don't have to call this functions for a #GStaticMutex with an
743 unbounded lifetime, i.e. objects declared 'static', but if you have a
744 #GStaticMutex as a member of a structure and the structure is freed,
745 you should also free the #GStaticMutex.
746 </para>
747
748 @mutex: a #GStaticMutex to be freed
749
750
751 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
752
753 <para>
754 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
755 with the advantage that they will expand to nothing in programs
756 compiled against a thread-disabled GLib, saving code and memory
757 there. #G_LOCK_DEFINE defines a lock. It can appear, where variable
758 definitions may appear in programs, i.e. in the first block of a
759 function or outside of functions. The @name parameter will be mangled
760 to get the name of the #GStaticMutex. This means, that you can use
761 names of existing variables as the parameter, e.g. the name of the
762 variable you intent to protect with the lock. Look at our
763 give_me_next_number() example using the G_LOCK_* macros:
764 </para>
765
766 <para>
767 <example>
768 <title>Using the G_LOCK_* convenience macros</title>
769 <programlisting>
770 G_LOCK_DEFINE (current_number);
771
772 int give_me_next_number ()
773   {
774     static int current_number = 0;
775     int ret_val;
776
777     G_LOCK (current_number);
778     ret_val = current_number = calc_next_number (current_number); 
779     G_UNLOCK (current_number);
780     return ret_val;
781   }
782 </programlisting>
783 </example>
784 </para>
785
786 @name: the name of the lock
787
788
789 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
790
791 <para>
792 This works like #G_LOCK_DEFINE, but it creates a static object.
793 </para>
794
795 @name: the name of the lock
796
797
798 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
799
800 <para>
801 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
802 </para>
803
804 @name: the name of the lock
805
806
807 <!-- ##### MACRO G_LOCK ##### -->
808
809 <para>
810 Works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
811 </para>
812
813 @name: the name of the lock
814
815
816 <!-- ##### MACRO G_TRYLOCK ##### -->
817
818 <para>
819 Works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
820 </para>
821
822 @name: the name of the lock
823 @Returns: TRUE, if the lock could be locked
824
825
826 <!-- ##### MACRO G_UNLOCK ##### -->
827
828 <para>
829 Works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
830 </para>
831
832 @name: the name of the lock
833
834
835 <!-- ##### STRUCT GStaticRecMutex ##### -->
836 <para>
837 A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
838 multiple times by one thread. If you enter it n times, however, you
839 have to unlock it n times again to let other threads lock it. An
840 exception is the function g_static_rec_mutex_unlock_full(), that
841 allows you to unlock a #GStaticRecMutex completely returning the depth,
842 i.e. the number of times this mutex was locked. The depth can later be
843 used to restore the state by calling g_static_rec_mutex_lock_full().
844 </para>
845
846 <para>
847 Even though #GStaticRecMutex is not opaque, it should only be used with
848 the following functions.
849 </para>
850
851 <para>
852 All of the g_static_rec_mutex_* functions can also be used, if
853 g_thread_init() has not been called.
854 </para>
855
856 @mutex: 
857 @depth: 
858 @owner: 
859
860 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
861 <para>
862 A #GStaticRecMutex must be initialized with this macro, before it can
863 be used. This macro can used be to initialize a variable, but it
864 cannot be assigned to a variable. In that case you have to use
865 g_static_rec_mutex_init().
866 </para>
867
868 <para>
869 <informalexample>
870 <programlisting>
871 GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
872 </programlisting>
873 </informalexample>
874 </para>
875
876
877
878 <!-- ##### FUNCTION g_static_rec_mutex_init ##### -->
879 <para>
880 A #GStaticRecMutex must be initialized with this function, before it
881 can be used. Alternatively you can initialize it with
882 #G_STATIC_REC_MUTEX_INIT.
883 </para>
884
885 @mutex: a #GStaticRecMutex to be initialized
886
887
888 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
889 <para>
890 Locks @mutex. If @mutex is already locked by another thread, the
891 current thread will block until @mutex is unlocked by the other
892 thread. If @mutex is already locked by the calling thread, this
893 functions increases the depth of @mutex and returns immediately.
894 </para>
895
896 @mutex: a #GStaticRecMutex to lock
897
898
899 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
900 <para>
901 Tries to lock @mutex. If @mutex is already locked by another thread,
902 it immediately returns #FALSE. Otherwise it locks @mutex and returns
903 #TRUE. If @mutex is already locked by the calling thread, this
904 functions increases the depth of @mutex and immediately  returns #TRUE.
905 </para>
906
907 @mutex: a #GStaticRecMutex to lock
908 @Returns: TRUE, if @mutex could be locked
909
910
911 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
912 <para>
913 Unlocks @mutex. Another threads can, however, only lock @mutex when it
914 has been unlocked as many times, as it had been locked before. If
915 @mutex is completely unlocked and another thread is blocked in a
916 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
917 lock @mutex itself.
918 </para>
919
920 @mutex: a #GStaticRecMutex to unlock
921
922
923 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
924 <para>
925 Works like calling g_static_rec_mutex_lock() for @mutex n times.
926 </para>
927
928 @mutex: a #GStaticRecMutex to lock
929 @depth: number of times this mutex has to be unlocked to be completely unlocked
930
931
932 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
933 <para>
934 Completely unlocks @mutex. If another thread is blocked in a
935 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
936 lock @mutex itself. This function returns the number of times, that
937 @mutex has been locked by the current thread. To restore the state
938 before the call to g_static_rec_mutex_unlock_full() you can call
939 g_static_rec_mutex_lock_full() with the depth returned by this
940 function.
941 </para>
942
943 @mutex: a #GStaticRecMutex to completely unlock
944 @Returns: number of times @mutex has been locked by the current thread
945
946
947 <!-- ##### FUNCTION g_static_rec_mutex_free ##### -->
948 <para>
949 Releases all resources allocated to a #GStaticRecMutex.
950 </para>
951
952 <para>
953 You don't have to call this functions for a #GStaticRecMutex with an
954 unbounded lifetime, i.e. objects declared 'static', but if you have a
955 #GStaticRecMutex as a member of a structure and the structure is
956 freed, you should also free the #GStaticRecMutex.
957 </para>
958
959 @mutex: a #GStaticRecMutex to be freed
960
961
962 <!-- ##### STRUCT GStaticRWLock ##### -->
963 <para>
964 The #GStaticRWLock struct represents a read-write lock. A read-write
965 lock can be used for protecting data, that some portions of code only
966 read from, while others also write. In such situations it is
967 desirable, that several readers can read at once, whereas of course
968 only one writer may write at a time. Take a look at the following
969 example:
970
971 <example>
972 <title>An array with access functions</title>
973 <programlisting>
974   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
975
976   GPtrArray *array;
977
978   gpointer my_array_get (guint index)
979   {
980     gpointer retval = NULL;
981
982     if (!array)
983       return NULL;
984
985     g_static_rw_lock_reader_lock (&amp;rwlock);
986
987     if (index < array->len)
988       retval = g_ptr_array_index (array, index);
989
990     g_static_rw_lock_reader_unlock (&amp;rwlock);
991
992     return retval;
993   }
994
995   void my_array_set (guint index, gpointer data)
996   {
997     g_static_rw_lock_writer_lock (&amp;rwlock);
998
999     if (!array)
1000       array = g_ptr_array_new ();
1001
1002     if (index >= array->len)
1003       g_ptr_array_set_size (array, index+1);
1004
1005     g_ptr_array_index (array, index) = data; 
1006
1007     g_static_rw_lock_writer_unlock (&amp;rwlock);
1008   }
1009 </programlisting>
1010 </example>
1011 </para>
1012
1013 <para>
1014 This example shows an array, which can be accessed by many readers
1015 (the my_array_get function) simultaneously, whereas the writers (the
1016 my_array_set function) only will be allowed once a time and only if no
1017 readers currently access the array. This is because of the potentially
1018 dangerous resizing of the array. Using that functions is fully
1019 multi-thread safe now. 
1020 </para>
1021
1022 <para>
1023 Most of the time the writers should have precedence of readers. That
1024 means for this implementation, that as soon as a writer wants to lock
1025 the data, no other reader is allowed to lock the data, whereas of
1026 course the readers, that already have locked the data are allowed to
1027 finish their operation. As soon as the last reader unlocks the data,
1028 the writer will lock it.
1029 </para>
1030
1031 <para>
1032 Even though #GStaticRWLock is not opaque, it should only be used with
1033 the following functions.
1034 </para>
1035
1036 <para>
1037 All of the g_static_rw_lock_* functions can also be used, if
1038 g_thread_init() has not been called.
1039 </para>
1040
1041 <note>
1042 <para>
1043 A read-write lock has a higher overhead as a mutex. For example both
1044 g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock()
1045 has to lock and unlock a #GStaticMutex, so it takes at least twice the
1046 time to lock and unlock a #GStaticRWLock than to lock and unlock a
1047 #GStaticMutex. So only data structures, that are accessed by multiple
1048 readers, which keep the lock for a considerable time justify a
1049 #GStaticRWLock. The above example most probably would fare better with
1050 a #GStaticMutex.
1051 </para>
1052 </note>
1053
1054 @mutex: 
1055 @read_cond: 
1056 @write_cond: 
1057 @read_counter: 
1058 @write: 
1059 @want_to_write: 
1060
1061 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
1062 <para>
1063 A #GStaticRWLock must be initialized with this macro, before it can
1064 be used. This macro can used be to initialize a variable, but it
1065 cannot be assigned to a variable. In that case you have to use
1066 g_static_rw_lock_init().
1067 </para>
1068
1069 <para>
1070 <informalexample>
1071 <programlisting>
1072 GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
1073 </programlisting>
1074 </informalexample>
1075 </para>
1076
1077
1078
1079 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
1080 <para>
1081 A #GStaticRWLock must be initialized with this function, before it can
1082 be used. Alternatively you can initialize it with
1083 #G_STATIC_RW_LOCK_INIT.
1084 </para>
1085
1086 @lock: a #GStaticRWLock to be initialized
1087
1088
1089 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
1090 <para>
1091 Locks @lock for reading. There may be unlimited concurrent locks for
1092 reading of a #GStaticRWLock at the same time.  If @lock is already
1093 locked for writing by another thread or if another thread is already
1094 waiting to lock @lock for writing, this function will block until
1095 @lock is unlocked by the other writing thread and no other writing
1096 threads want to lock @lock. This lock has to be unlocked by
1097 g_static_rw_lock_reader_unlock().
1098 </para>
1099
1100 <para>
1101 #GStaticRWLock in general is not recursive, but as there may be
1102 unlimited concurrent locks for reading, it effectively is for
1103 recursive for reading, but for reading only. Locking for writing after
1104 locking for reading will deadlock, the same holds true for the
1105 opposite order.
1106 </para>
1107
1108 @lock: a #GStaticRWLock to lock for reading
1109
1110
1111 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
1112 <para>
1113 Tries to lock @lock for reading. If @lock is already locked for
1114 writing by another thread or if another thread is already waiting to
1115 lock @lock for writing, it immediately returns #FALSE. Otherwise it
1116 locks @lock for reading and returns TRUE. This lock has to be unlocked
1117 by g_static_rw_lock_reader_unlock().
1118 </para>
1119
1120 @lock: a #GStaticRWLock to lock for reading
1121 @Returns: TRUE, if @lock could be locked for reading
1122
1123
1124 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
1125 <para>
1126 Unlocks @lock. If a thread waits to lock @lock for writing and all
1127 locks for reading have been unlocked, the waiting thread is woken up
1128 and can lock @lock for writing.
1129 </para>
1130
1131 @lock: a #GStaticRWLock to unlock after reading
1132
1133
1134 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
1135 <para>
1136 Locks @lock for writing. If @lock is already locked for writing or
1137 reading by other threads, this function will block until @lock is
1138 completely unlocked and then lock @lock for writing. While this
1139 functions waits to lock @lock, no other thread can lock @lock for
1140 reading. When @lock is locked for writing, no other thread can lock
1141 @lock (neither for reading nor writing). This lock has to be unlocked
1142 by g_static_rw_lock_writer_unlock().
1143 </para>
1144
1145 @lock: a #GStaticRWLock to lock for writing
1146
1147
1148 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
1149 <para>
1150 Tries to lock @lock for writing. If @lock is already locked (for
1151 either reading or writing) by another thread, it immediately returns
1152 #FALSE. Otherwise it locks @lock for writing and returns TRUE. This
1153 lock has to be unlocked by g_static_rw_lock_writer_unlock().
1154 </para>
1155
1156 @lock: a #GStaticRWLock to lock for writing
1157 @Returns: TRUE, if @lock could be locked for writing
1158
1159
1160 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
1161 <para>
1162 Unlocks @lock. If a thread waits to lock @lock for writing and all
1163 locks for reading have been unlocked, the waiting thread is woken up
1164 and can lock @lock for writing. If no thread waits to lock @lock for
1165 writing and threads wait to lock @lock for reading, the waiting
1166 threads are woken up and can lock @lock for reading.
1167 </para>
1168
1169 @lock: a #GStaticRWLock to unlock after writing
1170
1171
1172 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
1173 <para>
1174 Releases all resources allocated to @lock. 
1175 </para>
1176
1177 <para>
1178 You don't have to call this functions for a #GStaticRWLock with an
1179 unbounded lifetime, i.e. objects declared 'static', but if you have a
1180 #GStaticRWLock as a member of a structure and the structure is freed,
1181 you should also free the #GStaticRWLock.
1182 </para>
1183
1184 @lock: a #GStaticRWLock to be freed
1185
1186
1187 <!-- ##### STRUCT GCond ##### -->
1188
1189 <para>
1190 The #GCond struct is an opaque data structure to represent a
1191 condition. A #GCond is an object, that threads can block on, if they
1192 find a certain condition to be false. If other threads change the
1193 state of this condition they can signal the #GCond, such that the
1194 waiting thread is woken up. 
1195 </para>
1196
1197 <para>
1198 <example>
1199 <title>Using GCond to block a thread until a condition is satisfied</title>
1200 <programlisting>
1201 GCond* data_cond = NULL;   /* Must be initialized somewhere */
1202 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1203 gpointer current_data = NULL;
1204
1205 void push_data (gpointer data)
1206 {
1207   g_mutex_lock (data_mutex);
1208   current_data = data;
1209   g_cond_signal (data_cond);
1210   g_mutex_unlock (data_mutex);
1211 }
1212
1213 gpointer pop_data ()
1214 {
1215   gpointer data;
1216
1217   g_mutex_lock (data_mutex);
1218   while (!current_data)
1219       g_cond_wait (data_cond, data_mutex);
1220   data = current_data;
1221   current_data = NULL;
1222   g_mutex_unlock (data_mutex);
1223   return data;
1224 }
1225 </programlisting>
1226 </example>
1227 </para>
1228
1229 <para>
1230 Whenever a thread calls pop_data() now, it will wait until
1231 current_data is non-NULL, i.e. until some other thread has called
1232 push_data().
1233 </para>
1234
1235 <note>
1236 <para>
1237 It is important to use the g_cond_wait() and g_cond_timed_wait()
1238 functions only inside a loop, which checks for the condition to be
1239 true as it is not guaranteed that the waiting thread will find it
1240 fulfilled, even if the signaling thread left the condition
1241 in that state. This is because another thread can have altered the
1242 condition, before the waiting thread got the chance to be woken up,
1243 even if the condition itself is protected by a #GMutex, like above.
1244 </para>
1245 </note>
1246
1247 <para>
1248 A #GCond should only be accessed via the following functions.
1249 </para>
1250
1251 <note>
1252 <para>
1253 All of the g_cond_* functions are actually macros. Apart from taking
1254 the addresses of them, you can however use them as if they were functions.
1255 </para>
1256 </note>
1257
1258
1259 <!-- ##### FUNCTION g_cond_new ##### -->
1260
1261 <para>
1262 Creates a new #GCond. This function will abort, if g_thread_init()
1263 has not been called yet.
1264 </para>
1265
1266 @Returns: a new #GCond
1267
1268
1269 <!-- ##### FUNCTION g_cond_signal ##### -->
1270 <para>
1271 If threads are waiting for @cond, exactly one of them is woken up. It
1272 is good practice to hold the same lock as the waiting thread, while
1273 calling this function, though not required.
1274 </para>
1275
1276 <para>
1277 This function can also be used, if g_thread_init() has
1278 not yet been called and will do nothing then.
1279 </para>
1280
1281 @cond: a #GCond
1282
1283
1284 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1285
1286 <para>
1287 If threads are waiting for @cond, all of them are woken up. It is good
1288 practice to lock the same mutex as the waiting threads, while calling
1289 this function, though not required.
1290 </para>
1291
1292 <para>
1293 This function can also be used, if g_thread_init() has
1294 not yet been called and will do nothing then.
1295 </para>
1296
1297 @cond: a #GCond
1298
1299
1300 <!-- ##### FUNCTION g_cond_wait ##### -->
1301
1302 <para>
1303 Waits until this thread is woken up on @cond. The @mutex is unlocked
1304 before falling asleep and locked again before resuming.
1305 </para>
1306
1307 <para>
1308 This function can also be used, if g_thread_init() has not yet been
1309 called and will immediately return then.
1310 </para>
1311
1312 @cond: a #GCond
1313 @mutex: a #GMutex, that is currently locked
1314
1315
1316 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1317
1318 <para>
1319 Waits until this thread is woken up on @cond, but not longer than
1320 until the time, that is specified by @abs_time. The @mutex is
1321 unlocked before falling asleep and locked again before resuming.
1322 </para>
1323
1324 <para>
1325 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
1326 </para>
1327
1328 <para>
1329 This function can also be used, if g_thread_init() has not yet been
1330 called and will immediately return TRUE then.
1331 </para>
1332
1333 @cond: a #GCond
1334 @mutex: a #GMutex, that is currently locked
1335 @abs_time: a #GTimeVal, determining the final time
1336 @Returns: TRUE, if the thread is woken up in time
1337
1338
1339 <!-- ##### FUNCTION g_cond_free ##### -->
1340
1341 <para>
1342 Destroys the #GCond.
1343 </para>
1344
1345 @cond: a #GCond
1346
1347
1348 <!-- ##### STRUCT GPrivate ##### -->
1349 <para>
1350 The #GPrivate struct is an opaque data structure to represent a thread
1351 private data key. Threads can thereby obtain and set a pointer, which
1352 is private to the current thread. Take our give_me_next_number()
1353 example from above.  Now we don't want current_number to be shared
1354 between the threads, but to be private to each thread. This can be
1355 done as follows:
1356
1357 <example>
1358 <title>Using GPrivate for per-thread data</title>
1359 <programlisting>
1360   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1361                                        /* with g_private_new (g_free); */
1362
1363   int give_me_next_number ()
1364   {
1365     int *current_number = g_private_get (current_number_key);
1366
1367     if (!current_number)
1368     {
1369       current_number = g_new (int,1);
1370       *current_number = 0;
1371       g_private_set (current_number_key, current_number);
1372     }
1373     *current_number = calc_next_number (*current_number); 
1374     return *current_number;
1375   }
1376 </programlisting>
1377 </example>
1378 </para>
1379
1380 <para>
1381 Here the pointer belonging to the key current_number_key is read. If
1382 it is NULL, it has not been set yet. Then get memory for an integer
1383 value, assign this memory to the pointer and write the pointer
1384 back. Now we have an integer value, that is private to the current
1385 thread.
1386 </para>
1387
1388 <para>
1389 The #GPrivate struct should only be accessed via the following functions.
1390 </para>
1391
1392 <note>
1393 <para>
1394 All of the g_private_* functions are actually macros. Apart from taking
1395 the addresses of them, you can however use them as if they were functions.
1396 </para>
1397 </note>
1398
1399
1400 <!-- ##### FUNCTION g_private_new ##### -->
1401
1402 <para>
1403 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1404 to a destructor function. Whenever a thread ends and the corresponding
1405 pointer keyed to this instance of #GPrivate is non-NULL, the
1406 destructor is called with this pointer as the argument.
1407 </para>
1408
1409 <note>
1410 <para>
1411 @destructor is working quite differently from @notify in
1412 g_static_private_set().
1413 </para>
1414 </note>
1415
1416 <note>
1417 <para>
1418 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1419 shortage or use #GStaticPrivate.
1420 </para>
1421 </note>
1422
1423 <note>
1424 <para>
1425 This function will abort, if g_thread_init() has not been called yet.
1426 </para>
1427 </note>
1428
1429 @destructor: a function to handle the data keyed to #GPrivate, when a
1430 thread ends
1431 @Returns: a new #GPrivate
1432
1433
1434 <!-- ##### FUNCTION g_private_get ##### -->
1435
1436 <para>
1437 Returns the pointer keyed to @private_key for the current thread. This
1438 pointer is NULL, when g_private_set() hasn't been called for the
1439 current @private_key and thread yet.
1440 </para>
1441
1442 <para>
1443 This function can also be used, if g_thread_init() has not yet been
1444 called and will return the value of @private_key casted to #gpointer then.
1445 </para>
1446
1447 @private_key: a #GPrivate
1448 @Returns: the corresponding pointer
1449
1450
1451 <!-- ##### FUNCTION g_private_set ##### -->
1452
1453 <para>
1454 Sets the pointer keyed to @private_key for the current thread.
1455 </para>
1456
1457 <para>
1458 This function can also be used, if g_thread_init() has not yet been
1459 called and will set @private_key to @data casted to #GPrivate* then.
1460 </para>
1461
1462 @private_key: a #GPrivate
1463 @data: the new pointer
1464
1465
1466 <!-- ##### STRUCT GStaticPrivate ##### -->
1467
1468 <para>
1469 A #GStaticPrivate works almost like a #GPrivate, but it has one
1470 significant advantage. It doesn't need to be created at run-time like
1471 a #GPrivate, but can be defined at compile-time. This is similar to
1472 the difference between #GMutex and #GStaticMutex. Now look at our
1473 give_me_next_number() example with #GStaticPrivate:
1474 </para>
1475
1476 <para>
1477 <example>
1478 <title>Using GStaticPrivate for per-thread data</title>
1479 <programlisting>
1480   int give_me_next_number ()
1481   {
1482     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1483     int *current_number = g_static_private_get (&amp;current_number_key);
1484
1485     if (!current_number)
1486     {
1487       current_number = g_new (int,1);
1488       *current_number = 0;
1489       g_static_private_set (&amp;current_number_key, current_number, g_free);
1490     }
1491     *current_number = calc_next_number (*current_number); 
1492     return *current_number;
1493   }
1494 </programlisting>
1495 </example>
1496 </para>
1497
1498 @index: 
1499
1500 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1501 <para>
1502 Every #GStaticPrivate must be initialized with this macro, before it can
1503 be used.
1504 </para>
1505
1506 <para>
1507 <informalexample>
1508 <programlisting>
1509 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1510 </programlisting>
1511 </informalexample>
1512 </para>
1513
1514
1515
1516 <!-- ##### FUNCTION g_static_private_init ##### -->
1517 <para>
1518 Initializes @private_key. Alternatively you can initialize it with
1519 #G_STATIC_PRIVATE_INIT.
1520 </para>
1521
1522 @private_key: a #GStaticPrivate to be initialized
1523
1524
1525 <!-- ##### FUNCTION g_static_private_get ##### -->
1526 <para>
1527 Works like g_private_get() only for a #GStaticPrivate.
1528 </para>
1529
1530 <para>
1531 This function also works, if g_thread_init() has not yet been called.
1532 </para>
1533
1534 @private_key: a #GStaticPrivate
1535 @Returns: the corresponding pointer
1536
1537
1538 <!-- ##### FUNCTION g_static_private_set ##### -->
1539 <para>
1540 Sets the pointer keyed to @private_key for the current thread and the
1541 function @notify to be called with that pointer (NULL or non-NULL),
1542 whenever the pointer is set again or whenever the current thread ends.
1543 </para>
1544
1545 <para>
1546 This function also works, if g_thread_init() has not yet been
1547 called. If g_thread_init() is called later, the @data keyed to
1548 @private_key will be inherited only by the main thread, i.e. the one that
1549 called g_thread_init().
1550 </para>
1551
1552 <note>
1553 <para>
1554 @notify is working quite differently from @destructor in
1555 g_private_new().
1556 </para>
1557 </note>
1558
1559 @private_key: a #GStaticPrivate
1560 @data: the new pointer
1561 @notify: a function to be called with the pointer, whenever the
1562 current thread ends or sets this pointer again
1563
1564
1565 <!-- ##### FUNCTION g_static_private_free ##### -->
1566 <para>
1567 Releases all resources allocated to @private_key. 
1568 </para>
1569
1570 <para>
1571 You don't have to call this functions for a #GStaticPrivate with an
1572 unbounded lifetime, i.e. objects declared 'static', but if you have a
1573 #GStaticPrivate as a member of a structure and the structure is freed,
1574 you should also free the #GStaticPrivate.
1575 </para>
1576
1577 @private_key: a #GStaticPrivate to be freed
1578
1579