RetroArch
switch_pthread.h
Go to the documentation of this file.
1 /* Copyright (C) 2018 - M4xw <[email protected]>, RetroArch Team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (switch_pthread.h).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef _SWITCH_PTHREAD_WRAP_
24 #define _SWITCH_PTHREAD_WRAP_
25 
26 #include <time.h>
27 #include <stdio.h>
28 #include <switch.h>
29 #include <errno.h>
30 
31 #include <retro_inline.h>
32 
33 #define THREADVARS_MAGIC 0x21545624 /* !TV$ */
34 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
35 void pthread_exit(void *retval);
36 
37 /* This structure is exactly 0x20 bytes, if more is needed modify getThreadVars() below */
38 typedef struct
39 {
40  /* Magic value used to check if the struct is initialized */
42 
43  /* Thread handle, for mutexes */
44  Handle handle;
45 
46  /* Pointer to the current thread (if exists) */
47  Thread *thread_ptr;
48 
49  /* Pointer to this thread's newlib state */
50  struct _reent *reent;
51 
52  /* Pointer to this thread's thread-local segment */
53  void *tls_tp; /* !! Offset needs to be TLS+0x1F8 for __aarch64_read_tp !! */
54 } ThreadVars;
55 
57 {
58  return (ThreadVars *)((u8 *)armGetTls() + 0x1E0);
59 }
60 
61 static INLINE Thread threadGetCurrent(void)
62 {
63  ThreadVars *tv = getThreadVars();
64  return *tv->thread_ptr;
65 }
66 
68 {
69  return threadGetCurrent();
70 }
71 
73 {
74  mutexInit(mutex);
75 
76  return 0;
77 }
78 
80 {
81  /* Nothing */
82  *mutex = 0;
83 
84  return 0;
85 }
86 
88 {
89  mutexLock(mutex);
90  return 0;
91 }
92 
94 {
95  mutexUnlock(mutex);
96 
97  return 0;
98 }
99 
101 {
102  (void)thread;
103  /* Nothing for now */
104  return 0;
105 }
106 
107 static INLINE int pthread_join(pthread_t thread, void **retval)
108 {
109  printf("[Threading]: Waiting for Thread Exit\n");
110  threadWaitForExit(&thread);
111  threadClose(&thread);
112 
113  return 0;
114 }
115 
117 {
118  return mutexTryLock(mutex) ? 0 : 1;
119 }
120 
122 {
123  condvarWait(cond, mutex);
124 
125  return 0;
126 }
127 
128 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
129 {
130  condvarWaitTimeout(cond, mutex, abstime->tv_nsec);
131 
132  return 0;
133 }
134 
136 {
137  condvarInit(cond);
138 
139  return 0;
140 }
141 
143 {
144  condvarWakeOne(cond);
145  return 0;
146 }
147 
149 {
150  condvarWakeAll(cond);
151  return 0;
152 }
153 
155 {
156  /* Nothing */
157  return 0;
158 }
159 
161 {
162  if (t1.handle == t2.handle)
163  return 1;
164 
165  return 0;
166 }
167 
168 #endif
static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: switch_pthread.h:79
#define INLINE
Definition: retro_inline.h:35
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: pte_main.c:103
Handle handle
Definition: switch_pthread.h:44
struct _reent * reent
Definition: switch_pthread.h:50
static INLINE int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: switch_pthread.h:121
static int cond(LexState *ls)
Definition: lparser.c:1177
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
static sys_sem mutex
Definition: memp.c:120
static INLINE int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: switch_pthread.h:135
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
Definition: switch_pthread.h:116
INLINE int pthread_equal(pthread_t t1, pthread_t t2)
Definition: switch_pthread.h:160
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: switch_pthread.h:93
Definition: implement.h:146
Definition: switch_pthread.h:38
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: switch_pthread.h:148
Definition: psp_pthread.h:45
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: switch_pthread.h:72
static INLINE ThreadVars * getThreadVars(void)
Definition: switch_pthread.h:56
void * pthread_t
Definition: pthread.h:398
void pthread_exit(void *retval)
Definition: pthread.c:648
static INLINE int pthread_join(pthread_t thread, void **retval)
Definition: switch_pthread.h:107
void * tls_tp
Definition: switch_pthread.h:53
INLINE int pthread_detach(pthread_t thread)
Definition: switch_pthread.h:100
static INLINE pthread_t pthread_self(void)
Definition: switch_pthread.h:67
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: switch_pthread.h:87
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: switch_pthread.h:128
INLINE int pthread_cond_destroy(pthread_cond_t *cond)
Definition: switch_pthread.h:154
u32 magic
Definition: switch_pthread.h:41
Definition: implement.h:163
Definition: implement.h:116
static INLINE Thread threadGetCurrent(void)
Definition: switch_pthread.h:61
uint8_t u8
8bit unsigned integer
Definition: gctypes.h:17
Definition: implement.h:251
uint32_t u32
32bit unsigned integer
Definition: gctypes.h:19
static INLINE int pthread_cond_signal(pthread_cond_t *cond)
Definition: switch_pthread.h:142
Thread * thread_ptr
Definition: switch_pthread.h:47