836f4cb5a74d7b3776c99d1bd5d30cd57572b552
[pcsx_rearmed.git] / frontend / 3ds / pthread.h
1
2 #ifndef _3DS_PTHREAD_WRAP__
3 #define _3DS_PTHREAD_WRAP__
4
5 #include "3ds.h"
6 #include "stdlib.h"
7 #include "string.h"
8 #include "stdio.h"
9
10
11 #define CTR_PTHREAD_STACK_SIZE 0x10000
12
13 typedef struct
14 {
15    Handle handle;
16    u32* stack;
17 }pthread_t;
18 typedef int pthread_attr_t;
19
20 //#ifndef DEBUG_HOLD
21 //#include "stdio.h"
22 //void wait_for_input(void);
23
24 //#define DEBUG_HOLD() do{printf("%s@%s:%d.\n",__FUNCTION__, __FILE__, __LINE__);fflush(stdout);wait_for_input();}while(0)
25 //#endif
26
27 static inline int pthread_create(pthread_t *thread,
28       const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
29 {
30
31 //   DEBUG_HOLD();
32
33    thread->stack =  linearMemAlign(CTR_PTHREAD_STACK_SIZE, 8);
34
35    svcCreateThread(&thread->handle, (ThreadFunc)start_routine,arg,
36                    (u32*)((u32)thread->stack + CTR_PTHREAD_STACK_SIZE),
37                    0x25, 1);
38
39    return 1;
40 }
41
42
43 static inline int pthread_join(pthread_t thread, void **retval)
44 {
45    (void)retval;
46
47 //   DEBUG_HOLD();
48    if(svcWaitSynchronization(thread.handle, INT64_MAX))
49       return -1;
50
51    linearFree(thread.stack);
52
53    return 0;
54 }
55
56
57 static inline void pthread_exit(void *retval)
58 {   
59    (void)retval;
60
61 //   DEBUG_HOLD();
62    svcExitThread();
63 }
64
65 //#undef DEBUG_HOLD
66
67 #endif //_3DS_PTHREAD_WRAP__
68