preempt.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef __ASM_PREEMPT_H
  2. #define __ASM_PREEMPT_H
  3. #include <asm/rmwcc.h>
  4. #include <asm/percpu.h>
  5. #include <linux/thread_info.h>
  6. DECLARE_PER_CPU(int, __preempt_count);
  7. /*
  8. * We mask the PREEMPT_NEED_RESCHED bit so as not to confuse all current users
  9. * that think a non-zero value indicates we cannot preempt.
  10. */
  11. static __always_inline int preempt_count(void)
  12. {
  13. return __this_cpu_read_4(__preempt_count) & ~PREEMPT_NEED_RESCHED;
  14. }
  15. static __always_inline void preempt_count_set(int pc)
  16. {
  17. __this_cpu_write_4(__preempt_count, pc);
  18. }
  19. /*
  20. * must be macros to avoid header recursion hell
  21. */
  22. #define task_preempt_count(p) \
  23. (task_thread_info(p)->saved_preempt_count & ~PREEMPT_NEED_RESCHED)
  24. #define init_task_preempt_count(p) do { \
  25. task_thread_info(p)->saved_preempt_count = PREEMPT_DISABLED; \
  26. } while (0)
  27. #define init_idle_preempt_count(p, cpu) do { \
  28. task_thread_info(p)->saved_preempt_count = PREEMPT_ENABLED; \
  29. per_cpu(__preempt_count, (cpu)) = PREEMPT_ENABLED; \
  30. } while (0)
  31. /*
  32. * We fold the NEED_RESCHED bit into the preempt count such that
  33. * preempt_enable() can decrement and test for needing to reschedule with a
  34. * single instruction.
  35. *
  36. * We invert the actual bit, so that when the decrement hits 0 we know we both
  37. * need to resched (the bit is cleared) and can resched (no preempt count).
  38. */
  39. static __always_inline void set_preempt_need_resched(void)
  40. {
  41. __this_cpu_and_4(__preempt_count, ~PREEMPT_NEED_RESCHED);
  42. }
  43. static __always_inline void clear_preempt_need_resched(void)
  44. {
  45. __this_cpu_or_4(__preempt_count, PREEMPT_NEED_RESCHED);
  46. }
  47. static __always_inline bool test_preempt_need_resched(void)
  48. {
  49. return !(__this_cpu_read_4(__preempt_count) & PREEMPT_NEED_RESCHED);
  50. }
  51. /*
  52. * The various preempt_count add/sub methods
  53. */
  54. static __always_inline void __preempt_count_add(int val)
  55. {
  56. __this_cpu_add_4(__preempt_count, val);
  57. }
  58. static __always_inline void __preempt_count_sub(int val)
  59. {
  60. __this_cpu_add_4(__preempt_count, -val);
  61. }
  62. static __always_inline bool __preempt_count_dec_and_test(void)
  63. {
  64. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  65. }
  66. /*
  67. * Returns true when we need to resched and can (barring IRQ state).
  68. */
  69. static __always_inline bool should_resched(void)
  70. {
  71. return unlikely(!__this_cpu_read_4(__preempt_count));
  72. }
  73. #ifdef CONFIG_PREEMPT
  74. extern asmlinkage void ___preempt_schedule(void);
  75. # define __preempt_schedule() asm ("call ___preempt_schedule")
  76. extern asmlinkage void preempt_schedule(void);
  77. # ifdef CONFIG_CONTEXT_TRACKING
  78. extern asmlinkage void ___preempt_schedule_context(void);
  79. # define __preempt_schedule_context() asm ("call ___preempt_schedule_context")
  80. # endif
  81. #endif
  82. #endif /* __ASM_PREEMPT_H */