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>
14 #include <shortlock.h>
17 struct sThreadIntMutex { int lock; };
18 struct sThreadIntSem { int val; };
23 int Threads_int_ThreadingEnabled(void)
25 return !!pthread_create;
28 tThreadIntMutex *Threads_int_MutexCreate(void)
30 if( pthread_mutex_init )
32 tThreadIntMutex *ret = malloc(sizeof(pthread_mutex_t));
33 pthread_mutex_init( (void*)ret, NULL );
38 return calloc(sizeof(tThreadIntMutex), 1);
42 void Threads_int_MutexLock(tThreadIntMutex *Mutex)
47 if( pthread_mutex_lock )
49 pthread_mutex_lock( (void*)Mutex );
54 Log_KernelPanic("Threads", "Double mutex lock");
59 void Threads_int_MutexRelease(tThreadIntMutex *Mutex)
65 if( pthread_mutex_unlock )
67 pthread_mutex_unlock( (void*)Mutex );
72 Log_Notice("Threads", "Release of non-locked mutex %p", Mutex);
77 tThreadIntSem *Threads_int_SemCreate(void)
81 tThreadIntSem *ret = malloc(sizeof(sem_t));
82 sem_init( (void*)ret, 0, 0 );
87 return calloc(sizeof(tThreadIntSem), 1);
91 void Threads_int_SemSignal(tThreadIntSem *Sem)
95 sem_wait( (void*)Sem );
103 void Threads_int_SemWaitAll(tThreadIntSem *Sem)
107 sem_post( (void*)Sem );
112 Log_KernelPanic("Threads", "Waiting on empty semaphre %p", Sem);
117 void *Threads_int_ThreadRoot(void *ThreadPtr)
119 tThread *thread = ThreadPtr;
120 lpThreads_This = thread;
121 Log_Debug("Threads", "SpawnFcn: %p, SpawnData: %p", thread->SpawnFcn, thread->SpawnData);
122 thread->SpawnFcn(thread->SpawnData);
126 int Threads_int_CreateThread(tThread *Thread)
130 pthread_t *pthread = malloc(sizeof(pthread_t));
131 Thread->ThreadHandle = pthread;
132 return pthread_create(pthread, NULL, &Threads_int_ThreadRoot, Thread);
136 Log_KernelPanic("Threads", "Link with pthreads to use threading");
141 void SHORTLOCK(tShortSpinlock *Lock)
143 if( !pthread_mutex_init )
145 if(*Lock) Log_KernelPanic("---", "Double short lock");
151 *Lock = malloc(sizeof(pthread_mutex_t));
152 pthread_mutex_init(*Lock, NULL);
154 pthread_mutex_lock(*Lock);
158 void SHORTREL(tShortSpinlock *Lock)
160 if( !pthread_mutex_init )
162 if(!*Lock) Log_Notice("---", "Short release when not held");
167 pthread_mutex_unlock(*Lock);
171 int CPU_HAS_LOCK(tShortSpinlock *Lock)