gatomic.c 19.3 KB
Newer Older
1 2 3
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
4
 * g_atomic_*: atomic operations.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (C) 2003 Sebastian Wilhelmi
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
23
#include "config.h"
24

25
#include "glib.h"
26
#include "gthreadprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
27
#include "galias.h"
28 29 30 31 32 33

#if defined (__GNUC__)
# if defined (G_ATOMIC_I486)
/* Adapted from CVS version 1.10 of glibc's sysdeps/i386/i486/bits/atomic.h 
 */
gint
34 35
g_atomic_int_exchange_and_add (volatile gint *atomic, 
			       gint           val)
36 37 38 39 40 41 42 43 44 45
{
  gint result;

  __asm__ __volatile__ ("lock; xaddl %0,%1"
                        : "=r" (result), "=m" (*atomic) 
			: "0" (val), "m" (*atomic));
  return result;
}
 
void
46 47
g_atomic_int_add (volatile gint *atomic, 
		  gint           val)
48 49 50 51 52 53 54
{
  __asm__ __volatile__ ("lock; addl %1,%0"
			: "=m" (*atomic) 
			: "ir" (val), "m" (*atomic));
}

gboolean
55 56 57
g_atomic_int_compare_and_exchange (volatile gint *atomic, 
				   gint           oldval, 
				   gint           newval)
58 59 60
{
  gint result;
 
61 62 63
  __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
			: "=a" (result), "=m" (*atomic)
			: "r" (newval), "m" (*atomic), "0" (oldval)); 
64 65 66 67 68 69 70 71 72

  return result == oldval;
}

/* The same code as above, as on i386 gpointer is 32 bit as well.
 * Duplicating the code here seems more natural than casting the
 * arguments and calling the former function */

gboolean
73 74 75
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
76 77 78
{
  gpointer result;
 
79 80 81
  __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
			: "=a" (result), "=m" (*atomic)
			: "r" (newval), "m" (*atomic), "0" (oldval)); 
82 83 84 85 86 87 88 89 90 91

  return result == oldval;
}

# elif defined (G_ATOMIC_SPARCV9)
/* Adapted from CVS version 1.3 of glibc's sysdeps/sparc/sparc64/bits/atomic.h
 */
#  define ATOMIC_INT_CMP_XCHG(atomic, oldval, newval)			\
  ({ 									\
     gint __result;							\
92 93 94 95
     __asm__ __volatile__ ("cas [%4], %2, %0"				\
                           : "=r" (__result), "=m" (*(atomic))		\
                           : "r" (oldval), "m" (*(atomic)), "r" (atomic),\
                           "0" (newval));				\
96 97 98 99 100
     __result == oldval;						\
  })

#  if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
gboolean
101 102 103
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
104 105
{
  gpointer result;
106 107 108 109
  __asm__ __volatile__ ("cas [%4], %2, %0"
			: "=r" (result), "=m" (*atomic)
			: "r" (oldval), "m" (*atomic), "r" (atomic),
			"0" (newval));
110 111 112 113
  return result == oldval;
}
#  elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean
114 115 116
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
117 118 119
{
  gpointer result;
  gpointer *a = atomic;
120 121 122 123
  __asm__ __volatile__ ("casx [%4], %2, %0"
			: "=r" (result), "=m" (*a)
			: "r" (oldval), "m" (*a), "r" (a),
			"0" (newval));
124
  return result == oldval;
125 126 127 128 129
}
#  else /* What's that */
#    error "Your system has an unsupported pointer size"
#  endif /* GLIB_SIZEOF_VOID_P */
#  define G_ATOMIC_MEMORY_BARRIER					\
130 131
  __asm__ __volatile__ ("membar #LoadLoad | #LoadStore"			\
                        " | #StoreLoad | #StoreStore" : : : "memory")
132 133 134

# elif defined (G_ATOMIC_ALPHA)
/* Adapted from CVS version 1.3 of glibc's sysdeps/alpha/bits/atomic.h
135
 */
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#  define ATOMIC_INT_CMP_XCHG(atomic, oldval, newval)			\
  ({ 									\
     gint __result;							\
     gint __prev;							\
     __asm__ __volatile__ (						\
        "       mb\n"							\
        "1:     ldl_l   %0,%2\n"					\
        "       cmpeq   %0,%3,%1\n"					\
        "       beq     %1,2f\n"					\
        "       mov     %4,%1\n"					\
        "       stl_c   %1,%2\n"					\
        "       beq     %1,1b\n"					\
        "       mb\n"							\
        "2:"								\
        : "=&r" (__prev), 						\
          "=&r" (__result)						\
        : "m" (*(atomic)),						\
          "Ir" (oldval),						\
          "Ir" (newval)							\
        : "memory");							\
     __result != 0;							\
  })
#  if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
gboolean
160 161 162
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
  gint result;
  gpointer prev;
  __asm__ __volatile__ (
        "       mb\n"
        "1:     ldl_l   %0,%2\n"
        "       cmpeq   %0,%3,%1\n"
        "       beq     %1,2f\n"
        "       mov     %4,%1\n"
        "       stl_c   %1,%2\n"
        "       beq     %1,1b\n"
        "       mb\n"
        "2:"
        : "=&r" (prev), 
          "=&r" (result)
        : "m" (*atomic),
          "Ir" (oldval),
          "Ir" (newval)
        : "memory");
  return result != 0;
}
#  elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean
186 187 188
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
{
  gint result;
  gpointer prev;
  __asm__ __volatile__ (
        "       mb\n"
        "1:     ldq_l   %0,%2\n"
        "       cmpeq   %0,%3,%1\n"
        "       beq     %1,2f\n"
        "       mov     %4,%1\n"
        "       stq_c   %1,%2\n"
        "       beq     %1,1b\n"
        "       mb\n"
        "2:"
        : "=&r" (prev), 
          "=&r" (result)
        : "m" (*atomic),
          "Ir" (oldval),
          "Ir" (newval)
        : "memory");
  return result != 0;
}
#  else /* What's that */
#   error "Your system has an unsupported pointer size"
#  endif /* GLIB_SIZEOF_VOID_P */
213
#  define G_ATOMIC_MEMORY_BARRIER  __asm__ ("mb" : : : "memory")
214 215 216 217
# elif defined (G_ATOMIC_X86_64)
/* Adapted from CVS version 1.9 of glibc's sysdeps/x86_64/bits/atomic.h 
 */
gint
218 219
g_atomic_int_exchange_and_add (volatile gint *atomic,
			       gint           val)
220 221
{
  gint result;
222

223 224 225 226 227 228 229
  __asm__ __volatile__ ("lock; xaddl %0,%1"
                        : "=r" (result), "=m" (*atomic) 
			: "0" (val), "m" (*atomic));
  return result;
}
 
void
230 231
g_atomic_int_add (volatile gint *atomic, 
		  gint           val)
232
{
233 234 235 236 237 238
  __asm__ __volatile__ ("lock; addl %1,%0"
			: "=m" (*atomic) 
			: "ir" (val), "m" (*atomic));
}

gboolean
239 240 241
g_atomic_int_compare_and_exchange (volatile gint *atomic, 
				   gint           oldval, 
				   gint           newval)
242 243 244
{
  gint result;
 
245 246 247
  __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
			: "=a" (result), "=m" (*atomic)
			: "r" (newval), "m" (*atomic), "0" (oldval)); 
248 249

  return result == oldval;
250 251
}

252
gboolean
253 254 255
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
256 257 258
{
  gpointer result;
 
259 260 261
  __asm__ __volatile__ ("lock; cmpxchgq %q2, %1"
			: "=a" (result), "=m" (*atomic)
			: "r" (newval), "m" (*atomic), "0" (oldval)); 
262

263 264 265 266
  return result == oldval;
}

# elif defined (G_ATOMIC_POWERPC)
267 268 269
/* Adapted from CVS version 1.16 of glibc's sysdeps/powerpc/bits/atomic.h 
 * and CVS version 1.4 of glibc's sysdeps/powerpc/powerpc32/bits/atomic.h 
 * and CVS version 1.7 of glibc's sysdeps/powerpc/powerpc64/bits/atomic.h 
270
 */
271 272 273
#   ifdef __OPTIMIZE__
/* Non-optimizing compile bails on the following two asm statements
 * for reasons unknown to the author */
274
gint
275 276
g_atomic_int_exchange_and_add (volatile gint *atomic, 
			       gint           val)
277 278
{
  gint result, temp;
279
  __asm__ __volatile__ (".Lieaa%=:       lwarx   %0,0,%3\n"
280 281
			"         add     %1,%0,%4\n"
			"         stwcx.  %1,0,%3\n"
282
			"         bne-    .Lieaa%="
283
			: "=&b" (result), "=&r" (temp), "=m" (*atomic)
284
			: "b" (atomic), "r" (val), "m" (*atomic)
285
			: "cr0", "memory");
286 287 288 289
  return result;
}
 
/* The same as above, to save a function call repeated here */
290
void
291 292
g_atomic_int_add (volatile gint *atomic, 
		  gint           val)
293
{
294
  gint result, temp;  
295
  __asm__ __volatile__ (".Lia%=:       lwarx   %0,0,%3\n"
296 297
			"         add     %1,%0,%4\n"
			"         stwcx.  %1,0,%3\n"
298
			"         bne-    .Lia%="
299
			: "=&b" (result), "=&r" (temp), "=m" (*atomic)
300
			: "b" (atomic), "r" (val), "m" (*atomic)
301
			: "cr0", "memory");
302
}
303 304
#   else /* !__OPTIMIZE__ */
gint
305 306
g_atomic_int_exchange_and_add (volatile gint *atomic, 
			       gint           val)
307 308 309 310 311 312 313 314 315 316
{
  gint result;
  do
    result = *atomic;
  while (!g_atomic_int_compare_and_exchange (atomic, result, result + val));

  return result;
}
 
void
317 318
g_atomic_int_add (volatile gint *atomic,
		  gint           val)
319 320 321 322 323 324 325
{
  gint result;
  do
    result = *atomic;
  while (!g_atomic_int_compare_and_exchange (atomic, result, result + val));
}
#   endif /* !__OPTIMIZE__ */
326

327
#   if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
328
gboolean
329 330 331
g_atomic_int_compare_and_exchange (volatile gint *atomic, 
				   gint           oldval, 
				   gint           newval)
332
{
333
  gint result;
334
  __asm__ __volatile__ ("sync\n"
335
			".L1icae%=: lwarx   %0,0,%1\n"
336
			"   subf.   %0,%2,%0\n"
337
			"   bne     .L2icae%=\n"
338
			"   stwcx.  %3,0,%1\n"
339 340
			"   bne-    .L1icae%=\n"
			".L2icae%=: isync"
341 342 343
			: "=&r" (result)
			: "b" (atomic), "r" (oldval), "r" (newval)
			: "cr0", "memory"); 
344
  return result == 0;
345 346 347
}

gboolean
348 349 350
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
351
{
352
  gpointer result;
353
  __asm__ __volatile__ ("sync\n"
354
			".L1pcae%=: lwarx   %0,0,%1\n"
355
			"   subf.   %0,%2,%0\n"
356
			"   bne     .L2pcae%=\n"
357
			"   stwcx.  %3,0,%1\n"
358 359
			"   bne-    .L1pcae%=\n"
			".L2pcae%=: isync"
360 361 362
			: "=&r" (result)
			: "b" (atomic), "r" (oldval), "r" (newval)
			: "cr0", "memory"); 
363 364 365 366
  return result == 0;
}
#   elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean
367 368 369
g_atomic_int_compare_and_exchange (volatile gint *atomic,
				   gint           oldval, 
				   gint           newval)
370
{
371
  gpointer result;
372
  __asm__ __volatile__ ("sync\n"
373
			".L1icae%=: lwarx   %0,0,%1\n"
374 375
			"   extsw   %0,%0\n"
			"   subf.   %0,%2,%0\n"
376
			"   bne     .L2icae%=\n"
377
			"   stwcx.  %3,0,%1\n"
378 379
			"   bne-    .L1icae%=\n"
			".L2icae%=: isync"
380 381 382
			: "=&r" (result)
			: "b" (atomic), "r" (oldval), "r" (newval)
			: "cr0", "memory"); 
383
  return result == 0;
384 385
}

386
gboolean
387 388 389
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
390
{
391
  gpointer result;
392
  __asm__ __volatile__ ("sync\n"
393
			".L1pcae%=: ldarx   %0,0,%1\n"
394
			"   subf.   %0,%2,%0\n"
395
			"   bne     .L2pcae%=\n"
396
			"   stdcx.  %3,0,%1\n"
397 398
			"   bne-    .L1pcae%=\n"
			".L2pcae%=: isync"
399 400 401
			: "=&r" (result)
			: "b" (atomic), "r" (oldval), "r" (newval)
			: "cr0", "memory"); 
402
  return result == 0;
403
}
404 405 406 407
#  else /* What's that */
#   error "Your system has an unsupported pointer size"
#  endif /* GLIB_SIZEOF_VOID_P */

408
#  define G_ATOMIC_MEMORY_BARRIER __asm__ ("sync" : : : "memory")
409

410 411 412 413
# elif defined (G_ATOMIC_IA64)
/* Adapted from CVS version 1.8 of glibc's sysdeps/ia64/bits/atomic.h
 */
gint
414 415
g_atomic_int_exchange_and_add (volatile gint *atomic,
			       gint           val)
416
{
Matthias Clasen's avatar
Matthias Clasen committed
417
  return __sync_fetch_and_add (atomic, val);
418 419 420
}
 
void
421
g_atomic_int_add (volatile gint *atomic, 
422 423
		  gint val)
{
Matthias Clasen's avatar
Matthias Clasen committed
424
  __sync_fetch_and_add (atomic, val);
425 426 427
}

gboolean
428 429 430
g_atomic_int_compare_and_exchange (volatile gint *atomic,
				   gint           oldval, 
				   gint           newval)
431
{
Matthias Clasen's avatar
Matthias Clasen committed
432
  return __sync_bool_compare_and_swap (atomic, oldval, newval);
433 434 435
}

gboolean
436 437 438
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
				       gpointer           oldval, 
				       gpointer           newval)
439
{
Matthias Clasen's avatar
Matthias Clasen committed
440 441
  return __sync_bool_compare_and_swap ((long *)atomic, 
				       (long)oldval, (long)newval);
442 443 444
}

#  define G_ATOMIC_MEMORY_BARRIER __sync_synchronize ()
445 446 447 448 449 450 451 452 453 454 455 456 457 458
# elif defined (G_ATOMIC_S390)
/* Adapted from glibc's sysdeps/s390/bits/atomic.h
 */
#  define ATOMIC_INT_CMP_XCHG(atomic, oldval, newval)			\
  ({ 									\
     gint __result = oldval;					\
     __asm__ __volatile__ ("cs %0, %2, %1"				\
                           : "+d" (__result), "=Q" (*(atomic))		\
                           : "d" (newval), "m" (*(atomic)) : "cc" );	\
     __result == oldval;						\
  })

#  if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
gboolean
459 460 461
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
				       gpointer           oldval,
				       gpointer           newval)
462 463 464 465 466
{
  gpointer result = oldval;
  __asm__ __volatile__ ("cs %0, %2, %1"
			: "+d" (result), "=Q" (*(atomic))
			: "d" (newval), "m" (*(atomic)) : "cc" );
467
  return result == oldval;
468 469 470
}
#  elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean
471 472 473
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
				       gpointer           oldval,
				       gpointer           newval)
474 475 476 477 478 479
{
  gpointer result = oldval;
  gpointer *a = atomic;
  __asm__ __volatile__ ("csg %0, %2, %1"
			: "+d" (result), "=Q" (*a)
			: "d" ((long)(newval)), "m" (*a) : "cc" );
480
  return result == oldval;
481 482 483 484 485
}
#  else /* What's that */
#    error "Your system has an unsupported pointer size"
#  endif /* GLIB_SIZEOF_VOID_P */
# else /* !G_ATOMIC_IA64 */
486
#  define DEFINE_WITH_MUTEXES
487
# endif /* G_ATOMIC_IA64 */
488
#else /* !__GNUC__ */
489 490 491 492 493
# ifdef G_PLATFORM_WIN32
#  define DEFINE_WITH_WIN32_INTERLOCKED
# else
#  define DEFINE_WITH_MUTEXES
# endif
494
#endif /* __GNUC__ */
495

496 497
#ifdef DEFINE_WITH_WIN32_INTERLOCKED
# include <windows.h>
498 499 500 501 502 503 504 505 506 507 508
/* Following indicates that InterlockedCompareExchangePointer is
 * declared in winbase.h (included by windows.h) and needs to be
 * commented out if not true. It is defined iff WINVER > 0x0400,
 * which is usually correct but can be wrong if WINVER is set before
 * windows.h is included.
 */
# if WINVER > 0x0400
#  define HAVE_INTERLOCKED_COMPARE_EXCHANGE_POINTER
# endif

gint32
509 510
g_atomic_int_exchange_and_add (volatile gint32 *atomic,
			       gint32           val)
511 512 513 514 515
{
  return InterlockedExchangeAdd (atomic, val);
}

void     
516 517
g_atomic_int_add (volatile gint32 *atomic, 
		  gint32           val)
518 519 520 521 522
{
  InterlockedExchangeAdd (atomic, val);
}

gboolean 
523 524 525
g_atomic_int_compare_and_exchange (volatile gint32 *atomic,
				   gint32           oldval,
				   gint32           newval)
526
{
527
#ifndef HAVE_INTERLOCKED_COMPARE_EXCHANGE_POINTER
528 529 530
  return (guint32) InterlockedCompareExchange ((PVOID*)atomic, 
                                               (PVOID)newval, 
                                               (PVOID)oldval) == oldval;
531 532 533 534 535
#else
  return InterlockedCompareExchange (atomic, 
                                     newval, 
                                     oldval) == oldval;
#endif
536 537 538
}

gboolean 
539 540 541
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
				       gpointer           oldval,
				       gpointer           newval)
542
{
543 544
# ifdef HAVE_INTERLOCKED_COMPARE_EXCHANGE_POINTER
  return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
545
# else
546 547 548
#  if GLIB_SIZEOF_VOID_P != 4 /* no 32-bit system */
#   error "InterlockedCompareExchangePointer needed"
#  else
549
   return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
550
#  endif
551 552 553 554
# endif
}
#endif /* DEFINE_WITH_WIN32_INTERLOCKED */

555
#ifdef DEFINE_WITH_MUTEXES
556
/* We have to use the slow, but safe locking method */
557 558
static GMutex *g_atomic_mutex; 

559
gint
560 561
g_atomic_int_exchange_and_add (volatile gint *atomic, 
			       gint           val)
562
{
563
  gint result;
564
    
565
  g_mutex_lock (g_atomic_mutex);
566 567
  result = *atomic;
  *atomic += val;
568
  g_mutex_unlock (g_atomic_mutex);
569 570 571 572 573 574

  return result;
}


void
575 576
g_atomic_int_add (volatile gint *atomic,
		  gint           val)
577
{
578
  g_mutex_lock (g_atomic_mutex);
579
  *atomic += val;
580
  g_mutex_unlock (g_atomic_mutex);
581 582 583
}

gboolean
584 585 586
g_atomic_int_compare_and_exchange (volatile gint *atomic, 
				   gint           oldval, 
				   gint           newval)
587 588 589
{
  gboolean result;
    
590
  g_mutex_lock (g_atomic_mutex);
591 592 593 594 595 596 597
  if (*atomic == oldval)
    {
      result = TRUE;
      *atomic = newval;
    }
  else
    result = FALSE;
598
  g_mutex_unlock (g_atomic_mutex);
599 600 601 602 603

  return result;
}

gboolean
604 605 606
g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic, 
				       gpointer           oldval, 
				       gpointer           newval)
607 608 609
{
  gboolean result;
    
610
  g_mutex_lock (g_atomic_mutex);
611 612 613 614 615 616 617
  if (*atomic == oldval)
    {
      result = TRUE;
      *atomic = newval;
    }
  else
    result = FALSE;
618
  g_mutex_unlock (g_atomic_mutex);
619 620 621 622

  return result;
}

623
#ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
624
gint
625
g_atomic_int_get (volatile gint *atomic)
626
{
627
  gint result;
628

629
  g_mutex_lock (g_atomic_mutex);
630
  result = *atomic;
631
  g_mutex_unlock (g_atomic_mutex);
632 633 634 635

  return result;
}

636 637 638 639 640 641 642 643 644
void
g_atomic_int_set (volatile gint *atomic,
                  gint           newval)
{
  g_mutex_lock (g_atomic_mutex);
  *atomic = newval;
  g_mutex_unlock (g_atomic_mutex);
}

645
gpointer
646
g_atomic_pointer_get (volatile gpointer *atomic)
647 648 649
{
  gpointer result;

650
  g_mutex_lock (g_atomic_mutex);
651
  result = *atomic;
652
  g_mutex_unlock (g_atomic_mutex);
653 654

  return result;
655
}
656 657 658 659 660 661 662 663 664

void
g_atomic_pointer_set (volatile gpointer *atomic,
                      gpointer           newval)
{
  g_mutex_lock (g_atomic_mutex);
  *atomic = newval;
  g_mutex_unlock (g_atomic_mutex);
}
665
#endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */   
666 667
#elif defined (G_ATOMIC_OP_MEMORY_BARRIER_NEEDED)
gint
668
g_atomic_int_get (volatile gint *atomic)
669 670
{
  G_ATOMIC_MEMORY_BARRIER;
671 672
  return *atomic;
}
673

674 675 676 677 678 679
void
g_atomic_int_set (volatile gint *atomic,
                  gint           newval)
{
  *atomic = newval;
  G_ATOMIC_MEMORY_BARRIER; 
680 681 682
}

gpointer
683
g_atomic_pointer_get (volatile gpointer *atomic)
684
{
685
  G_ATOMIC_MEMORY_BARRIER;
686
  return *atomic;
687
}   
688 689 690 691 692 693 694 695

void
g_atomic_pointer_set (volatile gpointer *atomic,
                      gpointer           newval)
{
  *atomic = newval;
  G_ATOMIC_MEMORY_BARRIER; 
}
696 697 698 699
#endif /* DEFINE_WITH_MUTEXES || G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */

#ifdef ATOMIC_INT_CMP_XCHG
gboolean
700 701 702
g_atomic_int_compare_and_exchange (volatile gint *atomic,
				   gint           oldval,
				   gint           newval)
703 704
{
  return ATOMIC_INT_CMP_XCHG (atomic, oldval, newval);
705 706
}

707
gint
708 709
g_atomic_int_exchange_and_add (volatile gint *atomic,
			       gint           val)
710 711 712 713 714 715 716 717 718 719
{
  gint result;
  do
    result = *atomic;
  while (!ATOMIC_INT_CMP_XCHG (atomic, result, result + val));

  return result;
}
 
void
720 721
g_atomic_int_add (volatile gint *atomic,
		  gint           val)
722 723 724 725 726 727 728
{
  gint result;
  do
    result = *atomic;
  while (!ATOMIC_INT_CMP_XCHG (atomic, result, result + val));
}
#endif /* ATOMIC_INT_CMP_XCHG */
729 730

void 
731
_g_atomic_thread_init (void)
732 733 734 735 736
{
#ifdef DEFINE_WITH_MUTEXES
  g_atomic_mutex = g_mutex_new ();
#endif /* DEFINE_WITH_MUTEXES */
}
737

738 739
#ifndef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
gint
740
(g_atomic_int_get) (volatile gint *atomic)
741 742 743 744
{
  return g_atomic_int_get (atomic);
}

745 746 747 748 749 750 751
void
(g_atomic_int_set) (volatile gint *atomic,
		    gint           newval)
{
  g_atomic_int_set (atomic, newval);
}

752
gpointer
753
(g_atomic_pointer_get) (volatile gpointer *atomic)
754 755 756
{
  return g_atomic_pointer_get (atomic);
}
757 758 759 760 761 762 763

void
(g_atomic_pointer_set) (volatile gpointer *atomic,
			gpointer           newval)
{
  g_atomic_pointer_set (atomic, newval);
}
764 765
#endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */

Matthias Clasen's avatar
Matthias Clasen committed
766
#define __G_ATOMIC_C__
767
#include "galiasdef.c"