sched.h revision 219820
1109998Smarkm/*-
2160814Ssimon * Copyright (c) 2010 Isilon Systems, Inc.
3160814Ssimon * Copyright (c) 2010 iX Systems, Inc.
4160814Ssimon * Copyright (c) 2010 Panasas, Inc.
5238405Sjkim * All rights reserved.
6238405Sjkim *
7238405Sjkim * Redistribution and use in source and binary forms, with or without
8238405Sjkim * modification, are permitted provided that the following conditions
9109998Smarkm * are met:
10238405Sjkim * 1. Redistributions of source code must retain the above copyright
11109998Smarkm *    notice unmodified, this list of conditions, and the following
12109998Smarkm *    disclaimer.
13109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
14109998Smarkm *    notice, this list of conditions and the following disclaimer in the
15109998Smarkm *    documentation and/or other materials provided with the distribution.
16109998Smarkm *
17109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18109998Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19109998Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20109998Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21109998Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23109998Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24109998Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25109998Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26109998Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27109998Smarkm */
28109998Smarkm#ifndef	_LINUX_SCHED_H_
29109998Smarkm#define	_LINUX_SCHED_H_
30109998Smarkm
31109998Smarkm#include <sys/param.h>
32109998Smarkm#include <sys/systm.h>
33109998Smarkm#include <sys/proc.h>
34109998Smarkm#include <sys/sched.h>
35109998Smarkm#include <sys/sleepqueue.h>
36109998Smarkm
37109998Smarkm#define	MAX_SCHEDULE_TIMEOUT	LONG_MAX
38109998Smarkm
39109998Smarkm#define	TASK_RUNNING		0
40109998Smarkm#define	TASK_INTERRUPTIBLE	1
41109998Smarkm#define	TASK_UNINTERRUPTIBLE	2
42109998Smarkm#define	TASK_DEAD		64
43109998Smarkm#define	TASK_WAKEKILL		128
44109998Smarkm#define	TASK_WAKING		256
45109998Smarkm
46109998Smarkm#define	TASK_SHOULD_STOP	1
47109998Smarkm#define	TASK_STOPPED		2
48109998Smarkm
49109998Smarkm/*
50109998Smarkm * A task_struct is only provided for those tasks created with kthread.
51109998Smarkm * Using these routines with threads not started via kthread will cause
52109998Smarkm * panics because no task_struct is allocated and td_retval[1] is
53109998Smarkm * overwritten by syscalls which kernel threads will not make use of.
54109998Smarkm */
55109998Smarkmstruct task_struct {
56109998Smarkm	struct	thread *task_thread;
57109998Smarkm	int	(*task_fn)(void *data);
58109998Smarkm	void	*task_data;
59109998Smarkm	int	task_ret;
60109998Smarkm	int	state;
61109998Smarkm	int	should_stop;
62160814Ssimon};
63160814Ssimon
64160814Ssimon#define	current			((struct task_struct *)curthread->td_retval[1])
65160814Ssimon#define	task_struct_get(x)	(struct task_struct *)(x)->td_retval[1]
66160814Ssimon#define	task_struct_set(x, y)	(x)->td_retval[1] = (register_t)(y)
67160814Ssimon
68160814Ssimon#define	set_current_state(x)						\
69160814Ssimon	atomic_store_rel_int((volatile int *)&current->state, (x))
70160814Ssimon#define	__set_current_state(x)	current->state = (x)
71160814Ssimon
72160814Ssimon
73160814Ssimon#define	schedule()							\
74160814Ssimondo {									\
75109998Smarkm	void *c;							\
76109998Smarkm									\
77109998Smarkm	if (cold)							\
78109998Smarkm		break;							\
79160814Ssimon	c = curthread;							\
80160814Ssimon	sleepq_lock(c);							\
81109998Smarkm	if (current->state == TASK_INTERRUPTIBLE ||			\
82109998Smarkm	    current->state == TASK_UNINTERRUPTIBLE) {			\
83109998Smarkm		sleepq_add(c, NULL, "task", SLEEPQ_SLEEP, 0);		\
84109998Smarkm		sleepq_wait(c, 0);					\
85160814Ssimon	} else {							\
86160814Ssimon		sleepq_release(c);					\
87160814Ssimon		sched_relinquish(curthread);				\
88109998Smarkm	}								\
89160814Ssimon} while (0)
90109998Smarkm
91109998Smarkm#define	wake_up_process(x)						\
92109998Smarkmdo {									\
93160814Ssimon	int wakeup_swapper;						\
94160814Ssimon	void *c;							\
95160814Ssimon									\
96160814Ssimon	c = (x)->task_thread;						\
97109998Smarkm	sleepq_lock(c);							\
98109998Smarkm	(x)->state = TASK_RUNNING;					\
99238405Sjkim	wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);		\
100162911Ssimon	sleepq_release(c);						\
101162911Ssimon	if (wakeup_swapper)						\
102162911Ssimon		kick_proc0();						\
103162911Ssimon} while (0)
104238405Sjkim
105238405Sjkim#define	cond_resched()	if (!cold)	sched_relinquish(curthread)
106109998Smarkm
107238405Sjkim#define	sched_yield()	sched_relinquish(curthread)
108238405Sjkim
109109998Smarkm#endif	/* _LINUX_SCHED_H_ */
110238405Sjkim