user_thread #9

Merged
mathieu merged 12 commits from user_thread into master 2024-02-28 20:24:14 +01:00
8 changed files with 33 additions and 4 deletions
Showing only changes of commit 90436a4f46 - Show all commits

View File

@ -11,7 +11,6 @@
#include "uaddrspace.h" #include "uaddrspace.h"
#include "zero.h" #include "zero.h"
int syscallExecute(int syscallId, const struct cpu_state *userCtx) int syscallExecute(int syscallId, const struct cpu_state *userCtx)
{ {
@ -146,6 +145,17 @@ int syscallExecute(int syscallId, const struct cpu_state *userCtx)
break; break;
} }
case SYSCALL_ID_USLEEP: {
unsigned int sleep;
ret = syscallGet1arg(userCtx, &sleep);
if (ret)
break;
ret = threadUsleep(sleep);
break;
}
default: default:
printf("Unknon syscall id %d\n", syscallId); printf("Unknon syscall id %d\n", syscallId);
ret = -ENOENT; ret = -ENOENT;

View File

@ -12,6 +12,7 @@
#define SYSCALL_ID_MMAP 7 #define SYSCALL_ID_MMAP 7
#define SYSCALL_ID_MUNMAP 8 #define SYSCALL_ID_MUNMAP 8
#define SYSCALL_ID_NEW_THREAD 9 #define SYSCALL_ID_NEW_THREAD 9
#define SYSCALL_ID_USLEEP 10
#ifdef __KERNEL__ #ifdef __KERNEL__
int syscallExecute(int syscallId, const struct cpu_state *user_ctx); int syscallExecute(int syscallId, const struct cpu_state *user_ctx);

View File

@ -292,6 +292,11 @@ int threadYield()
} }
int threadMsleep(unsigned long msec) int threadMsleep(unsigned long msec)
{
return threadUsleep(msec*1000);
}
int threadUsleep(unsigned long usec)
{ {
uint32_t flags; uint32_t flags;
struct thread *next, *current; struct thread *next, *current;
@ -299,12 +304,12 @@ int threadMsleep(unsigned long msec)
disable_IRQs(flags); disable_IRQs(flags);
current = currentThread; current = currentThread;
assertmsg(current->state == RUNNING, "thread %s is in state %d for %lu\n", current->name, assertmsg(current->state == RUNNING, "thread %s is in state %d for %lu us\n", current->name,
current->state, msec); current->state, usec);
current->state = SLEEPING; current->state = SLEEPING;
current->sleepHaveTimeouted = 0; current->sleepHaveTimeouted = 0;
current->jiffiesSleeping = msecs_to_jiffies(msec); current->jiffiesSleeping = usecs_to_jiffies(usec);
next = threadSelectNext(); next = threadSelectNext();
assert(next != current); assert(next != current);

View File

@ -83,6 +83,7 @@ int threadYield();
int threadWait(struct thread *current, struct thread *next, unsigned long msec); int threadWait(struct thread *current, struct thread *next, unsigned long msec);
int threadUnsched(struct thread *th); int threadUnsched(struct thread *th);
int threadMsleep(unsigned long msec); int threadMsleep(unsigned long msec);
int threadUsleep(unsigned long usec);
int threadOnJieffiesTick(); int threadOnJieffiesTick();
struct thread *getCurrentThread(); struct thread *getCurrentThread();
int threadAddThread(struct thread *th); int threadAddThread(struct thread *th);

View File

@ -12,6 +12,7 @@
#define SYSCALL_ID_MMAP 7 #define SYSCALL_ID_MMAP 7
#define SYSCALL_ID_MUNMAP 8 #define SYSCALL_ID_MUNMAP 8
#define SYSCALL_ID_NEW_THREAD 9 #define SYSCALL_ID_NEW_THREAD 9
#define SYSCALL_ID_USLEEP 10
#ifdef __KERNEL__ #ifdef __KERNEL__
int syscallExecute(int syscallId, const struct cpu_state *user_ctx); int syscallExecute(int syscallId, const struct cpu_state *user_ctx);

View File

@ -5,6 +5,7 @@
#include "swintr.h" #include "swintr.h"
#include "sys/mman.h" #include "sys/mman.h"
#include "syscall.h" #include "syscall.h"
#include "unistd.h"
int errno = 0; int errno = 0;
int memcmp(const void *aptr, const void *bptr, size_t size) int memcmp(const void *aptr, const void *bptr, size_t size)
@ -765,3 +766,7 @@ int new_thread(start_routine *func, void *arg, size_t stackSize)
return syscall4(SYSCALL_ID_NEW_THREAD, (unsigned int)thread_runner, (unsigned int)func, return syscall4(SYSCALL_ID_NEW_THREAD, (unsigned int)thread_runner, (unsigned int)func,
(unsigned int)arg, stackSize); (unsigned int)arg, stackSize);
} }
int usleep(useconds_t usec) {
return syscall1(SYSCALL_ID_USLEEP, (unsigned int)usec);
}

View File

@ -4,6 +4,7 @@
#include "stddef.h" #include "stddef.h"
#include "sys/mman.h" #include "sys/mman.h"
#include "tiny.h" #include "tiny.h"
#include "unistd.h"
int func_help() int func_help()
{ {
@ -122,6 +123,7 @@ int func_munmap()
static void *print_hello(void *arg) { static void *print_hello(void *arg) {
(void)arg; (void)arg;
printf("Hello World from thread\n"); printf("Hello World from thread\n");
usleep(100);
return NULL; return NULL;
} }

4
userspace/unistd.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
typedef unsigned int useconds_t;
int usleep(useconds_t usec);