pt-machine.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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)
  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. #ifndef PT_EI
  32. # define PT_EI extern inline
  33. #endif
  34. extern long int testandset (int *spinlock);
  35. extern int __compare_and_swap (long int *p, long int oldval, long int newval);
  36. /* Spinlock implementation; required. */
  37. PT_EI long int
  38. testandset (int *spinlock)
  39. {
  40. return _test_and_set(spinlock, 1);
  41. }
  42. /* Get some notion of the current stack. Need not be exactly the top
  43. of the stack, just something somewhere in the current frame. */
  44. #define CURRENT_STACK_FRAME stack_pointer
  45. register char * stack_pointer __asm__ ("sp");
  46. /* Compare-and-swap for semaphores. */
  47. #define HAS_COMPARE_AND_SWAP
  48. PT_EI int
  49. __compare_and_swap(long int *p, long int oldval, long int newval)
  50. {
  51. long int result;
  52. __asm__ __volatile__(
  53. "/* Inline compare and swap */\n"
  54. "1: ssrf 5\n"
  55. " ld.w %[result], %[mem]\n"
  56. " eor %[result], %[old]\n"
  57. " brne 2f\n"
  58. " stcond %[mem], %[new]\n"
  59. " brne 1b\n"
  60. "2:"
  61. : [result] "=&r"(result), [mem] "=m"(*p)
  62. : "m"(*p), [new] "r"(newval), [old] "r"(oldval)
  63. : "cc", "memory");
  64. return result == 0;
  65. }
  66. #endif /* pt-machine.h */