Tools/nativelib - More things implimented
authorJohn Hodge (sonata) <[email protected]>
Mon, 28 Jan 2013 15:13:07 +0000 (23:13 +0800)
committerJohn Hodge (sonata) <[email protected]>
Mon, 28 Jan 2013 15:13:07 +0000 (23:13 +0800)
Tools/NetTest/main.c
Tools/NetTest/vfs_shim.c
Tools/nativelib/include/acess.h
Tools/nativelib/include/shortlock.h [new file with mode: 0644]
Tools/nativelib/logging.c
Tools/nativelib/semaphore.c
Tools/nativelib/threads.c
Tools/nativelib/threads_int.c

index 9ce48a4..d3728c5 100644 (file)
@@ -55,6 +55,7 @@ int main(int argc, char *argv[])
                                if( argc-i != 3 ) {
                                        Log_Error("NetTest", "'netcat' <addr> <port>");
                                        PrintUsage(argv[0]);
+                                       return -1;
                                }
 
                                NetTest_Suite_Netcat(argv[i+1], strtol(argv[i+2], NULL, 0));
index 1b0c686..97ac4db 100644 (file)
 // === 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)
 {
index 7198865..b83b977 100644 (file)
@@ -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 <shortlock.h>
 
 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 (file)
index 0000000..f050eb6
--- /dev/null
@@ -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
index 0cb3ed3..c2d8a76 100644 (file)
@@ -1,5 +1,9 @@
 /*
- * 
+ * Acess2 libnative (Kernel Simulation Library)
+ * - By John Hodge (thePowersGang)
+ *
+ * logging.c
+ * - Logging functions
  */
 #include <stdio.h>
 #include <stdarg.h>
 #include <acess_logging.h>
 #include <ctype.h>
 #include <inttypes.h>
+#include <shortlock.h>
 
-#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")
index c1476f6..20c3293 100644 (file)
@@ -8,6 +8,11 @@
 #include <acess.h>
 #include <semaphore.h>
 
+#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)
 {
index 1231c43..7393c12 100644 (file)
@@ -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();
 
index f25ab95..3b7b08a 100644 (file)
@@ -11,6 +11,7 @@
 #include <acess_logging.h>
 #include <threads_int.h>
 #include <pthread_weak.h>
+#include <shortlock.h>
 
 // === 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;
+}
+

UCC git Repository :: git.ucc.asn.au