//The main function. //Should be pretty simple to understand void start_func(void *j) { MyThread tmp[100]; MySemaphore sem[10]; printf("0. Root thread started.\n"); printf("...<MyThreadCreate>>\n"); tmp[1]=MyThreadCreate(printv,NULL ); printf("1. Created a Test Thread. \n"); printf("...<MyThreadYield>>\n"); MyThreadYield(); tmp[2]=MyThreadCreate(printa,2); printf("4. Return value from a valid join (0=%i)\n",MyThreadJoin(tmp[2])); printf("5. Return value from an expired join (-1=%i)\n",MyThreadJoin(tmp[2])); printf("...<MyThreadJoinAll>>\n"); printf("6. Create three threads & then joinall on them.\n"); tmp[3]=MyThreadCreate(printj,NULL); tmp[4]=MyThreadCreate(printj,NULL); tmp[5]=MyThreadCreate(printj,NULL); MyThreadJoinAll(); printf("7. Returned from Join All. \n"); printf("...<MySemaphoreInit>>\n"); sem[1]=MySemaphoreInit(1); sem[2]=MySemaphoreInit(2); sem[3]=MySemaphoreInit(3); printf("8. Created 3 Semaphores with initialvalues 1,2,3.\n"); printf("...<MySemaphoreWait>>\n"); MySemaphoreWait(sem[1]); MySemaphoreWait(sem[2]); MySemaphoreWait(sem[3]); printf("9. Waited on all three Semaphores should have values 0,1,2.\n"); tmp[6]=MyThreadCreate(semtest,sem[1]); MyThreadYield(); MySemaphoreSignal(sem[1]); printf("... Parent Signaled \n"); MyThreadYield(); MySemaphoreWait(sem[2]); tmp[7]=MyThreadCreate(semtest2,sem[2]); printf("10. Lets try it in reverse...\n"); printf("...parent waits on a semaphore with value 0.\n"); MySemaphoreWait(sem[2]); printf("...parent passes through now.\n"); printf("11. Recursively build %i threads ",rcount); MyThreadCreate(recursion,1); MyThreadJoinAll(); printf("... success.\n"); rcount=1000; printf("12. Create a producer/consumer scenario with %i threads",pcount*2); tmp[8]=MyThreadCreate(producer_consumer,NULL); MyThreadJoin(tmp[8]); printf("... success \n"); MyThreadExit(); }
//A recusion test that spawns up to "rcount" threads void recursion(int i) { int j; j=i+1; if (i < rcount) { MyThreadCreate(recursion,j); } MyThreadYield(); MyThreadExit(); }
void t1(void * who) { int i; printf("t%d start\n", (int)who); for (i = 0; i < n; i++) { printf("t%d yield\n", (int)who); MyThreadYield(); } printf("t%d end\n", (int)who); MyThreadExit(); }
int makeThreads(char *me, void (*t)(void *), int many) { MyThread T; int i; for (i = 0; i < many; i++) { printf("\n%s create %d\n", me, i); T = MyThreadCreate(t, (void *)i); if (yield) MyThreadYield(); } if (join) MyThreadJoin(T); }
//A simple function that is created and then joined void printj(void) { printf("... Joinall Test\n"); MyThreadYield(); MyThreadExit(); }