1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
void runcallback(ThreadParams_p *threadparams)
{
dolog("threads","Runcallback...");
//Now run the requested thread!
if (threadparams) //Gotten params?
{
ThreadParams_p realparams = *threadparams; //The real parameters!
if (realparams) //Gotten real params?
{
if (realparams->callback) //Found the callback?
{
dolog("threads","RunRealCallback...");
realparams->callback(); //Execute the real callback!
}
}
}
dolog("threads","Callback RET.");
}
int threadhandler(SceSize args, void *params)
{
dolog("threads","threadhandler...");
dolog("threads","threadhandler2...");
ThreadParams_p *threadparams;
threadparams = params; //Get our parameters!
dolog("threads","threadhandler: Mark thread active...");
activeThread(threadparams); //Mark us as running!
dolog("threads","threadhandler: Run callback...");
runcallback(threadparams); //Run the callback!
dolog("threads","threadhandler: Terminate thread...");
terminateThread(threadparams); //Terminate ourselves!
dolog("threads","threadhandler: ThreadRET?...");
return 0; //Disable threads?
}
void termThreads() //Terminate all threads but our own!
{
dolog("threads","termThreads...");
int i;
SceUID my_thid = sceKernelGetThreadId(); //My own thread ID!
for (i=0;i<NUMITEMS(threadpool);i++) //Process all of our threads!
{
if (is_pooledthread(&threadpool[i])) //Used?
{
if (threadpool[i]->threadID!=my_thid) //Not ourselves?
{
switch (threadpool[i]->status) //What status?
{
case THREADSTATUS_ALLOCATED: //Allocated?
dolog("threads","termThreads: Allocated!");
freeThread(&threadpool[i]); //Deallocate it!
break; //Just allocated, nothing more!
case THREADSTATUS_CREATEN: //Createn?
dolog("threads","termThreads: Createn!");
deleteThread(&threadpool[i]); //Delete it!
break; //Removed!
case THREADSTATUS_RUNNING: //Running?
dolog("threads","termThreads: Running!");
terminateThread(&threadpool[i]); //Terminate it!
break;
}
}
}
}
}
void debug_threads()
{
char thread_name[256];
bzero(thread_name,sizeof(thread_name)); //Init!
while (1)
{
byte numthreads = 0; //Number of installed threads running!
int i,i2;
for (i=0;i<NUMITEMS(threadpool);i++)
{
if (threadpool[i]) //Allocated?
{
if (threadpool[i]->status>=THREADSTATUS_CREATEN) //Created or running?
{
++numthreads; //Count ammount of threads!
gotoxy(0,30-NUMITEMS(threadpool)+numthreads); //Goto the debug row start!
fontcolor(RGB(0xFF,0x00,0x00));
backcolor(RGB(0x00,0xFF,0x00)); //Red on green!
sprintf(thread_name,"Active thread: %s",threadpool[i]->name); //Get data string!
printf(thread_name);
for (i2=strlen(thread_name);i2<=50;i2++)
{
printf(" "); //Filler to 50 chars!
}
}
}
}
gotoxy(0,30);
printf("Number of threads: %i",numthreads); //Debug the ammount of threads used!
delay(100000); //Wait 100ms!
}
}
void initThreads() //Initialise&reset thread subsystem!
{
dolog("threads","initThreads...");
dolog("threads","initThreads: termThreads...");
termThreads(); //Make sure all running threads are stopped!
dolog("threads","debugThreads?...");
if (DEBUG_THREADS) startThread(&debug_threads,"Thread Debugger",DEFAULT_PRIORITY); //Plain debug threads!
}
void threadCreaten(ThreadParams_p *params, SceUID threadID, char *name)
{
dolog("threads","threadCreaten...");
if (params) //Gotten params pointer?
{
dolog("threads","threadCreaten1...");
if (*params) //Gotten params?
{
dolog("threads","threadCreaten2...");
ThreadParams_p realparams = *params; //Real params?
realparams->status = THREADSTATUS_CREATEN; //Createn!
realparams->threadID = threadID; //The thread ID!
bzero(realparams->name,sizeof(realparams->name));
strcpy(realparams->name,name); //Save the name for usage!
}
}
}
void startThread(Handler thefunc, char *name, int priority) //Start a thread!
{
dolog("threads","startThread...");
if (!thefunc || thefunc==NULL) //No func?
{
dolog("threads","startThread: NULLfunc...");
raiseError("thread manager","NULL thread: %s",name); //Show the thread as being NULL!
return; //Don't start: can't start no function!
}
//We create our handler in dynamic memory, because we need to keep it in the threadhandler!
dolog("threads","startThread: allocThread...");
//First, allocate a thread position!
ThreadParams_p *params = allocateThread(); //Allocate a thread for us, wait for any to come free in the meanwhile!
//Next, start the timer function!
SceUID thid; //The thread ID to allocate!
dolog("threads","startThread: createThread...");
docreatethread: //Try to create a thread!
thid = sceKernelCreateThread(name, threadhandler, priority, THREAD_STACKSIZE, PSP_THREAD_ATTR_USER|PSP_THREAD_ATTR_NO_FILLSTACK|PSP_THREAD_ATTR_CLEAR_STACK, NULL); //Try to create the thread!
switch ((thid>>16)&0xFFFF) //Failed to create?
{
case 0x0000: //Might be null?
dolog("threads","startThread: failcreateNULL?...");
if (!thid) //NULL?
{
dolog("threads","startThread: failcreateNULL!...");
goto docreatethread; //Retry until a thread comes free to use!
}
break; //Just continue!
case 0x8002: //Kernel error?
case 0x8001: //LibC error?
dolog("threads","startThread: delay(1)!...");
delay(1); //Wait for any threads to come free!
dolog("threads","startThread: checkUnpooled!...");
if (!is_pooledthread(params)) //We've been terminated?
{
dolog("threads","startThread: Unpooled!...");
freeThread(params); //Free the thread!
return; //Abort: we've been terminated!
}
dolog("threads","startThread: Retry800X!...");
goto docreatethread; //Retry until a thread comes free to use!
break; //Just here to be sure!
default: //No error?
dolog("threads","startThread: ThreadCreaten!...");
break; //Just continue!
}
dolog("threads","startThread: threadCreaten...");
threadCreaten(params,thid,name); //We've been createn!
dolog("threads","startThread: checking params...");
if (params) //Valid params?
{
ThreadParams_p realparams = *params;
dolog("threads","startThread: Ready to go?");
if (realparams->status) //We're ready to go?
{
dolog("threads","startThread: Ready! Setting callback...");
//Set our parameters!
realparams->callback = thefunc; //The function to run!
dolog("threads","startThread: Ready! StartThread...");
//We have a thread we can use now!
sceKernelStartThread(thid, sizeof(params), params); //Start thread, if possible! Give it the handler we need!
}
else //Remove the thread: we're done with it!
{
dolog("threads","startThread: Status wrong. Calling deleteThread...");
deleteThread(params); //Cleanup the parameters!
}
}
}
|