atomicity.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Low-level functions for atomic operations. H8/300 version.
  2. Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _ATOMICITY_H
  17. #define _ATOMICITY_H 1
  18. #include <inttypes.h>
  19. static inline uint32_t
  20. __attribute__ ((unused))
  21. exchange_and_add (volatile uint32_t *mem, uint32_t val)
  22. {
  23. uint32_t result;
  24. __asm__ __volatile__ ("stc ccr,@-sp\n\t"
  25. "orc #0x80,ccr\n\t"
  26. "mov.l %1,er1\n\t"
  27. "mov.l %0,%1\n\t"
  28. "add.l er1,%0\n\t"
  29. "ldc @sp+,ccr"
  30. : "=r" (result), "=m" (*mem)
  31. : "0" (val), "1" (*mem)
  32. : "er1");
  33. return result;
  34. }
  35. static inline void
  36. __attribute__ ((unused))
  37. atomic_add (volatile uint32_t *mem, int val)
  38. {
  39. __asm__ __volatile__ ("stc ccr,@-sp\n\t"
  40. "orc #0x80,ccr\n\t"
  41. "mov.l %0,er0\n\t"
  42. "add %1,er0\n\t"
  43. "mov.l er0,%0\n\t"
  44. "ldc @sp+,ccr"
  45. : "=m" (*mem)
  46. : "r" (val), "0" (*mem)
  47. : "er0");
  48. }
  49. static inline int
  50. __attribute__ ((unused))
  51. compare_and_swap (volatile long int *p, long int oldval, long int newval)
  52. {
  53. int ret = 0;
  54. __asm__ __volatile__ ("stc ccr,@-sp\n\t"
  55. "orc #0x80,ccr\n\t"
  56. "mov.l %1,er0\n\t"
  57. "cmp.l %2,er0\n\t"
  58. "bne 1f\n\t"
  59. "mov.l %3,%1\n\t"
  60. "inc.l #1,%0\n"
  61. "1:\n\t"
  62. "ldc @sp+,ccr"
  63. : "=r"(ret),"=m"(*p)
  64. : "r"(oldval),"r"(newval),"0"(ret),"1"(*p)
  65. : "er0");
  66. return ret;
  67. }
  68. #endif /* atomicity.h */