Support thread sleep system call

This commit is contained in:
Ali Mashtizadeh 2015-01-16 14:50:10 -08:00
parent 35b633417f
commit 60799c824e
3 changed files with 33 additions and 1 deletions

View File

@ -4,6 +4,7 @@
#include <sys/queue.h>
#include <sys/handle.h>
#include <sys/ktimer.h>
struct Thread;
typedef struct Thread Thread;
@ -28,6 +29,7 @@ typedef struct Thread {
// Scheduler
int schedState;
TAILQ_ENTRY(Thread) schedQueue;
TimerEvent *timerEvt; // Timer event for wakeups
// Statistics
uint64_t ctxSwitches;
uint64_t userTime;
@ -56,6 +58,7 @@ Thread *Thread_Create(Process *proc);
Thread *Thread_KThreadCreate(void (*f)(void*), void *arg);
Thread *Thread_UThreadCreate(Thread *oldThr, uint64_t rip);
void Thread_SetRunnable(Thread *thr);
void Thread_SetWaiting(Thread *thr);
void Thread_SetZombie(Thread *thr);
void Thread_Destroy(Thread *thr);
void Thread_Switch(Thread *oldthr, Thread *newthr);

View File

@ -8,6 +8,7 @@
#include <sys/kassert.h>
#include <sys/kmem.h>
#include <sys/ktime.h>
#include <sys/ktimer.h>
#include <sys/thread.h>
#include <sys/loader.h>
#include <sys/syscall.h>
@ -311,10 +312,26 @@ Syscall_ThreadExit(uint64_t status)
Panic("Returned to exited thread!\n");
}
static void
ThreadWakeupHelper(void *arg)
{
Thread *thr = (Thread *)arg;
Thread_SetRunnable(thr);
Timer_Release(thr->timerEvt);
thr->timerEvt = NULL;
}
uint64_t
Syscall_ThreadSleep(uint64_t time)
{
// XXX: Support sleeping
Thread *cur = Thread_Current();
cur->timerEvt = Timer_Create(time, ThreadWakeupHelper, cur);
if (cur->timerEvt == NULL)
return -ENOMEM;
Thread_SetWaiting(cur);
Thread_Scheduler();
return 0;

View File

@ -97,6 +97,7 @@ Thread_Create(Process *proc)
}
thr->schedState = SCHED_STATE_NULL;
thr->timerEvt = NULL;
Thread_InitArch(thr);
// Initialize queue
@ -186,6 +187,17 @@ Thread_SetRunnable(Thread *thr)
Spinlock_Unlock(&threadLock);
}
void
Thread_SetWaiting(Thread *thr)
{
Spinlock_Lock(&threadLock);
thr->schedState = SCHED_STATE_WAITING;
TAILQ_INSERT_TAIL(&waitQueue, thr, schedQueue);
Spinlock_Unlock(&threadLock);
}
void
Thread_SetZombie(Thread *thr)
{