preempt.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #endif /* __ASM_PREEMPT_H */