Bug 652827 - glib-2.29.8 no longer builds with mingw.org's toolchain
[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  * Before version 2.30, this function did not return a value
210  * (but g_atomic_int_exchange_and_add() did, and had the same meaning).
211  *
212  * Returns: the value of @atomic before the add, signed
213  *
214  * Since: 2.4
215  **/
216 gint
217 (g_atomic_int_add) (volatile gint *atomic,
218                     gint           val)
219 {
220   return g_atomic_int_add (atomic, val);
221 }
222
223 /**
224  * g_atomic_int_and:
225  * @atomic: a pointer to a #gint or #guint
226  * @val: the value to 'and'
227  *
228  * Performs an atomic bitwise 'and' of the value of @atomic and @val,
229  * storing the result back in @atomic.
230  *
231  * This call acts as a full compiler and hardware memory barrier.
232  *
233  * Think of this operation as an atomic version of
234  * <literal>{ tmp = *atomic; *@atomic &= @val; return tmp; }</literal>
235  *
236  * Returns: the value of @atomic before the operation, unsigned
237  *
238  * Since: 2.30
239  **/
240 guint
241 (g_atomic_int_and) (volatile guint *atomic,
242                     guint           val)
243 {
244   return g_atomic_int_and (atomic, val);
245 }
246
247 /**
248  * g_atomic_int_or:
249  * @atomic: a pointer to a #gint or #guint
250  * @val: the value to 'or'
251  *
252  * Performs an atomic bitwise 'or' of the value of @atomic and @val,
253  * storing the result back in @atomic.
254  *
255  * Think of this operation as an atomic version of
256  * <literal>{ tmp = *atomic; *@atomic |= @val; return tmp; }</literal>
257  *
258  * This call acts as a full compiler and hardware memory barrier.
259  *
260  * Returns: the value of @atomic before the operation, unsigned
261  *
262  * Since: 2.30
263  **/
264 guint
265 (g_atomic_int_or) (volatile guint *atomic,
266                    guint           val)
267 {
268   return g_atomic_int_or (atomic, val);
269 }
270
271 /**
272  * g_atomic_int_xor:
273  * @atomic: a pointer to a #gint or #guint
274  * @val: the value to 'xor'
275  *
276  * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
277  * storing the result back in @atomic.
278  *
279  * Think of this operation as an atomic version of
280  * <literal>{ tmp = *atomic; *@atomic ^= @val; return tmp; }</literal>
281  *
282  * This call acts as a full compiler and hardware memory barrier.
283  *
284  * Returns: the value of @atomic before the operation, unsigned
285  *
286  * Since: 2.30
287  **/
288 guint
289 (g_atomic_int_xor) (volatile guint *atomic,
290                     guint           val)
291 {
292   return g_atomic_int_xor (atomic, val);
293 }
294
295
296 /**
297  * g_atomic_pointer_get:
298  * @atomic: a pointer to a #gpointer-sized value
299  *
300  * Gets the current value of @atomic.
301  *
302  * This call acts as a full compiler and hardware
303  * memory barrier (before the get).
304  *
305  * Returns: the value of the pointer
306  *
307  * Since: 2.4
308  **/
309 gpointer
310 (g_atomic_pointer_get) (volatile void *atomic)
311 {
312   return g_atomic_pointer_get ((volatile gpointer *) atomic);
313 }
314
315 /**
316  * g_atomic_pointer_set:
317  * @atomic: a pointer to a #gpointer-sized value
318  * @newval: a new value to store
319  *
320  * Sets the value of @atomic to @newval.
321  *
322  * This call acts as a full compiler and hardware
323  * memory barrier (after the set).
324  *
325  * Since: 2.4
326  **/
327 void
328 (g_atomic_pointer_set) (volatile void *atomic,
329                         gpointer       newval)
330 {
331   g_atomic_pointer_set ((volatile gpointer *) atomic, newval);
332 }
333
334 /**
335  * g_atomic_pointer_compare_and_exchange:
336  * @atomic: a pointer to a #gpointer-sized value
337  * @oldval: the value to compare with
338  * @newval: the value to conditionally replace with
339  *
340  * Compares @atomic to @oldval and, if equal, sets it to @newval.
341  * If @atomic was not equal to @oldval then no change occurs.
342  *
343  * This compare and exchange is done atomically.
344  *
345  * Think of this operation as an atomic version of
346  * <literal>{ if (*@atomic == @oldval) { *@atomic = @newval; return TRUE; } else return FALSE; }</literal>
347  *
348  * This call acts as a full compiler and hardware memory barrier.
349  *
350  * Returns: %TRUE if the exchange took place
351  *
352  * Since: 2.4
353  **/
354 gboolean
355 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
356                                          gpointer       oldval,
357                                          gpointer       newval)
358 {
359   return g_atomic_pointer_compare_and_exchange ((volatile gpointer *) atomic,
360                                                 oldval, newval);
361 }
362
363 /**
364  * g_atomic_pointer_add:
365  * @atomic: a pointer to a #gpointer-sized value
366  * @val: the value to add
367  *
368  * Atomically adds @val to the value of @atomic.
369  *
370  * Think of this operation as an atomic version of
371  * <literal>{ tmp = *atomic; *@atomic += @val; return tmp; }</literal>
372  *
373  * This call acts as a full compiler and hardware memory barrier.
374  *
375  * Returns: the value of @atomic before the add, signed
376  *
377  * Since: 2.30
378  **/
379 gssize
380 (g_atomic_pointer_add) (volatile void *atomic,
381                         gssize         val)
382 {
383   return g_atomic_pointer_add ((volatile gpointer *) atomic, val);
384 }
385
386 /**
387  * g_atomic_pointer_and:
388  * @atomic: a pointer to a #gpointer-sized value
389  * @val: the value to 'and'
390  *
391  * Performs an atomic bitwise 'and' of the value of @atomic and @val,
392  * storing the result back in @atomic.
393  *
394  * Think of this operation as an atomic version of
395  * <literal>{ tmp = *atomic; *@atomic &= @val; return tmp; }</literal>
396  *
397  * This call acts as a full compiler and hardware memory barrier.
398  *
399  * Returns: the value of @atomic before the operation, unsigned
400  *
401  * Since: 2.30
402  **/
403 gsize
404 (g_atomic_pointer_and) (volatile void *atomic,
405                         gsize          val)
406 {
407   return g_atomic_pointer_and ((volatile gpointer *) atomic, val);
408 }
409
410 /**
411  * g_atomic_pointer_or:
412  * @atomic: a pointer to a #gpointer-sized value
413  * @val: the value to 'or'
414  *
415  * Performs an atomic bitwise 'or' of the value of @atomic and @val,
416  * storing the result back in @atomic.
417  *
418  * Think of this operation as an atomic version of
419  * <literal>{ tmp = *atomic; *@atomic |= @val; return tmp; }</literal>
420  *
421  * This call acts as a full compiler and hardware memory barrier.
422  *
423  * Returns: the value of @atomic before the operation, unsigned
424  *
425  * Since: 2.30
426  **/
427 gsize
428 (g_atomic_pointer_or) (volatile void *atomic,
429                        gsize          val)
430 {
431   return g_atomic_pointer_or ((volatile gpointer *) atomic, val);
432 }
433
434 /**
435  * g_atomic_pointer_xor:
436  * @atomic: a pointer to a #gpointer-sized value
437  * @val: the value to 'xor'
438  *
439  * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
440  * storing the result back in @atomic.
441  *
442  * Think of this operation as an atomic version of
443  * <literal>{ tmp = *atomic; *@atomic ^= @val; return tmp; }</literal>
444  *
445  * This call acts as a full compiler and hardware memory barrier.
446  *
447  * Returns: the value of @atomic before the operation, unsigned
448  *
449  * Since: 2.30
450  **/
451 gsize
452 (g_atomic_pointer_xor) (volatile void *atomic,
453                         gsize          val)
454 {
455   return g_atomic_pointer_xor ((volatile gpointer *) atomic, val);
456 }
457
458 #elif defined (G_PLATFORM_WIN32) && defined(HAVE_WIN32_BUILTINS_FOR_ATOMIC_OPERATIONS)
459
460 #include <windows.h>
461 #if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64)
462 #define InterlockedAnd _InterlockedAnd
463 #define InterlockedOr _InterlockedOr
464 #define InterlockedXor _InterlockedXor
465 #endif
466
467 /*
468  * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
469  */
470 gint
471 (g_atomic_int_get) (volatile gint *atomic)
472 {
473   MemoryBarrier ();
474   return *atomic;
475 }
476
477 void
478 (g_atomic_int_set) (volatile gint *atomic,
479                     gint           newval)
480 {
481   *atomic = newval;
482   MemoryBarrier ();
483 }
484
485 void
486 (g_atomic_int_inc) (volatile gint *atomic)
487 {
488   InterlockedIncrement (atomic);
489 }
490
491 gboolean
492 (g_atomic_int_dec_and_test) (volatile gint *atomic)
493 {
494   return InterlockedDecrement (atomic) == 0;
495 }
496
497 gboolean
498 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
499                                      gint           oldval,
500                                      gint           newval)
501 {
502   return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
503 }
504
505 gint
506 (g_atomic_int_add) (volatile gint *atomic,
507                     gint           val)
508 {
509   return InterlockedExchangeAdd (atomic, val);
510 }
511
512 guint
513 (g_atomic_int_and) (volatile guint *atomic,
514                     guint           val)
515 {
516   return InterlockedAnd (atomic, val);
517 }
518
519 guint
520 (g_atomic_int_or) (volatile guint *atomic,
521                    guint           val)
522 {
523   return InterlockedOr (atomic, val);
524 }
525
526 guint
527 (g_atomic_int_xor) (volatile guint *atomic,
528                     guint           val)
529 {
530   return InterlockedXor (atomic, val);
531 }
532
533
534 gpointer
535 (g_atomic_pointer_get) (volatile void *atomic)
536 {
537   volatile gpointer *ptr = atomic;
538
539   MemoryBarrier ();
540   return *ptr;
541 }
542
543 void
544 (g_atomic_pointer_set) (volatile void *atomic,
545                         gpointer       newval)
546 {
547   volatile gpointer *ptr = atomic;
548
549   *ptr = newval;
550   MemoryBarrier ();
551 }
552
553 gboolean
554 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
555                                          gpointer       oldval,
556                                          gpointer       newval)
557 {
558   return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
559 }
560
561 gssize
562 (g_atomic_pointer_add) (volatile void *atomic,
563                         gssize         val)
564 {
565 #if GLIB_SIZEOF_VOID_P == 8
566   return InterlockedExchangeAdd64 (atomic, val);
567 #else
568   return InterlockedExchangeAdd (atomic, val);
569 #endif
570 }
571
572 gsize
573 (g_atomic_pointer_and) (volatile void *atomic,
574                         gsize          val)
575 {
576 #if GLIB_SIZEOF_VOID_P == 8
577   return InterlockedAnd64 (atomic, val);
578 #else
579   return InterlockedAnd (atomic, val);
580 #endif
581 }
582
583 gsize
584 (g_atomic_pointer_or) (volatile void *atomic,
585                        gsize          val)
586 {
587 #if GLIB_SIZEOF_VOID_P == 8
588   return InterlockedOr64 (atomic, val);
589 #else
590   return InterlockedOr (atomic, val);
591 #endif
592 }
593
594 gsize
595 (g_atomic_pointer_xor) (volatile void *atomic,
596                         gsize          val)
597 {
598 #if GLIB_SIZEOF_VOID_P == 8
599   return InterlockedXor64 (atomic, val);
600 #else
601   return InterlockedXor (atomic, val);
602 #endif
603 }
604
605 #else
606
607 #include "gthread.h"
608
609 static GStaticMutex g_atomic_lock;
610
611 gint
612 (g_atomic_int_get) (volatile gint *atomic)
613 {
614   gint value;
615
616   g_static_mutex_lock (&g_atomic_lock);
617   value = *atomic;
618   g_static_mutex_unlock (&g_atomic_lock);
619
620   return value;
621 }
622
623 void
624 (g_atomic_int_set) (volatile gint *atomic,
625                     gint           value)
626 {
627   g_static_mutex_lock (&g_atomic_lock);
628   *atomic = value;
629   g_static_mutex_unlock (&g_atomic_lock);
630 }
631
632 void
633 (g_atomic_int_inc) (volatile gint *atomic)
634 {
635   g_static_mutex_lock (&g_atomic_lock);
636   (*atomic)++;
637   g_static_mutex_unlock (&g_atomic_lock);
638 }
639
640 gboolean
641 (g_atomic_int_dec_and_test) (volatile gint *atomic)
642 {
643   gboolean is_zero;
644
645   g_static_mutex_lock (&g_atomic_lock);
646   is_zero = --(*atomic) == 0;
647   g_static_mutex_unlock (&g_atomic_lock);
648
649   return is_zero;
650 }
651
652 gboolean
653 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
654                                      gint           oldval,
655                                      gint           newval)
656 {
657   gboolean success;
658
659   g_static_mutex_lock (&g_atomic_lock);
660
661   if ((success = (*atomic == oldval)))
662     *atomic = newval;
663
664   g_static_mutex_unlock (&g_atomic_lock);
665
666   return success;
667 }
668
669 gint
670 (g_atomic_int_add) (volatile gint *atomic,
671                     gint           val)
672 {
673   gint oldval;
674
675   g_static_mutex_lock (&g_atomic_lock);
676   oldval = *atomic;
677   *atomic = oldval + val;
678   g_static_mutex_unlock (&g_atomic_lock);
679
680   return oldval;
681 }
682
683 guint
684 (g_atomic_int_and) (volatile guint *atomic,
685                     guint           val)
686 {
687   guint oldval;
688
689   g_static_mutex_lock (&g_atomic_lock);
690   oldval = *atomic;
691   *atomic = oldval & val;
692   g_static_mutex_unlock (&g_atomic_lock);
693
694   return oldval;
695 }
696
697 guint
698 (g_atomic_int_or) (volatile guint *atomic,
699                    guint           val)
700 {
701   guint oldval;
702
703   g_static_mutex_lock (&g_atomic_lock);
704   oldval = *atomic;
705   *atomic = oldval | val;
706   g_static_mutex_unlock (&g_atomic_lock);
707
708   return oldval;
709 }
710
711 guint
712 (g_atomic_int_xor) (volatile guint *atomic,
713                     guint           val)
714 {
715   guint oldval;
716
717   g_static_mutex_lock (&g_atomic_lock);
718   oldval = *atomic;
719   *atomic = oldval ^ val;
720   g_static_mutex_unlock (&g_atomic_lock);
721
722   return oldval;
723 }
724
725
726 gpointer
727 (g_atomic_pointer_get) (volatile void *atomic)
728 {
729   volatile gpointer *ptr = atomic;
730   gpointer value;
731
732   g_static_mutex_lock (&g_atomic_lock);
733   value = *ptr;
734   g_static_mutex_unlock (&g_atomic_lock);
735
736   return value;
737 }
738
739 void
740 (g_atomic_pointer_set) (volatile void *atomic,
741                         gpointer       newval)
742 {
743   volatile gpointer *ptr = atomic;
744
745   g_static_mutex_lock (&g_atomic_lock);
746   *ptr = newval;
747   g_static_mutex_unlock (&g_atomic_lock);
748 }
749
750 gboolean
751 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
752                                          gpointer       oldval,
753                                          gpointer       newval)
754 {
755   volatile gpointer *ptr = atomic;
756   gboolean success;
757
758   g_static_mutex_lock (&g_atomic_lock);
759
760   if ((success = (*ptr == oldval)))
761     *ptr = newval;
762
763   g_static_mutex_unlock (&g_atomic_lock);
764
765   return success;
766 }
767
768 gssize
769 (g_atomic_pointer_add) (volatile void *atomic,
770                         gssize         val)
771 {
772   volatile gssize *ptr = atomic;
773   gssize oldval;
774
775   g_static_mutex_lock (&g_atomic_lock);
776   oldval = *ptr;
777   *ptr = oldval + val;
778   g_static_mutex_unlock (&g_atomic_lock);
779
780   return oldval;
781 }
782
783 gsize
784 (g_atomic_pointer_and) (volatile void *atomic,
785                         gsize          val)
786 {
787   volatile gsize *ptr = atomic;
788   gsize oldval;
789
790   g_static_mutex_lock (&g_atomic_lock);
791   oldval = *ptr;
792   *ptr = oldval & val;
793   g_static_mutex_unlock (&g_atomic_lock);
794
795   return oldval;
796 }
797
798 gsize
799 (g_atomic_pointer_or) (volatile void *atomic,
800                        gsize          val)
801 {
802   volatile gsize *ptr = atomic;
803   gsize oldval;
804
805   g_static_mutex_lock (&g_atomic_lock);
806   oldval = *ptr;
807   *ptr = oldval | val;
808   g_static_mutex_unlock (&g_atomic_lock);
809
810   return oldval;
811 }
812
813 gsize
814 (g_atomic_pointer_xor) (volatile void *atomic,
815                         gsize          val)
816 {
817   volatile gsize *ptr = atomic;
818   gsize oldval;
819
820   g_static_mutex_lock (&g_atomic_lock);
821   oldval = *ptr;
822   *ptr = oldval ^ val;
823   g_static_mutex_unlock (&g_atomic_lock);
824
825   return oldval;
826 }
827
828 #endif
829
830 /**
831  * g_atomic_int_exchange_and_add:
832  * @atomic: a pointer to a #gint
833  * @val: the value to add
834  *
835  * This function existed before g_atomic_int_add() returned the prior
836  * value of the integer (which it now does).  It is retained only for
837  * compatibility reasons.  Don't use this function in new code.
838  *
839  * Returns: the value of @atomic before the add, signed
840  * Since: 2.4
841  * Deprecated: 2.30: Use g_atomic_int_add() instead.
842  **/
843 gint
844 g_atomic_int_exchange_and_add (volatile gint *atomic,
845                                gint           val)
846 {
847   return (g_atomic_int_add) (atomic, val);
848 }