2 #ifndef _3DS_PTHREAD_WRAP__
3 #define _3DS_PTHREAD_WRAP__
11 #define CTR_PTHREAD_STACK_SIZE 0x10000
20 #if !defined(PTHREAD_SCOPE_PROCESS)
21 /* An earlier version of devkitARM does not define the pthread types. Can remove in r54+. */
23 typedef uint32_t pthread_t;
24 typedef int pthread_attr_t;
26 typedef LightLock pthread_mutex_t;
27 typedef int pthread_mutexattr_t;
29 typedef uint32_t pthread_cond_t;
30 typedef int pthread_condattr_t;
34 static inline int pthread_create(pthread_t *thread,
35 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
37 int procnum = -2; // use default cpu
39 APT_CheckNew3DS(&isNew3DS);
44 *thread = threadCreate(start_routine, arg, CTR_PTHREAD_STACK_SIZE, 0x25, procnum, FALSE);
49 static inline int pthread_join(pthread_t thread, void **retval)
53 if(threadJoin(thread, INT64_MAX))
62 static inline void pthread_exit(void *retval)
69 static inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) {
70 LightLock_Init(mutex);
74 static inline int pthread_mutex_lock(pthread_mutex_t *mutex) {
75 LightLock_Lock(mutex);
79 static inline int pthread_mutex_unlock(pthread_mutex_t *mutex) {
80 LightLock_Unlock(mutex);
84 static inline int pthread_mutex_destroy(pthread_mutex_t *mutex) {
88 static inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) {
89 cond_t *cond_data = calloc(1, sizeof(cond_t));
93 if (svcCreateSemaphore(&cond_data->semaphore, 0, 1))
96 LightLock_Init(&cond_data->lock);
97 cond_data->waiting = 0;
102 svcCloseHandle(cond_data->semaphore);
108 static inline int pthread_cond_signal(pthread_cond_t *cond) {
110 cond_t *cond_data = (cond_t *)*cond;
111 LightLock_Lock(&cond_data->lock);
112 if (cond_data->waiting) {
113 cond_data->waiting--;
114 svcReleaseSemaphore(&count, cond_data->semaphore, 1);
116 LightLock_Unlock(&cond_data->lock);
120 static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *lock) {
121 cond_t *cond_data = (cond_t *)*cond;
122 LightLock_Lock(&cond_data->lock);
123 cond_data->waiting++;
124 LightLock_Unlock(lock);
125 LightLock_Unlock(&cond_data->lock);
126 svcWaitSynchronization(cond_data->semaphore, INT64_MAX);
127 LightLock_Lock(lock);
131 static inline int pthread_cond_destroy(pthread_cond_t *cond) {
133 cond_t *cond_data = (cond_t *)*cond;
135 svcCloseHandle(cond_data->semaphore);
143 #endif //_3DS_PTHREAD_WRAP__