Fixed stupid typo.
[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 The #GStaticRWLock struct represents a read-write lock. A read-write
923 lock can be used for protecting data, that some portions of code only
924 read from, while others also write. In such situations it is
925 desirable, that several readers can read at once, whereas of course
926 only one writer may write at a time. Take a look at the followin
927 example:
928
929 <example>
930 <title>An array with access functions</title>
931 <programlisting>
932   GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
933
934   GPtrArray *array;
935
936   gpointer my_array_get (guint index)
937   {
938     gpointer retval = NULL;
939
940     if (!array)
941       return NULL;
942
943     g_static_rw_lock_reader_lock (&amp;rwlock);
944
945     if (index < array->len)
946       retval = g_ptr_array_index (array, index);
947
948     g_static_rw_lock_reader_unlock (&amp;rwlock);
949
950     return retval;
951   }
952
953   void my_array_set (guint index, gpointer data)
954   {
955     g_static_rw_lock_writer_lock (&amp;rwlock);
956
957     if (!array)
958       array = g_ptr_array_new ();
959
960     if (index >= array->len)
961       g_ptr_array_set_size (array, index+1);
962
963     g_ptr_array_index (array, index) = data; 
964
965     g_static_rw_lock_writer_unlock (&amp;rwlock);
966   }
967 </programlisting>
968 </example>
969 </para>
970
971 <para>
972 This example shows an array, which can be accessed by many readers
973 (the my_array_get function) simultaniously, whereas the writers (the
974 my_array_set function) only will be allowed once a time and only if no
975 readers currently access the array. This is because of the potentially
976 dangerous resizing of the array. Using that functions is fully
977 multithread safe now. 
978 </para>
979
980 <para>
981 Most of the time the writers should have precedence of readers. That
982 means for this implementation, that as soon as a writer wants to lock
983 the data, no other reader is allowed to lock the data, whereas of
984 course the readers, that already have locked the data are allowed to
985 finish their operation. As soon as the last reader unlocks the data,
986 the writer will lock it.
987 </para>
988
989 <para>
990 Even though #GStaticRWLoc is not opaque, it should only be used with
991 the following functions.
992 </para>
993
994 <para>
995 All of the g_static_rw_lock_* functions can also be used, if
996 g_thread_init() has not been called.
997 </para>
998
999 <note>
1000 <para>
1001 A read-write lock has a higher overhead as a mutex. For example both
1002 g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock()
1003 has to lock and unlock a #GStaticMutex, so it takes at least twice the
1004 time to lock and unlock a #GStaticRWLock than to lock and unlock a
1005 #GStaticMutex. So only data structures, that are accessed by multiple
1006 readers, which keep the lock for a considerable time justify a
1007 #GStaticRWLock. The above example most probably would fare better with
1008 a #GStaticMutex.
1009 </para>
1010 </note>
1011
1012 @mutex: 
1013 @read_cond: 
1014 @write_cond: 
1015 @read_counter: 
1016 @write: 
1017 @want_to_write: 
1018
1019 <!-- ##### MACRO G_STATIC_RW_LOCK_INIT ##### -->
1020 <para>
1021 A #GStaticRWLock must be initialized with this macro, before it can
1022 be used. This macro can used be to initialize a variable, but it
1023 cannot be assigned to a variable. In that case you have to use
1024 g_static_rw_lock_init().
1025 </para>
1026
1027 <para>
1028 <informalexample>
1029 <programlisting>
1030 GStaticRWLock my_lock = G_STATIC_RW_LOCK_INIT;
1031 </programlisting>
1032 </informalexample>
1033 </para>
1034
1035
1036
1037 <!-- ##### FUNCTION g_static_rw_lock_init ##### -->
1038 <para>
1039 A #GStaticRWLock must be initialized with this function, before it can
1040 be used. Alternatively you can initialize it with
1041 #G_STATIC_RW_LOCK_INIT.
1042 </para>
1043
1044 @lock: a #GStaticRWLock to be initialized
1045
1046
1047 <!-- ##### FUNCTION g_static_rw_lock_reader_lock ##### -->
1048 <para>
1049 Locks @lock for reading. There may be unlimited concurrent locks for
1050 reading of a #GStaticRWLock at the same time.  If @lock is already
1051 locked for writing by another thread or if another thread is already
1052 waiting to lock @lock for writing, this function will block until
1053 @lock is unlocked by the other writing thread and no other writing
1054 threads want to lock @lock. This lock has to be unlocked by
1055 g_static_rw_lock_reader_unlock().
1056 </para>
1057
1058 <para>
1059 #GStaticRWLock in general is not recursive, but as there may be
1060 unlimited concurrent locks for reading, it effectively is for
1061 recursive for reading, but for reading only. Locking for writing after
1062 locking for reading will deadlock, the same holds true for the
1063 opposite order.
1064 </para>
1065
1066 @lock: a #GStaticRWLock to lock for reading
1067
1068
1069 <!-- ##### FUNCTION g_static_rw_lock_reader_trylock ##### -->
1070 <para>
1071 Tries to lock @lock for reading. If @lock is already locked for
1072 writing by another thread or if another thread is already waiting to
1073 lock @lock for writing, it immediately returns #FALSE. Otherwise it
1074 locks @lock for reading and returns TRUE. This lock has to be unlocked
1075 by g_static_rw_lock_reader_unlock().
1076 </para>
1077
1078 @lock: a #GStaticRWLock to lock for reading
1079 @Returns: TRUE, if @lock could be locked for reading
1080
1081
1082 <!-- ##### FUNCTION g_static_rw_lock_reader_unlock ##### -->
1083 <para>
1084 Unlocks @lock. If a thread waits to lock @lock for writing and all
1085 locks for reading have been unlocked, the waiting thread is woken up
1086 and can lock @lock for writing.
1087 </para>
1088
1089 @lock: a #GStaticRWLock to unlock after reading
1090
1091
1092 <!-- ##### FUNCTION g_static_rw_lock_writer_lock ##### -->
1093 <para>
1094 Locks @lock for writing. If @lock is already locked for writing or
1095 reading by other threads, this function will block until @lock is
1096 completly unlocked and then lock @lock for writing. While this
1097 functions waits to lock @lock, no other thread can lock @lock for
1098 reading. When @lock is locked for writing, no other thread can lock
1099 @lock (neither for reading nor writing). This lock has to be unlocked
1100 by g_static_rw_lock_writer_unlock().
1101 </para>
1102
1103 @lock: a #GStaticRWLock to lock for writing
1104
1105
1106 <!-- ##### FUNCTION g_static_rw_lock_writer_trylock ##### -->
1107 <para>
1108 Tries to lock @lock for writing. If @lock is already locked (for
1109 either reading or writing) by another thread, it immediately returns
1110 #FALSE. Otherwise it locks @lock for writing and returns TRUE. This
1111 lock has to be unlocked by g_static_rw_lock_writer_unlock().
1112 </para>
1113
1114 @lock: a #GStaticRWLock to lock for writing
1115 @Returns: TRUE, if @lock could be locked for writing
1116
1117
1118 <!-- ##### FUNCTION g_static_rw_lock_writer_unlock ##### -->
1119 <para>
1120 Unlocks @lock. If a thread waits to lock @lock for writing and all
1121 locks for reading have been unlocked, the waiting thread is woken up
1122 and can lock @lock for writing. If no thread waits to lock @lock for
1123 writing and threads wait to lock @lock for reading, the waiting
1124 threads are woken up and can lock @lock for reading.
1125 </para>
1126
1127 @lock: a #GStaticRWLock to unlock after writing
1128
1129
1130 <!-- ##### FUNCTION g_static_rw_lock_free ##### -->
1131 <para>
1132 Releases all resources allocated to @lock. 
1133 </para>
1134
1135 <para>
1136 You don't have to call this functions for a #GStaticRWLock with an
1137 unbounded lifetime, i.e. objects declared 'static', but if you have a
1138 #GStaticRWLock as a member of a structure and the structure is freed,
1139 you should also free the #GStaticRWLock.
1140 </para>
1141
1142 @lock: a #GStaticRWLock to be freed
1143
1144
1145 <!-- ##### STRUCT GCond ##### -->
1146
1147 <para>
1148 The #GCond struct is an opaque data structure to represent a
1149 condition. A #GCond is an object, that threads can block on, if they
1150 find a certain condition to be false. If other threads change the
1151 state of this condition they can signal the #GCond, such that the
1152 waiting thread is woken up. 
1153 </para>
1154
1155 <para>
1156 <example>
1157 <title>Using GCond to block a thread until a condition is satisfied</title>
1158 <programlisting>
1159 GCond* data_cond = NULL;   /* Must be initialized somewhere */
1160 GMutex* data_mutex = NULL; /* Must be initialized somewhere */
1161 gpointer current_data = NULL;
1162
1163 void push_data (gpointer data)
1164 {
1165   g_mutex_lock (data_mutex);
1166   current_data = data;
1167   g_cond_signal (data_cond);
1168   g_mutex_unlock (data_mutex);
1169 }
1170
1171 gpointer pop_data ()
1172 {
1173   gpointer data;
1174
1175   g_mutex_lock (data_mutex);
1176   while (!current_data)
1177       g_cond_wait (data_cond, data_mutex);
1178   data = current_data;
1179   current_data = NULL;
1180   g_mutex_unlock (data_mutex);
1181   return data;
1182 }
1183 </programlisting>
1184 </example>
1185 </para>
1186
1187 <para>
1188 Whenever a thread calls pop_data() now, it will wait until
1189 current_data is non-NULL, i.e. until some other thread has called
1190 push_data().
1191 </para>
1192
1193 <note>
1194 <para>
1195 It is important to use the g_cond_wait() and g_cond_timed_wait()
1196 functions only inside a loop, which checks for the condition to be
1197 true as it is not guaranteed that the waiting thread will find it
1198 fulfilled, even if the signaling thread left the condition
1199 in that state. This is because another thread can have altered the
1200 condition, before the waiting thread got the chance to be woken up,
1201 even if the condition itself is protected by a #GMutex, like above.
1202 </para>
1203 </note>
1204
1205 <para>
1206 A #GCond should only be accessed via the following functions.
1207 </para>
1208
1209 <note>
1210 <para>
1211 All of the g_cond_* functions are actually macros. Apart from taking
1212 the addresses of them, you can however use them as if they were functions.
1213 </para>
1214 </note>
1215
1216
1217 <!-- ##### FUNCTION g_cond_new ##### -->
1218
1219 <para>
1220 Creates a new #GCond. This function will abort, if g_thread_init()
1221 has not been called yet.
1222 </para>
1223
1224 @Returns: a new #GCond
1225
1226
1227 <!-- ##### FUNCTION g_cond_signal ##### -->
1228 <para>
1229 If threads are waiting for @cond, exactly one of them is woken up. It
1230 is good practice to hold the same lock as the waiting thread, while
1231 calling this function, though not required.
1232 </para>
1233
1234 <para>
1235 This function can also be used, if g_thread_init() has
1236 not yet been called and will do nothing then.
1237 </para>
1238
1239 @cond: a #GCond
1240
1241
1242 <!-- ##### FUNCTION g_cond_broadcast ##### -->
1243
1244 <para>
1245 If threads are waiting for @cond, all of them are woken up. It is good
1246 practice to lock the same mutex as the waiting threads, while calling
1247 this function, though not required.
1248 </para>
1249
1250 <para>
1251 This function can also be used, if g_thread_init() has
1252 not yet been called and will do nothing then.
1253 </para>
1254
1255 @cond: a #GCond
1256
1257
1258 <!-- ##### FUNCTION g_cond_wait ##### -->
1259
1260 <para>
1261 Waits until this thread is woken up on @cond. The @mutex is unlocked
1262 before falling asleep and locked again before resuming.
1263 </para>
1264
1265 <para>
1266 This function can also be used, if g_thread_init() has not yet been
1267 called and will immediately return then.
1268 </para>
1269
1270 @cond: a #GCond
1271 @mutex: a #GMutex, that is currently locked
1272
1273
1274 <!-- ##### FUNCTION g_cond_timed_wait ##### -->
1275
1276 <para>
1277 Waits until this thread is woken up on @cond, but not longer than
1278 until the time, that is specified by @abs_time. The @mutex is
1279 unlocked before falling asleep and locked again before resuming.
1280 </para>
1281
1282 <para>
1283 If @abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
1284 </para>
1285
1286 <para>
1287 This function can also be used, if g_thread_init() has not yet been
1288 called and will immediately return TRUE then.
1289 </para>
1290
1291 @cond: a #GCond
1292 @mutex: a #GMutex, that is currently locked
1293 @abs_time: a #GTimeVal, determining the final time
1294 @Returns: TRUE, if the thread is woken up in time
1295
1296
1297 <!-- ##### FUNCTION g_cond_free ##### -->
1298
1299 <para>
1300 Destroys the #GCond.
1301 </para>
1302
1303 @cond: a #GCond
1304
1305
1306 <!-- ##### STRUCT GPrivate ##### -->
1307 <para>
1308 The #GPrivate struct is an opaque data structure to represent a thread
1309 private data key. Threads can thereby obtain and set a pointer, which
1310 is private to the current thread. Take our give_me_next_number()
1311 example from above.  Now we don't want current_number to be shared
1312 between the threads, but to be private to each thread. This can be
1313 done as follows:
1314
1315 <example>
1316 <title>Using GPrivate for per-thread data</title>
1317 <programlisting>
1318   GPrivate* current_number_key = NULL; /* Must be initialized somewhere */
1319                                        /* with g_private_new (g_free); */
1320
1321   int give_me_next_number ()
1322   {
1323     int *current_number = g_private_get (current_number_key);
1324
1325     if (!current_number)
1326     {
1327       current_number = g_new (int,1);
1328       *current_number = 0;
1329       g_private_set (current_number_key, current_number);
1330     }
1331     *current_number = calc_next_number (*current_number); 
1332     return *current_number;
1333   }
1334 </programlisting>
1335 </example>
1336 </para>
1337
1338 <para>
1339 Here the pointer belonging to the key current_number_key is read. If
1340 it is NULL, it has not been set yet. Then get memory for an integer
1341 value, assign this memory to the pointer and write the pointer
1342 back. Now we have an integer value, that is private to the current
1343 thread.
1344 </para>
1345
1346 <para>
1347 The #GPrivate struct should only be accessed via the following functions.
1348 </para>
1349
1350 <note>
1351 <para>
1352 All of the g_private_* functions are actually macros. Apart from taking
1353 the addresses of them, you can however use them as if they were functions.
1354 </para>
1355 </note>
1356
1357
1358 <!-- ##### FUNCTION g_private_new ##### -->
1359
1360 <para>
1361 Creates a new #GPrivate. If @destructor is non-NULL, it is a pointer
1362 to a destructor function. Whenever a thread ends and the corresponding
1363 pointer keyed to this instance of #GPrivate is non-NULL, the
1364 destructor is called with this pointer as the argument.
1365 </para>
1366
1367 <note>
1368 <para>
1369 @destructor is working quite differently from @notify in
1370 g_static_private_set().
1371 </para>
1372 </note>
1373
1374 <note>
1375 <para>
1376 A #GPrivate can not be freed. Reuse it instead, if you can to avoid
1377 shortage or use #GStaticPrivate.
1378 </para>
1379 </note>
1380
1381 <note>
1382 <para>
1383 This function will abort, if g_thread_init() has not been called yet.
1384 </para>
1385 </note>
1386
1387 @destructor: a function to handle the data keyed to #GPrivate, when a
1388 thread ends
1389 @Returns: a new #GPrivate
1390
1391
1392 <!-- ##### FUNCTION g_private_get ##### -->
1393
1394 <para>
1395 Returns the pointer keyed to @private_key for the current thread. This
1396 pointer is NULL, when g_private_set() hasn't been called for the
1397 current @private_key and thread yet.
1398 </para>
1399
1400 <para>
1401 This function can also be used, if g_thread_init() has not yet been
1402 called and will return the value of @private_key casted to #gpointer then.
1403 </para>
1404
1405 @private_key: a #GPrivate
1406 @Returns: the corresponding pointer
1407
1408
1409 <!-- ##### FUNCTION g_private_set ##### -->
1410
1411 <para>
1412 Sets the pointer keyed to @private_key for the current thread.
1413 </para>
1414
1415 <para>
1416 This function can also be used, if g_thread_init() has not yet been
1417 called and will set @private_key to @data casted to #GPrivate* then.
1418 </para>
1419
1420 @private_key: a #GPrivate
1421 @data: the new pointer
1422
1423
1424 <!-- ##### STRUCT GStaticPrivate ##### -->
1425
1426 <para>
1427 A #GStaticPrivate works almost like a #GPrivate, but it has one
1428 significant advantage. It doesn't need to be created at run-time like
1429 a #GPrivate, but can be defined at compile-time. This is similar to
1430 the difference between #GMutex and #GStaticMutex. Now look at our
1431 give_me_next_number() example with #GStaticPrivate:
1432 </para>
1433
1434 <para>
1435 <example>
1436 <title>Using GStaticPrivate for per-thread data</title>
1437 <programlisting>
1438   int give_me_next_number ()
1439   {
1440     static GStaticPrivate current_number_key = G_STATIC_PRIVATE_INIT;
1441     int *current_number = g_static_private_get (&amp;current_number_key);
1442
1443     if (!current_number)
1444     {
1445       current_number = g_new (int,1);
1446       *current_number = 0;
1447       g_static_private_set (&amp;current_number_key, current_number, g_free);
1448     }
1449     *current_number = calc_next_number (*current_number); 
1450     return *current_number;
1451   }
1452 </programlisting>
1453 </example>
1454 </para>
1455
1456 @index: 
1457
1458 <!-- ##### MACRO G_STATIC_PRIVATE_INIT ##### -->
1459 <para>
1460 Every #GStaticPrivate must be initialized with this macro, before it can
1461 be used.
1462 </para>
1463
1464 <para>
1465 <informalexample>
1466 <programlisting>
1467 GStaticPrivate my_private = G_STATIC_PRIVATE_INIT;
1468 </programlisting>
1469 </informalexample>
1470 </para>
1471
1472
1473
1474 <!-- ##### FUNCTION g_static_private_init ##### -->
1475 <para>
1476 Initializes @private_key. Alternatively you can initialize it with
1477 #G_STATIC_PRIVATE_INIT.
1478 </para>
1479
1480 @private_key: a #GStaticPrivate to be initialized
1481
1482
1483 <!-- ##### FUNCTION g_static_private_get ##### -->
1484 <para>
1485 Works like g_private_get() only for a #GStaticPrivate.
1486 </para>
1487
1488 <para>
1489 This function also works, if g_thread_init() has not yet been called.
1490 </para>
1491
1492 @private_key: a #GStaticPrivate
1493 @Returns: the corresponding pointer
1494
1495
1496 <!-- ##### FUNCTION g_static_private_get_for_thread ##### -->
1497 <para>
1498
1499 </para>
1500
1501 @private_key: 
1502 @thread: 
1503 @Returns: 
1504
1505
1506 <!-- ##### FUNCTION g_static_private_set ##### -->
1507 <para>
1508 Sets the pointer keyed to @private_key for the current thread and the
1509 function @notify to be called with that pointer (NULL or non-NULL),
1510 whenever the pointer is set again or whenever the current thread ends.
1511 </para>
1512
1513 <para>
1514 This function also works, if g_thread_init() has not yet been
1515 called. If g_thread_init() is called later, the @data keyed to
1516 @private_key will be inherited only by the main thread, i.e. the one that
1517 called g_thread_init().
1518 </para>
1519
1520 <note>
1521 <para>
1522 @notify is working quite differently from @destructor in
1523 g_private_new().
1524 </para>
1525 </note>
1526
1527 @private_key: a #GStaticPrivate
1528 @data: the new pointer
1529 @notify: a function to be called with the pointer, whenever the
1530 current thread ends or sets this pointer again
1531
1532
1533 <!-- ##### FUNCTION g_static_private_set_for_thread ##### -->
1534 <para>
1535
1536 </para>
1537
1538 @private_key: 
1539 @thread: 
1540 @data: 
1541 @notify: 
1542
1543
1544 <!-- ##### FUNCTION g_static_private_free ##### -->
1545 <para>
1546 Releases all resources allocated to @private_key. 
1547 </para>
1548
1549 <para>
1550 You don't have to call this functions for a #GStaticPrivate with an
1551 unbounded lifetime, i.e. objects declared 'static', but if you have a
1552 #GStaticPrivate as a member of a structure and the structure is freed,
1553 you should also free the #GStaticPrivate.
1554 </para>
1555
1556 @private_key: a #GStaticPrivate to be freed
1557
1558