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