summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/sys/timer.h
blob: db279604137c566c5ce70ae031ea0f5935737827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 2020 Joyent, Inc.
 */

#ifndef	_SYS_TIMER_H
#define	_SYS_TIMER_H

#include <sys/types.h>
#include <sys/proc.h>
#include <sys/thread.h>
#include <sys/param.h>
#include <sys/siginfo.h>
#include <sys/port.h>

#ifdef	__cplusplus
extern "C" {
#endif

#ifdef	_KERNEL

#define	_TIMER_MAX	32
/*
 * Max timers per process.  This is patchable via /etc/system and can be
 * updated via kmdb.  Sticking to positive powers of 2 is recommended.
 * The default value is 4 * NCPU. Setting timer_max to a value below the
 * default via /etc/system is ignored.
 */
extern	int	timer_max;

#define	_TIMER_ALLOC_INIT	8	/* initial size for p_itimer array */

/*
 * Bit values for the it_lock field.
 */
#define	ITLK_LOCKED		0x01
#define	ITLK_WANTED		0x02
#define	ITLK_REMOVE		0x04

/*
 * Bit values for the it_flags field.
 */
#define	IT_SIGNAL		0x01
#define	IT_PORT			0x02	/* use event port notification */
#define	IT_CALLBACK		0x04	/* custom callback function */

struct clock_backend;

struct itimer;
typedef struct itimer itimer_t;

struct itimer {
	itimerspec_t	it_itime;
	hrtime_t	it_hrtime;
	ushort_t	it_flags;
	ushort_t	it_lock;
	void		*it_arg;	/* clock backend-specific data */
	struct proc	*it_proc;
	union {
		struct {
			sigqueue_t	*__it_sigq;
			klwp_t		*__it_lwp;
		} __proc;
		void *__it_frontend;
	} __data;			/* timer frontend-specific data */
	kcondvar_t	it_cv;
	int		it_blockers;
	int		it_pending;
	int		it_overrun;
	struct clock_backend *it_backend;
	void		(*it_fire)(itimer_t *);
	kmutex_t	it_mutex;
	union {
		struct {
			void	*_it_portev;	/* port_kevent_t pointer */
			void	*_it_portsrc;	/* port_source_t pointer */
			int	_it_portfd;	/* port file descriptor */
		} _it_ev_port;
		struct {
			void		(*_it_cb_func)(itimer_t *);
			uintptr_t	_it_cb_data[2];
		} _it_ev_cb;
	} _it_ev_data;
};

#define	it_sigq		__data.__proc.__it_sigq
#define	it_lwp		__data.__proc.__it_lwp
#define	it_frontend	__data.__it_frontend
#define	it_portev	_it_ev_data._it_ev_port._it_portev
#define	it_portsrc	_it_ev_data._it_ev_port._it_portsrc
#define	it_portfd	_it_ev_data._it_ev_port._it_portfd
#define	it_cb_func	_it_ev_data._it_ev_cb._it_cb_func
#define	it_cb_data	_it_ev_data._it_ev_cb._it_cb_data

typedef struct clock_backend {
	struct sigevent clk_default;
	int (*clk_clock_settime)(timespec_t *);
	int (*clk_clock_gettime)(timespec_t *);
	int (*clk_clock_getres)(timespec_t *);
	int (*clk_timer_create)(itimer_t *, void (*)(itimer_t *));
	int (*clk_timer_settime)(itimer_t *, int, const struct itimerspec *);
	int (*clk_timer_gettime)(itimer_t *, struct itimerspec *);
	int (*clk_timer_delete)(itimer_t *);
	void (*clk_timer_lwpbind)(itimer_t *);
} clock_backend_t;

extern void clock_add_backend(clockid_t clock, clock_backend_t *backend);
extern clock_backend_t *clock_get_backend(clockid_t clock);

extern void timer_release(struct proc *, itimer_t *);
extern void timer_delete_grabbed(struct proc *, timer_t tid, itimer_t *it);
extern void timer_lwpbind();
extern int timer_setup(clock_backend_t *, struct sigevent *, port_notify_t *,
    itimer_t **, timer_t *);

extern	void	timer_func(sigqueue_t *);
extern	void	timer_exit(void);
extern	void	timer_lwpexit(void);
extern	clock_t	hzto(struct timeval *);
extern	clock_t	timespectohz(timespec_t *, timespec_t);
extern	int64_t	timespectohz64(timespec_t *);
extern	int	itimerspecfix(timespec_t *);
extern	void	timespecadd(timespec_t *, timespec_t *);
extern	void	timespecsub(timespec_t *, timespec_t *);
extern	void	timespecfix(timespec_t *);
extern	int	xgetitimer(uint_t, struct itimerval *, int);
extern	int	xsetitimer(uint_t, struct itimerval *, int);
extern	void	delete_itimer_realprof(void);

#define	timerspecisset(tvp)		((tvp)->tv_sec || (tvp)->tv_nsec)
#define	timerspeccmp(tvp, uvp)		(((tvp)->tv_sec - (uvp)->tv_sec) ? \
	((tvp)->tv_sec - (uvp)->tv_sec):((tvp)->tv_nsec - (uvp)->tv_nsec))
#define	timerspecclear(tvp)		((tvp)->tv_sec = (tvp)->tv_nsec = 0)

struct oldsigevent {
	/* structure definition prior to notification attributes member */
	int		_notify;
	union {
		int		_signo;
		void		(*_notify_function)(union sigval);
	} _un;
	union sigval	_value;
};

#if defined(_SYSCALL32)

struct oldsigevent32 {
	int32_t		_notify;
	union {
		int32_t		_signo;
		caddr32_t	_notify_function;
	} _un;
	union sigval32	_value;
};

#endif	/* _SYSCALL32 */
#endif	/* _KERNEL */

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_TIMER_H */