VOID UtWaitForSyncObject ( PWAIT_REQUEST WaitRequest ) { BOOL Done; PSYNC_OBJECT SyncObject; WAIT_BLOCK WaitBlock; SyncObject = WaitRequest->SyncObject; EnterCriticalSection(&SyncObject->Lock); { // // If the wait request cannot be immediatly satisfied, insert a wait block in the object's wait list. // if ((Done = SyncObject->TrySatisfyWait(WaitRequest)) == FALSE) { WaitBlock.ThreadHandle = UtSelf(); WaitBlock.WaitRequest = WaitRequest; InsertHeadList(&SyncObject->WaitListHead, &WaitBlock.WaitListEntry); } } LeaveCriticalSection(&SyncObject->Lock); if (!Done) { UtPark(); } }
BOOL UtMsgRcv (PVOID Data, PDWORD Size, PHANDLE Sender){ PLIST_ENTRY List; PUTHREAD RunningThread = (PUTHREAD)UtSelf(); if(RunningThread->nMessages == 0){ if(nMsg == 0){ EventInit(RunningThread->ev , FALSE); EventWait(RunningThread->ev); }else{ PMessage m = (PMessage)RemoveHeadList(&QMSG); Data = m->Buffer; Size = m->SizeBuffer; InterlockedIncrement(&nMsg); if(Sender != NULL) Sender = (PHANDLE)RunningThread; return TRUE; } } List = RunningThread->MessageQueue.Flink; PMessage msg = (PMessage)CONTAINING_RECORD(List , Message, Link); if(msg->SizeBuffer > Size){ Size = msg->SizeBuffer; return FALSE; } RemoveHeadList(&RunningThread->MessageQueue); Data = msg->Buffer; Size = msg->SizeBuffer; if(Sender != NULL) Sender = (PHANDLE)RunningThread; return TRUE; }
VOID UtReleaseMutex ( PUTHREAD_MUTEX Mutex ) { BOOL MustUnpark; PWAIT_BLOCK WaitBlock; _ASSERTE(UtSelf() == Mutex->Owner); MustUnpark = FALSE; EnterCriticalSection(&Mutex->Header.Lock); { if ((Mutex->Header.SignalState -= 1) == 0) { if (!IsListEmpty(&Mutex->Header.WaitListHead)) { WaitBlock = CONTAINING_RECORD(RemoveHeadList(&Mutex->Header.WaitListHead), WAIT_BLOCK, WaitListEntry); Mutex->Owner = WaitBlock->ThreadHandle; Mutex->Header.SignalState = 1; MustUnpark = TRUE; } else { Mutex->Owner = NULL; } } } LeaveCriticalSection(&Mutex->Header.Lock); if (MustUnpark) { UtUnpark(WaitBlock->ThreadHandle); } }
BOOL TryAcquireMutex ( PWAIT_REQUEST WaitRequest ) { HANDLE CurrentThread; PUTHREAD_MUTEX Mutex; CurrentThread = UtSelf(); Mutex = (PUTHREAD_MUTEX) WaitRequest->SyncObject; if (Mutex->Header.SignalState == 0) { // // Mutex is free. Acquire the mutex by setting its owner to the requesting thread. // Mutex->Owner = CurrentThread; Mutex->Header.SignalState = 1; return TRUE; } if (CurrentThread == Mutex->Owner) { // // Recursive aquisition. Increment the recursion counter. // Mutex->Header.SignalState += 1; return TRUE; } return FALSE; }
VOID Test1_Thread(UT_ARGUMENT Argument) { UCHAR Char; ULONG Index; Char = (UCHAR)Argument; if(UtThreadState(UtSelf())==1) ++Test1_Count; }
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 UtInitializeMutex ( PUTHREAD_MUTEX Mutex, BOOL Owned ) { InitializeCriticalSection(&Mutex->Header.Lock); InitializeListHead(&Mutex->Header.WaitListHead); Mutex->Header.TrySatisfyWait = TryAcquireMutex; Mutex->Header.SignalState = Owned ? 1 : 0; Mutex->Owner = Owned ? UtSelf() : NULL; }
BOOL UtMsgRcv (PVOID Data, PDWORD Size, PHANDLE Sender){ PLIST_ENTRY List , AUXQMSG , Entry; /*PUTHREAD */RunningThread = (PUTHREAD)UtSelf(); if(RunningThread->nMessages == 0){ Entry = QMSG.Flink; AUXQMSG = QMSG.Flink; do{ PBUFMSG m = CONTAINING_RECORD(AUXQMSG , BUFMSG , Link); if(m->Thread == (HANDLE)RunningThread){ RemoveHeadList(&QMSG); Data = m->Buffer; SaveValue = (DWORD)m->Buffer; Size = m->nSize; InterlockedDecrement(&nMsg); if(Sender != NULL) ClientId = m->Thread; return TRUE; } AUXQMSG = AUXQMSG->Flink; }while(AUXQMSG != Entry->Blink); ((PUTHREAD)UtSelf())->isWaiting = 1; EventWait(&RunningThread->ev); } RunningThread = (PUTHREAD)UtSelf(); if(RunningThread->nMessages == 0){ Entry = QMSG.Flink; AUXQMSG = QMSG.Flink; do{ PBUFMSG m = CONTAINING_RECORD(AUXQMSG , BUFMSG , Link); if(m->Thread == (HANDLE)RunningThread){ RemoveHeadList(&QMSG); Data = m->Buffer; SaveValue = (DWORD)m->Buffer; Size = m->nSize; InterlockedDecrement(&nMsg); if(Sender != NULL) ClientId = m->Thread; return TRUE; } AUXQMSG = AUXQMSG->Flink; }while(AUXQMSG != Entry->Blink); } List = RunningThread->MessageQueue.Flink; PMessage msg = (PMessage)CONTAINING_RECORD(List , Message, Link); if(msg->SizeBuffer > Size){ Size = msg->SizeBuffer; return FALSE; } InterlockedDecrement(&RunningThread->nMessages); RemoveHeadList(&RunningThread->MessageQueue); Data = msg->Buffer; SaveValue = (DWORD)msg->Buffer; Size = msg->SizeBuffer; if(Sender != NULL) Sender = (PHANDLE)RunningThread; return TRUE; }