VOID Test3_FirstThread ( UT_ARGUMENT Argument ) { MAILBOX Mailbox; UNREFERENCED_PARAMETER(Argument); Mailbox_Initialize(&Mailbox); Test3_CountProducers = 0; Test3_CountConsumers = 0; UtCreate(Test3_ConsumerThread, &Mailbox); UtCreate(Test3_ConsumerThread, &Mailbox); UtCreate(Test3_ProducerThread, &Mailbox); UtCreate(Test3_ProducerThread, &Mailbox); UtCreate(Test3_ProducerThread, &Mailbox); UtCreate(Test3_ProducerThread, &Mailbox); do { UtYield(); } while (Test3_CountProducers != 4); Mailbox_Post(&Mailbox, TERMINATOR); Mailbox_Post(&Mailbox, TERMINATOR); do { UtYield(); } while (Test3_CountConsumers != 2); Mailbox_Close(&Mailbox); }
VOID Test3_ProducerThread ( __in UT_ARGUMENT Argument ) { static ULONG CurrentId = 0; ULONG ProducerId; PMAILBOX Mailbox; PCHAR Message; ULONG MessageNumber; Mailbox = (PMAILBOX) Argument; ProducerId = ++CurrentId; for (MessageNumber = 0; MessageNumber < 5000; ++MessageNumber) { Message = (PCHAR) malloc(64); sprintf_s(Message, 64, "Message %04d from producer %d", MessageNumber, ProducerId); printf(" ** producer %d: sending message %04d [0x%08x]\n", ProducerId, MessageNumber, Message); Mailbox_Post(Mailbox, Message); if ((rand() % 2) == 0) { UtYield(); } //Sleep(1000); } ++Test3_CountProducers; }
VOID Test2_Thread3 ( UT_ARGUMENT Argument ) { PUTHREAD_MUTEX Mutex; WAIT_REQUEST WaitRequest; Mutex = (PUTHREAD_MUTEX) Argument; WaitRequest.SyncObject = &Mutex->Header; printf("Thread3 running\n"); printf("Thread3 acquiring the mutex...\n"); UtWaitForSyncObject(&WaitRequest); printf("Thread3 acquired the mutex...\n"); UtYield(); printf("Thread3 releasing the mutex...\n"); UtReleaseMutex(Mutex); printf("Thread3 released the mutex...\n"); printf("Thread3 exiting\n"); ++Test2_Count; }
VOID Test1_Thread (UT_ARGUMENT Argument) { UCHAR Char; ULONG Index; Char = (UCHAR) Argument; for (Index = 0; Index < 10000; ++Index) { putchar(Char); if ((rand() % 4) == 0) { UtYield(); } } ++Test1_Count; }
static PVOID Mailbox_Wait ( __inout PMAILBOX Mailbox ) { PVOID Data; PMAILBOX_MESSAGE Message; SEMAPHORE_WAIT_REQUEST SemaphoreRequest; // // Wait for a message to be available in the mailbox. // SemaphoreRequest.Header.SyncObject = &Mailbox->Semaphore.Header; SemaphoreRequest.Permits = 1; UtWaitForSyncObject(&SemaphoreRequest.Header); // // Get the envelope from the mailbox queue. // SemaphoreRequest.Header.SyncObject = &Mailbox->Lock.Header; UtWaitForSyncObject(&SemaphoreRequest.Header); UtYield(); Message = CONTAINING_RECORD(RemoveHeadList(&Mailbox->MessageQueue), MAILBOX_MESSAGE, QueueEntry); _ASSERTE(Message != NULL); //printf("** dequeued: 0x%08x **\n", Message); UtReleaseMutex(&Mailbox->Lock); // // Extract the message from the envelope. // Data = Message->Data; // // Destroy the envelope and return the message. // free(Message); return Data; }
VOID Test1_Thread(UT_ARGUMENT Argument) { UCHAR Char; ULONG Index; Char = (UCHAR)Argument; for (Index = 0; Index < 7; ++Index) { putchar(Char); printf("Print Threads State Before UtYield() \n"); PrintUtThreadAlive(handleArray); if ((rand() % 4) == 0) { UtYield(); printf("Print Threads State After UtYield() \n"); PrintUtThreadAlive(handleArray); } } ++Test1_Count; }
static VOID Mailbox_Post ( __inout PMAILBOX Mailbox, __in PVOID Data ) { PMAILBOX_MESSAGE Message; WAIT_REQUEST WaitRequest; // // Create an envelope. // Message = (PMAILBOX_MESSAGE) malloc(sizeof *Message); _ASSERTE(Message != NULL); Message->Data = Data;; // // Insert the message in the mailbox queue. // WaitRequest.SyncObject = &Mailbox->Lock.Header; UtWaitForSyncObject(&WaitRequest); UtYield(); InsertTailList(&Mailbox->MessageQueue, &Message->QueueEntry); //printf("** enqueued: 0x%08x **\n", Message); UtReleaseMutex(&Mailbox->Lock); // // Add one permit to indicate the availability of one more message. // UtReleaseSemaphore(&Mailbox->Semaphore, 1); }
void auxFunctionThatYields(UT_ARGUMENT arg){ while (ex3FunctionsFinished<totalEx3Tests) { UtYield(); } }