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