1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/*
* Copyright (c) 1996, 1998, 1999 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Initialize the threads subsystem.
*/
#include <threads/pthread_internal.h>
#include <threads/pthread_ipc.h>
#include <math.h>
#include <assert.h>
/*
* Current thread and idlethread, per processor.
*/
pthread_thread_t *threads_curthreads[MAXCPUS] = { 0 };
pthread_thread_t *threads_idlethreads[MAXCPUS] = { 0 };
pthread_thread_t threads_mainthread;
int threads_preempt_enable[MAXCPUS] = { 0 };
int threads_preempt_needed[MAXCPUS] = { 0 };
int threads_preempt_ready = 0;
int threads_alive = 1; /* Main */
#ifdef SMP
pthread_lock_t threads_smpboot_lock = PTHREAD_LOCK_INITIALIZER;
#endif
int threads_initialized = 0;
int threads_preemptible = 0;
int threads_debug = 0;
int threads_num_processors = 0;
int threads_base_processor = 0;
int threads_switch_mode = 0;
oskit_u32_t threads_realtime = 0;
/*
* The array of thread structure pointers, indexed by TIDs.
*
* The only reason for doing this is so application code can be presented
* with small integers for thread IDs, while internally we use the address
* of the thread structure.
*/
pthread_thread_t *threads_tidtothread[THREADS_MAX_THREAD];
/*
* Hooks for user defined callouts.
*/
void *(*threads_allocator)(size_t) = pthread_alloc_memory;
void (*threads_deallocator)(void *) = pthread_dealloc_memory;
void pthread_init_internal(int preemptible, pthread_thread_t *main);
#ifdef THREADS_DEBUG
/*
* Deadlock detection.
*/
int threads_sleepers = 0;
pthread_lock_t threads_sleepers_lock = PTHREAD_LOCK_INITIALIZER;
#endif
void
pthread_init_withhooks(int preemptible,
void *(*allocator)(size_t),
void (*deallocator)(void *))
{
pthread_init_internal(preemptible, &threads_mainthread);
threads_allocator = allocator;
threads_deallocator = deallocator;
}
void
pthread_init(int preemptible)
{
pthread_init_internal(preemptible, &threads_mainthread);
}
void
pthread_init_internal(int preemptible, pthread_thread_t *mainthread)
{
if (threads_initialized)
return;
pthread_init_attributes();
pthread_init_comlock();
pthread_init_osenv_sleep();
pthread_init_keytable();
pthread_init_process_lock();
pthread_init_exit();
pthread_init_ipc();
oskit_init_libc();
oskit_clock_init();
pthread_init_scheduler();
#ifdef STACKGUARD
pthread_init_guard();
#endif
thread_machdep_init();
threads_base_processor = 0;
threads_num_processors = 1;
#ifdef SMP
/*
* Look for more processors
*/
if (!smp_init()) {
threads_num_processors = smp_get_num_cpus();
threads_base_processor = smp_find_cur_cpu();
}
printf("pthread_init: %d Processors, Processor %d\n",
threads_num_processors, threads_base_processor);
#endif
/*
* Create the main thread.
*/
threads_curthreads[threads_base_processor] =
pthread_init_mainthread(mainthread);
#ifndef CPU_INHERIT
/*
* Create the idle thread(s).
*/
threads_idlethreads[threads_base_processor] =
pthread_create_internal(pthread_idle_function, 0, 0);
#endif
#ifdef SMP
/*
* Look for more processors
*/
if (threads_num_processors > 1) {
int num, cur;
void pthread_smp_booted(void *ignored);
void pthread_ipi_handler(void *ignored);
char *pstkmem;
if (osenv_irq_alloc(SMP_IPI_VECTOR, pthread_ipi_handler, 0, 0))
panic("pthread_init: osenv_irq_alloc");
/* allow receiving IPI */
smp_message_pass_enable[smp_find_cur_cpu()] = 1;
pstkmem = pthread_alloc_memory(512 *
(threads_num_processors - 1));
num = 1;
cur = -1;
while (num < threads_num_processors) {
cur = smp_find_cpu(cur);
if (cur != smp_find_cur_cpu()) {
threads_idlethreads[cur] =
pthread_create_internal(pthread_idle_function,
0, 0);
printf("starting cpu %d\n", cur);
smp_start_cpu(cur, pthread_smp_booted, 0,
pstkmem + 512);
pstkmem += 512;
num++;
}
}
}
#endif
/*
* Now it is safe to do this ...
*/
if (pthread_register_interface())
panic("pthread_init: Could not register interface");
if (preemptible) {
void pthread_interrupt_handler(void);
osenv_timer_register(pthread_interrupt_handler, PTHREAD_HZ);
}
PREEMPT_ENABLE = 1;
threads_preempt_ready = 1;
#ifdef CPU_INHERIT
/*
* Bootstrap the root scheduler.
*/
{
void *(*function)(void *);
void *argument;
bootstrap_root_scheduler(mainthread->tid, preemptible,
&function, &argument);
mainthread->scheduler = pthread_root_scheduler =
pthread_create_internal(function, argument, 0);
/* XXX */
threads_tidtothread[0] = pthread_root_scheduler;
pthread_root_scheduler->tid = 0;
/*
* Create the idle thread. This thread is never actually
* scheduled, but instead is run out of the rescheduler
* when the root scheduler has nothing to do.
*
* Why an idle thread? A place to call to pthread_delay()
* in usermode, but mostly to avoid spinning on a stack
* belonging to some arbitrary thread that was switching
* out.
*/
threads_idlethreads[threads_base_processor] =
pthread_create_internal(pthread_idle_function, 0, 0);
pthread_sched_switchto(pthread_root_scheduler);
}
#endif
/*
* Running in main thread again ...
*/
pthread_init_signals();
threads_initialized = 1;
}
/*
* Preemption interrupt.
*/
void
pthread_interrupt_handler(void)
{
if (CURPTHREAD()) {
CURPTHREAD()->cputime++;
CURPTHREAD()->cpticks++;
#ifdef CPU_INHERIT
{
pthread_thread_t *ptmp;
ptmp = CURPTHREAD()->inherits_from;
while (ptmp) {
ptmp->childtime++;
ptmp = ptmp->inherits_from;
}
pthread_sched_clocktick();
}
#endif
}
threads_realtime++;
/*
* Recompute CPU percentages once a second.
*/
if ((threads_realtime % PTHREAD_HZ) == 0) {
int i;
pthread_thread_t *pthread;
#if defined(CPU_INHERIT) && defined(DEMO)
char foo[BUFSIZ], *bp = foo;
int count = 0;
#endif
for (i = 0; i < THREADS_MAX_THREAD; i++) {
if ((pthread = threads_tidtothread[i]) != 0) {
pthread->pctcpu = pthread->cpticks;
pthread->cpticks = 0;
}
}
#if defined(CPU_INHERIT) && defined(DEMO)
/*
* Demo stuff. Output percentages.
*/
bp += sprintf(bp, "||root:Time:%d ", threads_realtime);
for (i = 0; i < THREADS_MAX_THREAD; i++) {
if ((pthread = threads_tidtothread[i]) != 0) {
if (pthread->pctcpu > 0) {
#if 0
printf("||-a:graph the_graph ");
printf("tid%d_line %d %d||\n",
pthread->tid,
threads_realtime,
count + pthread->pctcpu);
#endif
bp += sprintf(bp, "%d:%d ",
pthread->tid,
pthread->pctcpu);
count += pthread->pctcpu;
}
}
}
printf("%s - %d||\n", foo, count);
#endif
}
#ifndef CPU_INHERIT
#ifdef SMP
if (THISCPU == threads_base_processor) {
int curr = -1;
int i;
for (i = 0; i < threads_num_processors; i++) {
curr = smp_find_cpu(curr);
if (curr != threads_base_processor)
smp_message_pass(curr);
}
}
else {
if (PREEMPT_ENABLE && CURPTHREAD() != IDLETHREAD)
pthread_preempt();
return;
}
#endif
if (CURPTHREAD() != IDLETHREAD)
softint_request(SOFTINT_TIMEOUT);
#endif
return;
}
/*
* Handle an async interrupt request.
*/
void
base_irq_softint_handler(struct trap_state *ts)
{
int switch_mode = threads_switch_mode;
if (! PREEMPT_ENABLE) {
threads_switch_mode = 0;
return;
}
assert(osenv_intr_enabled() != 0);
osenv_intr_disable();
threads_switch_mode = 0;
if (switch_mode & SOFTINT_TIMEOUT) {
pthread_preempt();
}
else if (switch_mode & SOFTINT_ASYNCREQ) {
pthread_yield();
}
assert(osenv_intr_enabled() == 0);
osenv_intr_enable();
}
#ifdef SMP
void
pthread_smp_booted(void *ignored)
{
printf("pthread_smp_booted: CPU %d\n", smp_find_cur_cpu());
splpreempt();
/* allow receiving IPI */
smp_message_pass_enable[smp_find_cur_cpu()] = 1;
pthread_lock(&threads_smpboot_lock);
thread_switch(IDLETHREAD, &threads_smpboot_lock, CURPTHREAD());
printf("pthread_smp_booted: CPU %d returned\n", smp_find_cur_cpu());
exit(1);
}
void
pthread_ipi_handler(void *ignored)
{
/* ack the interrupt */
smp_apic_ack();
pthread_interrupt();
}
#endif
int
oskit_pthread_whichcpu(void)
{
return THISCPU;
}
|