Tools/nativelib - Many features implimented
[tpg/acess2.git] / Tools / nativelib / threads.c
index eafe87f..1231c43 100644 (file)
@@ -9,10 +9,20 @@
 #include <threads.h>
 #include <threads_int.h>
 
+// === PROTOTYPES ===
+void   Threads_int_Init(void)  __attribute__((constructor(101)));
+tThread        *Threads_int_CreateTCB(tThread *Parent);
+
 // === GLOBALS ===
+tThread        *gThreads_List;
 tThread __thread       *lpThreads_This;
 
 // === CODE ===
+void Threads_int_Init(void)
+{
+       lpThreads_This = Threads_int_CreateTCB(NULL);
+}
+
 tThread *Proc_GetCurThread(void)
 {
        return lpThreads_This;
@@ -33,7 +43,7 @@ void Threads_PostEvent(tThread *Thread, Uint32 Events)
 
 Uint32 Threads_WaitEvents(Uint32 Events)
 {
-       if( !lpThreads_This ) {
+       if( Threads_int_ThreadingEnabled() ) {
                Log_Notice("Threads", "_WaitEvents: Threading disabled");
                return 0;
        }
@@ -46,7 +56,7 @@ Uint32 Threads_WaitEvents(Uint32 Events)
 
 void Threads_ClearEvent(Uint32 Mask)
 {
-       if( !lpThreads_This ) {
+       if( Threads_int_ThreadingEnabled() ) {
                Log_Notice("Threads", "_ClearEvent: Threading disabled");
                return ;
        }
@@ -60,13 +70,15 @@ tGID Threads_GetGID(void) { return 0; }
 
 tTID Threads_GetTID(void) { return 0; }
 
-int *Threads_GetMaxFD(void) { static int max_fd=32; return &max_fd; }
-char **Threads_GetCWD(void) { static char *cwd; return &cwd; }
-char **Threads_GetChroot(void) { static char *chroot; return &chroot; }
+int *Threads_GetMaxFD(void)        { return &lpThreads_This->Process->MaxFDs;  }
+char **Threads_GetCWD(void)        { return &lpThreads_This->Process->CWD;     }
+char **Threads_GetChroot(void)     { return &lpThreads_This->Process->Chroot;  }
+void **Threads_GetHandlesPtr(void) { return &lpThreads_This->Process->Handles; }
 
 void Threads_Yield(void)
 {
-       Log_Warning("Threads", "Threads_Yield DEFINITELY shouldn't be used");
+       Log_KernelPanic("Threads", "Threads_Yield DEFINITELY shouldn't be used (%p)",
+               __builtin_return_address(0));
 }
 
 void Threads_Sleep(void)
@@ -76,28 +88,65 @@ void Threads_Sleep(void)
 
 int Threads_SetName(const char *Name)
 {
-       Log_Notice("Threads", "TODO: Threads_SetName('%s')", Name);
+       if( !lpThreads_This )
+               return 0;
+
+       if( lpThreads_This->Name )
+               free(lpThreads_This->Name);
+       lpThreads_This->Name = strdup(Name);
+
        return 0;
 }
 
 int *Threads_GetErrno(void) __attribute__ ((weak));
 
-int *Threads_GetErrno(void)// __attribute__ ((weak))
+int *Threads_GetErrno(void)
 {
        static int a_errno;
        return &a_errno;
 }
 
+struct sProcess *Threads_int_CreateProcess(void)
+{
+       struct sProcess *ret = calloc(sizeof(struct sProcess), 1);
+
+       ret->MaxFDs = 32;
+
+       return ret;
+}
+
+tThread *Threads_int_CreateTCB(tThread *Parent)
+{
+       tThread *ret = calloc( sizeof(tThread), 1 );
+       ret->WaitSemaphore = Threads_int_SemCreate();
+       ret->Protector = Threads_int_MutexCreate();
+
+       if( !Parent )
+       {
+               ret->Process = Threads_int_CreateProcess();
+       }
+       else
+               ret->Process = Parent->Process;
+
+       ret->ProcNext = ret->Process->Threads;
+       ret->Process->Threads = ret;
+
+       ret->Next = gThreads_List;
+       gThreads_List = ret;
+       
+       return ret;
+}
+
 struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data)
 {
-       if( !lpThreads_This )
+       if( !Threads_int_ThreadingEnabled() )
        {
                Log_Error("Threads", "Multithreading is disabled in this build");
                return NULL;
        }
        else
        {
-               tThread *ret = malloc( sizeof(tThread) );
+               tThread *ret = Threads_int_CreateTCB(lpThreads_This);
                ret->SpawnFcn = Fcn;
                ret->SpawnData = Data;
                Threads_int_CreateThread(ret);

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