Add async CD access
[pcsx_rearmed.git] / frontend / 3ds / pthread.h
CommitLineData
fc99395c 1
2#ifndef _3DS_PTHREAD_WRAP__
3#define _3DS_PTHREAD_WRAP__
4
f72db18e 5#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
fc99395c 8
f72db18e 9#include "3ds_utils.h"
fc99395c 10
37c2f059 11#define CTR_PTHREAD_STACK_SIZE 0x10000
679b71e2 12#define FALSE 0
fc99395c 13
93c24a56 14typedef int32_t pthread_t;
fc99395c 15typedef int pthread_attr_t;
16
679b71e2
JW
17typedef LightLock pthread_mutex_t;
18typedef int pthread_mutexattr_t;
19
20typedef struct {
21 uint32_t semaphore;
22 LightLock lock;
23 uint32_t waiting;
24} pthread_cond_t;
25
26typedef int pthread_condattr_t;
27
fc99395c 28static inline int pthread_create(pthread_t *thread,
29 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
30{
679b71e2
JW
31 int procnum = -2; // use default cpu
32 bool isNew3DS;
33 APT_CheckNew3DS(&isNew3DS);
34
35 if (isNew3DS)
36 procnum = 2;
37
38 *thread = threadCreate(start_routine, arg, CTR_PTHREAD_STACK_SIZE, 0x25, procnum, FALSE);
39 return 0;
fc99395c 40}
41
42
43static inline int pthread_join(pthread_t thread, void **retval)
44{
45 (void)retval;
46
93c24a56 47 if(threadJoin(thread, INT64_MAX))
fc99395c 48 return -1;
49
93c24a56 50 threadFree(thread);
fc99395c 51
52 return 0;
53}
54
55
56static inline void pthread_exit(void *retval)
57{
58 (void)retval;
59
93c24a56 60 threadExit(0);
fc99395c 61}
62
679b71e2
JW
63static inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) {
64 LightLock_Init(mutex);
65 return 0;
66}
67
68static inline int pthread_mutex_lock(pthread_mutex_t *mutex) {
69 LightLock_Lock(mutex);
70 return 0;
71}
72
73static inline int pthread_mutex_unlock(pthread_mutex_t *mutex) {
74 LightLock_Unlock(mutex);
75 return 0;
76}
77
78static inline int pthread_mutex_destroy(pthread_mutex_t *mutex) {
79 return 0;
80}
81
82static inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) {
83 if (svcCreateSemaphore(&cond->semaphore, 0, 1))
84 goto error;
85
86 LightLock_Init(&cond->lock);
87 cond->waiting = 0;
88 return 0;
89
90 error:
91 svcCloseHandle(cond->semaphore);
92 return -1;
93}
94
95static inline int pthread_cond_signal(pthread_cond_t *cond) {
96 int32_t count;
97 LightLock_Lock(&cond->lock);
98 if (cond->waiting) {
99 cond->waiting--;
100 svcReleaseSemaphore(&count, cond->semaphore, 1);
101 }
102 LightLock_Unlock(&cond->lock);
103 return 0;
104}
105
106static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *lock) {
107 LightLock_Lock(&cond->lock);
108 cond->waiting++;
109 LightLock_Unlock(lock);
110 LightLock_Unlock(&cond->lock);
111 svcWaitSynchronization(cond->semaphore, INT64_MAX);
112 LightLock_Lock(lock);
113 return 0;
114}
115
116static inline int pthread_cond_destroy(pthread_cond_t *cond) {
117 svcCloseHandle(cond->semaphore);
118 return 0;
119}
120
fc99395c 121
122#endif //_3DS_PTHREAD_WRAP__
123