atomicity.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Low-level functions for atomic operations. m680x0 version, x >= 2.
  2. Copyright (C) 1997 Free Software Foundation, Inc.
  3. Contributed by Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but 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. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _ATOMICITY_H
  18. #define _ATOMICITY_H 1
  19. #include <inttypes.h>
  20. static inline int
  21. __attribute_used__
  22. exchange_and_add (volatile uint32_t *mem, int val)
  23. {
  24. register int result = *mem;
  25. register int temp;
  26. __asm__ __volatile__ ("1: move%.l %0,%1;"
  27. " add%.l %2,%1;"
  28. " cas%.l %0,%1,%3;"
  29. " jbne 1b"
  30. : "=d" (result), "=&d" (temp)
  31. : "d" (val), "m" (*mem), "0" (result) : "memory");
  32. return result;
  33. }
  34. static inline void
  35. __attribute_used__
  36. atomic_add (volatile uint32_t *mem, int val)
  37. {
  38. /* XXX Use cas here as well? */
  39. __asm__ __volatile__ ("add%.l %0,%1"
  40. : : "id" (val), "m" (*mem) : "memory");
  41. }
  42. static inline int
  43. __attribute_used__
  44. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  45. {
  46. char ret;
  47. long int readval;
  48. __asm__ __volatile__ ("cas%.l %2,%3,%1; seq %0"
  49. : "=dm" (ret), "=m" (*p), "=d" (readval)
  50. : "d" (newval), "m" (*p), "2" (oldval));
  51. return ret;
  52. }
  53. #endif /* atomicity.h */