]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/thread_swap.c
xnu-517.3.7.tar.gz
[apple/xnu.git] / osfmk / kern / thread_swap.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55
56 #include <kern/thread.h>
57 #include <kern/lock.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <mach/vm_param.h>
61 #include <kern/sched_prim.h>
62 #include <kern/processor.h>
63 #include <kern/thread_swap.h>
64 #include <kern/spl.h> /* for splsched */
65 #include <kern/misc_protos.h>
66 #include <kern/counters.h>
67 #include <mach/policy.h>
68
69 queue_head_t swapin_queue;
70 decl_simple_lock_data(,swapin_lock)
71
72 mach_counter_t c_swapin_thread_block;
73
74 void swapin_thread(void);
75
76 /*
77 * swapin_init: [exported]
78 *
79 * Initialize the swapper module.
80 */
81 void
82 swapin_init(void)
83 {
84 queue_init(&swapin_queue);
85 simple_lock_init(&swapin_lock, ETAP_THREAD_SWAPPER);
86 kernel_thread_with_priority(swapin_thread, MINPRI_KERNEL);
87 }
88
89 /*
90 * thread_swapin: [exported]
91 *
92 * Place the specified thread in the list of threads to swapin.
93 * Called with thread locked, returned unlocked.
94 */
95
96 void
97 thread_swapin(
98 register thread_t thread)
99 {
100 switch (thread->state & TH_STACK_STATE) {
101
102 case TH_STACK_HANDOFF:
103 /*
104 * Swapped out.
105 */
106 thread->state = (thread->state & ~TH_STACK_STATE) | TH_STACK_ALLOC;
107 thread_unlock(thread);
108 simple_lock(&swapin_lock);
109 enqueue_tail(&swapin_queue, (queue_entry_t) thread);
110 simple_unlock(&swapin_lock);
111 thread_wakeup((event_t)&swapin_queue);
112 break;
113
114 case TH_STACK_ALLOC:
115 /*
116 * Already queued.
117 */
118 thread_unlock(thread);
119 break;
120
121 default:
122 /*
123 * Already swapped in.
124 */
125 panic("thread_swapin");
126 }
127 }
128
129 /*
130 * thread_doswapin:
131 *
132 * Swapin the specified thread, if it should be runnable, then put
133 * it on a run queue.
134 */
135 void
136 thread_doswapin(
137 register thread_t thread)
138 {
139 vm_offset_t stack;
140 spl_t s;
141
142 /*
143 * Allocate the kernel stack.
144 */
145 stack = stack_alloc(thread, thread_continue);
146 assert(stack);
147
148 /*
149 * Place on run queue.
150 */
151 s = splsched();
152 thread_lock(thread);
153 thread->state &= ~(TH_STACK_HANDOFF | TH_STACK_ALLOC);
154 if (thread->state & TH_RUN)
155 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
156 thread_unlock(thread);
157 (void) splx(s);
158 }
159
160 /*
161 * swapin_thread: [exported]
162 *
163 * This procedure executes as a kernel thread. Threads that need to
164 * be swapped in are swapped in by this thread.
165 */
166 void
167 swapin_thread_continue(void)
168 {
169 register thread_t thread;
170
171 (void)splsched();
172 simple_lock(&swapin_lock);
173
174 while ((thread = (thread_t)dequeue_head(&swapin_queue)) != THREAD_NULL) {
175 simple_unlock(&swapin_lock);
176 (void)spllo();
177
178 thread_doswapin(thread);
179
180 (void)splsched();
181 simple_lock(&swapin_lock);
182 }
183
184 assert_wait((event_t)&swapin_queue, THREAD_UNINT);
185 simple_unlock(&swapin_lock);
186 (void)spllo();
187
188 counter(c_swapin_thread_block++);
189 thread_block(swapin_thread_continue);
190 /*NOTREACHED*/
191 }
192
193 void
194 swapin_thread(void)
195 {
196 swapin_thread_continue();
197 /*NOTREACHED*/
198 }