Change the order to match the order in g_thread_create().
[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 multithreaded 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 multithread
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 multithread 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 callback 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
228
229 <!-- ##### ENUM GThreadPriority ##### -->
230 <para>
231 Specifies the priority of a thread. 
232 </para>
233
234 <note>
235 <para>
236 It is not guaranteed, that threads with different priorities really
237 behave accordingly. On some systems (e.g. Linux) only root can increase
238 priorities. On other systems (e.g. Solaris) there doesn't seem to be
239 different scheduling for different priorities. All in all try to avoid
240 being dependent on priorities.
241 </para>
242 </note>
243
244 @G_THREAD_PRIORITY_LOW: a priority lower than normal
245 @G_THREAD_PRIORITY_NORMAL: the default priority
246 @G_THREAD_PRIORITY_HIGH: a priority higher than normal
247 @G_THREAD_PRIORITY_URGENT: the highest priority
248
249 <!-- ##### STRUCT GThread ##### -->
250 <para>
251 The #Gthread struct represents a running thread. It has three public
252 members, but the underlying struct is bigger, so you must not copy
253 this struct. You also must not write that information.
254 </para>
255
256 <note>
257 <para>
258 Resources for a joinable thread are not fully released until
259 g_thread_join() is called for that thread.
260 </para>
261 </note>
262
263 @joinable: is this thread joinable?
264 @bound: is this thread bound to a system thread?
265 @priority: the priority of the thread
266
267 <!-- ##### FUNCTION g_thread_create ##### -->
268 <para>
269 This function creates a new thread with the priority @priority. The
270 stack gets the size @stack_size or the default value for the current
271 platform, if @stack_size is 0.
272 </para>
273
274 <para>
275 If @joinable is #TRUE, you can wait for this threads termination
276 calling g_thread_wait(). Otherwise the thread will just disappear, when
277 ready. If @bound is #TRUE, this thread will be scheduled in the system
278 scope, otherwise the implementation is free to do scheduling in the
279 process scope. The first variant is more expensive resource-wise, but
280 generally faster. On some systems (e.g. Linux) all threads are bound.
281 </para>
282
283 <para>
284 The new thread executes the function @thread_func with the argument
285 @arg. If the thread was created successfully, it is returned.
286 </para>
287
288 <para>
289 @error can be NULL to ignore errors, or non-NULL to report errors. The
290 error is set, if and only if the function returns #NULL.
291 </para>
292
293 <note>
294 <para>
295 It is not guaranteed, that threads with different priorities really
296 behave accordingly. On some systems (e.g. Linux) only root can increase
297 priorities. On other systems (e.g. Solaris) there doesn't seem to be
298 different scheduling for different priorities. All in all try to avoid
299 being dependent on priorities. Use %G_THREAD_PRIORITY_NORMAL here as a
300 default.
301 </para>
302 </note>
303
304 @thread_func: a function to execute in the new thread
305 @arg: an argument to supply to the new thread
306 @stack_size: a stack size for the new thread
307 @joinable: should this thread be joinable?
308 @bound: should this thread be bound to a system thread?
309 @priority: a priority for the thread
310 @error: return location for error.
311 @Returns: the new #GThread on success
312
313
314 <!-- ##### FUNCTION g_thread_self ##### -->
315 <para>
316 This functions returns the #GThread corresponding to the calling thread.
317 </para>
318
319 @Returns: the current thread
320
321
322 <!-- ##### FUNCTION g_thread_join ##### -->
323 <para>
324 Waits until @thread finishes, i.e. the function @thread_func, as given
325 to g_thread_create, returns or g_thread_exit() is called by
326 @thread. All resources of @thread including the #GThread struct are
327 released. @thread must have been created with @joinable=#TRUE in
328 g_thread_create().
329 </para>
330
331 @thread: a #GThread to be waited for
332
333
334 <!-- ##### FUNCTION g_thread_set_priority ##### -->
335 <para>
336 Change the priority of @thread to @priority.
337 </para>
338
339 <note>
340 <para>
341 It is not guaranteed, that threads with different priorities really
342 behave accordingly. On some systems (e.g. Linux) only root can increase
343 priorities. On other systems (e.g. Solaris) there doesn't seem to be
344 different scheduling for different priorities. All in all try to avoid
345 being dependent on priorities.
346 </para>
347 </note>
348
349 @thread: a #GThread
350 @priority: a new priority for @thread
351
352
353 <!-- ##### FUNCTION g_thread_yield ##### -->
354 <para>
355 Give way to other threads waiting to be scheduled. 
356 </para>
357
358 <para>
359 This function is often used as a method to make busy wait less
360 evil. But in most cases, you will encounter, there are better methods
361 to do that. So in general you shouldn't use that function.
362 </para>
363
364
365
366 <!-- ##### FUNCTION g_thread_exit ##### -->
367 <para>
368 Exit the current thread. If another thread is waiting for that thread
369 using g_thread_join(), that thread will be woken up. 
370 </para>
371
372 <note>
373 <para>
374 Never call g_thread_exit from within a thread of a #GThreadPool, as
375 that will mess up the bookkeeping and lead to funny and unwanted
376 results.
377 </para>
378 </note>
379
380
381
382 <!-- ##### STRUCT GMutex ##### -->
383
384 <para>
385 The #GMutex struct is an opaque data structure to represent a mutex
386 (mutual exclusion). It can be used to protect data against shared
387 access. Take for example the following function:
388
389 <example>
390 <title>A function which will not work in a threaded environment</title>
391 <programlisting>
392   int give_me_next_number ()
393   {
394     static int current_number = 0;
395
396     /* now do a very complicated calculation to calculate the new number,
397        this might for example be a random number generator */
398     current_number = calc_next_number (current_number); 
399     return current_number;
400   }
401 </programlisting>
402 </example>
403 </para>
404
405 <para>
406 It is easy to see, that this won't work in a multithreaded
407 application. There current_number must be protected against shared
408 access. A first naive implementation would be:
409 </para>
410
411 <para>
412 <example>
413 <title>The wrong way to write a thread-safe function</title>
414 <programlisting>
415   int give_me_next_number ()
416   {
417     static int current_number = 0;
418     int ret_val;
419     static GMutex * mutex = NULL;
420
421     if (!mutex)
422       mutex = g_mutex_new ();
423     g_mutex_lock (mutex);
424     ret_val = current_number = calc_next_number (current_number); 
425     g_mutex_unlock (mutex);
426     return ret_val;
427   }
428 </programlisting>
429 </example>
430 </para>
431
432 <para>
433 This looks like it would work, but there is a race condition while
434 constructing the mutex and this code cannot work reliable. So please do
435 not use such constructs in your own programs. One working solution is:
436 </para>
437
438 <para>
439 <example>
440 <title>A correct thread-safe function</title>
441 <programlisting>
442   static GMutex *give_me_next_number_mutex = NULL;
443
444   /* this function must be called before any call to give_me_next_number ()
445      it must be called exactly once. */
446   void init_give_me_next_number () 
447   {
448     g_assert (give_me_next_number_mutex == NULL);
449     give_me_next_number_mutex = g_mutex_new ();
450   }
451
452   int give_me_next_number ()
453   {
454     static int current_number = 0;
455     int ret_val;
456
457     g_mutex_lock (give_me_next_number_mutex);
458     ret_val = current_number = calc_next_number (current_number); 
459     g_mutex_unlock (give_me_next_number_mutex);
460     return ret_val;
461   }
462 </programlisting>
463 </example>
464 </para>
465
466 <para>
467 #GStaticMutex provides a simpler and safer way of doing this.
468 </para>
469
470 <para>
471 If you want to use a mutex, but your code should also work without
472 calling g_thread_init() first, you can not use a #GMutex, as
473 g_mutex_new() requires that. Use a #GStaticMutex instead.
474 </para>
475
476 <para>
477 A #GMutex should only be accessed via the following functions.
478 </para>
479
480 <note>
481 <para>
482 All of the g_mutex_* functions are actually macros. Apart from taking
483 the addresses of them, you can however use them as if they were functions.
484 </para>
485 </note>
486
487
488 <!-- ##### FUNCTION g_mutex_new ##### -->
489
490 <para>
491 Creates a new #GMutex. 
492 </para>
493
494 <note>
495 <para>
496 This function will abort, if g_thread_init() has not been called yet.
497 </para>
498 </note>
499
500 @Returns: a new #GMutex
501
502
503 <!-- ##### FUNCTION g_mutex_lock ##### -->
504
505 <para>
506 Locks @mutex. If @mutex is already locked by another thread, the
507 current thread will block until @mutex is unlocked by the other
508 thread.
509 </para>
510
511 <para>
512 This function can also be used, if g_thread_init() has not yet been
513 called and will do nothing then.
514 </para>
515
516 <note>
517 <para>
518 #GMutex is not recursive, i.e. a thread will deadlock, if it already
519 has locked the #GMutex while calling g_mutex_lock(). Use
520 #GStaticRecMutex instead, if you need recursive mutexes.
521 </para>
522 </note>
523
524 @mutex: a #GMutex
525
526
527 <!-- ##### FUNCTION g_mutex_trylock ##### -->
528
529 <para>
530 Tries to lock @mutex. If @mutex is already locked by another
531 thread, it immediately returns FALSE. Otherwise it locks @mutex
532 and returns TRUE.
533 </para>
534
535 <para>
536 This function can also be used, if g_thread_init() has not yet been
537 called and will immediately return TRUE then.
538 </para>
539
540 @mutex: a #GMutex
541 @Returns: TRUE, if @mutex could be locked
542
543
544 <!-- ##### FUNCTION g_mutex_unlock ##### -->
545
546 <para>
547 Unlocks @mutex. If another thread is blocked in a g_mutex_lock() call
548 for @mutex, it will be woken and can lock @mutex itself.
549 </para>
550
551 <para>
552 This function can also be used, if g_thread_init() has not yet been
553 called and will do nothing then.
554 </para>
555
556 @mutex: a #GMutex
557
558
559 <!-- ##### FUNCTION g_mutex_free ##### -->
560
561 <para>
562 Destroys @mutex.
563 </para>
564
565 @mutex: a #GMutex
566
567
568 <!-- ##### STRUCT GStaticMutex ##### -->
569
570 <para>
571 A #GStaticMutex works like a #GMutex, but it has one significant
572 advantage. It doesn't need to be created at run-time like a #GMutex,
573 but can be defined at compile-time. Here is a shorter, easier and
574 safer version of our give_me_next_number() example:
575 </para>
576
577 <para>
578 <example>
579 <title>Using GStaticMutex to simplify thread-safe programming</title>
580 <programlisting>
581   int give_me_next_number ()
582   {
583     static int current_number = 0;
584     int ret_val;
585     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
586
587     g_static_mutex_lock (&amp;mutex);
588     ret_val = current_number = calc_next_number (current_number); 
589     g_static_mutex_unlock (&amp;mutex);
590     return ret_val;
591   }
592 </programlisting>
593 </example>
594 </para>
595
596 <para>
597 Sometimes you would like to dynamically create a mutex. If you don't
598 want to require prior calling to g_thread_init(), because your code
599 should also be usable in non-threaded programs, you are not able to
600 use g_mutex_new() and thus #GMutex, as that requires a prior call to
601 g_thread_init(). In theses cases you can also use a #GStaticMutex. It
602 must be initialized with g_static_mutex_init() before using it and
603 freed with with g_static_mutex_free() when not needed anymore to free
604 up any allocated recourses.
605 </para>
606
607 <para>
608 Even though #GStaticMutex is not opaque, it should only be used with
609 the following functions, as it is defined differently on different
610 platforms.
611 </para>
612
613 <para>
614 All of the g_static_mutex_* functions can also be used, if
615 g_thread_init() has not yet been called.
616 </para>
617
618 <note>
619 <para>
620 All of the g_static_mutex_* functions are actually macros. Apart from
621 taking the addresses of them, you can however use them as if they were
622 functions.
623 </para>
624 </note>
625
626
627 <!-- ##### MACRO G_STATIC_MUTEX_INIT ##### -->
628
629 <para>
630 A #GStaticMutex must be initialized with this macro, before it can be
631 used. This macro can used be to initialize a variable, but it cannot
632 be assigned to a variable. In that case you have to use
633 g_static_mutex_init().
634 </para>
635
636 <para>
637 <informalexample>
638 <programlisting>
639 GStaticMutex my_mutex = G_STATIC_MUTEX_INIT;
640 </programlisting>
641 </informalexample>
642 </para>
643
644
645
646 <!-- ##### FUNCTION g_static_mutex_init ##### -->
647 <para>
648 Initializes @mutex. Alternatively you can initialize it with
649 #G_STATIC_MUTEX_INIT.
650 </para>
651
652 @mutex: a #GStaticMutex to be initialized
653
654
655 <!-- ##### FUNCTION g_static_mutex_lock ##### -->
656 <para>
657 works like g_mutex_lock(), but for a #GStaticMutex.
658 </para>
659
660 @mutex: a #GStaticMutex
661
662
663 <!-- ##### FUNCTION g_static_mutex_trylock ##### -->
664
665 <para>
666 works like g_mutex_trylock(), but for a #GStaticMutex.
667 </para>
668
669 @mutex: a #GStaticMutex
670 @Returns: TRUE, if the #GStaticMutex could be locked
671
672
673 <!-- ##### FUNCTION g_static_mutex_unlock ##### -->
674
675 <para>
676 works like g_mutex_unlock(), but for a #GStaticMutex.
677 </para>
678
679 @mutex: a #GStaticMutex
680
681
682 <!-- ##### FUNCTION g_static_mutex_get_mutex ##### -->
683
684 <para>
685 For some operations (like g_cond_wait()) you must have a #GMutex
686 instead of a #GStaticMutex. This function will return the
687 corresponding #GMutex for @mutex.
688 </para>
689
690 @mutex: a #GStaticMutex
691 @Returns: the #GMutex corresponding to @mutex
692
693
694 <!-- ##### FUNCTION g_static_mutex_free ##### -->
695 <para>
696 Releases all resources allocated to @mutex. 
697 </para>
698
699 <para>
700 You don't have to call this functions for a #GStaticMutex with an
701 unbounded lifetime, i.e. objects declared 'static', but if you have a
702 #GStaticMutex as a member of a structure and the structure is freed,
703 you should also free the #GStaticMutex.
704 </para>
705
706 @mutex: a #GStaticMutex to be freed
707
708
709 <!-- ##### MACRO G_LOCK_DEFINE ##### -->
710
711 <para>
712 The G_LOCK_* macros provide a convenient interface to #GStaticMutex
713 with the advantage that they will expand to nothing in programs
714 compiled against a thread-disabled GLib, saving code and memory
715 there. #G_LOCK_DEFINE defines a lock. It can appear, where variable
716 definitions may appear in programs, i.e. in the first block of a
717 function or outside of functions. The @name parameter will be mangled
718 to get the name of the #GStaticMutex. This means, that you can use
719 names of existing variables as the parameter, e.g. the name of the
720 variable you intent to protect with the lock. Look at our
721 give_me_next_number() example using the G_LOCK_* macros:
722 </para>
723
724 <para>
725 <example>
726 <title>Using the G_LOCK_* convenience macros</title>
727 <programlisting>
728 G_LOCK_DEFINE (current_number);
729
730 int give_me_next_number ()
731   {
732     static int current_number = 0;
733     int ret_val;
734
735     G_LOCK (current_number);
736     ret_val = current_number = calc_next_number (current_number); 
737     G_UNLOCK (current_number);
738     return ret_val;
739   }
740 </programlisting>
741 </example>
742 </para>
743
744 @name: the name of the lock
745
746
747 <!-- ##### MACRO G_LOCK_DEFINE_STATIC ##### -->
748
749 <para>
750 This works like #G_LOCK_DEFINE, but it creates a static object.
751 </para>
752
753 @name: the name of the lock
754
755
756 <!-- ##### MACRO G_LOCK_EXTERN ##### -->
757
758 <para>
759 This declares a lock, that is defined with #G_LOCK_DEFINE in another module.
760 </para>
761
762 @name: the name of the lock
763
764
765 <!-- ##### MACRO G_LOCK ##### -->
766
767 <para>
768 works like g_mutex_lock(), but for a lock defined with #G_LOCK_DEFINE.
769 </para>
770
771 @name: the name of the lock
772
773
774 <!-- ##### MACRO G_TRYLOCK ##### -->
775
776 <para>
777 works like g_mutex_trylock(), but for a lock defined with #G_LOCK_DEFINE.
778 </para>
779
780 @name: the name of the lock
781 @Returns: TRUE, if the lock could be locked
782
783
784 <!-- ##### MACRO G_UNLOCK ##### -->
785
786 <para>
787 works like g_mutex_unlock(), but for a lock defined with #G_LOCK_DEFINE.
788 </para>
789
790 @name: the name of the lock
791
792
793 <!-- ##### STRUCT GStaticRecMutex ##### -->
794 <para>
795 A #GStaticRecMutex works like a #GStaticMutex, but it can be locked
796 multiple times by one thread. If you enter it n times, however, you
797 have to unlock it n times again to let other threads lock it. An
798 exception is the function g_static_rec_mutex_unlock_full(), that
799 allows you to unlock a #GStaticRecMutex completely returning the depth,
800 i.e. the number of times this mutex was locked. The depth can later be
801 used to restore the state by calling g_static_rec_mutex_lock_full().
802 </para>
803
804 <para>
805 Even though #GStaticRecMutex is not opaque, it should only be used with
806 the following functions.
807 </para>
808
809 <para>
810 All of the g_static_rec_mutex_* functions can also be used, if
811 g_thread_init() has not been called.
812 </para>
813
814 @mutex: 
815 @depth: 
816 @owner: 
817
818 <!-- ##### MACRO G_STATIC_REC_MUTEX_INIT ##### -->
819 <para>
820 A #GStaticRecMutex must be initialized with this macro, before it can
821 be used. This macro can used be to initialize a variable, but it
822 cannot be assigned to a variable. In that case you have to use
823 g_static_rec_mutex_init().
824 </para>
825
826 <para>
827 <informalexample>
828 <programlisting>
829 GStaticRecMutex my_mutex = G_STATIC_REC_MUTEX_INIT;
830 </programlisting>
831 </informalexample>
832 </para>
833
834
835
836 <!-- ##### FUNCTION g_static_rec_mutex_init ##### -->
837 <para>
838 A #GStaticRecMutex must be initialized with this function, before it
839 can be used. Alternatively you can initialize it with
840 #G_STATIC_REC_MUTEX_INIT.
841 </para>
842
843 @mutex: a #GStaticRecMutex to be initialized
844
845
846 <!-- ##### FUNCTION g_static_rec_mutex_lock ##### -->
847 <para>
848 Locks @mutex. If @mutex is already locked by another thread, the
849 current thread will block until @mutex is unlocked by the other
850 thread. If @mutex is already locked by the calling thread, this
851 functions increases the depth of @mutex and returns immediately.
852 </para>
853
854 @mutex: a #GStaticRecMutex to lock
855
856
857 <!-- ##### FUNCTION g_static_rec_mutex_trylock ##### -->
858 <para>
859 Tries to lock @mutex. If @mutex is already locked by another thread,
860 it immediately returns #FALSE. Otherwise it locks @mutex and returns
861 #TRUE. If @mutex is already locked by the calling thread, this
862 functions increases the depth of @mutex and immediately  returns #TRUE.
863 </para>
864
865 @mutex: a #GStaticRecMutex to lock
866 @Returns: TRUE, if @mutex could be locked
867
868
869 <!-- ##### FUNCTION g_static_rec_mutex_unlock ##### -->
870 <para>
871 Unlocks @mutex. Another threads can, however, only lock @mutex when it
872 has been unlocked as many times, as it had been locked before. If
873 @mutex is completely unlocked and another thread is blocked in a
874 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
875 lock @mutex itself.
876 </para>
877
878 @mutex: a #GStaticRecMutex to unlock
879
880
881 <!-- ##### FUNCTION g_static_rec_mutex_lock_full ##### -->
882 <para>
883 Works like calling g_static_rec_mutex_lock() for @mutex n times.
884 </para>
885
886 @mutex: a #GStaticRecMutex to lock
887 @depth: number of times this mutex has to be unlocked to be completely unlocked
888
889
890 <!-- ##### FUNCTION g_static_rec_mutex_unlock_full ##### -->
891 <para>
892 Completely unlocks @mutex. If another thread is blocked in a
893 g_static_rec_mutex_lock() call for @mutex, it will be woken and can
894 lock @mutex itself. This function returns the number of times, that
895 @mutex has been locked by the current thread. To restore the state
896 before the call to g_static_rec_mutex_unlock_full() you can call
897 g_static_rec_mutex_lock_full() with the depth returned by this
898 function.
899 </para>
900
901 @mutex: a #GStaticRecMutex to completely unlock
902 @Returns: number of times @mutex has been locked by the current thread
903
904
905 <!-- ##### FUNCTION g_static_rec_mutex_free ##### -->
906 <para>
907 Releases all resources allocated to a #GStaticRecMutex.
908 </para>
909
910 <para>
911 You don't have to call this functions for a #GStaticRecMutex with an
912 unbounded lifetime, i.e. objects declared 'static', but if you have a
913 #GStaticRecMutex as a member of a structure and the structure is
914 freed, you should also free the #GStaticRecMutex.
915 </para>
916
917 @mutex: a #GStaticRecMutex to be freed
918
919
920 <!-- ##### STRUCT GStaticRWLock ##### -->
921 <para>
922
923 </para>
924
925 @mutex: 
926 @read_cond: 
927 @write_cond: 
928 @read_counter: 
929 @write: 
930 @want_to_write: 
931
932 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
933 <para>
934
935 </para>
936
937
938
939 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
940 <para>
941
942 </para>
943
944 @lock: 
945
946
947 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
948 <para>
949
950 </para>
951
952 @lock: 
953
954
955 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
956 <para>
957
958 </para>
959
960 @lock: 
961 @Returns: 
962
963
964 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
965 <para>
966
967 </para>
968
969 @lock: 
970
971
972 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
973 <para>
974
975 </para>
976
977 @lock: 
978
979
980 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
981 <para>
982
983 </para>
984
985 @lock: 
986 @Returns: 
987
988
989 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
990 <para>
991
992 </para>
993
994 @lock: 
995
996
997 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
998 <para>
999
1000 </para>
1001
1002 @lock: 
1003
1004
1005 <!-- ##### STRUCT GCond ##### -->
1006
1007 <para>
1008 The #GCond struct is an opaque data structure to represent a
1009 condition. A #GCond is an object, that threads can block on, if they
1010 find a certain condition to be false. If other threads change the
1011 state of this condition they can signal the #GCond, such that the
1012 waiting thread is woken up. 
1013 </para>
1014
1015 <para>
1016 <example>
1017 <title>Using GCond to block a thread until a condition is satisfied</title>
1018 <programlisting>
1019 GCond* data_cond = NULL;   /* Must be initialized somewhere */
1020 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1021 gpointer current_data = NULL;
1022
1023 void push_data (gpointer data)
1024 {
1025   g_mutex_lock (data_mutex);
1026   current_data = data;
1027   g_cond_signal (data_cond);
1028   g_mutex_unlock (data_mutex);
1029 }
1030
1031 gpointer pop_data ()
1032 {
1033   gpointer data;
1034
1035   g_mutex_lock (data_mutex);
1036   while (!current_data)
1037       g_cond_wait (data_cond, data_mutex);
1038   data = current_data;
1039   current_data = NULL;
1040   g_mutex_unlock (data_mutex);
1041   return data;
1042 }
1043 </programlisting>
1044 </example>
1045 </para>
1046
1047 <para>
1048 Whenever a thread calls pop_data() now, it will wait until
1049 current_data is non-NULL, i.e. until some other thread has called
1050 push_data().
1051 </para>
1052
1053 <note>
1054 <para>
1055 It is important to use the g_cond_wait() and g_cond_timed_wait()
1056 functions only inside a loop, which checks for the condition to be
1057 true as it is not guaranteed that the waiting thread will find it
1058 fulfilled, even if the signaling thread left the condition
1059 in that state. This is because another thread can have altered the
1060 condition, before the waiting thread got the chance to be woken up,
1061 even if the condition itself is protected by a #GMutex, like above.
1062 </para>
1063 </note>
1064
1065 <para>
1066 A #GCond should only be accessed via the following functions.
1067 </para>
1068
1069 <note>
1070 <para>
1071 All of the g_cond_* functions are actually macros. Apart from taking
1072 the addresses of them, you can however use them as if they were functions.
1073 </para>
1074 </note>
1075
1076
1077 <!-- ##### FUNCTION g_cond_new ##### -->
1078
1079 <para>
1080 Creates a new #GCond. This function will abort, if g_thread_init()
1081 has not been called yet.
1082 </para>
1083
1084 @Returns: a new #GCond
1085
1086
1087 <!-- ##### FUNCTION g_cond_signal ##### -->
1088 <para>
1089 If threads are waiting for @cond, exactly one of them is woken up. It
1090 is good practice to hold the same lock as the waiting thread, while
1091 calling this function, though not required.
1092 </para>
1093
1094 <para>
1095 This function can also be used, if g_thread_init() has
1096 not yet been called and will do nothing then.
1097 </para>
1098
1099 @cond: a #GCond
1100
1101
1102 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1103
1104 <para>
1105 If threads are waiting for @cond, all of them are woken up. It is good
1106 practice to lock the same mutex as the waiting threads, while calling
1107 this function, though not required.
1108 </para>
1109
1110 <para>
1111 This function can also be used, if g_thread_init() has
1112 not yet been called and will do nothing then.
1113 </para>
1114
1115 @cond: a #GCond
1116
1117
1118 <!-- ##### FUNCTION g_cond_wait ##### -->
1119
1120 <para>
1121 Waits until this thread is woken up on @cond. The @mutex is unlocked
1122 before falling asleep and locked again before resuming.
1123 </para>
1124
1125 <para>
1126 This function can also be used, if g_thread_init() has not yet been
1127 called and will immediately return then.
1128 </para>
1129
1130 @cond: a #GCond
1131 @mutex: a #GMutex, that is currently locked
1132
1133
1134 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1135
1136 <para>
1137 Waits until this thread is woken up on @cond, but not longer than
1138 until the time, that is specified by @abs_time. The @mutex is
1139 unlocked before falling asleep and locked again before resuming.
1140 </para>
1141
1142 <para>
1143 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
1144 </para>
1145
1146 <para>
1147 This function can also be used, if g_thread_init() has not yet been
1148 called and will immediately return TRUE then.
1149 </para>
1150
1151 @cond: a #GCond
1152 @mutex: a #GMutex, that is currently locked
1153 @abs_time: a #GTimeVal, determining the final time
1154 @Returns: TRUE, if the thread is woken up in time
1155
1156
1157 <!-- ##### FUNCTION g_cond_free ##### -->
1158
1159 <para>
1160 Destroys the #GCond.
1161 </para>
1162
1163 @cond: a #GCond
1164
1165
1166 <!-- ##### STRUCT GPrivate ##### -->
1167 <para>
1168 The #GPrivate struct is an opaque data structure to represent a thread
1169 private data key. Threads can thereby obtain and set a pointer, which
1170 is private to the current thread. Take our give_me_next_number()
1171 example from above.  Now we don't want current_number to be shared
1172 between the threads, but to be private to each thread. This can be
1173 done as follows:
1174
1175 <example>
1176 <title>Using GPrivate for per-thread data</title>
1177 <programlisting>
1178   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1179                                        /* with g_private_new (g_free); */
1180
1181   int give_me_next_number ()
1182   {
1183     int *current_number = g_private_get (current_number_key);
1184
1185     if (!current_number)
1186     {
1187       current_number = g_new (int,1);
1188       *current_number = 0;
1189       g_private_set (current_number_key, current_number);
1190     }
1191     *current_number = calc_next_number (*current_number); 
1192     return *current_number;
1193   }
1194 </programlisting>
1195 </example>
1196 </para>
1197
1198 <para>
1199 Here the pointer belonging to the key current_number_key is read. If
1200 it is NULL, it has not been set yet. Then get memory for an integer
1201 value, assign this memory to the pointer and write the pointer
1202 back. Now we have an integer value, that is private to the current
1203 thread.
1204 </para>
1205
1206 <para>
1207 The #GPrivate struct should only be accessed via the following functions.
1208 </para>
1209
1210 <note>
1211 <para>
1212 All of the g_private_* functions are actually macros. Apart from taking
1213 the addresses of them, you can however use them as if they were functions.
1214 </para>
1215 </note>
1216
1217
1218 <!-- ##### FUNCTION g_private_new ##### -->
1219
1220 <para>
1221 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1222 to a destructor function. Whenever a thread ends and the corresponding
1223 pointer keyed to this instance of #GPrivate is non-NULL, the
1224 destructor is called with this pointer as the argument.
1225 </para>
1226
1227 <note>
1228 <para>
1229 @destructor is working quite differently from @notify in
1230 g_static_private_set().
1231 </para>
1232 </note>
1233
1234 <note>
1235 <para>
1236 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1237 shortage or use #GStaticPrivate.
1238 </para>
1239 </note>
1240
1241 <note>
1242 <para>
1243 This function will abort, if g_thread_init() has not been called yet.
1244 </para>
1245 </note>
1246
1247 @destructor: a function to handle the data keyed to #GPrivate, when a
1248 thread ends
1249 @Returns: a new #GPrivate
1250
1251
1252 <!-- ##### FUNCTION g_private_get ##### -->
1253
1254 <para>
1255 Returns the pointer keyed to @private_key for the current thread. This
1256 pointer is NULL, when g_private_set() hasn't been called for the
1257 current @private_key and thread yet.
1258 </para>
1259
1260 <para>
1261 This function can also be used, if g_thread_init() has not yet been
1262 called and will return the value of @private_key casted to #gpointer then.
1263 </para>
1264
1265 @private_key: a #GPrivate
1266 @Returns: the corresponding pointer
1267
1268
1269 <!-- ##### FUNCTION g_private_set ##### -->
1270
1271 <para>
1272 Sets the pointer keyed to @private_key for the current thread.
1273 </para>
1274
1275 <para>
1276 This function can also be used, if g_thread_init() has not yet been
1277 called and will set @private_key to @data casted to #GPrivate* then.
1278 </para>
1279
1280 @private_key: a #GPrivate
1281 @data: the new pointer
1282
1283
1284 <!-- ##### STRUCT GStaticPrivate ##### -->
1285
1286 <para>
1287 A #GStaticPrivate works almost like a #GPrivate, but it has one
1288 significant advantage. It doesn't need to be created at run-time like
1289 a #GPrivate, but can be defined at compile-time. This is similar to
1290 the difference between #GMutex and #GStaticMutex. Now look at our
1291 give_me_next_number() example with #GStaticPrivate:
1292 </para>
1293
1294 <para>
1295 <example>
1296 <title>Using GStaticPrivate for per-thread data</title>
1297 <programlisting>
1298   int give_me_next_number ()
1299   {
1300     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1301     int *current_number = g_static_private_get (&amp;current_number_key);
1302
1303     if (!current_number)
1304     {
1305       current_number = g_new (int,1);
1306       *current_number = 0;
1307       g_static_private_set (&amp;current_number_key, current_number, g_free);
1308     }
1309     *current_number = calc_next_number (*current_number); 
1310     return *current_number;
1311   }
1312 </programlisting>
1313 </example>
1314 </para>
1315
1316 @index: 
1317
1318 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1319 <para>
1320 Every #GStaticPrivate must be initialized with this macro, before it can
1321 be used.
1322 </para>
1323
1324 <para>
1325 <informalexample>
1326 <programlisting>
1327 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1328 </programlisting>
1329 </informalexample>
1330 </para>
1331
1332
1333
1334 <!-- ##### FUNCTION g_static_private_init ##### -->
1335 <para>
1336 Initializes @private_key. Alternatively you can initialize it with
1337 #G_STATIC_PRIVATE_INIT.
1338 </para>
1339
1340 @private_key: a #GStaticPrivate to be initialized
1341
1342
1343 <!-- ##### FUNCTION g_static_private_get ##### -->
1344 <para>
1345 Works like g_private_get() only for a #GStaticPrivate.
1346 </para>
1347
1348 <para>
1349 This function also works, if g_thread_init() has not yet been called.
1350 </para>
1351
1352 @private_key: a #GStaticPrivate
1353 @Returns: the corresponding pointer
1354
1355
1356 <!-- ##### FUNCTION g_static_private_get_for_thread ##### -->
1357 <para>
1358
1359 </para>
1360
1361 @private_key: 
1362 @thread: 
1363 @Returns: 
1364
1365
1366 <!-- ##### FUNCTION g_static_private_set ##### -->
1367 <para>
1368 Sets the pointer keyed to @private_key for the current thread and the
1369 function @notify to be called with that pointer (NULL or non-NULL),
1370 whenever the pointer is set again or whenever the current thread ends.
1371 </para>
1372
1373 <para>
1374 This function also works, if g_thread_init() has not yet been
1375 called. If g_thread_init() is called later, the @data keyed to
1376 @private_key will be inherited only by the main thread, i.e. the one that
1377 called g_thread_init().
1378 </para>
1379
1380 <note>
1381 <para>
1382 @notify is working quite differently from @destructor in
1383 g_private_new().
1384 </para>
1385 </note>
1386
1387 @private_key: a #GStaticPrivate
1388 @data: the new pointer
1389 @notify: a function to be called with the pointer, whenever the
1390 current thread ends or sets this pointer again
1391
1392
1393 <!-- ##### FUNCTION g_static_private_set_for_thread ##### -->
1394 <para>
1395
1396 </para>
1397
1398 @private_key: 
1399 @thread: 
1400 @data: 
1401 @notify: 
1402
1403
1404 <!-- ##### FUNCTION g_static_private_free ##### -->
1405 <para>
1406 Releases all resources allocated to @private_key. 
1407 </para>
1408
1409 <para>
1410 You don't have to call this functions for a #GStaticPrivate with an
1411 unbounded lifetime, i.e. objects declared 'static', but if you have a
1412 #GStaticPrivate as a member of a structure and the structure is freed,
1413 you should also free the #GStaticPrivate.
1414 </para>
1415
1416 @private_key: a #GStaticPrivate to be freed
1417
1418