From 2287a74537b331627d12e2db728886d78ed37900 Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Mon, 28 Jan 2013 23:13:07 +0800 Subject: [PATCH] Tools/nativelib - More things implimented --- Tools/NetTest/main.c | 1 + Tools/NetTest/vfs_shim.c | 47 ++++++++++++++++++++++++++++- Tools/nativelib/include/acess.h | 14 +-------- Tools/nativelib/include/shortlock.h | 17 +++++++++++ Tools/nativelib/logging.c | 21 +++++++++++-- Tools/nativelib/semaphore.c | 5 +++ Tools/nativelib/threads.c | 8 +++-- Tools/nativelib/threads_int.c | 37 ++++++++++++++++++++++- 8 files changed, 129 insertions(+), 21 deletions(-) create mode 100644 Tools/nativelib/include/shortlock.h diff --git a/Tools/NetTest/main.c b/Tools/NetTest/main.c index 9ce48a4a..d3728c5f 100644 --- a/Tools/NetTest/main.c +++ b/Tools/NetTest/main.c @@ -55,6 +55,7 @@ int main(int argc, char *argv[]) if( argc-i != 3 ) { Log_Error("NetTest", "'netcat' "); PrintUsage(argv[0]); + return -1; } NetTest_Suite_Netcat(argv[i+1], strtol(argv[i+2], NULL, 0)); diff --git a/Tools/NetTest/vfs_shim.c b/Tools/NetTest/vfs_shim.c index 1b0c686a..97ac4db1 100644 --- a/Tools/NetTest/vfs_shim.c +++ b/Tools/NetTest/vfs_shim.c @@ -12,8 +12,44 @@ // === CODE === int VFS_SelectNode(tVFS_Node *Node, int Type, tTime *Timeout, const char *Name) { + tThread *us = Proc_GetCurThread(); + + int ret = 0; + + Threads_ClearEvent(THREAD_EVENT_VFS); + + if( Type & VFS_SELECT_READ ) { + Node->ReadThreads = (void*)us; + if(Node->DataAvaliable) ret |= VFS_SELECT_READ; + } + if( Type & VFS_SELECT_WRITE ) { + Node->WriteThreads = (void*)us; + if(!Node->BufferFull) ret |= VFS_SELECT_WRITE; + } + if( Type & VFS_SELECT_ERROR ) { + Node->ErrorThreads = (void*)us; + if(Node->ErrorOccurred) ret |= VFS_SELECT_ERROR; + } - return 0; + if( !ret ) + { + // TODO: Timeout + Threads_WaitEvents(THREAD_EVENT_VFS); + } + + if( Type & VFS_SELECT_READ ) { + Node->ReadThreads = NULL; + if(Node->DataAvaliable) ret |= VFS_SELECT_READ; + } + if( Type & VFS_SELECT_WRITE ) { + Node->WriteThreads = NULL; + if(!Node->BufferFull) ret |= VFS_SELECT_WRITE; + } + if( Type & VFS_SELECT_ERROR ) { + Node->ErrorThreads = NULL; + if(Node->ErrorOccurred) ret |= VFS_SELECT_ERROR; + } + return ret; } int VFS_MarkAvaliable(tVFS_Node *Node, BOOL bAvail) @@ -32,6 +68,15 @@ int VFS_MarkError(tVFS_Node *Node, BOOL bError) return 0; } +int VFS_MarkFull(tVFS_Node *Node, BOOL bError) +{ + Node->BufferFull = bError; + if( !Node->BufferFull && Node->WriteThreads ) + Threads_PostEvent( (void*)Node->WriteThreads, THREAD_EVENT_VFS ); + return 0; +} + + #if 0 int VFS_Open(const char *Path, Uint Flags) { diff --git a/Tools/nativelib/include/acess.h b/Tools/nativelib/include/acess.h index 71988653..b83b9773 100644 --- a/Tools/nativelib/include/acess.h +++ b/Tools/nativelib/include/acess.h @@ -50,9 +50,6 @@ typedef uint32_t tUID; typedef uint32_t tGID; typedef uint32_t tTID; -// NOTE: Since this is single-threaded (for now) mutexes can be implimented as simple locks -typedef char tShortSpinlock; - typedef int64_t tTime; extern tTime now(void); extern int64_t timestamp(int sec, int min, int hr, int day, int month, int year); @@ -147,16 +144,7 @@ extern uint64_t DivMod64U(uint64_t Num, uint64_t Den, uint64_t *Rem); static inline int MIN(int a, int b) { return a < b ? a : b; } static inline int MAX(int a, int b) { return a > b ? a : b; } -#if USE_MULTITHREADING -#error "TODO: Impliment multithreaded SHORTLOCK" -#else -static inline void SHORTLOCK(tShortSpinlock *Lock) { - if(*Lock) Log_KernelPanic("---", "Double short lock"); - *Lock = 1; -} -static inline void SHORTREL(tShortSpinlock *m) { *m = 0; } -static inline int CPU_HAS_LOCK(tShortSpinlock *m) { return *m; } -#endif +#include static inline intptr_t MM_GetPhysAddr(void *Ptr) { return 1; } static inline int MM_IsUser(const void *Ptr) { return 1; } diff --git a/Tools/nativelib/include/shortlock.h b/Tools/nativelib/include/shortlock.h new file mode 100644 index 00000000..f050eb6c --- /dev/null +++ b/Tools/nativelib/include/shortlock.h @@ -0,0 +1,17 @@ +/* + * Acess2 native library + * - By John Hodge (thePowersGang) + * + * shortlock.h + * - Short locks :) + */ +#ifndef _SHORTLOCK_H_ +#define _SHORTLOCK_H_ + +typedef void *tShortSpinlock; + +extern void SHORTLOCK(tShortSpinlock *Lock); +extern void SHORTREL(tShortSpinlock *m); +extern int CPU_HAS_LOCK(tShortSpinlock *m); + +#endif diff --git a/Tools/nativelib/logging.c b/Tools/nativelib/logging.c index 0cb3ed32..c2d8a76a 100644 --- a/Tools/nativelib/logging.c +++ b/Tools/nativelib/logging.c @@ -1,5 +1,9 @@ /* - * + * Acess2 libnative (Kernel Simulation Library) + * - By John Hodge (thePowersGang) + * + * logging.c + * - Logging functions */ #include #include @@ -8,22 +12,33 @@ #include #include #include +#include -#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type" ", Ident) +extern int Threads_GetTID(); + +#define LOGHDR(col,type) fprintf(stderr, "\e["col"m[%-8.8s]"type"%2i ", Ident, Threads_GetTID()) #define LOGTAIL() fprintf(stderr, "\e[0m\n") #define PUTERR(col,type) {\ + if(!gbThreadInLog) SHORTLOCK(&glDebugLock); \ + gbThreadInLog ++; \ LOGHDR(col,type);\ va_list args; va_start(args, Message);\ vfprintf(stderr, Message, args);\ va_end(args);\ LOGTAIL();\ + gbThreadInLog --; \ + if(!gbThreadInLog) SHORTREL(&glDebugLock); \ } +// === GLOBALS === +int __thread gbThreadInLog; +tShortSpinlock glDebugLock; + // === CODE === void Log_KernelPanic(const char *Ident, const char *Message, ...) { PUTERR("35", "k") - abort(); + exit(-1); } void Log_Panic(const char *Ident, const char *Message, ...) PUTERR("34", "p") diff --git a/Tools/nativelib/semaphore.c b/Tools/nativelib/semaphore.c index c1476f62..20c32939 100644 --- a/Tools/nativelib/semaphore.c +++ b/Tools/nativelib/semaphore.c @@ -8,6 +8,11 @@ #include #include +#if 0 +TODO:: Rework kernel-land semaphore code to use events +- Allows it to be used here and be tested easier +#endif + // === CODE === void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name) { diff --git a/Tools/nativelib/threads.c b/Tools/nativelib/threads.c index 1231c439..7393c122 100644 --- a/Tools/nativelib/threads.c +++ b/Tools/nativelib/threads.c @@ -16,6 +16,7 @@ tThread *Threads_int_CreateTCB(tThread *Parent); // === GLOBALS === tThread *gThreads_List; tThread __thread *lpThreads_This; + int giThreads_NextTID = 1; // === CODE === void Threads_int_Init(void) @@ -43,7 +44,7 @@ void Threads_PostEvent(tThread *Thread, Uint32 Events) Uint32 Threads_WaitEvents(Uint32 Events) { - if( Threads_int_ThreadingEnabled() ) { + if( !Threads_int_ThreadingEnabled() ) { Log_Notice("Threads", "_WaitEvents: Threading disabled"); return 0; } @@ -56,7 +57,7 @@ Uint32 Threads_WaitEvents(Uint32 Events) void Threads_ClearEvent(Uint32 Mask) { - if( Threads_int_ThreadingEnabled() ) { + if( !Threads_int_ThreadingEnabled() ) { Log_Notice("Threads", "_ClearEvent: Threading disabled"); return ; } @@ -68,7 +69,7 @@ void Threads_ClearEvent(Uint32 Mask) tUID Threads_GetUID(void) { return 0; } tGID Threads_GetGID(void) { return 0; } -tTID Threads_GetTID(void) { return 0; } +tTID Threads_GetTID(void) { return lpThreads_This->TID; } int *Threads_GetMaxFD(void) { return &lpThreads_This->Process->MaxFDs; } char **Threads_GetCWD(void) { return &lpThreads_This->Process->CWD; } @@ -118,6 +119,7 @@ struct sProcess *Threads_int_CreateProcess(void) 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(); diff --git a/Tools/nativelib/threads_int.c b/Tools/nativelib/threads_int.c index f25ab95a..3b7b08a4 100644 --- a/Tools/nativelib/threads_int.c +++ b/Tools/nativelib/threads_int.c @@ -11,6 +11,7 @@ #include #include #include +#include // === TYPES === typedef struct sThread tThread; @@ -22,7 +23,6 @@ struct sThreadIntSem { int val; }; // === CODE === int Threads_int_ThreadingEnabled(void) { - Log_Debug("Threads", "pthread_create = %p", pthread_create); return !!pthread_create; } @@ -139,3 +139,38 @@ int Threads_int_CreateThread(tThread *Thread) } } +void SHORTLOCK(tShortSpinlock *Lock) +{ + if( !pthread_mutex_init ) + { + if(*Lock) Log_KernelPanic("---", "Double short lock"); + *Lock = (void*)1; + } + else + { + if( !*Lock ) { + *Lock = malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(*Lock, NULL); + } + pthread_mutex_lock(*Lock); + } +} + +void SHORTREL(tShortSpinlock *Lock) +{ + if( !pthread_mutex_init ) + { + if(!*Lock) Log_Notice("---", "Short release when not held"); + *Lock = NULL; + } + else + { + pthread_mutex_unlock(*Lock); + } +} + +int CPU_HAS_LOCK(tShortSpinlock *Lock) +{ + return 0; +} + -- 2.20.1