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)
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
+
*/
#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; }
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;
+ }
}
--- /dev/null
+/*
+ * 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
+ {
+
+ }
+}
+