blob: 58969b2a8a8292deb33a215006eade8659764d6d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_PREEMPT_H
2#define __LINUX_PREEMPT_H
3
4/*
5 * include/linux/preempt.h - macros for accessing and manipulating
6 * preempt_count (used for kernel preemption, interrupt count, etc.)
7 */
8
Al Virof0373602005-11-13 16:06:57 -08009#include <linux/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/linkage.h>
Avi Kivitye107be32007-07-26 13:40:43 +020011#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020013#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
Harvey Harrisonec701582008-02-08 04:19:55 -080014 extern void add_preempt_count(int val);
15 extern void sub_preempt_count(int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#else
17# define add_preempt_count(val) do { preempt_count() += (val); } while (0)
18# define sub_preempt_count(val) do { preempt_count() -= (val); } while (0)
19#endif
20
21#define inc_preempt_count() add_preempt_count(1)
22#define dec_preempt_count() sub_preempt_count(1)
23
24#define preempt_count() (current_thread_info()->preempt_count)
25
26#ifdef CONFIG_PREEMPT
27
28asmlinkage void preempt_schedule(void);
29
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +020030#define preempt_check_resched() \
31do { \
32 if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) \
33 preempt_schedule(); \
34} while (0)
35
36#else /* !CONFIG_PREEMPT */
37
38#define preempt_check_resched() do { } while (0)
39
40#endif /* CONFIG_PREEMPT */
41
42
43#ifdef CONFIG_PREEMPT_COUNT
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define preempt_disable() \
46do { \
47 inc_preempt_count(); \
48 barrier(); \
49} while (0)
50
51#define preempt_enable_no_resched() \
52do { \
53 barrier(); \
54 dec_preempt_count(); \
55} while (0)
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define preempt_enable() \
58do { \
59 preempt_enable_no_resched(); \
Nicolas Pitred6f02912005-12-21 12:26:25 -050060 barrier(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 preempt_check_resched(); \
62} while (0)
63
Steven Rostedt50282522008-05-12 21:20:41 +020064/* For debugging and tracer internals only! */
65#define add_preempt_count_notrace(val) \
66 do { preempt_count() += (val); } while (0)
67#define sub_preempt_count_notrace(val) \
68 do { preempt_count() -= (val); } while (0)
69#define inc_preempt_count_notrace() add_preempt_count_notrace(1)
70#define dec_preempt_count_notrace() sub_preempt_count_notrace(1)
71
72#define preempt_disable_notrace() \
73do { \
74 inc_preempt_count_notrace(); \
75 barrier(); \
76} while (0)
77
78#define preempt_enable_no_resched_notrace() \
79do { \
80 barrier(); \
81 dec_preempt_count_notrace(); \
82} while (0)
83
84/* preempt_check_resched is OK to trace */
85#define preempt_enable_notrace() \
86do { \
87 preempt_enable_no_resched_notrace(); \
88 barrier(); \
89 preempt_check_resched(); \
90} while (0)
91
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +020092#else /* !CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#define preempt_disable() do { } while (0)
95#define preempt_enable_no_resched() do { } while (0)
96#define preempt_enable() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Steven Rostedt50282522008-05-12 21:20:41 +020098#define preempt_disable_notrace() do { } while (0)
99#define preempt_enable_no_resched_notrace() do { } while (0)
100#define preempt_enable_notrace() do { } while (0)
101
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200102#endif /* CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Avi Kivitye107be32007-07-26 13:40:43 +0200104#ifdef CONFIG_PREEMPT_NOTIFIERS
105
106struct preempt_notifier;
107
108/**
109 * preempt_ops - notifiers called when a task is preempted and rescheduled
110 * @sched_in: we're about to be rescheduled:
111 * notifier: struct preempt_notifier for the task being scheduled
112 * cpu: cpu we're scheduled on
113 * @sched_out: we've just been preempted
114 * notifier: struct preempt_notifier for the task being preempted
115 * next: the task that's kicking us out
Tejun Heo8592e642009-12-02 12:56:46 +0900116 *
117 * Please note that sched_in and out are called under different
118 * contexts. sched_out is called with rq lock held and irq disabled
119 * while sched_in is called without rq lock and irq enabled. This
120 * difference is intentional and depended upon by its users.
Avi Kivitye107be32007-07-26 13:40:43 +0200121 */
122struct preempt_ops {
123 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
124 void (*sched_out)(struct preempt_notifier *notifier,
125 struct task_struct *next);
126};
127
128/**
129 * preempt_notifier - key for installing preemption notifiers
130 * @link: internal use
131 * @ops: defines the notifier functions to be called
132 *
133 * Usually used in conjunction with container_of().
134 */
135struct preempt_notifier {
136 struct hlist_node link;
137 struct preempt_ops *ops;
138};
139
140void preempt_notifier_register(struct preempt_notifier *notifier);
141void preempt_notifier_unregister(struct preempt_notifier *notifier);
142
143static inline void preempt_notifier_init(struct preempt_notifier *notifier,
144 struct preempt_ops *ops)
145{
146 INIT_HLIST_NODE(&notifier->link);
147 notifier->ops = ops;
148}
149
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif /* __LINUX_PREEMPT_H */