Skip to content

Instantly share code, notes, and snippets.

@aliaspider
Created May 21, 2018 19:40
Show Gist options
  • Save aliaspider/e115a4d7e8306b868fe441cd6ee5478b to your computer and use it in GitHub Desktop.
Save aliaspider/e115a4d7e8306b868fe441cd6ee5478b to your computer and use it in GitHub Desktop.
#define _POSIX_TIMEOUTS 1
#define _POSIX_THREADS 1
#define _GLIBCXX_HAS_GTHREADS 1
#define _GLIBCXX_GCC_GTHR_SINGLE_H 1
#define _UNIX98_THREAD_MUTEX_ATTRIBUTES 1
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <wiiu_dbg.h>
#include <wiiu/os/thread.h>
#include <wiiu/os/mutex.h>
#include <wiiu/os/condition.h>
int pthread_mutex_init (pthread_mutex_t *__mutex, _CONST pthread_mutexattr_t *__attr)
{
OSMutex* mutex = calloc(1, sizeof(OSMutex));
OSInitMutex(mutex);
*__mutex = (pthread_mutex_t)mutex;
return 0;
}
static void check_mutex(pthread_mutex_t* __mutex)
{
if (*__mutex == _PTHREAD_MUTEX_INITIALIZER)
pthread_mutex_init(__mutex, NULL);
}
int pthread_mutex_destroy (pthread_mutex_t *__mutex)
{
check_mutex(__mutex);
free((OSMutex*)*__mutex);
*__mutex = 0;
return 0;
}
int pthread_mutex_lock (pthread_mutex_t *__mutex)
{
check_mutex(__mutex);
OSLockMutex((OSMutex*)*__mutex);
return 0;
}
int pthread_mutex_trylock (pthread_mutex_t *__mutex)
{
check_mutex(__mutex);
OSTryLockMutex((OSMutex*)*__mutex);
return 0;
}
int pthread_mutex_unlock (pthread_mutex_t *__mutex)
{
check_mutex(__mutex);
OSUnlockMutex((OSMutex*)*__mutex);
return 0;
}
int pthread_mutexattr_init (pthread_mutexattr_t *__attr)
{
__attr->is_initialized = false;
__attr->recursive = false;
__attr->type = PTHREAD_MUTEX_NORMAL;
return 0;
}
int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr)
{
__attr->type = -1;
return 0;
}
int pthread_mutexattr_getpshared (_CONST pthread_mutexattr_t *__attr, int *__pshared);
int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr, int __pshared);
int pthread_mutexattr_gettype (_CONST pthread_mutexattr_t *__attr, int *__kind)
{
*__kind = __attr->type;
return 0;
}
int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind){
__attr->type = __kind;
return 0;
}
int pthread_cond_init (pthread_cond_t *__cond, _CONST pthread_condattr_t *__attr);
int pthread_cond_destroy (pthread_cond_t *__mutex)
{
DEBUG_LINE();
return 0;
}
int pthread_cond_signal (pthread_cond_t *__cond)
{
DEBUG_LINE();
return 0;
}
int pthread_cond_broadcast (pthread_cond_t *__cond);
int pthread_cond_wait (pthread_cond_t *__cond, pthread_mutex_t *__mutex)
{
DEBUG_LINE();
return 0;
}
int pthread_cond_timedwait (pthread_cond_t *__cond, pthread_mutex_t *__mutex, _CONST struct timespec *__abstime)
{
DEBUG_LINE();
return 0;
}
int pthread_once (pthread_once_t *__once_control, void (*__init_routine)(void))
{
DEBUG_LINE();
return 0;
}
int pthread_join (pthread_t __pthread, void **__value_ptr)
{
DEBUG_LINE();
OSThread*thread = (OSThread*)__pthread;
if(thread->tag != OS_THREAD_TAG)
return EINVAL;
DEBUG_LINE();
if(!OSJoinThread((OSThread*)__pthread, (int*)__value_ptr))
return EINVAL;
return 0;
}
int pthread_detach (pthread_t __pthread)
{
DEBUG_LINE();
OSThread*thread = (OSThread*)__pthread;
if(thread->tag != OS_THREAD_TAG)
return EINVAL;
DEBUG_LINE();
OSDetachThread(thread);
return 0;
}
static int OSThreadEntryPointFnWrap(int argc, const char **argv)
{
DEBUG_LINE();
void *(*__start_routine)( void * ) = (void*(*)(void*))(argv[0]);
void *__arg = (void*)argv[1];
DEBUG_VAR(__start_routine);
DEBUG_VAR(__arg);
return (int)__start_routine(__arg);
}
int pthread_create (pthread_t *__pthread, const pthread_attr_t *__attr, void *(*__start_routine)( void * ), void *__arg)
{
DEBUG_LINE();
DEBUG_VAR(__start_routine);
DEBUG_VAR(__arg);
OSThread* thread = calloc(1, sizeof(OSThread));
int stack_size = 16*1024;
if(__attr && __attr->stacksize)
stack_size = __attr->stacksize;
void* stack_addr = NULL;
if(__attr && __attr->stackaddr)
stack_addr = (u8*)__attr->stackaddr + __attr->stacksize;
OSThreadAttributes attr = OS_THREAD_ATTRIB_AFFINITY_ANY;
if(__attr && __attr->detachstate)
attr |= OS_THREAD_ATTRIB_DETACHED;
void* argv [] = {__start_routine, __arg, NULL};
DEBUG_LINE();
if(!OSCreateThread(thread, OSThreadEntryPointFnWrap, 2, (char*)argv, stack_addr, stack_size, 0, attr))
return EINVAL;
DEBUG_LINE();
*__pthread = (pthread_t)thread;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment