This commit is contained in:
Mathieu Maret 2020-08-16 00:24:59 +02:00
parent 6922f6dfa4
commit 9c6c4f3470
4 changed files with 17 additions and 10 deletions

View File

@ -193,23 +193,30 @@ int kthreadYield()
int kthreadMsleep(unsigned long msec) int kthreadMsleep(unsigned long msec)
{ {
uint32_t flags; uint32_t flags;
struct kthread *next, *current;
disable_IRQs(flags); disable_IRQs(flags);
struct kthread *current = currentThread;
current = currentThread;
assertmsg(current->state == RUNNING, "thread %s is in state %d for %d\n", current->name, assertmsg(current->state == RUNNING, "thread %s is in state %d for %d\n", current->name,
current->state, msec); current->state, msec);
current->state = SLEEPING;
struct kthread *next = kthreadSelectNext(); current->state = SLEEPING;
assert(next != current);
current->jiffiesSleeping = msecs_to_jiffies(msec); current->jiffiesSleeping = msecs_to_jiffies(msec);
next = kthreadSelectNext();
assert(next != current);
assert(next->state == READY); assert(next->state == READY);
currentThread = next; currentThread = next;
currentThread->state = RUNNING; currentThread->state = RUNNING;
cpu_context_switch(&current->cpuState, next->cpuState); cpu_context_switch(&current->cpuState, next->cpuState);
restore_IRQs(flags); restore_IRQs(flags);
return 0; return 0;
} }
struct kthread *getCurrenThread() struct kthread *getCurrentThread()
{ {
return currentThread; return currentThread;
} }

View File

@ -39,6 +39,6 @@ int kthreadYield();
int kthreadSaveAndYield(struct kthread *); int kthreadSaveAndYield(struct kthread *);
int kthreadMsleep(unsigned long msec); int kthreadMsleep(unsigned long msec);
int kthreadOnJieffiesTick(); int kthreadOnJieffiesTick();
struct kthread *getCurrenThread(); struct kthread *getCurrentThread();
int kthreadAddThread(struct kthread *th); int kthreadAddThread(struct kthread *th);
int kthreadUnsched(struct kthread *th); int kthreadUnsched(struct kthread *th);

View File

@ -37,7 +37,7 @@ int mutexFree(struct mutex *m)
#ifdef DEBUG #ifdef DEBUG
if (m->owner) { if (m->owner) {
printf("Warning: freeing a owned mutex 0x%. owned by 0x%p 0x%p\n", m, m->owner, printf("Warning: freeing a owned mutex 0x%. owned by 0x%p 0x%p\n", m, m->owner,
getCurrenThread()); getCurrentThread());
} }
#endif #endif
list_delete(waitQueues, m->wait); list_delete(waitQueues, m->wait);
@ -57,7 +57,7 @@ int mutexLock(struct mutex *m)
struct kthread *current; struct kthread *current;
disable_IRQs(flags); disable_IRQs(flags);
current = getCurrenThread(); current = getCurrentThread();
assert(m->owner != current); assert(m->owner != current);
wait_event(m->wait, m->owner == NULL); wait_event(m->wait, m->owner == NULL);
m->owner = current; m->owner = current;
@ -72,7 +72,7 @@ int mutexUnlock(struct mutex *m)
uint32_t flags; uint32_t flags;
disable_IRQs(flags); disable_IRQs(flags);
assert(m->owner == getCurrenThread()); assert(m->owner == getCurrentThread());
m->owner = NULL; m->owner = NULL;

View File

@ -26,7 +26,7 @@ int wait(struct wait_queue *wq)
disable_IRQs(flags); disable_IRQs(flags);
current = getCurrenThread(); current = getCurrentThread();
kthreadUnsched(current); kthreadUnsched(current);
list_add_tail(wq->thread, current); list_add_tail(wq->thread, current);
current->state = WAITING; current->state = WAITING;