sched.h revision 299526
1124961Ssobomax/*-
2124961Ssobomax * Copyright (c) 2010 Isilon Systems, Inc.
3138722Snjl * Copyright (c) 2010 iX Systems, Inc.
4124961Ssobomax * Copyright (c) 2010 Panasas, Inc.
5124961Ssobomax * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6124961Ssobomax * All rights reserved.
7124961Ssobomax *
8124961Ssobomax * Redistribution and use in source and binary forms, with or without
9124961Ssobomax * modification, are permitted provided that the following conditions
10124961Ssobomax * are met:
11124961Ssobomax * 1. Redistributions of source code must retain the above copyright
12124961Ssobomax *    notice unmodified, this list of conditions, and the following
13124961Ssobomax *    disclaimer.
14124961Ssobomax * 2. Redistributions in binary form must reproduce the above copyright
15124961Ssobomax *    notice, this list of conditions and the following disclaimer in the
16124961Ssobomax *    documentation and/or other materials provided with the distribution.
17124961Ssobomax *
18124961Ssobomax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19124961Ssobomax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20124961Ssobomax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21124961Ssobomax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22124961Ssobomax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23124961Ssobomax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24124961Ssobomax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25124961Ssobomax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26124961Ssobomax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27124961Ssobomax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28124961Ssobomax *
29124961Ssobomax * $FreeBSD: head/sys/compat/linuxkpi/common/include/linux/sched.h 299526 2016-05-12 09:06:54Z hselasky $
30124961Ssobomax */
31124961Ssobomax#ifndef	_LINUX_SCHED_H_
32124961Ssobomax#define	_LINUX_SCHED_H_
33124961Ssobomax
34124961Ssobomax#include <sys/param.h>
35124961Ssobomax#include <sys/systm.h>
36124961Ssobomax#include <sys/proc.h>
37124961Ssobomax#include <sys/sched.h>
38124961Ssobomax#include <sys/sleepqueue.h>
39124961Ssobomax
40124961Ssobomax#define	MAX_SCHEDULE_TIMEOUT	LONG_MAX
41124961Ssobomax
42124961Ssobomax#define	TASK_RUNNING		0
43124961Ssobomax#define	TASK_INTERRUPTIBLE	1
44124961Ssobomax#define	TASK_UNINTERRUPTIBLE	2
45124961Ssobomax#define	TASK_DEAD		64
46124961Ssobomax#define	TASK_WAKEKILL		128
47124961Ssobomax#define	TASK_WAKING		256
48124961Ssobomax
49185341Sjkim#define	TASK_SHOULD_STOP	1
50124961Ssobomax#define	TASK_STOPPED		2
51124961Ssobomax
52124961Ssobomax/*
53124961Ssobomax * A task_struct is only provided for threads created by kthread() and
54124961Ssobomax * file operation callbacks.
55124961Ssobomax *
56124961Ssobomax * Using these routines outside the above mentioned contexts will
57124961Ssobomax * cause panics because no task_struct is assigned and td_retval[1] is
58124961Ssobomax * overwritten by syscalls.
59124961Ssobomax */
60124961Ssobomaxstruct task_struct {
61124961Ssobomax	struct	thread *task_thread;
62124961Ssobomax	int	(*task_fn)(void *data);
63124961Ssobomax	void	*task_data;
64124961Ssobomax	int	task_ret;
65124961Ssobomax	int	state;
66124961Ssobomax	int	should_stop;
67124961Ssobomax	pid_t	pid;
68124961Ssobomax	const char    *comm;
69124961Ssobomax};
70124961Ssobomax
71124961Ssobomax#define	current			task_struct_get(curthread)
72124961Ssobomax#define	task_struct_get(x)	((struct task_struct *)(uintptr_t)(x)->td_retval[1])
73124961Ssobomax#define	task_struct_fill(x, y) do {		\
74124961Ssobomax  	(y)->task_thread = (x);			\
75124961Ssobomax	(y)->comm = (x)->td_name;		\
76124961Ssobomax	(y)->pid = (x)->td_tid;			\
77124961Ssobomax} while (0)
78124961Ssobomax#define	task_struct_set(x, y)	(x)->td_retval[1] = (uintptr_t)(y)
79124961Ssobomax
80124961Ssobomax/* ensure the task_struct pointer fits into the td_retval[1] field */
81124961SsobomaxCTASSERT(sizeof(((struct thread *)0)->td_retval[1]) >= sizeof(uintptr_t));
82124961Ssobomax
83124961Ssobomax#define	set_current_state(x)						\
84124961Ssobomax	atomic_store_rel_int((volatile int *)&current->state, (x))
85124961Ssobomax#define	__set_current_state(x)	current->state = (x)
86124961Ssobomax
87214346Sjhb
88124961Ssobomax#define	schedule()							\
89124961Ssobomaxdo {									\
90124961Ssobomax	void *c;							\
91214346Sjhb									\
92124961Ssobomax	if (cold)							\
93124961Ssobomax		break;							\
94124961Ssobomax	c = curthread;							\
95124961Ssobomax	sleepq_lock(c);							\
96124961Ssobomax	if (current->state == TASK_INTERRUPTIBLE ||			\
97124961Ssobomax	    current->state == TASK_UNINTERRUPTIBLE) {			\
98124961Ssobomax		sleepq_add(c, NULL, "task", SLEEPQ_SLEEP, 0);		\
99124961Ssobomax		sleepq_wait(c, 0);					\
100124961Ssobomax	} else {							\
101124961Ssobomax		sleepq_release(c);					\
102124961Ssobomax		sched_relinquish(curthread);				\
103124961Ssobomax	}								\
104124961Ssobomax} while (0)
105124961Ssobomax
106124961Ssobomax#define	wake_up_process(x)						\
107214346Sjhbdo {									\
108124961Ssobomax	int wakeup_swapper;						\
109124961Ssobomax	void *c;							\
110124961Ssobomax									\
111124961Ssobomax	c = (x)->task_thread;						\
112124961Ssobomax	sleepq_lock(c);							\
113124961Ssobomax	(x)->state = TASK_RUNNING;					\
114214346Sjhb	wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);		\
115124961Ssobomax	sleepq_release(c);						\
116124961Ssobomax	if (wakeup_swapper)						\
117214346Sjhb		kick_proc0();						\
118124961Ssobomax} while (0)
119124961Ssobomax
120124961Ssobomax#define	cond_resched()	if (!cold)	sched_relinquish(curthread)
121124961Ssobomax
122124961Ssobomax#define	sched_yield()	sched_relinquish(curthread)
123124961Ssobomax
124214346Sjhbstatic inline long
125124961Ssobomaxschedule_timeout(signed long timeout)
126124961Ssobomax{
127124961Ssobomax	if (timeout < 0)
128124961Ssobomax		return 0;
129124961Ssobomax
130124961Ssobomax	pause("lstim", timeout);
131214346Sjhb
132124961Ssobomax	return 0;
133124961Ssobomax}
134124961Ssobomax
135124961Ssobomax#endif	/* _LINUX_SCHED_H_ */
136124961Ssobomax