static void execPriorityTests(int attr, int changePriority) { printf("For attr %08X%s:\n", attr, changePriority ? " and changed priorities" : ""); schedulingLogPos = 0; SceUID threads[7]; int test[7] = {1, 2, 3, 4, 5, 6, 7}; mbx = sceKernelCreateMbx("mbx1", attr, NULL); PRINT_MBX(mbx); sendMbx(mbx, 0x20); sendMbx(mbx, 0x10); int i; for (i = 0; i < 7; i++) { threads[i] = CREATE_PRIORITY_THREAD(threadFunction, 0x18 - i); sceKernelStartThread(threads[i], sizeof(int), (void*)&test[i]); } sceKernelDelayThread(10 * 1000); // What we're gonna do now is change the threads' priorities to see whether // priority at time of wait (already happened) or at time of send matters. if (changePriority) { for (i = 0; i < 7; i++) { sceKernelChangeThreadPriority(threads[i], 0x18 - 7 + i); } printf("Priorities reversed. Have a nice day.\n"); } schedf("---\n"); PRINT_MBX(mbx); schedf("---\n"); sendMbx(mbx, 0x15); sceKernelDelayThread(10 * 1000); schedf("---\n"); PRINT_MBX(mbx); schedf("---\n"); sendMbx(mbx, 0x20); sendMbx(mbx, 0x10); sceKernelDelayThread(10 * 1000); schedf("---\n"); PRINT_MBX(mbx); schedf("---\n"); sceKernelDeleteMbx(mbx); schedf("\n\n"); printf("%s", schedulingLog); }
int main(int argc, char *argv[]) { SceUID thid; int error; void *data; pspDebugScreenInit(); if (argc > 0) { printf("Bootpath: %s\n", argv[0]); } SetupCallbacks(); /* Create a messagebox */ myMessagebox = sceKernelCreateMbx("pspSDK-testMBX", 0, 0); printf("MAIN: created messagebox %08x\n", myMessagebox); /* Create a task that will post in the messagebox */ thid = sceKernelCreateThread("subthread", SubThread, 17, 8192, THREAD_ATTR_USER, 0); sceKernelStartThread(thid, 0, NULL); printf("MAIN: started task %08x\n", thid); /* Wait for a message */ printf("MAIN: waiting for message\n"); error = sceKernelReceiveMbx(myMessagebox, &data, NULL); if(error < 0) printf("MAIN: ERROR %08x\n", error); else printf("MAIN: got message: \"%s\"\n", ((MyMessage *)data)->text); /* Wait for a message with timeout */ printf("MAIN: waiting with timeout (will fail the first couple of times)\n"); for(;;) { SceUInt timeout = 300000; /* microseconds */ error = sceKernelReceiveMbx(myMessagebox, &data, &timeout); if(error < 0) printf("MAIN: ERROR %08x\n", error); else { printf("MAIN: got message: \"%s\" (timeout remaining %d us)\n", ((MyMessage *)data)->text, timeout); break; } } /* Poll for messages */ printf("MAIN: polling for message (non-blocking)\n"); for(;;) { error = sceKernelPollMbx(myMessagebox, &data); if(error < 0) { printf("MAIN: ERROR %08x\n", error); /* Sleep for a little while to give the message a chance to arrive */ sceKernelDelayThread(300000); } else { printf("MAIN: got message: \"%s\"\n", ((MyMessage *)data)->text); break; } } /* This call to sceKernelReceiveMbx() will be interrupted by the sub task without a message being sent */ printf("MAIN: waiting for a message that will not arrive\n"); error = sceKernelReceiveMbx(myMessagebox, &data, NULL); if(error < 0) printf("MAIN: ERROR %08x\n", error); else printf("MAIN: got message: \"%s\"\n", ((MyMessage *)data)->text); /* Prepare to shutdown */ printf("MAIN: waiting for sub task to exit\n"); sceKernelWaitThreadEnd(thid, NULL); printf("MAIN: sub task exited, deleting messagebox\n"); error = sceKernelDeleteMbx(myMessagebox); if(error < 0) printf("MAIN: ERROR %08x\n", error); else printf("MAIN: all done\n"); sceKernelSleepThread(); return 0; }