Annotation of funnyos/kern/kern_sched.c, Revision 1.2
1.1 nbrk 1: /*
1.2 ! nbrk 2: * $Id: kern_sched.c,v 1.1 2007/11/19 15:54:33 nbrk Exp $
1.1 nbrk 3: */
4: #include <sys/types.h>
5: #include <sys/kern_sched.h>
1.2 ! nbrk 6: #include <sys/kern_time.h> /* for HZ */
! 7: #include <sys/mem.h>
1.1 nbrk 8:
9: #include <libkern/printf.h>
10:
1.2 ! nbrk 11:
! 12: /* config.c gives us tasks that we'll schedule */
! 13: extern struct u_task config_tasklist[];
! 14:
! 15: /* list of k_tasks */
! 16: struct k_task *ktasklist;
! 17:
! 18: /* pcb of running task; we will save context there when enter irq_mode */
! 19: uint32_t *curpcb;
! 20:
! 21: /* total tasks; used to TID generation */
! 22: uint8_t ntasks;
! 23:
! 24: /* "idle" task exists forever and occupy cpu when there is no other task pretending. */
! 25: const struct u_task idle_utask = {"idle", 0, NULL};
! 26:
! 27:
! 28: void
! 29: sched_init(void)
! 30: {
! 31: /*
! 32: * Create struct k_task for each u_task that user described in config_tasklist.
! 33: */
! 34: struct u_task *utaskp;
! 35: struct k_task *ktaskp, *oldktaskp;
! 36:
! 37: ntasks = 0;
! 38:
! 39: /*
! 40: * Create an "idle" task which always has TID 0.
! 41: */
! 42: ktaskp = kmalloc(sizeof(struct k_task));
! 43:
! 44: if (ktaskp == NULL)
! 45: panic("can't allocate memory for idle task\n");
! 46:
! 47: /* link u_task for idle task */
! 48: ktaskp->kt_utask = (struct u_task *)&idle_utask;
! 49:
! 50: /* TID (always 0) */
! 51: ktaskp->kt_tid = 0;
! 52:
! 53: /* idle task is always in state READY (or RUNNING, if on processor now) */
! 54: ktaskp->kt_state = TASK_READY;
! 55:
! 56: /* next task in list isn't up yet (and prev too) */
! 57: ktaskp->kt_next = NULL;
! 58:
! 59: /* make "idle" task first task in ktasklist, system k_task list */
! 60: ktasklist = ktaskp;
! 61:
! 62: /* bump ntasks to 1 */
! 63: ntasks = 1;
! 64:
! 65: /* preserve pointer to this k_task so we can link-in next task */
! 66: oldktaskp = ktaskp;
! 67:
! 68: /*
! 69: * Idle task set up completed.
! 70: * Configure all other system tasks,
! 71: */
! 72:
! 73: /* look through tasklist */
! 74: utaskp = config_tasklist;
! 75: while(utaskp->ut_name != NULL) {
! 76:
! 77: /* try to allocate memory for ktask */
! 78: ktaskp = kmalloc(sizeof(struct k_task));
! 79:
! 80: if (ktaskp == NULL)
! 81: panic("can't allocate memory for task '%s'\n", utaskp->ut_name);
! 82:
! 83: /*
! 84: * Successfully allocated new ktask.
! 85: */
! 86:
! 87: /* link u_task */
! 88: ktaskp->kt_utask = utaskp;
! 89:
! 90: /* set TID (Task Identificator) */
! 91: ktaskp->kt_tid = oldktaskp->kt_tid + 1;
! 92:
! 93: /* state */
! 94: ktaskp->kt_state = TASK_READY;
! 95:
! 96: /* link current task in previous task's structure */
! 97: oldktaskp->kt_next = ktaskp;
! 98:
! 99: /* NULLify kt_next of current task */
! 100: ktaskp->kt_next = NULL;
! 101:
! 102: /* save current ktask for use with next ktask */
! 103: oldktaskp = ktaskp;
! 104:
! 105: /* bump ntasks */
! 106: ntasks++;
! 107:
! 108: /* shift to next element in config_tasklist[] */
! 109: utaskp++;
! 110: }
! 111:
! 112: /* make this list circularly linked (e.g point list's last->next node into first) */
! 113: ktaskp->kt_next = ktasklist;
! 114:
! 115: printf("kern_sched: created %d tasks:", ntasks);
! 116:
! 117: /* print tasks' names */
! 118: ktaskp = ktasklist + 1; /* skip (first) "idle" task (display it as the end of circularly list) */
! 119: do {
! 120: printf(" %s", ktaskp->kt_utask->ut_name);
! 121:
! 122: if (ktaskp->kt_tid == NULL) /* that was an "idle" task */
! 123: break;
! 124:
! 125: ktaskp = ktaskp->kt_next;
! 126: } while(1);
! 127:
! 128: printf("; HZ=%d\n", HZ);
! 129:
! 130: }
1.1 nbrk 131:
CVSweb