pt-machine.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Machine-dependent pthreads configuration and inline functions.
  2. *
  3. * Copyright (C) 2005-2007 Atmel Corporation
  4. *
  5. * This file is subject to the terms and conditions of the GNU Lesser General
  6. * Public License. See the file "COPYING.LIB" in the main directory of this
  7. * archive for more details.
  8. */
  9. #ifndef _PT_MACHINE_H
  10. #define _PT_MACHINE_H 1
  11. #include <features.h>
  12. static inline int
  13. _test_and_set (int *p, int v) __THROW
  14. {
  15. int result;
  16. __asm__ __volatile__(
  17. "/* Inline test and set */\n"
  18. " xchg %[old], %[mem], %[new]"
  19. : [old] "=&r"(result)
  20. : [mem] "r"(p), [new] "r"(v)
  21. : "memory");
  22. return result;
  23. }
  24. #ifndef PT_EI
  25. # define PT_EI extern inline
  26. #endif
  27. extern long int testandset (int *spinlock);
  28. extern int __compare_and_swap (long int *p, long int oldval, long int newval);
  29. /* Spinlock implementation; required. */
  30. PT_EI long int
  31. testandset (int *spinlock)
  32. {
  33. return _test_and_set(spinlock, 1);
  34. }
  35. /* Get some notion of the current stack. Need not be exactly the top
  36. of the stack, just something somewhere in the current frame. */
  37. #define CURRENT_STACK_FRAME stack_pointer
  38. register char * stack_pointer __asm__ ("sp");
  39. /* Compare-and-swap for semaphores. */
  40. #define HAS_COMPARE_AND_SWAP
  41. PT_EI int
  42. __compare_and_swap(long int *p, long int oldval, long int newval)
  43. {
  44. int result;
  45. __asm__ __volatile__(
  46. "/* Inline compare and swap */\n"
  47. "1: ssrf 5\n"
  48. " ld.w %[result], %[mem]\n"
  49. " eor %[result], %[old]\n"
  50. " brne 2f\n"
  51. " stcond %[mem], %[new]\n"
  52. " brne 1b\n"
  53. "2:"
  54. : [result] "=&r"(result), [mem] "=m"(*p)
  55. : "m"(*p), [new] "r"(newval), [old] "r"(oldval)
  56. : "cc", "memory");
  57. return result == 0;
  58. }
  59. #endif /* pt-machine.h */