BOOL UtJoin(HANDLE ThreadHandle) { PUTHREAD Uthread = (PUTHREAD)ThreadHandle; if (Uthread == RunningThread || !UtAlive(ThreadHandle)) return FALSE; RunningThread->State = BLOCKED; InsertTailList(&Uthread->Joiners, &RunningThread->Link); UtDeactivate(); return TRUE; }
VOID WaitLatch (PCountDownLatch CountDownLatch) { // coloca a thread invocante em espera até que o contador interno tenha o valor zero if (CountDownLatch->counter > 0) { WAIT_BLOCK WaitBlock; WaitBlock.Thread = UtSelf(); InsertTailList(&CountDownLatch->Waiters, &WaitBlock.Link); UtDeactivate(); } }
VOID EventWait (PEVENT Event) { if (Event->Signaled == TRUE) { Event->Signaled = FALSE; } else { WAIT_BLOCK WaitBlock; WaitBlock.Thread = UtSelf(); InsertTailList(&Event->Waiters, &WaitBlock.Link); UtDeactivate(); } }
void Client(UT_ARGUMENT arg) { int i; long * numbers = (long *)arg; clientStarted = true; if ( ! serverStarted ) UtDeactivate(); for (i = 0; i < MAX_WORK; i++) { n = numbers[i]; hasWork = true; hasAnswer = false; UtActivate(utServer); if (!hasAnswer) UtDeactivate(); numbers[i] = res; } end = true; hasWork = true; UtActivate(utServer); }
void Server(UT_ARGUMENT arg) { serverStarted = true; if ( clientStarted ) UtActivate(utClient); for (;;) { if (!hasWork) UtDeactivate(); if (end) break; res = factorial(n); hasAnswer = true; hasWork = false; UtActivate(utClient); } }