From d05472ff8f6d8643d40a823e9c07bbe912e4da7d Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Fri, 1 Feb 2013 11:29:44 +0800 Subject: [PATCH] NativeLib - Including kernelland locks --- Tools/Makefile | 10 +++--- Tools/nativelib/Makefile | 20 ++++++----- Tools/nativelib/include/threads_int.h | 52 +++++++++++++++++++++++++-- Tools/nativelib/threads.c | 35 +++++++++++++----- 4 files changed, 95 insertions(+), 22 deletions(-) diff --git a/Tools/Makefile b/Tools/Makefile index e10b765f..52493d09 100644 --- a/Tools/Makefile +++ b/Tools/Makefile @@ -1,13 +1,15 @@ -.PHONY: all clean nativelib DiskTool NetTest +.PHONY: all clean +.PHONY: nativelib DiskTool NetTest +#.PHONY: nativelib-clean DiskTool-clean NetTest-clean -all: DiskTool NetTest +all clean: DiskTool NetTest img2sif: img2sif.c $(CC) -o $@ $< `sdl-config --libs --cflags` -lSDL_image -Wall nativelib: - $(MAKE) -C $@ + $(MAKE) -C $@ $(MAKECMDGOALS) DiskTool NetTest: nativelib - $(MAKE) -C $@ + $(MAKE) -C $@ $(MAKECMDGOALS) diff --git a/Tools/nativelib/Makefile b/Tools/nativelib/Makefile index e8e7f55a..6b1957ed 100644 --- a/Tools/nativelib/Makefile +++ b/Tools/nativelib/Makefile @@ -3,12 +3,14 @@ KERNEL_DIR := ../../KernelLand/Kernel NOBJ := logging.o misc.o threads_int.o -LOBJ := threads.o time.o mutex.o rwlock.o semaphore.o +LOBJ := threads.o time.o +# mutex.o rwlock.o semaphore.o KOBJ += vfs/main.o vfs/open.o vfs/acls.o vfs/io.o vfs/dir.o KOBJ += vfs/nodecache.o vfs/mount.o vfs/memfile.o # vfs/select.o KOBJ += vfs/fs/root.o vfs/fs/devfs.o KOBJ += drv/proc.o +KOBJ += mutex.o rwlock.o semaphore.o NOBJ := $(NOBJ:%.o=obj/%.o) LOBJ := $(LOBJ:%.o=obj/%.o) @@ -28,18 +30,20 @@ clean: $(RM) $(BIN) $(OBJ) $(BIN): $(OBJ) - ar cru $(BIN) $(OBJ) + ar cr $(BIN) $(OBJ) -$(NOBJ): obj/%.o: %.c +$(NOBJ): obj/%.o: %.c Makefile @echo [CC Native] $@ @mkdir -p $(dir $@) - @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) -$(LOBJ): obj/%.o: %.c + @$(CC) -o $@ -c $< -MMD -MF $@.dep -MT $@ -MP $(CFLAGS) $(CPPFLAGS) +$(LOBJ): obj/%.o: %.c Makefile @echo [CC Local] $@ @mkdir -p $(dir $@) - @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) -I $(KERNEL_DIR)/include + @$(CC) -o $@ -c $< -MMD -MF $@.dep -MT $@ -MP $(CFLAGS) $(CPPFLAGS) -I $(KERNEL_DIR)/include -$(KOBJ): obj/_Kernel/%.o: $(KERNEL_DIR)/%.c +$(KOBJ): obj/_Kernel/%.o: $(KERNEL_DIR)/%.c Makefile @echo [CC Kernel] $@ @mkdir -p $(dir $@) - @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) -I $(KERNEL_DIR)/include + @$(CC) -o $@ -c $< -MMD -MF $@.dep -MT $@ -MP $(CFLAGS) $(CPPFLAGS) -I $(KERNEL_DIR)/include + +-include $(OBJ:%=%.dep) diff --git a/Tools/nativelib/include/threads_int.h b/Tools/nativelib/include/threads_int.h index 36e5aba3..3e85232d 100644 --- a/Tools/nativelib/include/threads_int.h +++ b/Tools/nativelib/include/threads_int.h @@ -8,6 +8,10 @@ #ifndef _THREADS_INT_H_ #define _THREADS_INT_H_ +#include + +typedef struct sThread tThread; + #define THREAD_EVENT_RWLOCK (1 << 8) typedef struct sThreadIntMutex tThreadIntMutex; // actually pthreads @@ -39,24 +43,68 @@ struct sThread void *ThreadHandle; int TID; - tThreadIntMutex *Protector; + int Status; + + tShortSpinlock IsLocked; uint32_t PendingEvents; uint32_t WaitingEvents; tThreadIntSem *WaitSemaphore; // pthreads - char *Name; + char *ThreadName; + + int bInstrTrace; // Used for semaphore tracing + + int RetStatus; + void *WaitPointer; // Init Only void (*SpawnFcn)(void*); void *SpawnData; }; +enum eThreadStatus { + THREAD_STAT_NULL, // Invalid process + THREAD_STAT_ACTIVE, // Running and schedulable process + THREAD_STAT_SLEEPING, // Message Sleep + THREAD_STAT_MUTEXSLEEP, // Mutex Sleep + THREAD_STAT_RWLOCKSLEEP, // Read-Writer lock Sleep + THREAD_STAT_SEMAPHORESLEEP, // Semaphore Sleep + THREAD_STAT_QUEUESLEEP, // Queue + THREAD_STAT_EVENTSLEEP, // Event sleep + THREAD_STAT_WAITING, // ??? (Waiting for a thread) + THREAD_STAT_PREINIT, // Being created + THREAD_STAT_ZOMBIE, // Died/Killed, but parent not informed + THREAD_STAT_DEAD, // Awaiting burial (free) + THREAD_STAT_BURIED // If it's still on the list here, something's wrong +}; +static const char * const casTHREAD_STAT[] = { + "THREAD_STAT_NULL", + "THREAD_STAT_ACTIVE", + "THREAD_STAT_SLEEPING", + "THREAD_STAT_MUTEXSLEEP", + "THREAD_STAT_RWLOCKSLEEP", + "THREAD_STAT_SEMAPHORESLEEP", + "THREAD_STAT_QUEUESLEEP", + "THREAD_STAT_EVENTSLEEP", + "THREAD_STAT_WAITING", + "THREAD_STAT_PREINIT", + "THREAD_STAT_ZOMBIE", + "THREAD_STAT_DEAD", + "THREAD_STAT_BURIED" +}; + extern struct sThread __thread *lpThreads_This; +extern tShortSpinlock glThreadListLock; extern int Threads_int_CreateThread(struct sThread *Thread); extern int Threads_int_ThreadingEnabled(void); +extern tThread *Proc_GetCurThread(void); +extern tThread *Threads_RemActive(void); +extern void Threads_AddActive(tThread *Thread); +extern void Threads_int_WaitForStatusEnd(enum eThreadStatus Status); + extern tThreadIntMutex *Threads_int_MutexCreate(void); extern void Threads_int_MutexDestroy(tThreadIntMutex *Mutex); extern void Threads_int_MutexLock(tThreadIntMutex *Mutex); diff --git a/Tools/nativelib/threads.c b/Tools/nativelib/threads.c index 7393c122..b214f21c 100644 --- a/Tools/nativelib/threads.c +++ b/Tools/nativelib/threads.c @@ -17,6 +17,7 @@ tThread *Threads_int_CreateTCB(tThread *Parent); tThread *gThreads_List; tThread __thread *lpThreads_This; int giThreads_NextTID = 1; +tShortSpinlock glThreadListLock; // === CODE === void Threads_int_Init(void) @@ -35,11 +36,11 @@ void Threads_PostEvent(tThread *Thread, Uint32 Events) // nope.avi return ; } - Threads_int_MutexLock(Thread->Protector); + SHORTLOCK( &Thread->IsLocked ); Thread->PendingEvents |= Events; if( Thread->WaitingEvents & Events ) Threads_int_SemSignal(Thread->WaitSemaphore); - Threads_int_MutexRelease(Thread->Protector); + SHORTREL( &Thread->IsLocked ); } Uint32 Threads_WaitEvents(Uint32 Events) @@ -61,9 +62,9 @@ void Threads_ClearEvent(Uint32 Mask) Log_Notice("Threads", "_ClearEvent: Threading disabled"); return ; } - Threads_int_MutexLock(lpThreads_This->Protector); + SHORTLOCK(&lpThreads_This->IsLocked); lpThreads_This->PendingEvents &= ~Mask; - Threads_int_MutexRelease(lpThreads_This->Protector); + SHORTREL(&lpThreads_This->IsLocked); } tUID Threads_GetUID(void) { return 0; } @@ -87,14 +88,20 @@ void Threads_Sleep(void) Log_Warning("Threads", "Threads_Sleep shouldn't be used"); } +void Threads_int_WaitForStatusEnd(enum eThreadStatus Status) +{ + while( lpThreads_This->Status != Status ) + Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore); +} + int Threads_SetName(const char *Name) { if( !lpThreads_This ) return 0; - if( lpThreads_This->Name ) - free(lpThreads_This->Name); - lpThreads_This->Name = strdup(Name); + if( lpThreads_This->ThreadName ) + free(lpThreads_This->ThreadName); + lpThreads_This->ThreadName = strdup(Name); return 0; } @@ -107,6 +114,18 @@ int *Threads_GetErrno(void) return &a_errno; } +tThread *Threads_RemActive(void) +{ + return lpThreads_This; +} + +void Threads_AddActive(tThread *Thread) +{ + Thread->Status = THREAD_STAT_ACTIVE; + // Increment state-change semaphore + Threads_int_SemSignal(Thread->WaitSemaphore); +} + struct sProcess *Threads_int_CreateProcess(void) { struct sProcess *ret = calloc(sizeof(struct sProcess), 1); @@ -121,7 +140,7 @@ tThread *Threads_int_CreateTCB(tThread *Parent) tThread *ret = calloc( sizeof(tThread), 1 ); ret->TID = giThreads_NextTID ++; ret->WaitSemaphore = Threads_int_SemCreate(); - ret->Protector = Threads_int_MutexCreate(); + //ret->Protector = Threads_int_MutexCreate(); if( !Parent ) { -- 2.20.1