pt-machine.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  3. */
  4. #ifndef _PT_MACHINE_H
  5. #define _PT_MACHINE_H 1
  6. #include <features.h>
  7. #ifndef PT_EI
  8. # define PT_EI __extern_always_inline
  9. #endif
  10. #define HAS_COMPARE_AND_SWAP
  11. PT_EI int
  12. __compare_and_swap (long int *p, long int oldval, long int newval)
  13. {
  14. long int ret, temp;
  15. __asm__ __volatile__
  16. ("/* Inline compare & swap */\n"
  17. "1:\n\t"
  18. "lr.d %1,%2\n\t"
  19. "li %0,0\n\t"
  20. "bne %1,%3,2f\n\t"
  21. "li %0,1\n\t"
  22. "sc.d %1,%4,%2\n\t"
  23. "bnez %1,1b\n"
  24. "2:\n\t"
  25. "/* End compare & swap */"
  26. : "=&r" (ret), "=&r" (temp), "+A" (*p)
  27. : "r" (oldval), "r" (newval)
  28. : "memory");
  29. return ret;
  30. }
  31. extern long int testandset (int *spinlock);
  32. PT_EI long int
  33. testandset (int *spinlock)
  34. {
  35. unsigned int old = 1;
  36. int tmp = 1;
  37. __asm__ __volatile__ (
  38. "amoswap.w %0, %2, %1"
  39. : "=r" (old), "+A" (*spinlock)
  40. : "r" (tmp)
  41. : "memory");
  42. return old;
  43. }
  44. /* Get some notion of the current stack. Need not be exactly the top
  45. of the stack, just something somewhere in the current frame. */
  46. #define CURRENT_STACK_FRAME stack_pointer
  47. register char * stack_pointer __asm__ ("sp");
  48. #else
  49. #error PT_MACHINE already defined
  50. #endif /* pt-machine.h */