Pthread

From WiiBrew
Jump to navigation Jump to search

This information is somewhat out-dated, though it may work there is now a pthread.h file in the devkitPPC include directoy

This goes into %DEVKITPRO%/libogc/include/pthread.h

NOTE
pthread_create priority number (last param) must be >0 and <=127, typically 64
the stack can be changed to a smaller value but note it is unusually easy to eat up the stack. The define size is recommended
please dont change the define as libogc recommends 8k.

Example code can be found here It is best to google pthread and remember not all functionality has been implemented. ogc/lwp*.h has many more functions to be implemented.

#pragma once
#include <ogcsys.h>
#include <gccore.h>
#define STACKSIZE 8*1024

typedef lwp_t pthread_t;
typedef mutex_t pthread_mutex_t;
typedef void* pthread_mutexattr_t;
typedef int pthread_attr_t;

inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
//int pthread_cancel(pthread_t thread);

inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
inline int pthread_mutex_destroy(pthread_mutex_t *mutex);
inline int pthread_mutex_lock(pthread_mutex_t *mutex);
inline int pthread_mutex_unlock(pthread_mutex_t *mutex);

//imp
inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
{
	*thread = 0;
	return LWP_CreateThread(thread, start_routine, arg, 0, STACKSIZE, 64);
}

//int pthread_cancel(pthread_t thread);

inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
	return LWP_MutexInit(mutex, 0);
}
inline int pthread_mutex_destroy(pthread_mutex_t *mutex){ return LWP_MutexDestroy(*mutex);}

inline int pthread_mutex_lock(pthread_mutex_t *mutex) { return LWP_MutexLock(*mutex); }
inline int pthread_mutex_trylock(pthread_mutex_t *mutex){ return LWP_MutexTryLock(*mutex);}
inline int pthread_mutex_unlock(pthread_mutex_t *mutex) { return LWP_MutexUnlock(*mutex); }