kernel - Major signal path adjustments to fix races, tsleep race fixes, +more
[dragonfly.git] / sys / kern / kern_threads.c
CommitLineData
984263bc
MD
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 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * Copyright (c) 1996 Douglas Santry
43 *
44 * This code is subject to the beer copyright. If I chance to meet you in a
45 * bar and this code helped you in some way, you owe me a beer. Only
46 * in Germany will I accept domestic beer. This code may or may not work
47 * and I certainly make no claims as to its fitness for *any* purpose.
48 *
49 * $FreeBSD: src/sys/kern/kern_threads.c,v 1.15 1999/08/28 00:46:15 peter Exp $
9a379a4a 50 * $DragonFly: src/sys/kern/kern_threads.c,v 1.12 2007/02/18 16:12:43 corecode Exp $
984263bc
MD
51 */
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/kernel.h>
56#include <sys/proc.h>
57#include <sys/resourcevar.h>
58#include <sys/sysproto.h>
59
3919ced0 60#if 0
08f2f1bb
SS
61
62/*
63 * XXX lwp
64 *
65 * I am unhappy code, please remove me
66 */
67
68
984263bc
MD
69/*
70 * Low level support for sleep/wakeup paradigm
71 * If a timeout is specified:
72 * returns 0 if wakeup
73 * returns EAGAIN if timed out
74 * returns EINVAL if error
75 *
76 * If a timeout is not specified:
77 *
78 * returns time waiting in ticks.
79 */
80int
753fd850 81sys_thr_sleep(struct thr_sleep_args *uap)
41c20dac
MD
82{
83 struct proc *p = curproc;
9a379a4a 84 struct lwp *lp = curthread->td_lwp;
984263bc
MD
85 int sleepstart;
86 struct timespec ts;
87 struct timeval atv;
88 int error, timo;
89
90 timo = 0;
91 if (uap->timeout != 0) {
92 /*
93 * Get timespec struct
94 */
95 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0) {
96 p->p_wakeup = 0;
97 return error;
98 }
99 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) {
100 p->p_wakeup = 0;
101 return (EINVAL);
102 }
103 TIMESPEC_TO_TIMEVAL(&atv, &ts);
104 if (itimerfix(&atv)) {
105 p->p_wakeup = 0;
106 return (EINVAL);
107 }
a94976ad 108 timo = tvtohz_high(&atv);
984263bc
MD
109 }
110
c7114eea 111 uap->sysmsg_result = 0;
984263bc
MD
112 if (p->p_wakeup == 0) {
113 sleepstart = ticks;
4643740a 114 lp->lwp_flags |= LWP_SINTR;
377d4740 115 error = tsleep(p, 0, "thrslp", timo);
4643740a 116 lp->lwp_flags &= ~LWP_SINTR;
984263bc
MD
117 if (error == EWOULDBLOCK) {
118 p->p_wakeup = 0;
c7114eea 119 uap->sysmsg_result = EAGAIN;
984263bc
MD
120 return 0;
121 }
122 if (uap->timeout == 0)
c7114eea 123 uap->sysmsg_result = ticks - sleepstart;
984263bc
MD
124 }
125 p->p_wakeup = 0;
126 return (0);
127}
128
129int
753fd850 130sys_thr_wakeup(struct thr_wakeup_args *uap)
41c20dac
MD
131{
132 struct proc *p = curproc;
984263bc 133 struct proc *pSlave = p->p_leader;
164b8401 134 struct lwp *lpSlave;
984263bc
MD
135
136 while(pSlave && (pSlave->p_pid != uap->pid))
137 pSlave = pSlave->p_peers;
138
139 if(pSlave == 0) {
c7114eea 140 uap->sysmsg_result = ESRCH;
984263bc
MD
141 return(0);
142 }
143
164b8401 144 lpSlave = FIRST_LWP_IN_PROC(pSlave);
984263bc 145 pSlave->p_wakeup++;
164b8401 146 if((lpSlave->lwp_stat == LSSLEEP) && lpSlave->lwp_wchan == pSlave) {
984263bc
MD
147 wakeup(pSlave);
148 return(0);
149 }
150
c7114eea 151 uap->sysmsg_result = EAGAIN;
984263bc
MD
152 return 0;
153}
154
3919ced0
MD
155#endif
156
984263bc
MD
157/*
158 * General purpose yield system call
3919ced0
MD
159 *
160 * MPSAFE
984263bc
MD
161 */
162int
753fd850 163sys_yield(struct yield_args *uap)
41c20dac 164{
c7114eea 165 uap->sysmsg_result = 0;
f9235b6d 166 lwkt_user_yield();
984263bc
MD
167 return(0);
168}
169