pt-machine.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef PT_EI
  13. # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
  14. # define PT_EI static inline __attribute__((always_inline))
  15. # else
  16. # define PT_EI extern inline __attribute__((always_inline))
  17. # endif
  18. #endif
  19. static inline int
  20. _test_and_set (int *p, int v) __THROW
  21. {
  22. int result;
  23. __asm__ __volatile__(
  24. "/* Inline test and set */\n"
  25. " xchg %[old], %[mem], %[new]"
  26. : [old] "=&r"(result)
  27. : [mem] "r"(p), [new] "r"(v)
  28. : "memory");
  29. return result;
  30. }
  31. extern long int testandset (int *spinlock);
  32. extern int __compare_and_swap (long int *p, long int oldval, long int newval);
  33. /* Spinlock implementation; required. */
  34. PT_EI long int
  35. testandset (int *spinlock)
  36. {
  37. return _test_and_set(spinlock, 1);
  38. }
  39. /* Get some notion of the current stack. Need not be exactly the top
  40. of the stack, just something somewhere in the current frame. */
  41. #define CURRENT_STACK_FRAME stack_pointer
  42. register char * stack_pointer __asm__ ("sp");
  43. /* Compare-and-swap for semaphores. */
  44. #define HAS_COMPARE_AND_SWAP
  45. PT_EI int
  46. __compare_and_swap(long int *p, long int oldval, long int newval)
  47. {
  48. int result;
  49. __asm__ __volatile__(
  50. "/* Inline compare and swap */\n"
  51. "1: ssrf 5\n"
  52. " ld.w %[result], %[mem]\n"
  53. " eor %[result], %[old]\n"
  54. " brne 2f\n"
  55. " stcond %[mem], %[new]\n"
  56. " brne 1b\n"
  57. "2:"
  58. : [result] "=&r"(result), [mem] "=m"(*p)
  59. : "m"(*p), [new] "r"(newval), [old] "r"(oldval)
  60. : "cc", "memory");
  61. return result == 0;
  62. }
  63. #endif /* pt-machine.h */