sched.h revision 289566
11638Srgrimes/*- 21638Srgrimes * Copyright (c) 2010 Isilon Systems, Inc. 31638Srgrimes * Copyright (c) 2010 iX Systems, Inc. 41638Srgrimes * Copyright (c) 2010 Panasas, Inc. 51638Srgrimes * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 61638Srgrimes * All rights reserved. 71638Srgrimes * 81638Srgrimes * Redistribution and use in source and binary forms, with or without 91638Srgrimes * modification, are permitted provided that the following conditions 101638Srgrimes * are met: 111638Srgrimes * 1. Redistributions of source code must retain the above copyright 12263142Seadler * notice unmodified, this list of conditions, and the following 131638Srgrimes * disclaimer. 141638Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151638Srgrimes * notice, this list of conditions and the following disclaimer in the 161638Srgrimes * documentation and/or other materials provided with the distribution. 171638Srgrimes * 181638Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 191638Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 201638Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 211638Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 221638Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 231638Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 241638Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 251638Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 261638Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 271638Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 281638Srgrimes */ 291638Srgrimes#ifndef _LINUX_SCHED_H_ 301638Srgrimes#define _LINUX_SCHED_H_ 311638Srgrimes 321638Srgrimes#include <sys/param.h> 331638Srgrimes#include <sys/systm.h> 341638Srgrimes#include <sys/proc.h> 351638Srgrimes#include <sys/sched.h> 361638Srgrimes#include <sys/sleepqueue.h> 371638Srgrimes 381638Srgrimes#define MAX_SCHEDULE_TIMEOUT LONG_MAX 391638Srgrimes 408875Srgrimes#define TASK_RUNNING 0 411638Srgrimes#define TASK_INTERRUPTIBLE 1 421638Srgrimes#define TASK_UNINTERRUPTIBLE 2 431638Srgrimes#define TASK_DEAD 64 441638Srgrimes#define TASK_WAKEKILL 128 451638Srgrimes#define TASK_WAKING 256 461638Srgrimes 471638Srgrimes#define TASK_SHOULD_STOP 1 481638Srgrimes#define TASK_STOPPED 2 491638Srgrimes 501638Srgrimes/* 511638Srgrimes * A task_struct is only provided for those tasks created with kthread. 521638Srgrimes * Using these routines with threads not started via kthread will cause 531638Srgrimes * panics because no task_struct is allocated and td_retval[1] is 541638Srgrimes * overwritten by syscalls which kernel threads will not make use of. 551638Srgrimes */ 561638Srgrimesstruct task_struct { 571638Srgrimes struct thread *task_thread; 581638Srgrimes int (*task_fn)(void *data); 591638Srgrimes void *task_data; 601638Srgrimes int task_ret; 611638Srgrimes int state; 621638Srgrimes int should_stop; 631638Srgrimes}; 641638Srgrimes 651638Srgrimes#define current ((struct task_struct *)curthread->td_retval[1]) 661638Srgrimes#define task_struct_get(x) (struct task_struct *)(x)->td_retval[1] 671638Srgrimes#define task_struct_set(x, y) (x)->td_retval[1] = (register_t)(y) 68 69#define set_current_state(x) \ 70 atomic_store_rel_int((volatile int *)¤t->state, (x)) 71#define __set_current_state(x) current->state = (x) 72 73 74#define schedule() \ 75do { \ 76 void *c; \ 77 \ 78 if (cold) \ 79 break; \ 80 c = curthread; \ 81 sleepq_lock(c); \ 82 if (current->state == TASK_INTERRUPTIBLE || \ 83 current->state == TASK_UNINTERRUPTIBLE) { \ 84 sleepq_add(c, NULL, "task", SLEEPQ_SLEEP, 0); \ 85 sleepq_wait(c, 0); \ 86 } else { \ 87 sleepq_release(c); \ 88 sched_relinquish(curthread); \ 89 } \ 90} while (0) 91 92#define wake_up_process(x) \ 93do { \ 94 int wakeup_swapper; \ 95 void *c; \ 96 \ 97 c = (x)->task_thread; \ 98 sleepq_lock(c); \ 99 (x)->state = TASK_RUNNING; \ 100 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0); \ 101 sleepq_release(c); \ 102 if (wakeup_swapper) \ 103 kick_proc0(); \ 104} while (0) 105 106#define cond_resched() if (!cold) sched_relinquish(curthread) 107 108#define sched_yield() sched_relinquish(curthread) 109 110static inline long 111schedule_timeout(signed long timeout) 112{ 113 if (timeout < 0) 114 return 0; 115 116 pause("lstim", timeout); 117 118 return 0; 119} 120 121#endif /* _LINUX_SCHED_H_ */ 122