RetroArch
ctr_pthread.h
Go to the documentation of this file.
1 /* Copyright (C) 2010-2018 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (gx_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 _CTR_PTHREAD_WRAP_CTR_
24 #define _CTR_PTHREAD_WRAP_CTR_
25 
26 #include <3ds/thread.h>
27 #include <3ds/synchronization.h>
28 #include <3ds/svc.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <retro_inline.h>
32 
33 #define STACKSIZE (4 * 1024)
34 
35 typedef Thread pthread_t;
36 typedef LightLock pthread_mutex_t;
37 typedef void* pthread_mutexattr_t;
38 typedef int pthread_attr_t;
39 typedef LightEvent pthread_cond_t;
40 typedef int pthread_condattr_t;
41 
42 /* libctru threads return void but pthreads return void pointer */
43 static bool mutex_inited = false;
44 static LightLock safe_double_thread_launch;
45 static void *(*start_routine_jump)(void*);
46 
47 static void ctr_thread_launcher(void* data)
48 {
49  void *(*start_routine_jump_safe)(void*) = start_routine_jump;
50  LightLock_Unlock(&safe_double_thread_launch);
51  start_routine_jump_safe(data);
52 }
53 
54 static INLINE int pthread_create(pthread_t *thread,
55  const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
56 {
57  s32 prio = 0;
58  Thread new_ctr_thread;
59 
60  if (!mutex_inited)
61  {
62  LightLock_Init(&safe_double_thread_launch);
63  mutex_inited = true;
64  }
65 
66  /*Must wait if attempting to launch 2 threads at once to prevent corruption of function pointer*/
67  while (LightLock_TryLock(&safe_double_thread_launch) != 0);
68 
69  svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
70 
71  start_routine_jump = start_routine;
72  new_ctr_thread = threadCreate(ctr_thread_launcher, arg, STACKSIZE, prio - 1, -1/*No affinity, use any CPU*/, false);
73 
74  if (!new_ctr_thread)
75  {
76  LightLock_Unlock(&safe_double_thread_launch);
77  return EAGAIN;
78  }
79 
80  *thread = new_ctr_thread;
81  return 0;
82 }
83 
85 {
86  return threadGetCurrent();
87 }
88 
90  const pthread_mutexattr_t *attr)
91 {
92  LightLock_Init(mutex);
93  return 0;
94 }
95 
97 {
98  /*Nothing to destroy*/
99  return 0;
100 }
101 
103 {
104  return LightLock_TryLock(mutex);
105 }
106 
108 {
109  LightLock_Unlock(mutex);
110  return 0;
111 }
112 
113 static INLINE void pthread_exit(void *retval)
114 {
115  /*Yes the pointer to int cast is not ideal*/
116  /*threadExit((int)retval);*/
117  (void)retval;
118 }
119 
120 static INLINE int pthread_detach(pthread_t thread)
121 {
122  /* FIXME: pthread_detach equivalent missing? */
123  (void)thread;
124  return 0;
125 }
126 
127 static INLINE int pthread_join(pthread_t thread, void **retval)
128 {
129  /*retval is ignored*/
130  return threadJoin(thread, U64_MAX);
131 }
132 
134 {
135  return LightLock_TryLock(mutex);
136 }
137 
140 {
141  LightEvent_Wait(cond);
142  return 0;
143 }
144 
146  pthread_mutex_t *mutex, const struct timespec *abstime)
147 {
148  while (true)
149  {
150  struct timespec now = {0};
151  /* Missing clock_gettime*/
152  struct timeval tm;
153 
154  gettimeofday(&tm, NULL);
155  now.tv_sec = tm.tv_sec;
156  now.tv_nsec = tm.tv_usec * 1000;
157  if (LightEvent_TryWait(cond) != 0 || now.tv_sec > abstime->tv_sec || (now.tv_sec == abstime->tv_sec && now.tv_nsec > abstime->tv_nsec))
158  break;
159  }
160 
161  return 0;
162 }
163 
165  const pthread_condattr_t *attr)
166 {
167  LightEvent_Init(cond, RESET_ONESHOT);
168  return 0;
169 }
170 
172 {
173  LightEvent_Signal(cond);
174  return 0;
175 }
176 
178 {
179  LightEvent_Signal(cond);
180  return 0;
181 }
182 
184 {
185  /*nothing to destroy*/
186  return 0;
187 }
188 
190 {
191  if (threadGetHandle(t1) == threadGetHandle(t2))
192  return 1;
193  return 0;
194 }
195 
196 #endif
Thread pthread_t
Definition: ctr_pthread.h:35
static INLINE int pthread_detach(pthread_t thread)
Definition: ctr_pthread.h:120
static INLINE int pthread_equal(pthread_t t1, pthread_t t2)
Definition: ctr_pthread.h:189
int32_t s32
32bit signed integer
Definition: gctypes.h:24
#define INLINE
Definition: retro_inline.h:35
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: ctr_pthread.h:89
#define STACKSIZE
Definition: ctr_pthread.h:33
static INLINE int pthread_cond_signal(pthread_cond_t *cond)
Definition: ctr_pthread.h:171
static int cond(LexState *ls)
Definition: lparser.c:1177
static LightLock safe_double_thread_launch
Definition: ctr_pthread.h:44
#define EAGAIN
Definition: need_errno.h:42
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
int pthread_condattr_t
Definition: ctr_pthread.h:40
static sys_sem mutex
Definition: memp.c:120
Definition: ibxm.h:9
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
Definition: ctr_pthread.h:133
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: ctr_pthread.h:107
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: ctr_pthread.h:145
static bool mutex_inited
Definition: ctr_pthread.h:43
#define NULL
Pointer to 0.
Definition: gctypes.h:65
Definition: implement.h:146
static void ctr_thread_launcher(void *data)
Definition: ctr_pthread.h:47
Definition: psp_pthread.h:45
Definition: sockets.h:217
static INLINE int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: ctr_pthread.h:138
void * pthread_t
Definition: pthread.h:398
static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: ctr_pthread.h:96
static INLINE void pthread_exit(void *retval)
Definition: ctr_pthread.h:113
static INLINE pthread_t pthread_self(void)
Definition: ctr_pthread.h:84
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: ctr_pthread.h:102
static INLINE int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: ctr_pthread.h:164
Definition: implement.h:163
static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
Definition: ctr_pthread.h:183
Definition: implement.h:116
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: ctr_pthread.h:177
static INLINE Thread threadGetCurrent(void)
Definition: switch_pthread.h:61
int pthread_attr_t
Definition: ctr_pthread.h:38
void * pthread_mutexattr_t
Definition: ctr_pthread.h:37
long tv_usec
Definition: sockets.h:219
Definition: implement.h:251
LightLock pthread_mutex_t
Definition: ctr_pthread.h:36
LightEvent pthread_cond_t
Definition: ctr_pthread.h:39
long tv_sec
Definition: sockets.h:218
static INLINE int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: ctr_pthread.h:54
static void *(* start_routine_jump)(void *)
Definition: ctr_pthread.h:45
static INLINE int pthread_join(pthread_t thread, void **retval)
Definition: ctr_pthread.h:127