ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
thread.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Licenced under Academic Free License version 2.0
7 # Review ps2sdk README & LICENSE files for further details.
8 */
9 
15 #include "kernel.h"
16 
17 struct request{
20 };
21 
22 enum TOP_REQ{
26 };
27 
28 struct topArg {
30  int requestIn;
31  struct request request[512];
32 };
33 
34 extern int topId;
35 extern int topSema;
36 extern struct topArg topArg;
37 
38 #ifdef F__thread_internals
39 
40 extern void *_gp;
41 
42 u8 stack[0x400] __attribute__((aligned(16)));
43 
44 int topId = 0;
45 int topSema = 0;
46 struct topArg topArg = {0};
47 
48 static void topThread(void *arg)
49 {
50  int index;
51 
52  while(1)
53  {
55  index = topArg.requestOut & 0x1FF;
56  topArg.requestOut = index + 1;
57 
58  switch(topArg.request[index].mode)
59  {
60  case TOP_REQ_WAKEUP:
62  break;
63  case TOP_REQ_ROTATE:
65  break;
66  case TOP_REQ_SUSPEND:
68  break;
69  /* default:
70  Kprintf("## internal error in libkernel!\n"); */
71  }
72  }
73 }
74 
75 int InitThread(void)
76 {
77  ee_sema_t sema;
78  ee_thread_t thread;
79 
80  sema.max_count = 255;
81  sema.init_count = 0;
82  sema.option = (u32)"KernelTopThread";
83  if((topSema = CreateSema(&sema)) < 0)
84  return -1;
85 
86  thread.func = &topThread;
87  thread.stack = stack;
88  thread.stack_size = sizeof(stack);
89  thread.gp_reg = &_gp;
90  thread.option = (u32)"KernelTopThread";
91  thread.initial_priority = 0;
92  if((topId = CreateThread(&thread)) < 0)
93  {
95  return -1;
96  }
97 
98  topArg.requestOut = 0;
99  topArg.requestIn = 0;
101 
103 
104  return topId;
105 }
106 #endif
107 
108 #ifdef F_iWakeupThread
109 //The original iWakeupThread cannot wake up threads in THS_RUN state.
110 s32 iWakeupThread(s32 thread_id)
111 {
112  int index;
113 
114  if(_iGetThreadId() == thread_id)
115  {
116  if(thread_id < 256 && topId != 0)
117  {
118  index = topArg.requestIn & 0x1FF;
119  topArg.requestIn = index + 1;
121  topArg.request[index].data = thread_id;
122 
124  return 0;
125  } else
126  return -1;
127  } else {
128  return _iWakeupThread(thread_id);
129  }
130 }
131 #endif
132 
133 #ifdef F_iRotateThreadReadyQueue
134 //The original iRotateThreadReadyQueue will not change the current thread ID.
136 {
137  int index;
138 
139  if(priority < 128 && topId != 0)
140  {
141  index = topArg.requestIn & 0x1FF;
142  topArg.requestIn = index + 1;
144  topArg.request[index].data = priority;
145 
147  return 0;
148  } else
149  return -1;
150 }
151 #endif
152 
153 #ifdef F_iSuspendThread
154 //The original iSuspendThread allows a thread to suspend itself, but won't change the current thread ID.
155 s32 iSuspendThread(s32 thread_id)
156 {
157  int index;
158 
159  if(_iGetThreadId() == thread_id)
160  {
161  if(thread_id < 256 && topId != 0)
162  {
163  index = topArg.requestIn & 0x1FF;
164  topArg.requestIn = index + 1;
166  topArg.request[index].data = thread_id;
167 
169  return 0;
170  } else
171  return -1;
172  } else {
173  return _iSuspendThread(thread_id);
174  }
175 }
176 #endif
s32 CreateSema(ee_sema_t *sema)
s32 iWakeupThread(s32 thread_id)
s32 _iWakeupThread(s32 thread_id)
s32 DeleteSema(s32 sema_id)
s32 iSignalSema(s32 sema_id)
s32 _iSuspendThread(s32 thread_id)
s32 iRotateThreadReadyQueue(s32 priority)
s32 CreateThread(ee_thread_t *thread)
int InitThread(void)
s32 iSuspendThread(s32 thread_id)
s32 WakeupThread(s32 thread_id)
s32 WaitSema(s32 sema_id)
s32 StartThread(s32 thread_id, void *args)
void * _gp
s32 _iGetThreadId(void)
s32 RotateThreadReadyQueue(s32 priority)
s32 SuspendThread(s32 thread_id)
s32 ChangeThreadPriority(s32 thread_id, s32 priority)
s32 GetThreadId(void)
s32 index
Definition: libpad.c:194
int init_count
Definition: kernel.h:218
int max_count
Definition: kernel.h:217
u32 option
Definition: kernel.h:221
void * gp_reg
Definition: kernel.h:230
u32 option
Definition: kernel.h:234
int stack_size
Definition: kernel.h:229
void * func
Definition: kernel.h:227
void * stack
Definition: kernel.h:228
int initial_priority
Definition: kernel.h:231
Definition: thread.c:17
u8 mode
Definition: thread.c:18
u8 data
Definition: thread.c:19
Definition: thread.c:28
int requestIn
Definition: thread.c:30
struct request request[512]
Definition: thread.c:31
int requestOut
Definition: thread.c:29
signed int s32
Definition: tamtypes.h:58
unsigned int u32
Definition: tamtypes.h:30
unsigned char u8
Definition: tamtypes.h:23
int topSema
int topId
TOP_REQ
Definition: thread.c:22
@ TOP_REQ_SUSPEND
Definition: thread.c:25
@ TOP_REQ_WAKEUP
Definition: thread.c:23
@ TOP_REQ_ROTATE
Definition: thread.c:24