Tools/nativelib - Working on threading
authorJohn Hodge (sonata) <[email protected]>
Sun, 27 Jan 2013 08:12:28 +0000 (16:12 +0800)
committerJohn Hodge (sonata) <[email protected]>
Sun, 27 Jan 2013 08:12:28 +0000 (16:12 +0800)
Tools/nativelib/Makefile
Tools/nativelib/include/pthread_weak.h [new file with mode: 0644]
Tools/nativelib/include/threads_int.h [new file with mode: 0644]
Tools/nativelib/threads.c
Tools/nativelib/threads_int.c [new file with mode: 0644]

index 84ac328..e8ca923 100644 (file)
@@ -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 (file)
index 0000000..30ac250
--- /dev/null
@@ -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 <pthread.h>
+#include <semaphore.h>
+
+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 (file)
index 0000000..eb5f9df
--- /dev/null
@@ -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
+
index 501954d..d5f0465 100644 (file)
@@ -7,27 +7,41 @@
  */
 #include <acess.h>
 #include <threads.h>
+#include <threads_int.h>
+
+// === 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 (file)
index 0000000..35125a1
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Acess2 libnative (Kernel Simulation Library)
+ * - By John Hodge (thePowersGang)
+ *
+ * threads_int.c
+ * - Internal threading functions
+ */
+#include <stddef.h>
+#include <stdint.h>
+#include <acess_logging.h>
+#include <threads_int.h>
+#include <pthread_weak.h>
+
+// === CODE ===
+void Threads_int_LockMutex(void *Mutex)
+{
+       if( pthread_mutex_lock )
+       {
+       }
+       else
+       {
+               
+       }
+}
+

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