blob: 907f3fd191acaa1573445a0988e5161b8e408ec3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _linux_POSIX_TIMERS_H
2#define _linux_POSIX_TIMERS_H
3
4#include <linux/spinlock.h>
5#include <linux/list.h>
6#include <linux/sched.h>
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00007#include <linux/timex.h>
John Stultz9a7adcf2011-01-11 09:54:33 -08008#include <linux/alarmtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000010
11static inline unsigned long long cputime_to_expires(cputime_t expires)
12{
13 return (__force unsigned long long)expires;
14}
15
16static inline cputime_t expires_to_cputime(unsigned long long expires)
17{
18 return (__force cputime_t)expires;
19}
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21struct cpu_timer_list {
22 struct list_head entry;
Frederic Weisbecker55ccb612013-06-28 00:06:42 +000023 unsigned long long expires, incr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 struct task_struct *task;
25 int firing;
26};
27
Richard Cochran81e294c2011-02-01 13:52:32 +000028/*
29 * Bit fields within a clockid:
30 *
31 * The most significant 29 bits hold either a pid or a file descriptor.
32 *
33 * Bit 2 indicates whether a cpu clock refers to a thread or a process.
34 *
35 * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
36 *
37 * A clockid is invalid if bits 2, 1, and 0 are all set.
38 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
40#define CPUCLOCK_PERTHREAD(clock) \
41 (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
Richard Cochran0606f422011-02-01 13:52:35 +000042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define CPUCLOCK_PERTHREAD_MASK 4
44#define CPUCLOCK_WHICH(clock) ((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
45#define CPUCLOCK_CLOCK_MASK 3
46#define CPUCLOCK_PROF 0
47#define CPUCLOCK_VIRT 1
48#define CPUCLOCK_SCHED 2
49#define CPUCLOCK_MAX 3
Richard Cochran81e294c2011-02-01 13:52:32 +000050#define CLOCKFD CPUCLOCK_MAX
51#define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
54 ((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
55#define MAKE_THREAD_CPUCLOCK(tid, clock) \
56 MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
57
Richard Cochran0606f422011-02-01 13:52:35 +000058#define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
59#define CLOCKID_TO_FD(clk) ((unsigned int) ~((clk) >> 3))
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* POSIX.1b interval timer structure. */
62struct k_itimer {
63 struct list_head list; /* free/ allocate list */
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040064 struct hlist_node t_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 spinlock_t it_lock;
66 clockid_t it_clock; /* which timer type */
67 timer_t it_id; /* timer id */
68 int it_overrun; /* overrun on pending signal */
69 int it_overrun_last; /* overrun on last delivered signal */
Thomas Gleixner2a698972006-01-09 20:52:28 -080070 int it_requeue_pending; /* waiting to requeue this timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define REQUEUE_PENDING 1
72 int it_sigev_notify; /* notify word of sigevent struct */
Oleg Nesterov27af4242008-12-01 14:18:13 -080073 struct signal_struct *it_signal;
74 union {
75 struct pid *it_pid; /* pid of process to send signal to */
76 struct task_struct *it_process; /* for clock_nanosleep */
77 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct sigqueue *sigq; /* signal queue entry. */
79 union {
80 struct {
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -080081 struct hrtimer timer;
82 ktime_t interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } real;
84 struct cpu_timer_list cpu;
85 struct {
86 unsigned int clock;
87 unsigned int node;
88 unsigned long incr;
89 unsigned long expires;
90 } mmtimer;
John Stultz9e264762011-08-10 12:09:24 -070091 struct {
92 struct alarm alarmtimer;
93 ktime_t interval;
94 } alarm;
Eric Dumazet8af08872011-05-24 11:12:58 +020095 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 } it;
97};
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099struct k_clock {
Thomas Gleixnera924b042006-01-09 20:52:27 -0800100 int (*clock_getres) (const clockid_t which_clock, struct timespec *tp);
Richard Cochran1e6d7672011-02-01 13:50:58 +0000101 int (*clock_set) (const clockid_t which_clock,
102 const struct timespec *tp);
Thomas Gleixnera924b042006-01-09 20:52:27 -0800103 int (*clock_get) (const clockid_t which_clock, struct timespec * tp);
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000104 int (*clock_adj) (const clockid_t which_clock, struct timex *tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int (*timer_create) (struct k_itimer *timer);
Thomas Gleixner2a698972006-01-09 20:52:28 -0800106 int (*nsleep) (const clockid_t which_clock, int flags,
Thomas Gleixner97735f22006-01-09 20:52:37 -0800107 struct timespec *, struct timespec __user *);
Toyo Abe1711ef32006-09-29 02:00:28 -0700108 long (*nsleep_restart) (struct restart_block *restart_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int (*timer_set) (struct k_itimer * timr, int flags,
110 struct itimerspec * new_setting,
111 struct itimerspec * old_setting);
112 int (*timer_del) (struct k_itimer * timr);
113#define TIMER_RETRY 1
114 void (*timer_get) (struct k_itimer * timr,
115 struct itimerspec * cur_setting);
116};
117
Thomas Gleixner19769452011-02-01 13:51:06 +0000118extern struct k_clock clock_posix_cpu;
Richard Cochran0606f422011-02-01 13:52:35 +0000119extern struct k_clock clock_posix_dynamic;
Thomas Gleixner19769452011-02-01 13:51:06 +0000120
Thomas Gleixner52708732011-02-02 12:10:09 +0100121void posix_timers_register_clock(const clockid_t clock_id, struct k_clock *new_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* function to call to trigger timer event */
124int posix_timer_event(struct k_itimer *timr, int si_private);
125
Thomas Gleixner2a698972006-01-09 20:52:28 -0800126void posix_cpu_timer_schedule(struct k_itimer *timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Thomas Gleixner2a698972006-01-09 20:52:28 -0800128void run_posix_cpu_timers(struct task_struct *task);
129void posix_cpu_timers_exit(struct task_struct *task);
130void posix_cpu_timers_exit_group(struct task_struct *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Frederic Weisbecker555347f2013-04-19 16:17:38 +0200132bool posix_cpu_timers_can_stop_tick(struct task_struct *tsk);
133
Thomas Gleixner2a698972006-01-09 20:52:28 -0800134void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
135 cputime_t *newval, cputime_t *oldval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Toyo Abe1711ef32006-09-29 02:00:28 -0700137long clock_nanosleep_restart(struct restart_block *restart_block);
138
Jiri Slaby5ab46b32009-08-28 14:05:12 +0200139void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
Frank Mayharf06febc2008-09-12 09:54:39 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif