From 1ceea3c677a12cc9743ae52514bf135e559b0cab Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Mon, 2 Feb 2015 11:55:34 -0800 Subject: [PATCH] Various bugfixes to Thread_Wait and the pthread library --- include/errno.h | 22 +++++++++++----------- lib/libc/posix/pthread.c | 8 +++++--- sys/kern/syscall.c | 6 +++--- sys/kern/thread.c | 9 ++++++--- tests/pthreadtest.c | 6 ++++-- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/include/errno.h b/include/errno.h index 4c2d3c3..1c1cbad 100644 --- a/include/errno.h +++ b/include/errno.h @@ -2,17 +2,17 @@ #ifndef __ERRNO_H__ #define __ERRNO_H__ -#define EIO 0xBAD1 -#define EBADF 0xBAD2 -#define EINVAL 0xBAD3 -#define EFAULT 0xBAD4 -#define ENOMEM 0xBAD5 -#define ENOENT 0xBAD6 -#define ENOTDIR 0xBAD7 -#define ENAMETOOLONG 0xBAD8 -#define ENOSYS 0xBAD9 -#define EAGAIN 0xBADA -#define EBUSY 0xBADB +#define EIO 0x1BAD0001 +#define EBADF 0x1BAD0002 +#define EINVAL 0x1BAD0003 +#define EFAULT 0x1BAD0004 +#define ENOMEM 0x1BAD0005 +#define ENOENT 0x1BAD0006 +#define ENOTDIR 0x1BAD0007 +#define ENAMETOOLONG 0x1BAD0008 +#define ENOSYS 0x1BAD0009 +#define EAGAIN 0x1BAD000A +#define EBUSY 0x1BAD000B #endif /* __ERRNO_H__ */ diff --git a/lib/libc/posix/pthread.c b/lib/libc/posix/pthread.c index cc9199c..d37e768 100644 --- a/lib/libc/posix/pthread.c +++ b/lib/libc/posix/pthread.c @@ -90,7 +90,9 @@ pthreadCreateHelper(void *arg) { struct pthread *thr = (struct pthread *)arg; - OSThreadExit((uint64_t)(thr->entry)(thr->arg)); + thr->result = (thr->entry)(thr->arg); + + OSThreadExit(0); } int @@ -141,8 +143,8 @@ int pthread_join(pthread_t thread, void **value_ptr) { struct pthread *thr = thread; - uint64_t status = OSThreadWait(thr->tid); - if (status != 0) { + uint64_t status = OSThreadWait(0);//thr->tid); + if (SYSCALL_ERRCODE(status) != 0) { return status; } diff --git a/sys/kern/syscall.c b/sys/kern/syscall.c index d8fcc71..a0b4d21 100644 --- a/sys/kern/syscall.c +++ b/sys/kern/syscall.c @@ -365,13 +365,13 @@ Syscall_ThreadCreate(uint64_t rip, uint64_t arg) Thread_Release(curThread); if (newThread == NULL) { - return -1; + return SYSCALL_PACK(ENOMEM, 0); } threadId = newThread->tid; Sched_SetRunnable(newThread); - return threadId; + return SYSCALL_PACK(0, threadId); } uint64_t @@ -455,7 +455,7 @@ Syscall_ThreadWait(uint64_t tid) while (1) { Semaphore_Acquire(&cur->proc->zombieSemaphore); status = Thread_Wait(cur, tid); - if (status != 0) { + if (SYSCALL_ERRCODE(status) != EAGAIN) { Thread_Release(cur); return status; } diff --git a/sys/kern/thread.c b/sys/kern/thread.c index b1a665c..23f7e7c 100644 --- a/sys/kern/thread.c +++ b/sys/kern/thread.c @@ -3,6 +3,9 @@ #include #include +#include +#include + #include #include #include @@ -382,13 +385,13 @@ Thread_Wait(Thread *thr, uint64_t tid) if (tid == 0) { t = TAILQ_FIRST(&thr->proc->zombieQueue); if (!t) { - return 0; + return SYSCALL_PACK(EAGAIN, 0); } TAILQ_REMOVE(&thr->proc->zombieQueue, t, schedQueue); status = t->exitValue; Thread_Release(t); - return status; + return SYSCALL_PACK(0, status); } TAILQ_FOREACH(t, &thr->proc->zombieQueue, schedQueue) { @@ -396,7 +399,7 @@ Thread_Wait(Thread *thr, uint64_t tid) TAILQ_REMOVE(&thr->proc->zombieQueue, t, schedQueue); status = t->exitValue; Thread_Release(t); - return status; + return SYSCALL_PACK(0, status); } } diff --git a/tests/pthreadtest.c b/tests/pthreadtest.c index fbca42c..0cdc81c 100644 --- a/tests/pthreadtest.c +++ b/tests/pthreadtest.c @@ -5,6 +5,8 @@ #include #include +#include +#include void * thread_simple(void *arg) @@ -26,10 +28,8 @@ main(int argc, const char *argv[]) // Simple thread test printf("simple test\n"); status = pthread_create(&thr, NULL, thread_simple, NULL); - printf("status %d\n", status); assert(status == 0); status = pthread_join(thr, &result); - printf("status %d\n", status); assert(status == 0); assert(result == NULL); @@ -41,6 +41,8 @@ main(int argc, const char *argv[]) assert(status == 0); assert(result == (void *)1); + // Mutex test + printf("Success!\n"); return 0;