static void idle (void *aux UNUSED);
static struct thread *running_thread (void);
static struct thread *next_thread_to_run (void);
-static struct thread *new_thread (const char *name);
-static void init_thread (struct thread *, const char *name);
+static struct thread *new_thread (const char *name, int priority);
+static void init_thread (struct thread *, const char *name, int priority);
static bool is_thread (struct thread *);
static void *alloc_frame (struct thread *, size_t size);
static void destroy_thread (struct thread *);
/* Set up a thread structure for the running thread. */
initial_thread = running_thread ();
- init_thread (initial_thread, "main");
+ init_thread (initial_thread, "main", PRI_DEFAULT);
initial_thread->status = THREAD_RUNNING;
initial_thread->tid = allocate_tid ();
void
thread_start (void)
{
- thread_create ("idle", idle, NULL);
+ thread_create ("idle", PRI_DEFAULT, idle, NULL);
intr_enable ();
}
-/* Creates a new kernel thread named NAME, which executes
- FUNCTION passing AUX as the argument, and adds it to the ready
- queue. If thread_start() has been called, then the new thread
- may be scheduled before thread_create() returns. It could
- even exit before thread_create() returns. Use a semaphore or
- some other form of synchronization if you need to ensure
- ordering. Returns the thread identifier for the new thread,
- or TID_ERROR if creation fails. */
+/* Creates a new kernel thread named NAME with the given initial
+ PRIORITY, which executes FUNCTION passing AUX as the argument,
+ and adds it to the ready queue. If thread_start() has been
+ called, then the new thread may be scheduled before
+ thread_create() returns. It could even exit before
+ thread_create() returns. Use a semaphore or some other form
+ of synchronization if you need to ensure ordering. Returns
+ the thread identifier for the new thread, or TID_ERROR if
+ creation fails.
+
+ The code provided sets the new thread's `priority' member to
+ PRIORITY, but no actual priority scheduling is implemented.
+ Priority scheduling is the goal of Problem 1-3. */
tid_t
-thread_create (const char *name, thread_func *function, void *aux)
+thread_create (const char *name, int priority,
+ thread_func *function, void *aux)
{
struct thread *t;
struct kernel_thread_frame *kf;
ASSERT (function != NULL);
- t = new_thread (name);
+ t = new_thread (name, priority);
if (t == NULL)
return TID_ERROR;
- tid = t->tid = allocate_tid ();
+ tid = t->tid;
/* Stack frame for kernel_thread(). */
kf = alloc_frame (t, sizeof *kf);
ASSERT (filename != NULL);
- t = new_thread (filename);
+ t = new_thread (filename, PRI_DEFAULT);
if (t == NULL)
return TID_ERROR;
- tid = t->tid = allocate_tid ();
+ tid = t->tid;
if (!addrspace_load (t, filename, &start))
PANIC ("%s: program load failed", filename);
return t != NULL && t->magic == THREAD_MAGIC;
}
-/* Creates a new thread named NAME and initializes its fields.
- Returns the new thread if successful or a null pointer on
- failure. */
+/* Creates a new thread named NAME as a child of the running
+ thread. Returns the new thread if successful or a null
+ pointer on failure. */
static struct thread *
-new_thread (const char *name)
+new_thread (const char *name, int priority)
{
- struct thread *t;
-
- ASSERT (name != NULL);
-
- t = palloc_get (PAL_ZERO);
- if (t != NULL)
- init_thread (t, name);
+ struct thread *t = palloc_get (PAL_ZERO);
+ if (t != NULL)
+ {
+ init_thread (t, name, priority);
+ t->tid = allocate_tid ();
+ }
return t;
}
-/* Initializes T as a new, blocked thread named NAME. */
+/* Does basic initialization of T as a new, blocked thread named
+ NAME. */
static void
-init_thread (struct thread *t, const char *name)
+init_thread (struct thread *t, const char *name, int priority)
{
+ ASSERT (t != NULL);
+ ASSERT (PRI_MIN <= priority && priority <= PRI_MAX);
+ ASSERT (name != NULL);
+
memset (t, 0, sizeof *t);
t->status = THREAD_BLOCKED;
strlcpy (t->name, name, sizeof t->name);
t->stack = (uint8_t *) t + PGSIZE;
+ t->priority = priority;
t->magic = THREAD_MAGIC;
}
typedef int tid_t;
#define TID_ERROR ((tid_t) -1) /* Error value for tid_t. */
+/* Thread priorities. */
+#define PRI_MIN 0 /* Lowest priority. */
+#define PRI_DEFAULT 29 /* Default priority. */
+#define PRI_MAX 59 /* Highest priority. */
+
/* A kernel thread or user process.
Each thread structure is stored in its own 4 kB page. The
enum thread_status status; /* Thread state. */
char name[16]; /* Name (for debugging purposes). */
uint8_t *stack; /* Saved stack pointer. */
+ int priority; /* Priority. */
/* Shared between thread.c and synch.c. */
list_elem elem; /* List element. */
void thread_start (void);
typedef void thread_func (void *aux);
-tid_t thread_create (const char *name, thread_func *, void *);
+tid_t thread_create (const char *name, int priority, thread_func *, void *);
#ifdef USERPROG
tid_t thread_execute (const char *filename);
#endif
void thread_yield (void);
void thread_block (void);
-/* These functions will be implemented in project 1. */
+/* This function will be implemented in problem 1-2. */
void thread_join (tid_t);
-void thread_set_priority (tid_t, int);
-int thread_get_priority (tid_t);
+
+/* These functions will be implemented in problem 1-3. */
+void thread_set_priority (int);
+int thread_get_priority (void);
#endif /* threads/thread.h */