Documentation tweaks
[platform/upstream/glib.git] / glib / gatomic.c
1 /*
2  * Copyright © 2011 Ryan Lortie
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include "gatomic.h"
25
26 /**
27  * SECTION:atomic_operations
28  * @title: Atomic Operations
29  * @short_description: basic atomic integer and pointer operations
30  * @see_also: #GMutex
31  *
32  * The following is a collection of compiler macros to provide atomic
33  * access to integer and pointer-sized values.
34  *
35  * The macros that have 'int' in the name will operate on pointers to
36  * #gint and #guint.  The macros with 'pointer' in the name will operate
37  * on pointers to any pointer-sized value, including #gsize.  There is
38  * no support for 64bit operations on platforms with 32bit pointers
39  * because it is not generally possible to perform these operations
40  * atomically.
41  *
42  * The get, set and exchange operations for integers and pointers
43  * nominally operate on #gint and #gpointer, respectively.  Of the
44  * arithmetic operations, the 'add' operation operates on (and returns)
45  * signed integer values (#gint and #gssize) and the 'and', 'or', and
46  * 'xor' operations operate on (and return) unsigned integer values
47  * (#guint and #gsize).
48  *
49  * All of the operations act as a full compiler and (where appropriate)
50  * hardware memory barrier.  Acquire and release or producer and
51  * consumer barrier semantics are not available through this API.
52  *
53  * On GCC, these macros are implemented using GCC intrinsic operations.
54  * On non-GCC compilers they will evaluate to function calls to
55  * functions implemented by GLib.
56  *
57  * If GLib itself was compiled with GCC then these functions will again
58  * be implemented by the GCC intrinsics.  On Windows without GCC, the
59  * interlocked API is used to implement the functions.
60  *
61  * With non-GCC compilers on non-Windows systems, the functions are
62  * currently incapable of implementing true atomic operations --
63  * instead, they fallback to holding a global lock while performing the
64  * operation.  This provides atomicity between the threads of one
65  * process, but not between separate processes.  For this reason, one
66  * should exercise caution when attempting to use these options on
67  * shared memory regions.
68  *
69  * It is very important that all accesses to a particular integer or
70  * pointer be performed using only this API and that different sizes of
71  * operation are not mixed or used on overlapping memory regions.  Never
72  * read or assign directly from or to a value -- always use this API.
73  *
74  * For simple reference counting purposes you should use
75  * g_atomic_int_inc() and g_atomic_int_dec_and_test().  Other uses that
76  * fall outside of simple reference counting patterns are prone to
77  * subtle bugs and occasionally undefined behaviour.  It is also worth
78  * noting that since all of these operations require global
79  * synchronisation of the entire machine, they can be quite slow.  In
80  * the case of performing multiple atomic operations it can often be
81  * faster to simply acquire a mutex lock around the critical area,
82  * perform the operations normally and then release the lock.
83  **/
84
85 #ifdef G_ATOMIC_OP_USE_GCC_BUILTINS
86
87 #ifndef __GNUC__
88 #error Using GCC builtin atomic ops, but not compiling with GCC?
89 #endif
90
91 /**
92  * g_atomic_int_get:
93  * @atomic: a pointer to a #gint or #guint
94  *
95  * Gets the current value of @atomic.
96  *
97  * This call acts as a full compiler and hardware
98  * memory barrier (before the get).
99  *
100  * Returns: the value of the integer
101  *
102  * Since: 2.4
103  **/
104 gint
105 (g_atomic_int_get) (volatile gint *atomic)
106 {
107   return g_atomic_int_get (atomic);
108 }
109
110 /**
111  * g_atomic_int_set:
112  * @atomic: a pointer to a #gint or #guint
113  * @newval: a new value to store
114  *
115  * Sets the value of @atomic to @newval.
116  *
117  * This call acts as a full compiler and hardware
118  * memory barrier (after the set).
119  *
120  * Since: 2.4
121  */
122 void
123 (g_atomic_int_set) (volatile gint *atomic,
124                     gint           newval)
125 {
126   g_atomic_int_set (atomic, newval);
127 }
128
129 /**
130  * g_atomic_int_inc:
131  * @atomic: a pointer to a #gint or #guint
132  *
133  * Increments the value of @atomic by 1.
134  *
135  * Think of this operation as an atomic version of
136  * <literal>{ *@atomic += 1; }</literal>
137  *
138  * This call acts as a full compiler and hardware memory barrier.
139  *
140  * Since: 2.4
141  **/
142 void
143 (g_atomic_int_inc) (volatile gint *atomic)
144 {
145   g_atomic_int_inc (atomic);
146 }
147
148 /**
149  * g_atomic_int_dec_and_test:
150  * @atomic: a pointer to a #gint or #guint
151  *
152  * Decrements the value of @atomic by 1.
153  *
154  * Think of this operation as an atomic version of
155  * <literal>{ *@atomic -= 1; return (*@atomic == 0); }</literal>
156  *
157  * This call acts as a full compiler and hardware memory barrier.
158  *
159  * Returns: %TRUE if the resultant value is zero
160  *
161  * Since: 2.4
162  **/
163 gboolean
164 (g_atomic_int_dec_and_test) (volatile gint *atomic)
165 {
166   return g_atomic_int_dec_and_test (atomic);
167 }
168
169 /**
170  * g_atomic_int_compare_and_exchange:
171  * @atomic: a pointer to a #gint or #guint
172  * @oldval: the value to compare with
173  * @newval: the value to conditionally replace with
174  *
175  * Compares @atomic to @oldval and, if equal, sets it to @newval.
176  * If @atomic was not equal to @oldval then no change occurs.
177  *
178  * This compare and exchange is done atomically.
179  *
180  * Think of this operation as an atomic version of
181  * <literal>{ if (*@atomic == @oldval) { *@atomic = @newval; return TRUE; } else return FALSE; }</literal>
182  *
183  * This call acts as a full compiler and hardware memory barrier.
184  *
185  * Returns: %TRUE if the exchange took place
186  *
187  * Since: 2.4
188  **/
189 gboolean
190 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
191                                      gint           oldval,
192                                      gint           newval)
193 {
194   return g_atomic_int_compare_and_exchange (atomic, oldval, newval);
195 }
196
197 /**
198  * g_atomic_int_add:
199  * @atomic: a pointer to a #gint or #guint
200  * @val: the value to add
201  *
202  * Atomically adds @val to the value of @atomic.
203  *
204  * Think of this operation as an atomic version of
205  * <literal>{ tmp = *atomic; *@atomic += @val; return tmp; }</literal>
206  *
207  * This call acts as a full compiler and hardware memory barrier.
208  *
209  * Returns: the value of @atomic before the add, signed
210  *
211  * Since: 2.4
212  **/
213 gint
214 (g_atomic_int_add) (volatile gint *atomic,
215                     gint           val)
216 {
217   return g_atomic_int_add (atomic, val);
218 }
219
220 /**
221  * g_atomic_int_and:
222  * @atomic: a pointer to a #gint or #guint
223  * @val: the value to 'and'
224  *
225  * Performs an atomic bitwise 'and' of the value of @atomic and @val,
226  * storing the result back in @atomic.
227  *
228  * This call acts as a full compiler and hardware memory barrier.
229  *
230  * Think of this operation as an atomic version of
231  * <literal>{ tmp = *atomic; *@atomic &= @val; return tmp; }</literal>
232  *
233  * Returns: the value of @atomic before the operation, unsigned
234  *
235  * Since: 2.30
236  **/
237 guint
238 (g_atomic_int_and) (volatile guint *atomic,
239                     guint           val)
240 {
241   return g_atomic_int_and (atomic, val);
242 }
243
244 /**
245  * g_atomic_int_or:
246  * @atomic: a pointer to a #gint or #guint
247  * @val: the value to 'or'
248  *
249  * Performs an atomic bitwise 'or' of the value of @atomic and @val,
250  * storing the result back in @atomic.
251  *
252  * Think of this operation as an atomic version of
253  * <literal>{ tmp = *atomic; *@atomic |= @val; return tmp; }</literal>
254  *
255  * This call acts as a full compiler and hardware memory barrier.
256  *
257  * Returns: the value of @atomic before the operation, unsigned
258  *
259  * Since: 2.30
260  **/
261 guint
262 (g_atomic_int_or) (volatile guint *atomic,
263                    guint           val)
264 {
265   return g_atomic_int_or (atomic, val);
266 }
267
268 /**
269  * g_atomic_int_xor:
270  * @atomic: a pointer to a #gint or #guint
271  * @val: the value to 'xor'
272  *
273  * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
274  * storing the result back in @atomic.
275  *
276  * Think of this operation as an atomic version of
277  * <literal>{ tmp = *atomic; *@atomic ^= @val; return tmp; }</literal>
278  *
279  * This call acts as a full compiler and hardware memory barrier.
280  *
281  * Returns: the value of @atomic before the operation, unsigned
282  *
283  * Since: 2.30
284  **/
285 guint
286 (g_atomic_int_xor) (volatile guint *atomic,
287                     guint           val)
288 {
289   return g_atomic_int_xor (atomic, val);
290 }
291
292
293 /**
294  * g_atomic_pointer_get:
295  * @atomic: a pointer to a #gpointer-sized value
296  *
297  * Gets the current value of @atomic.
298  *
299  * This call acts as a full compiler and hardware
300  * memory barrier (before the get).
301  *
302  * Returns: the value of the pointer
303  *
304  * Since: 2.4
305  **/
306 gpointer
307 (g_atomic_pointer_get) (volatile void *atomic)
308 {
309   return g_atomic_pointer_get ((volatile gpointer *) atomic);
310 }
311
312 /**
313  * g_atomic_pointer_set:
314  * @atomic: a pointer to a #gpointer-sized value
315  * @newval: a new value to store
316  *
317  * Sets the value of @atomic to @newval.
318  *
319  * This call acts as a full compiler and hardware
320  * memory barrier (after the set).
321  *
322  * Since: 2.4
323  **/
324 void
325 (g_atomic_pointer_set) (volatile void *atomic,
326                         gpointer       newval)
327 {
328   g_atomic_pointer_set ((volatile gpointer *) atomic, newval);
329 }
330
331 /**
332  * g_atomic_pointer_compare_and_exchange:
333  * @atomic: a pointer to a #gpointer-sized value
334  * @oldval: the value to compare with
335  * @newval: the value to conditionally replace with
336  *
337  * Compares @atomic to @oldval and, if equal, sets it to @newval.
338  * If @atomic was not equal to @oldval then no change occurs.
339  *
340  * This compare and exchange is done atomically.
341  *
342  * Think of this operation as an atomic version of
343  * <literal>{ if (*@atomic == @oldval) { *@atomic = @newval; return TRUE; } else return FALSE; }</literal>
344  *
345  * This call acts as a full compiler and hardware memory barrier.
346  *
347  * Returns: %TRUE if the exchange took place
348  *
349  * Since: 2.4
350  **/
351 gboolean
352 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
353                                          gpointer       oldval,
354                                          gpointer       newval)
355 {
356   return g_atomic_pointer_compare_and_exchange ((volatile gpointer *) atomic,
357                                                 oldval, newval);
358 }
359
360 /**
361  * g_atomic_pointer_add:
362  * @atomic: a pointer to a #gpointer-sized value
363  * @val: the value to add
364  *
365  * Atomically adds @val to the value of @atomic.
366  *
367  * Think of this operation as an atomic version of
368  * <literal>{ tmp = *atomic; *@atomic += @val; return tmp; }</literal>
369  *
370  * This call acts as a full compiler and hardware memory barrier.
371  *
372  * Returns: the value of @atomic before the add, signed
373  *
374  * Since: 2.30
375  **/
376 gssize
377 (g_atomic_pointer_add) (volatile void *atomic,
378                         gssize         val)
379 {
380   return g_atomic_pointer_add ((volatile gpointer *) atomic, val);
381 }
382
383 /**
384  * g_atomic_pointer_and:
385  * @atomic: a pointer to a #gpointer-sized value
386  * @val: the value to 'and'
387  *
388  * Performs an atomic bitwise 'and' of the value of @atomic and @val,
389  * storing the result back in @atomic.
390  *
391  * Think of this operation as an atomic version of
392  * <literal>{ tmp = *atomic; *@atomic &= @val; return tmp; }</literal>
393  *
394  * This call acts as a full compiler and hardware memory barrier.
395  *
396  * Returns: the value of @atomic before the operation, unsigned
397  *
398  * Since: 2.30
399  **/
400 gsize
401 (g_atomic_pointer_and) (volatile void *atomic,
402                         gsize          val)
403 {
404   return g_atomic_pointer_and ((volatile gpointer *) atomic, val);
405 }
406
407 /**
408  * g_atomic_pointer_or:
409  * @atomic: a pointer to a #gpointer-sized value
410  * @val: the value to 'or'
411  *
412  * Performs an atomic bitwise 'or' of the value of @atomic and @val,
413  * storing the result back in @atomic.
414  *
415  * Think of this operation as an atomic version of
416  * <literal>{ tmp = *atomic; *@atomic |= @val; return tmp; }</literal>
417  *
418  * This call acts as a full compiler and hardware memory barrier.
419  *
420  * Returns: the value of @atomic before the operation, unsigned
421  *
422  * Since: 2.30
423  **/
424 gsize
425 (g_atomic_pointer_or) (volatile void *atomic,
426                        gsize          val)
427 {
428   return g_atomic_pointer_or ((volatile gpointer *) atomic, val);
429 }
430
431 /**
432  * g_atomic_pointer_xor:
433  * @atomic: a pointer to a #gpointer-sized value
434  * @val: the value to 'xor'
435  *
436  * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
437  * storing the result back in @atomic.
438  *
439  * Think of this operation as an atomic version of
440  * <literal>{ tmp = *atomic; *@atomic ^= @val; return tmp; }</literal>
441  *
442  * This call acts as a full compiler and hardware memory barrier.
443  *
444  * Returns: the value of @atomic before the operation, unsigned
445  *
446  * Since: 2.30
447  **/
448 gsize
449 (g_atomic_pointer_xor) (volatile void *atomic,
450                         gsize          val)
451 {
452   return g_atomic_pointer_xor ((volatile gpointer *) atomic, val);
453 }
454
455 #elif defined (G_PLATFORM_WIN32)
456
457 /*
458  * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
459  */
460 gint
461 (g_atomic_int_get) (volatile gint *atomic)
462 {
463   MemoryBarrier ();
464   return *atomic;
465 }
466
467 void
468 (g_atomic_int_set) (volatile gint *atomic,
469                     gint           newval)
470 {
471   *atomic = newval;
472   MemoryBarrier ();
473 }
474
475 void
476 (g_atomic_int_inc) (volatile gint *atomic)
477 {
478   InterlockedIncrement (atomic);
479 }
480
481 gboolean
482 (g_atomic_int_dec_and_test) (volatile gint *atomic)
483 {
484   return InterlockedDecrement (atomic) == 0;
485 }
486
487 gboolean
488 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
489                                      gint           oldval,
490                                      gint           newval)
491 {
492   return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
493 }
494
495 gint
496 (g_atomic_int_add) (volatile gint *atomic,
497                     gint           val)
498 {
499   return InterlockedExchangeAdd (atomic, val);
500 }
501
502 guint
503 (g_atomic_int_and) (volatile guint *atomic,
504                     guint           val)
505 {
506   return InterlockedAnd (atomic, val);
507 }
508
509 guint
510 (g_atomic_int_or) (volatile guint *atomic,
511                    guint           val)
512 {
513   return InterlockedOr (atomic, val);
514 }
515
516 guint
517 (g_atomic_int_xor) (volatile guint *atomic,
518                     guint           val)
519 {
520   return InterlockedXor (atomic, val);
521 }
522
523
524 gpointer
525 (g_atomic_pointer_get) (volatile void *atomic)
526 {
527   volatile gpointer *ptr = atomic;
528
529   MemoryBarrier ();
530   return *ptr;
531 }
532
533 void
534 (g_atomic_pointer_set) (volatile void *atomic,
535                         gpointer       newval)
536 {
537   volatile gpointer *ptr = atomic;
538
539   *ptr = newval;
540   MemoryBarrier ();
541 }
542
543 gboolean
544 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
545                                          gpointer       oldval,
546                                          gpointer       newval)
547 {
548   return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
549 }
550
551 gssize
552 (g_atomic_pointer_add) (volatile void *atomic,
553                         gssize         val)
554 {
555 #if GLIB_SIZEOF_VOID_P == 8
556   return InterlockedExchangeAdd64 (atomic, val);
557 #else
558   return InterlockedExchangeAdd (atomic, val);
559 #endif
560 }
561
562 gsize
563 (g_atomic_pointer_and) (volatile void *atomic,
564                         gsize          val)
565 {
566 #if GLIB_SIZEOF_VOID_P == 8
567   return InterlockedAnd64 (atomic, val);
568 #else
569   return InterlockedAnd (atomic, val);
570 #endif
571 }
572
573 gsize
574 (g_atomic_pointer_or) (volatile void *atomic,
575                        gsize          val)
576 {
577 #if GLIB_SIZEOF_VOID_P == 8
578   return InterlockedOr64 (atomic, val);
579 #else
580   return InterlockedOr (atomic, val);
581 #endif
582 }
583
584 gsize
585 (g_atomic_pointer_xor) (volatile void *atomic,
586                         gsize          val)
587 {
588 #if GLIB_SIZEOF_VOID_P == 8
589   return InterlockedXor64 (atomic, val);
590 #else
591   return InterlockedXor (atomic, val);
592 #endif
593 }
594
595 #else
596
597 #include "gthread.h"
598
599 static GStaticMutex g_atomic_lock;
600
601 gint
602 (g_atomic_int_get) (volatile gint *atomic)
603 {
604   gint value;
605
606   g_static_mutex_lock (&g_atomic_lock);
607   value = *atomic;
608   g_static_mutex_unlock (&g_atomic_lock);
609
610   return value;
611 }
612
613 void
614 (g_atomic_int_set) (volatile gint *atomic,
615                     gint           value)
616 {
617   g_static_mutex_lock (&g_atomic_lock);
618   *atomic = value;
619   g_static_mutex_unlock (&g_atomic_lock);
620 }
621
622 void
623 (g_atomic_int_inc) (volatile gint *atomic)
624 {
625   g_static_mutex_lock (&g_atomic_lock);
626   (*atomic)++;
627   g_static_mutex_unlock (&g_atomic_lock);
628 }
629
630 gboolean
631 (g_atomic_int_dec_and_test) (volatile gint *atomic)
632 {
633   gboolean is_zero;
634
635   g_static_mutex_lock (&g_atomic_lock);
636   is_zero = --(*atomic) == 0;
637   g_static_mutex_unlock (&g_atomic_lock);
638
639   return is_zero;
640 }
641
642 gboolean
643 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
644                                      gint           oldval,
645                                      gint           newval)
646 {
647   gboolean success;
648
649   g_static_mutex_lock (&g_atomic_lock);
650
651   if ((success = (*atomic == oldval)))
652     *atomic = newval;
653
654   g_static_mutex_unlock (&g_atomic_lock);
655
656   return success;
657 }
658
659 gint
660 (g_atomic_int_add) (volatile gint *atomic,
661                     gint           val)
662 {
663   gint oldval;
664
665   g_static_mutex_lock (&g_atomic_lock);
666   oldval = *atomic;
667   *atomic = oldval + val;
668   g_static_mutex_unlock (&g_atomic_lock);
669
670   return oldval;
671 }
672
673 guint
674 (g_atomic_int_and) (volatile guint *atomic,
675                     guint           val)
676 {
677   guint oldval;
678
679   g_static_mutex_lock (&g_atomic_lock);
680   oldval = *atomic;
681   *atomic = oldval & val;
682   g_static_mutex_unlock (&g_atomic_lock);
683
684   return oldval;
685 }
686
687 guint
688 (g_atomic_int_or) (volatile guint *atomic,
689                    guint           val)
690 {
691   guint oldval;
692
693   g_static_mutex_lock (&g_atomic_lock);
694   oldval = *atomic;
695   *atomic = oldval | val;
696   g_static_mutex_unlock (&g_atomic_lock);
697
698   return oldval;
699 }
700
701 guint
702 (g_atomic_int_xor) (volatile guint *atomic,
703                     guint           val)
704 {
705   guint oldval;
706
707   g_static_mutex_lock (&g_atomic_lock);
708   oldval = *atomic;
709   *atomic = oldval ^ val;
710   g_static_mutex_unlock (&g_atomic_lock);
711
712   return oldval;
713 }
714
715
716 gpointer
717 (g_atomic_pointer_get) (volatile void *atomic)
718 {
719   volatile gpointer *ptr = atomic;
720   gpointer value;
721
722   g_static_mutex_lock (&g_atomic_lock);
723   value = *ptr;
724   g_static_mutex_unlock (&g_atomic_lock);
725
726   return value;
727 }
728
729 void
730 (g_atomic_pointer_set) (volatile void *atomic,
731                         gpointer       newval)
732 {
733   volatile gpointer *ptr = atomic;
734
735   g_static_mutex_lock (&g_atomic_lock);
736   *ptr = newval;
737   g_static_mutex_unlock (&g_atomic_lock);
738 }
739
740 gboolean
741 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
742                                          gpointer       oldval,
743                                          gpointer       newval)
744 {
745   volatile gpointer *ptr = atomic;
746   gboolean success;
747
748   g_static_mutex_lock (&g_atomic_lock);
749
750   if ((success = (*ptr == oldval)))
751     *ptr = newval;
752
753   g_static_mutex_unlock (&g_atomic_lock);
754
755   return success;
756 }
757
758 gssize
759 (g_atomic_pointer_add) (volatile void *atomic,
760                         gssize         val)
761 {
762   volatile gssize *ptr = atomic;
763   gssize oldval;
764
765   g_static_mutex_lock (&g_atomic_lock);
766   oldval = *ptr;
767   *ptr = oldval + val;
768   g_static_mutex_unlock (&g_atomic_lock);
769
770   return oldval;
771 }
772
773 gsize
774 (g_atomic_pointer_and) (volatile void *atomic,
775                         gsize          val)
776 {
777   volatile gsize *ptr = atomic;
778   gsize oldval;
779
780   g_static_mutex_lock (&g_atomic_lock);
781   oldval = *ptr;
782   *ptr = oldval & val;
783   g_static_mutex_unlock (&g_atomic_lock);
784
785   return oldval;
786 }
787
788 gsize
789 (g_atomic_pointer_or) (volatile void *atomic,
790                        gsize          val)
791 {
792   volatile gsize *ptr = atomic;
793   gsize oldval;
794
795   g_static_mutex_lock (&g_atomic_lock);
796   oldval = *ptr;
797   *ptr = oldval | val;
798   g_static_mutex_unlock (&g_atomic_lock);
799
800   return oldval;
801 }
802
803 gsize
804 (g_atomic_pointer_xor) (volatile void *atomic,
805                         gsize          val)
806 {
807   volatile gsize *ptr = atomic;
808   gsize oldval;
809
810   g_static_mutex_lock (&g_atomic_lock);
811   oldval = *ptr;
812   *ptr = oldval ^ val;
813   g_static_mutex_unlock (&g_atomic_lock);
814
815   return oldval;
816 }
817
818 #endif
819
820 /**
821  * g_atomic_int_exchange_and_add:
822  * @atomic: a pointer to a #gint
823  * @val: the value to add
824  *
825  * This function existed before g_atomic_int_add() returned the prior
826  * value of the integer (which it now does).  It is retained only for
827  * compatibility reasons.  Don't use this function in new code.
828  *
829  * Returns: the value of @atomic before the add, signed
830  * Since: 2.4
831  * Deprecated: 2.30: Use g_atomic_int_add() instead.
832  **/
833 gint
834 g_atomic_int_exchange_and_add (volatile gint *atomic,
835                                gint           val)
836 {
837   return (g_atomic_int_add) (atomic, val);
838 }