224 |
* Include the rest of the atomic ops macros which are common to both |
* Include the rest of the atomic ops macros which are common to both |
225 |
* powerpc32 and powerpc64. |
* powerpc32 and powerpc64. |
226 |
*/ |
*/ |
227 |
|
#if _LIBC |
228 |
#include_next <bits/atomic.h> |
#include_next <bits/atomic.h> |
229 |
|
#els |
230 |
|
/* Atomic operations. PowerPC Common version. |
231 |
|
Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
232 |
|
This file is part of the GNU C Library. |
233 |
|
Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003. |
234 |
|
|
235 |
|
The GNU C Library is free software; you can redistribute it and/or |
236 |
|
modify it under the terms of the GNU Lesser General Public |
237 |
|
License as published by the Free Software Foundation; either |
238 |
|
version 2.1 of the License, or (at your option) any later version. |
239 |
|
|
240 |
|
The GNU C Library is distributed in the hope that it will be useful, |
241 |
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
242 |
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
243 |
|
Lesser General Public License for more details. |
244 |
|
|
245 |
|
You should have received a copy of the GNU Lesser General Public |
246 |
|
License along with the GNU C Library; if not, write to the Free |
247 |
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
248 |
|
02111-1307 USA. */ |
249 |
|
|
250 |
|
/* |
251 |
|
* Never include sysdeps/powerpc/bits/atomic.h directly. |
252 |
|
* Alway use include/atomic.h which will include either |
253 |
|
* sysdeps/powerpc/powerpc32/bits/atomic.h |
254 |
|
* or |
255 |
|
* sysdeps/powerpc/powerpc64/bits/atomic.h |
256 |
|
* as appropriate and which in turn include this file. |
257 |
|
*/ |
258 |
|
|
259 |
|
#include <stdint.h> |
260 |
|
|
261 |
|
typedef int32_t atomic32_t; |
262 |
|
typedef uint32_t uatomic32_t; |
263 |
|
typedef int_fast32_t atomic_fast32_t; |
264 |
|
typedef uint_fast32_t uatomic_fast32_t; |
265 |
|
|
266 |
|
typedef int64_t atomic64_t; |
267 |
|
typedef uint64_t uatomic64_t; |
268 |
|
typedef int_fast64_t atomic_fast64_t; |
269 |
|
typedef uint_fast64_t uatomic_fast64_t; |
270 |
|
|
271 |
|
typedef intptr_t atomicptr_t; |
272 |
|
typedef uintptr_t uatomicptr_t; |
273 |
|
typedef intmax_t atomic_max_t; |
274 |
|
typedef uintmax_t uatomic_max_t; |
275 |
|
|
276 |
|
/* |
277 |
|
* Powerpc does not have byte and halfword forms of load and reserve and |
278 |
|
* store conditional. So for powerpc we stub out the 8- and 16-bit forms. |
279 |
|
*/ |
280 |
|
#define __arch_compare_and_exchange_bool_8_acq(mem, newval, oldval) \ |
281 |
|
(abort (), 0) |
282 |
|
|
283 |
|
#define __arch_compare_and_exchange_bool_16_acq(mem, newval, oldval) \ |
284 |
|
(abort (), 0) |
285 |
|
|
286 |
|
#define __arch_compare_and_exchange_bool_8_rel(mem, newval, oldval) \ |
287 |
|
(abort (), 0) |
288 |
|
|
289 |
|
#define __arch_compare_and_exchange_bool_16_rel(mem, newval, oldval) \ |
290 |
|
(abort (), 0) |
291 |
|
|
292 |
|
#ifdef UP |
293 |
|
# define __ARCH_ACQ_INSTR "" |
294 |
|
# define __ARCH_REL_INSTR "" |
295 |
|
#else |
296 |
|
# define __ARCH_ACQ_INSTR "isync" |
297 |
|
# ifndef __ARCH_REL_INSTR |
298 |
|
# define __ARCH_REL_INSTR "sync" |
299 |
|
# endif |
300 |
|
#endif |
301 |
|
|
302 |
|
#define atomic_full_barrier() __asm ("sync" ::: "memory") |
303 |
|
#define atomic_write_barrier() __asm ("eieio" ::: "memory") |
304 |
|
|
305 |
|
#define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \ |
306 |
|
({ \ |
307 |
|
__typeof (*(mem)) __tmp; \ |
308 |
|
__typeof (mem) __memp = (mem); \ |
309 |
|
__asm __volatile ( \ |
310 |
|
"1: lwarx %0,0,%1\n" \ |
311 |
|
" cmpw %0,%2\n" \ |
312 |
|
" bne 2f\n" \ |
313 |
|
" stwcx. %3,0,%1\n" \ |
314 |
|
" bne- 1b\n" \ |
315 |
|
"2: " __ARCH_ACQ_INSTR \ |
316 |
|
: "=&r" (__tmp) \ |
317 |
|
: "b" (__memp), "r" (oldval), "r" (newval) \ |
318 |
|
: "cr0", "memory"); \ |
319 |
|
__tmp; \ |
320 |
|
}) |
321 |
|
|
322 |
|
#define __arch_compare_and_exchange_val_32_rel(mem, newval, oldval) \ |
323 |
|
({ \ |
324 |
|
__typeof (*(mem)) __tmp; \ |
325 |
|
__typeof (mem) __memp = (mem); \ |
326 |
|
__asm __volatile (__ARCH_REL_INSTR "\n" \ |
327 |
|
"1: lwarx %0,0,%1\n" \ |
328 |
|
" cmpw %0,%2\n" \ |
329 |
|
" bne 2f\n" \ |
330 |
|
" stwcx. %3,0,%1\n" \ |
331 |
|
" bne- 1b\n" \ |
332 |
|
"2: " \ |
333 |
|
: "=&r" (__tmp) \ |
334 |
|
: "b" (__memp), "r" (oldval), "r" (newval) \ |
335 |
|
: "cr0", "memory"); \ |
336 |
|
__tmp; \ |
337 |
|
}) |
338 |
|
|
339 |
|
#define __arch_atomic_exchange_32_acq(mem, value) \ |
340 |
|
({ \ |
341 |
|
__typeof (*mem) __val; \ |
342 |
|
__asm __volatile ( \ |
343 |
|
"1: lwarx %0,0,%2\n" \ |
344 |
|
" stwcx. %3,0,%2\n" \ |
345 |
|
" bne- 1b\n" \ |
346 |
|
" " __ARCH_ACQ_INSTR \ |
347 |
|
: "=&r" (__val), "=m" (*mem) \ |
348 |
|
: "b" (mem), "r" (value), "m" (*mem) \ |
349 |
|
: "cr0", "memory"); \ |
350 |
|
__val; \ |
351 |
|
}) |
352 |
|
|
353 |
|
#define __arch_atomic_exchange_32_rel(mem, value) \ |
354 |
|
({ \ |
355 |
|
__typeof (*mem) __val; \ |
356 |
|
__asm __volatile (__ARCH_REL_INSTR "\n" \ |
357 |
|
"1: lwarx %0,0,%2\n" \ |
358 |
|
" stwcx. %3,0,%2\n" \ |
359 |
|
" bne- 1b" \ |
360 |
|
: "=&r" (__val), "=m" (*mem) \ |
361 |
|
: "b" (mem), "r" (value), "m" (*mem) \ |
362 |
|
: "cr0", "memory"); \ |
363 |
|
__val; \ |
364 |
|
}) |
365 |
|
|
366 |
|
#define __arch_atomic_exchange_and_add_32(mem, value) \ |
367 |
|
({ \ |
368 |
|
__typeof (*mem) __val, __tmp; \ |
369 |
|
__asm __volatile ("1: lwarx %0,0,%3\n" \ |
370 |
|
" add %1,%0,%4\n" \ |
371 |
|
" stwcx. %1,0,%3\n" \ |
372 |
|
" bne- 1b" \ |
373 |
|
: "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \ |
374 |
|
: "b" (mem), "r" (value), "m" (*mem) \ |
375 |
|
: "cr0", "memory"); \ |
376 |
|
__val; \ |
377 |
|
}) |
378 |
|
|
379 |
|
#define __arch_atomic_increment_val_32(mem) \ |
380 |
|
({ \ |
381 |
|
__typeof (*(mem)) __val; \ |
382 |
|
__asm __volatile ("1: lwarx %0,0,%2\n" \ |
383 |
|
" addi %0,%0,1\n" \ |
384 |
|
" stwcx. %0,0,%2\n" \ |
385 |
|
" bne- 1b" \ |
386 |
|
: "=&b" (__val), "=m" (*mem) \ |
387 |
|
: "b" (mem), "m" (*mem) \ |
388 |
|
: "cr0", "memory"); \ |
389 |
|
__val; \ |
390 |
|
}) |
391 |
|
|
392 |
|
#define __arch_atomic_decrement_val_32(mem) \ |
393 |
|
({ \ |
394 |
|
__typeof (*(mem)) __val; \ |
395 |
|
__asm __volatile ("1: lwarx %0,0,%2\n" \ |
396 |
|
" subi %0,%0,1\n" \ |
397 |
|
" stwcx. %0,0,%2\n" \ |
398 |
|
" bne- 1b" \ |
399 |
|
: "=&b" (__val), "=m" (*mem) \ |
400 |
|
: "b" (mem), "m" (*mem) \ |
401 |
|
: "cr0", "memory"); \ |
402 |
|
__val; \ |
403 |
|
}) |
404 |
|
|
405 |
|
#define __arch_atomic_decrement_if_positive_32(mem) \ |
406 |
|
({ int __val, __tmp; \ |
407 |
|
__asm __volatile ("1: lwarx %0,0,%3\n" \ |
408 |
|
" cmpwi 0,%0,0\n" \ |
409 |
|
" addi %1,%0,-1\n" \ |
410 |
|
" ble 2f\n" \ |
411 |
|
" stwcx. %1,0,%3\n" \ |
412 |
|
" bne- 1b\n" \ |
413 |
|
"2: " __ARCH_ACQ_INSTR \ |
414 |
|
: "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \ |
415 |
|
: "b" (mem), "m" (*mem) \ |
416 |
|
: "cr0", "memory"); \ |
417 |
|
__val; \ |
418 |
|
}) |
419 |
|
|
420 |
|
#define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \ |
421 |
|
({ \ |
422 |
|
__typeof (*(mem)) __result; \ |
423 |
|
if (sizeof (*mem) == 4) \ |
424 |
|
__result = __arch_compare_and_exchange_val_32_acq(mem, newval, oldval); \ |
425 |
|
else if (sizeof (*mem) == 8) \ |
426 |
|
__result = __arch_compare_and_exchange_val_64_acq(mem, newval, oldval); \ |
427 |
|
else \ |
428 |
|
abort (); \ |
429 |
|
__result; \ |
430 |
|
}) |
431 |
|
|
432 |
|
#define atomic_compare_and_exchange_val_rel(mem, newval, oldval) \ |
433 |
|
({ \ |
434 |
|
__typeof (*(mem)) __result; \ |
435 |
|
if (sizeof (*mem) == 4) \ |
436 |
|
__result = __arch_compare_and_exchange_val_32_rel(mem, newval, oldval); \ |
437 |
|
else if (sizeof (*mem) == 8) \ |
438 |
|
__result = __arch_compare_and_exchange_val_64_rel(mem, newval, oldval); \ |
439 |
|
else \ |
440 |
|
abort (); \ |
441 |
|
__result; \ |
442 |
|
}) |
443 |
|
|
444 |
|
#define atomic_exchange_acq(mem, value) \ |
445 |
|
({ \ |
446 |
|
__typeof (*(mem)) __result; \ |
447 |
|
if (sizeof (*mem) == 4) \ |
448 |
|
__result = __arch_atomic_exchange_32_acq (mem, value); \ |
449 |
|
else if (sizeof (*mem) == 8) \ |
450 |
|
__result = __arch_atomic_exchange_64_acq (mem, value); \ |
451 |
|
else \ |
452 |
|
abort (); \ |
453 |
|
__result; \ |
454 |
|
}) |
455 |
|
|
456 |
|
#define atomic_exchange_rel(mem, value) \ |
457 |
|
({ \ |
458 |
|
__typeof (*(mem)) __result; \ |
459 |
|
if (sizeof (*mem) == 4) \ |
460 |
|
__result = __arch_atomic_exchange_32_rel (mem, value); \ |
461 |
|
else if (sizeof (*mem) == 8) \ |
462 |
|
__result = __arch_atomic_exchange_64_rel (mem, value); \ |
463 |
|
else \ |
464 |
|
abort (); \ |
465 |
|
__result; \ |
466 |
|
}) |
467 |
|
|
468 |
|
#define atomic_exchange_and_add(mem, value) \ |
469 |
|
({ \ |
470 |
|
__typeof (*(mem)) __result; \ |
471 |
|
if (sizeof (*mem) == 4) \ |
472 |
|
__result = __arch_atomic_exchange_and_add_32 (mem, value); \ |
473 |
|
else if (sizeof (*mem) == 8) \ |
474 |
|
__result = __arch_atomic_exchange_and_add_64 (mem, value); \ |
475 |
|
else \ |
476 |
|
abort (); \ |
477 |
|
__result; \ |
478 |
|
}) |
479 |
|
|
480 |
|
#define atomic_increment_val(mem) \ |
481 |
|
({ \ |
482 |
|
__typeof (*(mem)) __result; \ |
483 |
|
if (sizeof (*(mem)) == 4) \ |
484 |
|
__result = __arch_atomic_increment_val_32 (mem); \ |
485 |
|
else if (sizeof (*(mem)) == 8) \ |
486 |
|
__result = __arch_atomic_increment_val_64 (mem); \ |
487 |
|
else \ |
488 |
|
abort (); \ |
489 |
|
__result; \ |
490 |
|
}) |
491 |
|
|
492 |
|
#define atomic_increment(mem) ({ atomic_increment_val (mem); (void) 0; }) |
493 |
|
|
494 |
|
#define atomic_decrement_val(mem) \ |
495 |
|
({ \ |
496 |
|
__typeof (*(mem)) __result; \ |
497 |
|
if (sizeof (*(mem)) == 4) \ |
498 |
|
__result = __arch_atomic_decrement_val_32 (mem); \ |
499 |
|
else if (sizeof (*(mem)) == 8) \ |
500 |
|
__result = __arch_atomic_decrement_val_64 (mem); \ |
501 |
|
else \ |
502 |
|
abort (); \ |
503 |
|
__result; \ |
504 |
|
}) |
505 |
|
|
506 |
|
#define atomic_decrement(mem) ({ atomic_decrement_val (mem); (void) 0; }) |
507 |
|
|
508 |
|
|
509 |
|
/* Decrement *MEM if it is > 0, and return the old value. */ |
510 |
|
#define atomic_decrement_if_positive(mem) \ |
511 |
|
({ __typeof (*(mem)) __result; \ |
512 |
|
if (sizeof (*mem) == 4) \ |
513 |
|
__result = __arch_atomic_decrement_if_positive_32 (mem); \ |
514 |
|
else if (sizeof (*mem) == 8) \ |
515 |
|
__result = __arch_atomic_decrement_if_positive_64 (mem); \ |
516 |
|
else \ |
517 |
|
abort (); \ |
518 |
|
__result; \ |
519 |
|
}) |
520 |
|
#endif |