]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/thread_swap.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / osfmk / kern / thread_swap.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37
A
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
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
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.
1c79356b
A
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 */
1c79356b
A
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>
1c79356b
A
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
69queue_head_t swapin_queue;
9bccf70c 70decl_simple_lock_data(,swapin_lock)
1c79356b
A
71
72mach_counter_t c_swapin_thread_block;
73
0b4e3aa0
A
74void swapin_thread(void);
75
1c79356b 76/*
0b4e3aa0 77 * swapin_init: [exported]
1c79356b
A
78 *
79 * Initialize the swapper module.
80 */
0b4e3aa0
A
81void
82swapin_init(void)
1c79356b
A
83{
84 queue_init(&swapin_queue);
9bccf70c 85 simple_lock_init(&swapin_lock, ETAP_THREAD_SWAPPER);
55e303ae 86 kernel_thread_with_priority(swapin_thread, MINPRI_KERNEL);
1c79356b
A
87}
88
89/*
90 * thread_swapin: [exported]
91 *
9bccf70c
A
92 * Place the specified thread in the list of threads to swapin.
93 * Called with thread locked, returned unlocked.
1c79356b
A
94 */
95
0b4e3aa0
A
96void
97thread_swapin(
98 register thread_t thread)
1c79356b
A
99{
100 switch (thread->state & TH_STACK_STATE) {
0b4e3aa0 101
1c79356b
A
102 case TH_STACK_HANDOFF:
103 /*
9bccf70c 104 * Swapped out.
1c79356b 105 */
0b4e3aa0 106 thread->state = (thread->state & ~TH_STACK_STATE) | TH_STACK_ALLOC;
9bccf70c
A
107 thread_unlock(thread);
108 simple_lock(&swapin_lock);
1c79356b 109 enqueue_tail(&swapin_queue, (queue_entry_t) thread);
9bccf70c
A
110 simple_unlock(&swapin_lock);
111 thread_wakeup((event_t)&swapin_queue);
1c79356b
A
112 break;
113
9bccf70c 114 case TH_STACK_ALLOC:
1c79356b 115 /*
9bccf70c 116 * Already queued.
1c79356b 117 */
9bccf70c 118 thread_unlock(thread);
1c79356b
A
119 break;
120
9bccf70c 121 default:
1c79356b
A
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
9bccf70c 133 * it on a run queue.
1c79356b 134 */
0b4e3aa0
A
135void
136thread_doswapin(
137 register thread_t thread)
1c79356b 138{
0b4e3aa0
A
139 vm_offset_t stack;
140 spl_t s;
1c79356b
A
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 */
1c79356b
A
151 s = splsched();
152 thread_lock(thread);
0b4e3aa0 153 thread->state &= ~(TH_STACK_HANDOFF | TH_STACK_ALLOC);
1c79356b 154 if (thread->state & TH_RUN)
55e303ae 155 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
1c79356b
A
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 */
0b4e3aa0
A
166void
167swapin_thread_continue(void)
1c79356b 168{
0b4e3aa0
A
169 register thread_t thread;
170
0b4e3aa0 171 (void)splsched();
9bccf70c 172 simple_lock(&swapin_lock);
1c79356b 173
0b4e3aa0 174 while ((thread = (thread_t)dequeue_head(&swapin_queue)) != THREAD_NULL) {
9bccf70c 175 simple_unlock(&swapin_lock);
0b4e3aa0 176 (void)spllo();
1c79356b 177
0b4e3aa0 178 thread_doswapin(thread);
1c79356b 179
0b4e3aa0 180 (void)splsched();
9bccf70c 181 simple_lock(&swapin_lock);
0b4e3aa0 182 }
1c79356b 183
9bccf70c
A
184 assert_wait((event_t)&swapin_queue, THREAD_UNINT);
185 simple_unlock(&swapin_lock);
0b4e3aa0 186 (void)spllo();
1c79356b 187
0b4e3aa0 188 counter(c_swapin_thread_block++);
0b4e3aa0 189 thread_block(swapin_thread_continue);
0b4e3aa0 190 /*NOTREACHED*/
1c79356b
A
191}
192
0b4e3aa0
A
193void
194swapin_thread(void)
1c79356b 195{
1c79356b
A
196 swapin_thread_continue();
197 /*NOTREACHED*/
198}