From d7f28400e01f52ac2c58bee1144f229cc1f18626 Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Sun, 27 Jan 2013 16:12:28 +0800 Subject: [PATCH] Tools/nativelib - Working on threading --- Tools/nativelib/Makefile | 2 +- Tools/nativelib/include/pthread_weak.h | 24 ++++++++++++++++ Tools/nativelib/include/threads_int.h | 40 ++++++++++++++++++++++++++ Tools/nativelib/threads.c | 40 +++++++++++++++++++++----- Tools/nativelib/threads_int.c | 25 ++++++++++++++++ 5 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 Tools/nativelib/include/pthread_weak.h create mode 100644 Tools/nativelib/include/threads_int.h create mode 100644 Tools/nativelib/threads_int.c diff --git a/Tools/nativelib/Makefile b/Tools/nativelib/Makefile index 84ac3289..e8ca9236 100644 --- a/Tools/nativelib/Makefile +++ b/Tools/nativelib/Makefile @@ -1,7 +1,7 @@ KERNEL_DIR := ../../KernelLand/Kernel -NOBJ := logging.o misc.o +NOBJ := logging.o misc.o threads_int.o KOBJ := threads.o time.o mutex.o NOBJ := $(NOBJ:%.o=obj/%.o) diff --git a/Tools/nativelib/include/pthread_weak.h b/Tools/nativelib/include/pthread_weak.h new file mode 100644 index 00000000..30ac2503 --- /dev/null +++ b/Tools/nativelib/include/pthread_weak.h @@ -0,0 +1,24 @@ +/* + * Acess2 libnative (Kernel Simulation Library) + * - By John Hodge (thePowersGang) + * + * pthread_weak.h + * - Weakly linked copies of pthread_* and sem_* + */ +#ifndef _PTHREAD_WEAK_H +#define _PTHREAD_WEAK_H +#include +#include + +extern int pthread_create (pthread_t*, const pthread_attr_t*, void* (*)(void*), void*) __attribute__ ((weak)); +extern int pthread_mutex_init (pthread_mutex_t*, const pthread_mutexattr_t*) __attribute__ ((weak)); +extern int pthread_mutex_lock (pthread_mutex_t*) __attribute__ ((weak)); +extern int pthread_mutex_unlock (pthread_mutex_t*) __attribute__ ((weak)); +extern int pthread_mutex_destroy (pthread_mutex_t*) __attribute__ ((weak)); + +extern int sem_init(sem_t *sem, int pshared, unsigned int value) __attribute__ ((weak)); +extern int sem_wait(sem_t *sem) __attribute__ ((weak)); +extern int sem_post(sem_t *sem) __attribute__ ((weak)); +extern int sem_getvalue(sem_t *sem, int *sval) __attribute__ ((weak)); + +#endif diff --git a/Tools/nativelib/include/threads_int.h b/Tools/nativelib/include/threads_int.h new file mode 100644 index 00000000..eb5f9dff --- /dev/null +++ b/Tools/nativelib/include/threads_int.h @@ -0,0 +1,40 @@ +/* + * Acess2 libnative (Kernel Simulation Library) + * - By John Hodge (thePowersGang) + * + * threads_int.h + * - Threads handling definitions + */ +#ifndef _THREADS_INT_H_ +#define _THREADS_INT_H_ + +struct sProcess +{ + struct sProcess *Next; + + int PID; + int UID, GID; + + char *CWD; + char *Chroot; + int MaxFDs; +}; + +struct sThread +{ + struct sThread *Next; + int TID; + + uint32_t PendingEvents; + uint32_t WaitingEvents; + void *WaitSemaphore; // pthreads + + // Init Only + void (*SpawnFcn)(void*); + void *SpawnData; +}; + +extern int Threads_int_CreateThread(struct sThread *Thread); + +#endif + diff --git a/Tools/nativelib/threads.c b/Tools/nativelib/threads.c index 501954de..d5f04652 100644 --- a/Tools/nativelib/threads.c +++ b/Tools/nativelib/threads.c @@ -7,27 +7,41 @@ */ #include #include +#include + +// === GLOBALS === + int gbThreads_MultithreadingEnabled; +tThread __thread *lpThreads_This; // === CODE === tThread *Proc_GetCurThread(void) { - return NULL; + return lpThreads_This; } void Threads_PostEvent(tThread *Thread, Uint32 Events) { - + Threads_int_LockMutex(Thread->Protector); + Thread->PendingEvents |= Events; + if( Thread->WaitingEvents & Events ) + Threads_int_SemaphoreSignal(Thread->WaitSemaphore); + Threads_int_ReleaseMutex(Thread->Protector); } Uint32 Threads_WaitEvents(Uint32 Events) { - Log_KernelPanic("Threads", "Can't use _WaitEvents in DiskTool"); - return 0; + Thread->WaitingEvents = Events; + Threads_int_SemaphoreWaitAll(Thread->WaitSemaphore); + Thread->WaitingEvents = 0; + Uint32 rv = Thread->PendingEvents; + return rv; } void Threads_ClearEvent(Uint32 Mask) { - + Threads_int_LockMutex(Thread->Protector); + lpThreads_This->PendingEvents &= ~Mask; + Threads_int_ReleaseMutex(Thread->Protector); } tUID Threads_GetUID(void) { return 0; } @@ -65,7 +79,19 @@ int *Threads_GetErrno(void)// __attribute__ ((weak)) struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data) { - Log_Error("Threads", "TODO - Use pthreads to impliment Proc_SpawnWorker"); - return NULL; + if( !gbThreads_MultithreadingEnabled ) + { + Log_Error("Threads", "Multithreading is disabled in this build"); + return NULL; + } + else + { + tThread *ret = malloc( sizeof(tThread) ); + ret->SpawnFcn = Fcn; + ret->SpawnData = Data; + Threads_int_CreateThread(ret); + Log_Error("Threads", "TODO - Use pthreads to impliment Proc_SpawnWorker"); + return NULL; + } } diff --git a/Tools/nativelib/threads_int.c b/Tools/nativelib/threads_int.c new file mode 100644 index 00000000..35125a17 --- /dev/null +++ b/Tools/nativelib/threads_int.c @@ -0,0 +1,25 @@ +/* + * Acess2 libnative (Kernel Simulation Library) + * - By John Hodge (thePowersGang) + * + * threads_int.c + * - Internal threading functions + */ +#include +#include +#include +#include +#include + +// === CODE === +void Threads_int_LockMutex(void *Mutex) +{ + if( pthread_mutex_lock ) + { + } + else + { + + } +} + -- 2.20.1