2 * Acess2 libnative (Kernel Simulation Library)
3 * - By John Hodge (thePowersGang)
6 * - Internal threading functions
11 #include <acess_logging.h>
12 #include <threads_int.h>
13 #include <pthread_weak.h>
16 typedef struct sThread tThread;
17 struct sThreadIntMutex { int lock; };
18 struct sThreadIntSem { int val; };
23 int Threads_int_ThreadingEnabled(void)
25 Log_Debug("Threads", "pthread_create = %p", pthread_create);
26 return !!pthread_create;
29 tThreadIntMutex *Threads_int_MutexCreate(void)
31 if( pthread_mutex_init )
33 tThreadIntMutex *ret = malloc(sizeof(pthread_mutex_t));
34 pthread_mutex_init( (void*)ret, NULL );
39 return calloc(sizeof(tThreadIntMutex), 1);
43 void Threads_int_MutexLock(tThreadIntMutex *Mutex)
48 if( pthread_mutex_lock )
50 pthread_mutex_lock( (void*)Mutex );
55 Log_KernelPanic("Threads", "Double mutex lock");
60 void Threads_int_MutexRelease(tThreadIntMutex *Mutex)
66 if( pthread_mutex_unlock )
68 pthread_mutex_unlock( (void*)Mutex );
73 Log_Notice("Threads", "Release of non-locked mutex %p", Mutex);
78 tThreadIntSem *Threads_int_SemCreate(void)
82 tThreadIntSem *ret = malloc(sizeof(sem_t));
83 sem_init( (void*)ret, 0, 0 );
88 return calloc(sizeof(tThreadIntSem), 1);
92 void Threads_int_SemSignal(tThreadIntSem *Sem)
96 sem_wait( (void*)Sem );
104 void Threads_int_SemWaitAll(tThreadIntSem *Sem)
108 sem_post( (void*)Sem );
113 Log_KernelPanic("Threads", "Waiting on empty semaphre %p", Sem);
118 void *Threads_int_ThreadRoot(void *ThreadPtr)
120 tThread *thread = ThreadPtr;
121 lpThreads_This = thread;
122 Log_Debug("Threads", "SpawnFcn: %p, SpawnData: %p", thread->SpawnFcn, thread->SpawnData);
123 thread->SpawnFcn(thread->SpawnData);
127 int Threads_int_CreateThread(tThread *Thread)
131 pthread_t *pthread = malloc(sizeof(pthread_t));
132 Thread->ThreadHandle = pthread;
133 return pthread_create(pthread, NULL, &Threads_int_ThreadRoot, Thread);
137 Log_KernelPanic("Threads", "Link with pthreads to use threading");