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