Update annotations from gobject-introspection/gir/glib-2.0.c
[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 #include <windows.h>
458 #if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64)
459 #define InterlockedAnd _InterlockedAnd
460 #define InterlockedOr _InterlockedOr
461 #define InterlockedXor _InterlockedXor
462 #endif
463
464 /*
465  * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
466  */
467 gint
468 (g_atomic_int_get) (volatile gint *atomic)
469 {
470   MemoryBarrier ();
471   return *atomic;
472 }
473
474 void
475 (g_atomic_int_set) (volatile gint *atomic,
476                     gint           newval)
477 {
478   *atomic = newval;
479   MemoryBarrier ();
480 }
481
482 void
483 (g_atomic_int_inc) (volatile gint *atomic)
484 {
485   InterlockedIncrement (atomic);
486 }
487
488 gboolean
489 (g_atomic_int_dec_and_test) (volatile gint *atomic)
490 {
491   return InterlockedDecrement (atomic) == 0;
492 }
493
494 gboolean
495 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
496                                      gint           oldval,
497                                      gint           newval)
498 {
499   return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
500 }
501
502 gint
503 (g_atomic_int_add) (volatile gint *atomic,
504                     gint           val)
505 {
506   return InterlockedExchangeAdd (atomic, val);
507 }
508
509 guint
510 (g_atomic_int_and) (volatile guint *atomic,
511                     guint           val)
512 {
513   return InterlockedAnd (atomic, val);
514 }
515
516 guint
517 (g_atomic_int_or) (volatile guint *atomic,
518                    guint           val)
519 {
520   return InterlockedOr (atomic, val);
521 }
522
523 guint
524 (g_atomic_int_xor) (volatile guint *atomic,
525                     guint           val)
526 {
527   return InterlockedXor (atomic, val);
528 }
529
530
531 gpointer
532 (g_atomic_pointer_get) (volatile void *atomic)
533 {
534   volatile gpointer *ptr = atomic;
535
536   MemoryBarrier ();
537   return *ptr;
538 }
539
540 void
541 (g_atomic_pointer_set) (volatile void *atomic,
542                         gpointer       newval)
543 {
544   volatile gpointer *ptr = atomic;
545
546   *ptr = newval;
547   MemoryBarrier ();
548 }
549
550 gboolean
551 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
552                                          gpointer       oldval,
553                                          gpointer       newval)
554 {
555   return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
556 }
557
558 gssize
559 (g_atomic_pointer_add) (volatile void *atomic,
560                         gssize         val)
561 {
562 #if GLIB_SIZEOF_VOID_P == 8
563   return InterlockedExchangeAdd64 (atomic, val);
564 #else
565   return InterlockedExchangeAdd (atomic, val);
566 #endif
567 }
568
569 gsize
570 (g_atomic_pointer_and) (volatile void *atomic,
571                         gsize          val)
572 {
573 #if GLIB_SIZEOF_VOID_P == 8
574   return InterlockedAnd64 (atomic, val);
575 #else
576   return InterlockedAnd (atomic, val);
577 #endif
578 }
579
580 gsize
581 (g_atomic_pointer_or) (volatile void *atomic,
582                        gsize          val)
583 {
584 #if GLIB_SIZEOF_VOID_P == 8
585   return InterlockedOr64 (atomic, val);
586 #else
587   return InterlockedOr (atomic, val);
588 #endif
589 }
590
591 gsize
592 (g_atomic_pointer_xor) (volatile void *atomic,
593                         gsize          val)
594 {
595 #if GLIB_SIZEOF_VOID_P == 8
596   return InterlockedXor64 (atomic, val);
597 #else
598   return InterlockedXor (atomic, val);
599 #endif
600 }
601
602 #else
603
604 #include "gthread.h"
605
606 static GStaticMutex g_atomic_lock;
607
608 gint
609 (g_atomic_int_get) (volatile gint *atomic)
610 {
611   gint value;
612
613   g_static_mutex_lock (&g_atomic_lock);
614   value = *atomic;
615   g_static_mutex_unlock (&g_atomic_lock);
616
617   return value;
618 }
619
620 void
621 (g_atomic_int_set) (volatile gint *atomic,
622                     gint           value)
623 {
624   g_static_mutex_lock (&g_atomic_lock);
625   *atomic = value;
626   g_static_mutex_unlock (&g_atomic_lock);
627 }
628
629 void
630 (g_atomic_int_inc) (volatile gint *atomic)
631 {
632   g_static_mutex_lock (&g_atomic_lock);
633   (*atomic)++;
634   g_static_mutex_unlock (&g_atomic_lock);
635 }
636
637 gboolean
638 (g_atomic_int_dec_and_test) (volatile gint *atomic)
639 {
640   gboolean is_zero;
641
642   g_static_mutex_lock (&g_atomic_lock);
643   is_zero = --(*atomic) == 0;
644   g_static_mutex_unlock (&g_atomic_lock);
645
646   return is_zero;
647 }
648
649 gboolean
650 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
651                                      gint           oldval,
652                                      gint           newval)
653 {
654   gboolean success;
655
656   g_static_mutex_lock (&g_atomic_lock);
657
658   if ((success = (*atomic == oldval)))
659     *atomic = newval;
660
661   g_static_mutex_unlock (&g_atomic_lock);
662
663   return success;
664 }
665
666 gint
667 (g_atomic_int_add) (volatile gint *atomic,
668                     gint           val)
669 {
670   gint oldval;
671
672   g_static_mutex_lock (&g_atomic_lock);
673   oldval = *atomic;
674   *atomic = oldval + val;
675   g_static_mutex_unlock (&g_atomic_lock);
676
677   return oldval;
678 }
679
680 guint
681 (g_atomic_int_and) (volatile guint *atomic,
682                     guint           val)
683 {
684   guint oldval;
685
686   g_static_mutex_lock (&g_atomic_lock);
687   oldval = *atomic;
688   *atomic = oldval & val;
689   g_static_mutex_unlock (&g_atomic_lock);
690
691   return oldval;
692 }
693
694 guint
695 (g_atomic_int_or) (volatile guint *atomic,
696                    guint           val)
697 {
698   guint oldval;
699
700   g_static_mutex_lock (&g_atomic_lock);
701   oldval = *atomic;
702   *atomic = oldval | val;
703   g_static_mutex_unlock (&g_atomic_lock);
704
705   return oldval;
706 }
707
708 guint
709 (g_atomic_int_xor) (volatile guint *atomic,
710                     guint           val)
711 {
712   guint oldval;
713
714   g_static_mutex_lock (&g_atomic_lock);
715   oldval = *atomic;
716   *atomic = oldval ^ val;
717   g_static_mutex_unlock (&g_atomic_lock);
718
719   return oldval;
720 }
721
722
723 gpointer
724 (g_atomic_pointer_get) (volatile void *atomic)
725 {
726   volatile gpointer *ptr = atomic;
727   gpointer value;
728
729   g_static_mutex_lock (&g_atomic_lock);
730   value = *ptr;
731   g_static_mutex_unlock (&g_atomic_lock);
732
733   return value;
734 }
735
736 void
737 (g_atomic_pointer_set) (volatile void *atomic,
738                         gpointer       newval)
739 {
740   volatile gpointer *ptr = atomic;
741
742   g_static_mutex_lock (&g_atomic_lock);
743   *ptr = newval;
744   g_static_mutex_unlock (&g_atomic_lock);
745 }
746
747 gboolean
748 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
749                                          gpointer       oldval,
750                                          gpointer       newval)
751 {
752   volatile gpointer *ptr = atomic;
753   gboolean success;
754
755   g_static_mutex_lock (&g_atomic_lock);
756
757   if ((success = (*ptr == oldval)))
758     *ptr = newval;
759
760   g_static_mutex_unlock (&g_atomic_lock);
761
762   return success;
763 }
764
765 gssize
766 (g_atomic_pointer_add) (volatile void *atomic,
767                         gssize         val)
768 {
769   volatile gssize *ptr = atomic;
770   gssize oldval;
771
772   g_static_mutex_lock (&g_atomic_lock);
773   oldval = *ptr;
774   *ptr = oldval + val;
775   g_static_mutex_unlock (&g_atomic_lock);
776
777   return oldval;
778 }
779
780 gsize
781 (g_atomic_pointer_and) (volatile void *atomic,
782                         gsize          val)
783 {
784   volatile gsize *ptr = atomic;
785   gsize oldval;
786
787   g_static_mutex_lock (&g_atomic_lock);
788   oldval = *ptr;
789   *ptr = oldval & val;
790   g_static_mutex_unlock (&g_atomic_lock);
791
792   return oldval;
793 }
794
795 gsize
796 (g_atomic_pointer_or) (volatile void *atomic,
797                        gsize          val)
798 {
799   volatile gsize *ptr = atomic;
800   gsize oldval;
801
802   g_static_mutex_lock (&g_atomic_lock);
803   oldval = *ptr;
804   *ptr = oldval | val;
805   g_static_mutex_unlock (&g_atomic_lock);
806
807   return oldval;
808 }
809
810 gsize
811 (g_atomic_pointer_xor) (volatile void *atomic,
812                         gsize          val)
813 {
814   volatile gsize *ptr = atomic;
815   gsize oldval;
816
817   g_static_mutex_lock (&g_atomic_lock);
818   oldval = *ptr;
819   *ptr = oldval ^ val;
820   g_static_mutex_unlock (&g_atomic_lock);
821
822   return oldval;
823 }
824
825 #endif
826
827 /**
828  * g_atomic_int_exchange_and_add:
829  * @atomic: a pointer to a #gint
830  * @val: the value to add
831  *
832  * This function existed before g_atomic_int_add() returned the prior
833  * value of the integer (which it now does).  It is retained only for
834  * compatibility reasons.  Don't use this function in new code.
835  *
836  * Returns: the value of @atomic before the add, signed
837  * Since: 2.4
838  * Deprecated: 2.30: Use g_atomic_int_add() instead.
839  **/
840 gint
841 g_atomic_int_exchange_and_add (volatile gint *atomic,
842                                gint           val)
843 {
844   return (g_atomic_int_add) (atomic, val);
845 }