preempt.h 2.6 KB

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