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