Updated.
[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 @thread_func functions passed to
223 g_thread_create().
224 </para>
225
226 @value: data supplied 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 @joinable: is this thread joinable?
266 @bound: is this thread bound to a system thread?
267 @priority: the priority of the thread
268 @func: the function executing in that thread
269 @arg: the argument to the function
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 @thread_func with the argument
289 @arg. 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 @thread_func: a function to execute in the new thread
309 @arg: 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 @thread_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 @thread_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 @thread_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 the #GMutex 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 @mutex: a #GMutex
575 @Returns: TRUE, if @mutex could be locked
576
577
578 <!-- ##### FUNCTION g_mutex_unlock ##### -->
579
580 <para>
581 Unlocks @mutex. If another thread is blocked in a g_mutex_lock() call
582 for @mutex, it will be woken and can lock @mutex itself.
583 </para>
584
585 <para>
586 This function can also be used, if g_thread_init() has not yet been
587 called and will do nothing then.
588 </para>
589
590 @mutex: a #GMutex
591
592
593 <!-- ##### FUNCTION g_mutex_free ##### -->
594
595 <para>
596 Destroys @mutex.
597 </para>
598
599 @mutex: a #GMutex
600
601
602 <!-- ##### STRUCT GStaticMutex ##### -->
603
604 <para>
605 A #GStaticMutex works like a #GMutex, but it has one significant
606 advantage. It doesn't need to be created at run-time like a #GMutex,
607 but can be defined at compile-time. Here is a shorter, easier and
608 safer version of our give_me_next_number() example:
609 </para>
610
611 <para>
612 <example>
613 <title>Using GStaticMutex to simplify thread-safe programming</title>
614 <programlisting>
615   int give_me_next_number ()
616   {
617     static int current_number = 0;
618     int ret_val;
619     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
620
621     g_static_mutex_lock (&amp;mutex);
622     ret_val = current_number = calc_next_number (current_number); 
623     g_static_mutex_unlock (&amp;mutex);
624     return ret_val;
625   }
626 </programlisting>
627 </example>
628 </para>
629
630 <para>
631 Sometimes you would like to dynamically create a mutex. If you don't
632 want to require prior calling to g_thread_init(), because your code
633 should also be usable in non-threaded programs, you are not able to
634 use g_mutex_new() and thus #GMutex, as that requires a prior call to
635 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
636 must be initialized with g_static_mutex_init() before using it and
637 freed with with g_static_mutex_free() when not needed anymore to free
638 up any allocated recourses.
639 </para>
640
641 <para>
642 Even though #GStaticMutex is not opaque, it should only be used with
643 the following functions, as it is defined differently on different
644 platforms.
645 </para>
646
647 <para>
648 All of the g_static_mutex_* functions can also be used, if
649 g_thread_init() has not yet been called.
650 </para>
651
652 <note>
653 <para>
654 All of the g_static_mutex_* functions are actually macros. Apart from
655 taking the addresses of them, you can however use them as if they were
656 functions.
657 </para>
658 </note>
659
660
661 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
662
663 <para>
664 A #GStaticMutex must be initialized with this macro, before it can be
665 used. This macro can used be to initialize a variable, but it cannot
666 be assigned to a variable. In that case you have to use
667 g_static_mutex_init().
668 </para>
669
670 <para>
671 <informalexample>
672 <programlisting>
673 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
674 </programlisting>
675 </informalexample>
676 </para>
677
678
679
680 <!-- ##### FUNCTION g_static_mutex_init ##### -->
681 <para>
682 Initializes @mutex. Alternatively you can initialize it with
683 #G_STATIC_MUTEX_INIT.
684 </para>
685
686 @mutex: a #GStaticMutex to be initialized
687
688
689 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
690 <para>
691 Works like g_mutex_lock(), but for a #GStaticMutex.
692 </para>
693
694 @mutex: a #GStaticMutex
695
696
697 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
698
699 <para>
700 Works like g_mutex_trylock(), but for a #GStaticMutex.
701 </para>
702
703 @mutex: a #GStaticMutex
704 @Returns: TRUE, if the #GStaticMutex could be locked
705
706
707 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
708
709 <para>
710 Works like g_mutex_unlock(), but for a #GStaticMutex.
711 </para>
712
713 @mutex: a #GStaticMutex
714
715
716 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
717
718 <para>
719 For some operations (like g_cond_wait()) you must have a #GMutex
720 instead of a #GStaticMutex. This function will return the
721 corresponding #GMutex for @mutex.
722 </para>
723
724 @mutex: a #GStaticMutex
725 @Returns: the #GMutex corresponding to @mutex
726
727
728 <!-- ##### FUNCTION g_static_mutex_free ##### -->
729 <para>
730 Releases all resources allocated to @mutex. 
731 </para>
732
733 <para>
734 You don't have to call this functions for a #GStaticMutex with an
735 unbounded lifetime, i.e. objects declared 'static', but if you have a
736 #GStaticMutex as a member of a structure and the structure is freed,
737 you should also free the #GStaticMutex.
738 </para>
739
740 @mutex: a #GStaticMutex to be freed
741
742
743 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
744
745 <para>
746 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
747 with the advantage that they will expand to nothing in programs
748 compiled against a thread-disabled GLib, saving code and memory
749 there. #G_LOCK_DEFINE defines a lock. It can appear, where variable
750 definitions may appear in programs, i.e. in the first block of a
751 function or outside of functions. The @name parameter will be mangled
752 to get the name of the #GStaticMutex. This means, that you can use
753 names of existing variables as the parameter, e.g. the name of the
754 variable you intent to protect with the lock. Look at our
755 give_me_next_number() example using the G_LOCK_* macros:
756 </para>
757
758 <para>
759 <example>
760 <title>Using the G_LOCK_* convenience macros</title>
761 <programlisting>
762 G_LOCK_DEFINE (current_number);
763
764 int give_me_next_number ()
765   {
766     static int current_number = 0;
767     int ret_val;
768
769     G_LOCK (current_number);
770     ret_val = current_number = calc_next_number (current_number); 
771     G_UNLOCK (current_number);
772     return ret_val;
773   }
774 </programlisting>
775 </example>
776 </para>
777
778 @name: the name of the lock
779
780
781 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
782
783 <para>
784 This works like #G_LOCK_DEFINE, but it creates a static object.
785 </para>
786
787 @name: the name of the lock
788
789
790 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
791
792 <para>
793 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
794 </para>
795
796 @name: the name of the lock
797
798
799 <!-- ##### MACRO G_LOCK ##### -->
800
801 <para>
802 Works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
803 </para>
804
805 @name: the name of the lock
806
807
808 <!-- ##### MACRO G_TRYLOCK ##### -->
809
810 <para>
811 Works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
812 </para>
813
814 @name: the name of the lock
815 @Returns: TRUE, if the lock could be locked
816
817
818 <!-- ##### MACRO G_UNLOCK ##### -->
819
820 <para>
821 Works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
822 </para>
823
824 @name: the name of the lock
825
826
827 <!-- ##### STRUCT GStaticRecMutex ##### -->
828 <para>
829 A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
830 multiple times by one thread. If you enter it n times, however, you
831 have to unlock it n times again to let other threads lock it. An
832 exception is the function g_static_rec_mutex_unlock_full(), that
833 allows you to unlock a #GStaticRecMutex completely returning the depth,
834 i.e. the number of times this mutex was locked. The depth can later be
835 used to restore the state by calling g_static_rec_mutex_lock_full().
836 </para>
837
838 <para>
839 Even though #GStaticRecMutex is not opaque, it should only be used with
840 the following functions.
841 </para>
842
843 <para>
844 All of the g_static_rec_mutex_* functions can also be used, if
845 g_thread_init() has not been called.
846 </para>
847
848 @mutex: 
849 @depth: 
850 @owner: 
851
852 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
853 <para>
854 A #GStaticRecMutex must be initialized with this macro, before it can
855 be used. This macro can used be to initialize a variable, but it
856 cannot be assigned to a variable. In that case you have to use
857 g_static_rec_mutex_init().
858 </para>
859
860 <para>
861 <informalexample>
862 <programlisting>
863 GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
864 </programlisting>
865 </informalexample>
866 </para>
867
868
869
870 <!-- ##### FUNCTION g_static_rec_mutex_init ##### -->
871 <para>
872 A #GStaticRecMutex must be initialized with this function, before it
873 can be used. Alternatively you can initialize it with
874 #G_STATIC_REC_MUTEX_INIT.
875 </para>
876
877 @mutex: a #GStaticRecMutex to be initialized
878
879
880 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
881 <para>
882 Locks @mutex. If @mutex is already locked by another thread, the
883 current thread will block until @mutex is unlocked by the other
884 thread. If @mutex is already locked by the calling thread, this
885 functions increases the depth of @mutex and returns immediately.
886 </para>
887
888 @mutex: a #GStaticRecMutex to lock
889
890
891 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
892 <para>
893 Tries to lock @mutex. If @mutex is already locked by another thread,
894 it immediately returns #FALSE. Otherwise it locks @mutex and returns
895 #TRUE. If @mutex is already locked by the calling thread, this
896 functions increases the depth of @mutex and immediately  returns #TRUE.
897 </para>
898
899 @mutex: a #GStaticRecMutex to lock
900 @Returns: TRUE, if @mutex could be locked
901
902
903 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
904 <para>
905 Unlocks @mutex. Another threads can, however, only lock @mutex when it
906 has been unlocked as many times, as it had been locked before. If
907 @mutex is completely unlocked and another thread is blocked in a
908 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
909 lock @mutex itself.
910 </para>
911
912 @mutex: a #GStaticRecMutex to unlock
913
914
915 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
916 <para>
917 Works like calling g_static_rec_mutex_lock() for @mutex n times.
918 </para>
919
920 @mutex: a #GStaticRecMutex to lock
921 @depth: number of times this mutex has to be unlocked to be completely unlocked
922
923
924 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
925 <para>
926 Completely unlocks @mutex. If another thread is blocked in a
927 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
928 lock @mutex itself. This function returns the number of times, that
929 @mutex has been locked by the current thread. To restore the state
930 before the call to g_static_rec_mutex_unlock_full() you can call
931 g_static_rec_mutex_lock_full() with the depth returned by this
932 function.
933 </para>
934
935 @mutex: a #GStaticRecMutex to completely unlock
936 @Returns: number of times @mutex has been locked by the current thread
937
938
939 <!-- ##### FUNCTION g_static_rec_mutex_free ##### -->
940 <para>
941 Releases all resources allocated to a #GStaticRecMutex.
942 </para>
943
944 <para>
945 You don't have to call this functions for a #GStaticRecMutex with an
946 unbounded lifetime, i.e. objects declared 'static', but if you have a
947 #GStaticRecMutex as a member of a structure and the structure is
948 freed, you should also free the #GStaticRecMutex.
949 </para>
950
951 @mutex: a #GStaticRecMutex to be freed
952
953
954 <!-- ##### STRUCT GStaticRWLock ##### -->
955 <para>
956 The #GStaticRWLock struct represents a read-write lock. A read-write
957 lock can be used for protecting data, that some portions of code only
958 read from, while others also write. In such situations it is
959 desirable, that several readers can read at once, whereas of course
960 only one writer may write at a time. Take a look at the following
961 example:
962
963 <example>
964 <title>An array with access functions</title>
965 <programlisting>
966   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
967
968   GPtrArray *array;
969
970   gpointer my_array_get (guint index)
971   {
972     gpointer retval = NULL;
973
974     if (!array)
975       return NULL;
976
977     g_static_rw_lock_reader_lock (&amp;rwlock);
978
979     if (index < array->len)
980       retval = g_ptr_array_index (array, index);
981
982     g_static_rw_lock_reader_unlock (&amp;rwlock);
983
984     return retval;
985   }
986
987   void my_array_set (guint index, gpointer data)
988   {
989     g_static_rw_lock_writer_lock (&amp;rwlock);
990
991     if (!array)
992       array = g_ptr_array_new ();
993
994     if (index >= array->len)
995       g_ptr_array_set_size (array, index+1);
996
997     g_ptr_array_index (array, index) = data; 
998
999     g_static_rw_lock_writer_unlock (&amp;rwlock);
1000   }
1001 </programlisting>
1002 </example>
1003 </para>
1004
1005 <para>
1006 This example shows an array, which can be accessed by many readers
1007 (the my_array_get function) simultaneously, whereas the writers (the
1008 my_array_set function) only will be allowed once a time and only if no
1009 readers currently access the array. This is because of the potentially
1010 dangerous resizing of the array. Using that functions is fully
1011 multi-thread safe now. 
1012 </para>
1013
1014 <para>
1015 Most of the time the writers should have precedence of readers. That
1016 means for this implementation, that as soon as a writer wants to lock
1017 the data, no other reader is allowed to lock the data, whereas of
1018 course the readers, that already have locked the data are allowed to
1019 finish their operation. As soon as the last reader unlocks the data,
1020 the writer will lock it.
1021 </para>
1022
1023 <para>
1024 Even though #GStaticRWLock is not opaque, it should only be used with
1025 the following functions.
1026 </para>
1027
1028 <para>
1029 All of the g_static_rw_lock_* functions can also be used, if
1030 g_thread_init() has not been called.
1031 </para>
1032
1033 <note>
1034 <para>
1035 A read-write lock has a higher overhead as a mutex. For example both
1036 g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock()
1037 has to lock and unlock a #GStaticMutex, so it takes at least twice the
1038 time to lock and unlock a #GStaticRWLock than to lock and unlock a
1039 #GStaticMutex. So only data structures, that are accessed by multiple
1040 readers, which keep the lock for a considerable time justify a
1041 #GStaticRWLock. The above example most probably would fare better with
1042 a #GStaticMutex.
1043 </para>
1044 </note>
1045
1046 @mutex: 
1047 @read_cond: 
1048 @write_cond: 
1049 @read_counter: 
1050 @write: 
1051 @want_to_write: 
1052
1053 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
1054 <para>
1055 A #GStaticRWLock must be initialized with this macro, before it can
1056 be used. This macro can used be to initialize a variable, but it
1057 cannot be assigned to a variable. In that case you have to use
1058 g_static_rw_lock_init().
1059 </para>
1060
1061 <para>
1062 <informalexample>
1063 <programlisting>
1064 GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
1065 </programlisting>
1066 </informalexample>
1067 </para>
1068
1069
1070
1071 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
1072 <para>
1073 A #GStaticRWLock must be initialized with this function, before it can
1074 be used. Alternatively you can initialize it with
1075 #G_STATIC_RW_LOCK_INIT.
1076 </para>
1077
1078 @lock: a #GStaticRWLock to be initialized
1079
1080
1081 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
1082 <para>
1083 Locks @lock for reading. There may be unlimited concurrent locks for
1084 reading of a #GStaticRWLock at the same time.  If @lock is already
1085 locked for writing by another thread or if another thread is already
1086 waiting to lock @lock for writing, this function will block until
1087 @lock is unlocked by the other writing thread and no other writing
1088 threads want to lock @lock. This lock has to be unlocked by
1089 g_static_rw_lock_reader_unlock().
1090 </para>
1091
1092 <para>
1093 #GStaticRWLock in general is not recursive, but as there may be
1094 unlimited concurrent locks for reading, it effectively is for
1095 recursive for reading, but for reading only. Locking for writing after
1096 locking for reading will deadlock, the same holds true for the
1097 opposite order.
1098 </para>
1099
1100 @lock: a #GStaticRWLock to lock for reading
1101
1102
1103 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
1104 <para>
1105 Tries to lock @lock for reading. If @lock is already locked for
1106 writing by another thread or if another thread is already waiting to
1107 lock @lock for writing, it immediately returns #FALSE. Otherwise it
1108 locks @lock for reading and returns TRUE. This lock has to be unlocked
1109 by g_static_rw_lock_reader_unlock().
1110 </para>
1111
1112 @lock: a #GStaticRWLock to lock for reading
1113 @Returns: TRUE, if @lock could be locked for reading
1114
1115
1116 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
1117 <para>
1118 Unlocks @lock. If a thread waits to lock @lock for writing and all
1119 locks for reading have been unlocked, the waiting thread is woken up
1120 and can lock @lock for writing.
1121 </para>
1122
1123 @lock: a #GStaticRWLock to unlock after reading
1124
1125
1126 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
1127 <para>
1128 Locks @lock for writing. If @lock is already locked for writing or
1129 reading by other threads, this function will block until @lock is
1130 completely unlocked and then lock @lock for writing. While this
1131 functions waits to lock @lock, no other thread can lock @lock for
1132 reading. When @lock is locked for writing, no other thread can lock
1133 @lock (neither for reading nor writing). This lock has to be unlocked
1134 by g_static_rw_lock_writer_unlock().
1135 </para>
1136
1137 @lock: a #GStaticRWLock to lock for writing
1138
1139
1140 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
1141 <para>
1142 Tries to lock @lock for writing. If @lock is already locked (for
1143 either reading or writing) by another thread, it immediately returns
1144 #FALSE. Otherwise it locks @lock for writing and returns TRUE. This
1145 lock has to be unlocked by g_static_rw_lock_writer_unlock().
1146 </para>
1147
1148 @lock: a #GStaticRWLock to lock for writing
1149 @Returns: TRUE, if @lock could be locked for writing
1150
1151
1152 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
1153 <para>
1154 Unlocks @lock. If a thread waits to lock @lock for writing and all
1155 locks for reading have been unlocked, the waiting thread is woken up
1156 and can lock @lock for writing. If no thread waits to lock @lock for
1157 writing and threads wait to lock @lock for reading, the waiting
1158 threads are woken up and can lock @lock for reading.
1159 </para>
1160
1161 @lock: a #GStaticRWLock to unlock after writing
1162
1163
1164 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
1165 <para>
1166 Releases all resources allocated to @lock. 
1167 </para>
1168
1169 <para>
1170 You don't have to call this functions for a #GStaticRWLock with an
1171 unbounded lifetime, i.e. objects declared 'static', but if you have a
1172 #GStaticRWLock as a member of a structure and the structure is freed,
1173 you should also free the #GStaticRWLock.
1174 </para>
1175
1176 @lock: a #GStaticRWLock to be freed
1177
1178
1179 <!-- ##### STRUCT GCond ##### -->
1180
1181 <para>
1182 The #GCond struct is an opaque data structure to represent a
1183 condition. A #GCond is an object, that threads can block on, if they
1184 find a certain condition to be false. If other threads change the
1185 state of this condition they can signal the #GCond, such that the
1186 waiting thread is woken up. 
1187 </para>
1188
1189 <para>
1190 <example>
1191 <title>Using GCond to block a thread until a condition is satisfied</title>
1192 <programlisting>
1193 GCond* data_cond = NULL;   /* Must be initialized somewhere */
1194 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1195 gpointer current_data = NULL;
1196
1197 void push_data (gpointer data)
1198 {
1199   g_mutex_lock (data_mutex);
1200   current_data = data;
1201   g_cond_signal (data_cond);
1202   g_mutex_unlock (data_mutex);
1203 }
1204
1205 gpointer pop_data ()
1206 {
1207   gpointer data;
1208
1209   g_mutex_lock (data_mutex);
1210   while (!current_data)
1211       g_cond_wait (data_cond, data_mutex);
1212   data = current_data;
1213   current_data = NULL;
1214   g_mutex_unlock (data_mutex);
1215   return data;
1216 }
1217 </programlisting>
1218 </example>
1219 </para>
1220
1221 <para>
1222 Whenever a thread calls pop_data() now, it will wait until
1223 current_data is non-NULL, i.e. until some other thread has called
1224 push_data().
1225 </para>
1226
1227 <note>
1228 <para>
1229 It is important to use the g_cond_wait() and g_cond_timed_wait()
1230 functions only inside a loop, which checks for the condition to be
1231 true as it is not guaranteed that the waiting thread will find it
1232 fulfilled, even if the signaling thread left the condition
1233 in that state. This is because another thread can have altered the
1234 condition, before the waiting thread got the chance to be woken up,
1235 even if the condition itself is protected by a #GMutex, like above.
1236 </para>
1237 </note>
1238
1239 <para>
1240 A #GCond should only be accessed via the following functions.
1241 </para>
1242
1243 <note>
1244 <para>
1245 All of the g_cond_* functions are actually macros. Apart from taking
1246 the addresses of them, you can however use them as if they were functions.
1247 </para>
1248 </note>
1249
1250
1251 <!-- ##### FUNCTION g_cond_new ##### -->
1252
1253 <para>
1254 Creates a new #GCond. This function will abort, if g_thread_init()
1255 has not been called yet.
1256 </para>
1257
1258 @Returns: a new #GCond
1259
1260
1261 <!-- ##### FUNCTION g_cond_signal ##### -->
1262 <para>
1263 If threads are waiting for @cond, exactly one of them is woken up. It
1264 is good practice to hold the same lock as the waiting thread, while
1265 calling this function, though not required.
1266 </para>
1267
1268 <para>
1269 This function can also be used, if g_thread_init() has
1270 not yet been called and will do nothing then.
1271 </para>
1272
1273 @cond: a #GCond
1274
1275
1276 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1277
1278 <para>
1279 If threads are waiting for @cond, all of them are woken up. It is good
1280 practice to lock the same mutex as the waiting threads, while calling
1281 this function, though not required.
1282 </para>
1283
1284 <para>
1285 This function can also be used, if g_thread_init() has
1286 not yet been called and will do nothing then.
1287 </para>
1288
1289 @cond: a #GCond
1290
1291
1292 <!-- ##### FUNCTION g_cond_wait ##### -->
1293
1294 <para>
1295 Waits until this thread is woken up on @cond. The @mutex is unlocked
1296 before falling asleep and locked again before resuming.
1297 </para>
1298
1299 <para>
1300 This function can also be used, if g_thread_init() has not yet been
1301 called and will immediately return then.
1302 </para>
1303
1304 @cond: a #GCond
1305 @mutex: a #GMutex, that is currently locked
1306
1307
1308 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1309
1310 <para>
1311 Waits until this thread is woken up on @cond, but not longer than
1312 until the time, that is specified by @abs_time. The @mutex is
1313 unlocked before falling asleep and locked again before resuming.
1314 </para>
1315
1316 <para>
1317 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
1318 </para>
1319
1320 <para>
1321 This function can also be used, if g_thread_init() has not yet been
1322 called and will immediately return TRUE then.
1323 </para>
1324
1325 @cond: a #GCond
1326 @mutex: a #GMutex, that is currently locked
1327 @abs_time: a #GTimeVal, determining the final time
1328 @Returns: TRUE, if the thread is woken up in time
1329
1330
1331 <!-- ##### FUNCTION g_cond_free ##### -->
1332
1333 <para>
1334 Destroys the #GCond.
1335 </para>
1336
1337 @cond: a #GCond
1338
1339
1340 <!-- ##### STRUCT GPrivate ##### -->
1341 <para>
1342 The #GPrivate struct is an opaque data structure to represent a thread
1343 private data key. Threads can thereby obtain and set a pointer, which
1344 is private to the current thread. Take our give_me_next_number()
1345 example from above.  Now we don't want current_number to be shared
1346 between the threads, but to be private to each thread. This can be
1347 done as follows:
1348
1349 <example>
1350 <title>Using GPrivate for per-thread data</title>
1351 <programlisting>
1352   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1353                                        /* with g_private_new (g_free); */
1354
1355   int give_me_next_number ()
1356   {
1357     int *current_number = g_private_get (current_number_key);
1358
1359     if (!current_number)
1360     {
1361       current_number = g_new (int,1);
1362       *current_number = 0;
1363       g_private_set (current_number_key, current_number);
1364     }
1365     *current_number = calc_next_number (*current_number); 
1366     return *current_number;
1367   }
1368 </programlisting>
1369 </example>
1370 </para>
1371
1372 <para>
1373 Here the pointer belonging to the key current_number_key is read. If
1374 it is NULL, it has not been set yet. Then get memory for an integer
1375 value, assign this memory to the pointer and write the pointer
1376 back. Now we have an integer value, that is private to the current
1377 thread.
1378 </para>
1379
1380 <para>
1381 The #GPrivate struct should only be accessed via the following functions.
1382 </para>
1383
1384 <note>
1385 <para>
1386 All of the g_private_* functions are actually macros. Apart from taking
1387 the addresses of them, you can however use them as if they were functions.
1388 </para>
1389 </note>
1390
1391
1392 <!-- ##### FUNCTION g_private_new ##### -->
1393
1394 <para>
1395 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1396 to a destructor function. Whenever a thread ends and the corresponding
1397 pointer keyed to this instance of #GPrivate is non-NULL, the
1398 destructor is called with this pointer as the argument.
1399 </para>
1400
1401 <note>
1402 <para>
1403 @destructor is working quite differently from @notify in
1404 g_static_private_set().
1405 </para>
1406 </note>
1407
1408 <note>
1409 <para>
1410 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1411 shortage or use #GStaticPrivate.
1412 </para>
1413 </note>
1414
1415 <note>
1416 <para>
1417 This function will abort, if g_thread_init() has not been called yet.
1418 </para>
1419 </note>
1420
1421 @destructor: a function to handle the data keyed to #GPrivate, when a
1422 thread ends
1423 @Returns: a new #GPrivate
1424
1425
1426 <!-- ##### FUNCTION g_private_get ##### -->
1427
1428 <para>
1429 Returns the pointer keyed to @private_key for the current thread. This
1430 pointer is NULL, when g_private_set() hasn't been called for the
1431 current @private_key and thread yet.
1432 </para>
1433
1434 <para>
1435 This function can also be used, if g_thread_init() has not yet been
1436 called and will return the value of @private_key casted to #gpointer then.
1437 </para>
1438
1439 @private_key: a #GPrivate
1440 @Returns: the corresponding pointer
1441
1442
1443 <!-- ##### FUNCTION g_private_set ##### -->
1444
1445 <para>
1446 Sets the pointer keyed to @private_key for the current thread.
1447 </para>
1448
1449 <para>
1450 This function can also be used, if g_thread_init() has not yet been
1451 called and will set @private_key to @data casted to #GPrivate* then.
1452 </para>
1453
1454 @private_key: a #GPrivate
1455 @data: the new pointer
1456
1457
1458 <!-- ##### STRUCT GStaticPrivate ##### -->
1459
1460 <para>
1461 A #GStaticPrivate works almost like a #GPrivate, but it has one
1462 significant advantage. It doesn't need to be created at run-time like
1463 a #GPrivate, but can be defined at compile-time. This is similar to
1464 the difference between #GMutex and #GStaticMutex. Now look at our
1465 give_me_next_number() example with #GStaticPrivate:
1466 </para>
1467
1468 <para>
1469 <example>
1470 <title>Using GStaticPrivate for per-thread data</title>
1471 <programlisting>
1472   int give_me_next_number ()
1473   {
1474     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1475     int *current_number = g_static_private_get (&amp;current_number_key);
1476
1477     if (!current_number)
1478     {
1479       current_number = g_new (int,1);
1480       *current_number = 0;
1481       g_static_private_set (&amp;current_number_key, current_number, g_free);
1482     }
1483     *current_number = calc_next_number (*current_number); 
1484     return *current_number;
1485   }
1486 </programlisting>
1487 </example>
1488 </para>
1489
1490 @index: 
1491
1492 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1493 <para>
1494 Every #GStaticPrivate must be initialized with this macro, before it can
1495 be used.
1496 </para>
1497
1498 <para>
1499 <informalexample>
1500 <programlisting>
1501 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1502 </programlisting>
1503 </informalexample>
1504 </para>
1505
1506
1507
1508 <!-- ##### FUNCTION g_static_private_init ##### -->
1509 <para>
1510 Initializes @private_key. Alternatively you can initialize it with
1511 #G_STATIC_PRIVATE_INIT.
1512 </para>
1513
1514 @private_key: a #GStaticPrivate to be initialized
1515
1516
1517 <!-- ##### FUNCTION g_static_private_get ##### -->
1518 <para>
1519 Works like g_private_get() only for a #GStaticPrivate.
1520 </para>
1521
1522 <para>
1523 This function also works, if g_thread_init() has not yet been called.
1524 </para>
1525
1526 @private_key: a #GStaticPrivate
1527 @Returns: the corresponding pointer
1528
1529
1530 <!-- ##### FUNCTION g_static_private_set ##### -->
1531 <para>
1532 Sets the pointer keyed to @private_key for the current thread and the
1533 function @notify to be called with that pointer (NULL or non-NULL),
1534 whenever the pointer is set again or whenever the current thread ends.
1535 </para>
1536
1537 <para>
1538 This function also works, if g_thread_init() has not yet been
1539 called. If g_thread_init() is called later, the @data keyed to
1540 @private_key will be inherited only by the main thread, i.e. the one that
1541 called g_thread_init().
1542 </para>
1543
1544 <note>
1545 <para>
1546 @notify is working quite differently from @destructor in
1547 g_private_new().
1548 </para>
1549 </note>
1550
1551 @private_key: a #GStaticPrivate
1552 @data: the new pointer
1553 @notify: a function to be called with the pointer, whenever the
1554 current thread ends or sets this pointer again
1555
1556
1557 <!-- ##### FUNCTION g_static_private_free ##### -->
1558 <para>
1559 Releases all resources allocated to @private_key. 
1560 </para>
1561
1562 <para>
1563 You don't have to call this functions for a #GStaticPrivate with an
1564 unbounded lifetime, i.e. objects declared 'static', but if you have a
1565 #GStaticPrivate as a member of a structure and the structure is freed,
1566 you should also free the #GStaticPrivate.
1567 </para>
1568
1569 @private_key: a #GStaticPrivate to be freed
1570
1571