task.c 12.2 KB
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
/*
 * Copyright (C) 1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * Copyright (C) 1998 Digital Equipment Corporation
 *
 * This file is part of the SimOS distribution.
 * See LICENSE file for terms of the license.
 *
 */


/*
 * Routines for creating and scheduling tasks
 */

#include <stdio.h>

#include "icode.h"
#include "globals.h"
#include "wheel.h"
#include "symtab.h"
#include "hash.h"
#include "aint.h"

/* This is the queue on which blocked tasks are put */
static qnode_t block_q;

task_ptr task_free;
int num_tasks;			/* to help find mem leaks */
task_t active_tasks;		/* head node of list of allocated tasks */
extern hash_tab_ptr func_hash_tab;

/* This is the counter that holds the virtual time */
aint_time_t virtual_time;

/* function prototypes */

void sched_loop ();
int next_event (task_ptr ptask);
int return_advance (task_ptr ptask);
task_ptr unblock_proc (int pid, aint_time_t simtime);
task_ptr pri_sched_task (task_ptr ptask, PFTASK ufunc, aint_time_t simtime,
			 int priority);

void
execute ()
{
    int pid;

    wheel_init ();

    pid = 0;

    /* create a task for the first process to start at time 0 */
    /* need to set its runstate to R_SLEEP to satisfy the consistency check */
    threads[pid].runstate = R_SLEEP;
    unblock_proc (pid, (aint_time_t) 0);

    /* initialize the head node for the blocked queue */
    INLINE_INIT_Q (&block_q);

    sched_loop ();

}

void
sched_loop ()
{
    int advance_thread;
    task_ptr ptask, pnew;

    /* Get the first task */
    INLINE_TASK_EXTRACT (ptask);

    while (1) {
	virtual_time = ptask->time;
	advance_thread = (*ptask->ufunc) (ptask);
	if (advance_thread == T_ADVANCE) {
	    /*
	     * All tasks for this event have completed,
	     * so advance the thread to the next event
	     */
	    if (virtual_time == ptask->time) {
		/*
		 * If the current time is the same, call next_event()
		 * directly, thereby avoiding extra calls to task_insert()
		 * and task_extract()
		 */
		next_event (ptask);

		/* This reschedules the task, so dont free it */

		INLINE_TASK_EXTRACT (ptask);
	    }
	    else {
		/*
		 * The simulator changed the time of ptask so schedule
		 * the next_event() call for that time in the future.
		 */
		ptask->ufunc = next_event;

		/* put this on the time wheel */
		INLINE_TASK_INSERT (ptask);

		/* get the next task */
		INLINE_TASK_EXTRACT (ptask);
	    }
	}
	else if (advance_thread == T_CONTINUE) {
	    /*
	     * If there is a task on the task stack, then schedule it.
	     * otherwise, treat this is a T_ADVANCE, and call next_event().
	     */
	    if (virtual_time == ptask->time) {
		/*
		 * If the current time is the same, call next_event()
		 * directly. First though check the task stack for
		 * pending tasks 
		 */
		pnew = ptask->tstack;
		if (pnew) {
		    /* there are more tasks to execute before the next event */
		    FREE_ITEM (task_free, ptask);
		    pnew->time = virtual_time;
		    INLINE_TASK_INSERT (pnew);

		    INLINE_TASK_EXTRACT (ptask);
		}
		else {
		    /* there are no tasks on the task stack */
		    next_event (ptask);

		    INLINE_TASK_EXTRACT (ptask);
		}
	    }
	    else {
		/*
		 * The simulator changed the time of ptask so schedule 
		 * the next_event() call for that time in the future.
		 */

		/* First, though, check the task stack for pending tasks. */
		pnew = ptask->tstack;
		if (pnew) {
		    /* There are more tasks to execute before the next event */
		    pnew->time = ptask->time;
		    FREE_ITEM (task_free, ptask);
		    ptask = pnew;
		} else {
		    /* there are no tasks on the task stack */
		    ptask->ufunc = next_event;
		}
		INLINE_TASK_INSERT (ptask);

		INLINE_TASK_EXTRACT (ptask);
	    }
	} else /* == T_YIELD */ {
	    /* This was not the task that advances the thread */

	    /* If the last task terminated the last thread, then we're done. */

	    if (nprocs == 0)
		break;

	    if (advance_thread == T_NO_ADVANCE) {
		/* free this task */
		FREE_ITEM (task_free, ptask);
	    }
	    else if (advance_thread >= T_MAX) {
		fatal ("Unexpected return value from back end: %d\n",
		       advance_thread);
	    }
	    /* Get the next task with the minimum time */

	    INLINE_TASK_EXTRACT (ptask);
	}
    }
    if (!wheel_empty ())
	warning ("all processes terminated but not all tasks have"
		 " executed.\n"); 
}



task_ptr
unblock_proc (int pid, aint_time_t simtime)
{
    task_ptr pnew;

    /* Check that the unblocked process is in a consistent state */
    if (threads[pid].runstate != R_SLEEP)
	fatal ("unblock_proc: process %d runstate is %d, not R_SLEEP.\n",
	       pid, threads[pid].runstate);

    /* Create a temporary new task */
    NEW_ITEM (task_free, sizeof (task_t), pnew, "unblock_proc");

    pnew->pevent = threads[pid].pevent;
    pnew->pid = pid;
    pnew->tstack = NULL;
    pnew->ufunc = next_event;
    pnew->time = simtime;
    pnew->priority = DEF_PRIORITY;

    INLINE_TASK_INSERT (pnew);
    return (pnew);
}


/*
 * Create a new task to execute function "ufunc" at time "time",
 * with the specified priority
 */

task_ptr
pri_sched_task (task_ptr ptask, PFTASK ufunc, aint_time_t simtime, 
		int priority) 
{
    task_ptr pnew;

    /* create a temprary new task */
    NEW_ITEM (task_free, sizeof (task_t), pnew, "pri_sched_task");
    pnew->pevent = ptask->pevent;
    pnew->pid = ptask->pid;
    pnew->tstack = ptask->tstack;
    pnew->ufunc = ufunc;
    pnew->time = simtime;
    pnew->priority = priority;

    INLINE_TASK_INSERT (pnew);
    return (pnew);
}


/*
 * Create a new task to execute function "ufunc" at time "simtime",
 * with a default priority.
 */

task_ptr
sched_task (task_ptr ptask, PFTASK ufunc, aint_time_t simtime)
{
    task_ptr pnew;

    /* create a temporary new task */
    NEW_ITEM (task_free, sizeof (task_t), pnew, "sched_task");
    pnew->pevent = ptask->pevent;
    pnew->pid = ptask->pid;
    pnew->tstack = ptask->tstack;
    pnew->ufunc = ufunc;
    pnew->time = simtime;
    pnew->priority = DEF_PRIORITY;

    INLINE_TASK_INSERT (pnew);
    return pnew;
}

/*
 * This function is similar to task_unblock() except that a list of
 * tasks are unblocked instead of a single task. The sim_unblock()
 * fuction is called for each unblocked task with a pointer to that
 * task. The unblocking task continues execution.
 *
 * Thanks to tina@concave.cs.wits.ac.za for finding a bug in this
 * routine.
 */
int
task_unblock_list (task_ptr ptask)
{
    event_ptr pevent, ptmp, next;
    task_ptr unblocked;

    pevent = ptask->pevent;

    /* the events are linked through the next field */
    for (ptmp = pevent->next; ptmp; ptmp = next) {
	next = ptmp->next;

	/* reset the pointer to NULL for the next call to event_unblock_list */
	ptmp->next = NULL;

	unblocked = (task_ptr) ptmp->sptr;

	/* reset the task's time */
	unblocked->time = ptask->time;

	/* take the unblocked task off the blocked queue */
	INLINE_REMOVE (unblocked);

	/* change the event type in case sim_unblock() needs it */
	unblocked->pevent->type = E_UNBLOCK;
	sim_unblock (unblocked);

	/* schedule the newly unblocked task */
	INLINE_TASK_INSERT (unblocked);
    }

    /* reset the pointer to NULL for the next call to event_unblock_list */
    pevent->next = NULL;

    /* advance the unblocking thread */
    return T_ADVANCE;
}


int
task_yield (task_ptr ptask)
{
    return T_ADVANCE;
}

int
task_exit (task_ptr ptask)
{
    return sim_exit (ptask);
}

int
task_terminate (task_ptr ptask)
{
    stack_task (ptask, sim_terminate);
    return task_cleanup (ptask);
}


/*
 * Create a new task to execute whenever ptask returns T_CONTINUE.
 * If ptask does not return T_CONTINUE, then it must have scheduled
 * a task that will eventually return T_CONTINUE. Only one task in a
 * subree of related tasks may return T_CONTINUE. When that occurs, the
 * task stack is popped, and the next task is scheduled. If the stack is
 * empty, then next_event is called.
 */
task_ptr
stack_task (task_ptr ptask, PFTASK ufunc)
{
    task_ptr pnew;

    /* create a temporary new task */
    NEW_ITEM (task_free, sizeof (task_t), pnew, "sched_task");
    pnew->pevent = ptask->pevent;
    pnew->pid = ptask->pid;
    pnew->tstack = ptask->tstack;
    ptask->tstack = pnew;
    pnew->ufunc = ufunc;
    pnew->priority = DEF_PRIORITY;
    return pnew;
}

/*
 * This is called when a thread blocks
 */
int
task_block (task_ptr ptask)
{
    event_ptr pevent;

    sim_block (ptask);

    /*
     * Save the ptask pointer in the pevent structure
     * so that we can find it quickly when we unblock ptask.
     */
    pevent = ptask->pevent;
    pevent->sptr = (void *) ptask;

    /* Change the event to a yield so we don't re-execute this function. */
    ptask->ufunc = task_yield;

    /* Put this task on the blocked queue. */
    INLINE_ENQUEUE ((task_ptr) & block_q, ptask);

    /* return T_YIELD so that the execute_loop doesn't free this task */
    return T_YIELD;
}


/*
 * This is called when a thread blocks for a specified time
 */
int
task_timed_block (task_ptr ptask)
{
    event_ptr pevent;

    sim_block (ptask);
    pevent = ptask->pevent;
    
    /* Schedule the task to run after the specified time */
    ptask->ufunc = next_event;
    ptask->time += pevent->arg1;
    INLINE_TASK_INSERT (ptask);   

    /* return T_YIELD so that the execute_loop doesn't free this task */
    return T_YIELD;
}

/*
 * This function unblocks a task and schedules it. The sim_unblock()
 * function is called with a pointer to the unblocked task. The unblocking
 * task continues execution.
 */
int
task_unblock (task_ptr ptask)
{
    event_ptr pevent;
    task_ptr unblocked;

    pevent = ptask->pevent;
    unblocked = (task_ptr) pevent->pevent->sptr;

    /* reset the task's time */
    unblocked->time = ptask->time;

    /* take the unblocked task off the blocked queue */
    INLINE_REMOVE (unblocked);

    /* change the event type in case sim_unblock() needs it */
    unblocked->pevent->type = E_UNBLOCK;

    /* tell the backend that a task has been unblocked */
    sim_unblock (unblocked);

    /* change the ufunc of the of the unblocked task to next_event */
    unblocked->ufunc = next_event;
    
    /* schedule the newly unblocked task */
    INLINE_TASK_INSERT (unblocked);

    /* advance the unblocking thread */
    return T_ADVANCE;
}

int
task_done (task_ptr ptask)
{
    nprocs--;
    return T_NO_ADVANCE;
}

int 
task_fork (task_ptr ptask)
{
    event_ptr pevent, child_event;
    task_ptr child_task;

    sim_fork (ptask);

    pevent = ptask->pevent;

    /* get a pointer to the child event */
    child_event = pevent->pevent;

    /* allocate and initialize a task for the child */
    NEW_ITEM (task_free, sizeof (task_t), child_task, "task_fork");

    /* use the thread time rather than the task time */
    child_task->time = ptask->time;

    /*
     * This is an oracle specific modification to AINT. Trying to give a
     * larger timeslice to the child
     */
#ifdef ORACLE
    ptask->time += (CLOCKS_PER_SECOND * 2000);
#endif
    
    child_task->priority = 0;
    child_task->pid = child_event->pid;
    child_task->pevent = child_event;

    /* Change the parent's task function so we dont re-execute E_FORK */
    ptask->ufunc = task_yield;

    /* Give the child a yield event since it has no event yet. */
    child_task->ufunc = task_yield;

    /* insert the child task in the task queue */
    INLINE_TASK_INSERT (child_task);

    /* advance the parent */
    return T_ADVANCE;
}


task_ptr
schedule_task (task_ptr pnew)
{
    INLINE_TASK_INSERT (pnew);
    return pnew;
}

/*
 * This function creates a new task and initializes the fields.
 * The function pointer field is also copied. This function may be used
 * by simulators that need to create a task without scheduling it.
 */
task_ptr
new_task (task_ptr ptask)
{
    task_ptr pnew;

    NEW_ITEM (task_free, sizeof (task_t), pnew, "new_task");
    pnew->pevent = ptask->pevent;
    pnew->pid = ptask->pid;
    pnew->ufunc = ptask->ufunc;
    pnew->priority = DEF_PRIORITY;
    pnew->tstack = ptask->tstack;
    return pnew;
}

/*
 * This function creates a new task but does not initialize any fields.
 * The intended use of this function is for creating tasks that are
 * not associated with any process or event and do not return T_ADVANCE.
 */
task_ptr
newtask ()
{
    task_ptr pnew;

    NEW_ITEM (task_free, sizeof (task_t), pnew, "new_task");
    return pnew;
}

void
free_task (task_ptr ptask)
{
    FREE_ITEM (task_free, ptask);
}