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$
50 */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/proc.h>
56 #include <sys/sysproto.h>
57
58 /*
59 * Low level support for sleep/wakeup paradigm
60 * If a timeout is specified:
61 * returns 0 if wakeup
62 * returns EAGAIN if timed out
63 * returns EINVAL if error
64 *
65 * If a timeout is not specified:
66 *
67 * returns time waiting in ticks.
68 */
69 int
70 thr_sleep(struct proc *p, struct thr_sleep_args *uap) {
71 int sleepstart;
72 struct timespec ts;
73 struct timeval atv;
74 int error, timo;
75
76 timo = 0;
77 if (uap->timeout != 0) {
78 /*
79 * Get timespec struct
80 */
81 if (error = copyin((caddr_t) uap->timeout, (caddr_t) &ts, sizeof ts)) {
82 p->p_wakeup = 0;
83 return error;
84 }
85 if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000) {
86 p->p_wakeup = 0;
87 return (EINVAL);
88 }
89 TIMESPEC_TO_TIMEVAL(&atv, &ts);
90 if (itimerfix(&atv)) {
91 p->p_wakeup = 0;
92 return (EINVAL);
93 }
94 timo = tvtohz(&atv);
95 }
96
97 p->p_retval[0] = 0;
98 if (p->p_wakeup == 0) {
99 sleepstart = ticks;
100 p->p_flag |= P_SINTR;
101 error = tsleep(p, PRIBIO, "thrslp", timo);
102 p->p_flag &= ~P_SINTR;
103 if (error == EWOULDBLOCK) {
104 p->p_wakeup = 0;
105 p->p_retval[0] = EAGAIN;
106 return 0;
107 }
108 if (uap->timeout == 0)
109 p->p_retval[0] = ticks - sleepstart;
110 }
111 p->p_wakeup = 0;
112 return (0);
113 }
114
115 int
116 thr_wakeup(struct proc *p, struct thr_wakeup_args *uap) {
117 struct proc *pSlave = p->p_leader;
118
119 while(pSlave && (pSlave->p_pid != uap->pid))
120 pSlave = pSlave->p_peers;
121
122 if(pSlave == 0) {
123 p->p_retval[0] = ESRCH;
124 return(0);
125 }
126
127 pSlave->p_wakeup++;
128 if((pSlave->p_stat == SSLEEP) && (pSlave->p_wchan == pSlave)) {
129 wakeup(pSlave);
130 return(0);
131 }
132
133 p->p_retval[0] = EAGAIN;
134 return 0;
135 }
136
137 /*
138 * General purpose yield system call
139 */
140 int
141 yield(struct proc *p, struct yield_args *uap) {
142 int s;
143
144 p->p_retval[0] = 0;
145
146 s = splhigh();
147 p->p_priority = MAXPRI;
148 setrunqueue(p);
149 mi_switch();
150 splx(s);
151
152 return(0);
153 }
154
Cache object: 0aa6bcdeabc22fdda440360de0c52a07
|