xref: /dragonfly/sys/kern/kern_threads.c (revision 3170ffd7)
1 /*
2  *
3  * Portions of this code was derived from the file kern_fork.c and as such
4  * is subject to the copyrights below.
5  *
6  * Copyright (c) 1982, 1986, 1989, 1991, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * Copyright (c) 1996 Douglas Santry
39  *
40  * This code is subject to the beer copyright.  If I chance to meet you in a
41  * bar and this code helped you in some way, you owe me a beer.  Only
42  * in Germany will I accept domestic beer.  This code may or may not work
43  * and I certainly make no claims as to its fitness for *any* purpose.
44  *
45  * $FreeBSD: src/sys/kern/kern_threads.c,v 1.15 1999/08/28 00:46:15 peter Exp $
46  * $DragonFly: src/sys/kern/kern_threads.c,v 1.12 2007/02/18 16:12:43 corecode Exp $
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sysproto.h>
55 
56 #if 0
57 
58 /*
59  * XXX lwp
60  *
61  * I am unhappy code, please remove me
62  */
63 
64 
65 /*
66  * Low level support for sleep/wakeup paradigm
67  * If a timeout is specified:
68  *	returns 0 if wakeup
69  *	returns EAGAIN if timed out
70  *	returns EINVAL if error
71  *
72  * If a timeout is not specified:
73  *
74  *	returns time waiting in ticks.
75  */
76 int
77 sys_thr_sleep(struct thr_sleep_args *uap)
78 {
79 	struct proc *p = curproc;
80 	struct lwp *lp = curthread->td_lwp;
81 	int sleepstart;
82 	struct timespec ts;
83 	struct timeval atv;
84 	int error, timo;
85 
86 	timo = 0;
87 	if (uap->timeout != 0) {
88 		/*
89 		 * Get timespec struct
90 		 */
91 		if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) {
92 			p->p_wakeup = 0;
93 			return error;
94 		}
95 		if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) {
96 			p->p_wakeup = 0;
97 			return (EINVAL);
98 		}
99 		TIMESPEC_TO_TIMEVAL(&atv, &ts);
100 		if (itimerfix(&atv)) {
101 			p->p_wakeup = 0;
102 			return (EINVAL);
103 		}
104 		timo = tvtohz_high(&atv);
105 	}
106 
107 	uap->sysmsg_result = 0;
108 	if (p->p_wakeup == 0) {
109 		sleepstart = ticks;
110 		lp->lwp_flags |= LWP_SINTR;
111 		error = tsleep(p, 0, "thrslp", timo);
112 		lp->lwp_flags &= ~LWP_SINTR;
113 		if (error == EWOULDBLOCK) {
114 			p->p_wakeup = 0;
115 			uap->sysmsg_result = EAGAIN;
116 			return 0;
117 		}
118 		if (uap->timeout == 0)
119 			uap->sysmsg_result = ticks - sleepstart;
120 	}
121 	p->p_wakeup = 0;
122 	return (0);
123 }
124 
125 int
126 sys_thr_wakeup(struct thr_wakeup_args *uap)
127 {
128 	struct proc *p = curproc;
129 	struct proc *pSlave = p->p_leader;
130 	struct lwp *lpSlave;
131 
132 	while(pSlave && (pSlave->p_pid != uap->pid))
133 		pSlave = pSlave->p_peers;
134 
135 	if(pSlave == 0) {
136 		uap->sysmsg_result = ESRCH;
137 		return(0);
138 	}
139 
140 	lpSlave = FIRST_LWP_IN_PROC(pSlave);
141 	pSlave->p_wakeup++;
142 	if((lpSlave->lwp_stat == LSSLEEP) && lpSlave->lwp_wchan == pSlave) {
143 		wakeup(pSlave);
144 		return(0);
145 	}
146 
147 	uap->sysmsg_result = EAGAIN;
148 	return 0;
149 }
150 
151 #endif
152 
153 /*
154  * General purpose yield system call
155  *
156  * MPSAFE
157  */
158 int
159 sys_yield(struct yield_args *uap)
160 {
161 	uap->sysmsg_result = 0;
162 	lwkt_user_yield();
163 	return(0);
164 }
165 
166