// Copyright (c) Microsoft Corporation.// Licensed under the MIT license.#include "precomp.h"#include "WaitQueue.h"#include "WaitBlock.h"#include "..\host\globals.h"#include "..\host\utils.hpp"// Routine Description:// - Instantiates a new ConsoleWaitQueueConsoleWaitQueue::ConsoleWaitQueue() :_blocks(){}// Routine Description:// - Destructs a ConsoleWaitQueue// - This will notify any remaining waiting items that the associated process/object is dying.ConsoleWaitQueue::~ConsoleWaitQueue(){// Notify all blocks that the thread or object is dying when destroyed.NotifyWaiters(TRUE, WaitTerminationReason::ThreadDying);}// Routine Description:// - Establishes a wait (call me back later) for a particular message with a given callback routine and its parameter// Arguments:// - pWaitReplyMessage - The API message that we're deferring until data is available later.// - pWaiter - The context/callback information to restore and dispatch the call later.// Return Value:// - S_OK if enqueued appropriately and everything is alright. Or suitable HRESULT failure otherwise.[[nodiscard]]HRESULT ConsoleWaitQueue::s_CreateWait(_Inout_ CONSOLE_API_MSG* const pWaitReplyMessage,_In_ IWaitRoutine* const pWaiter){// Normally we'd have the Wait Queue handle the insertion of the block into the queue, but// the console does queues in a somewhat special way.//// Each block belongs in two queues:// 1. The process queue of the client that dispatched the request// 2. The object queue that the request will be serviced by// As such, when a wait occurs, it gets added to both queues.//// It will end up being serviced by one or the other queue, but when it is serviced, it must be// removed from both so it is not double processed.//// Therefore, I've inverted the queue management responsibility into the WaitBlock itself// and made it a friend to this WaitQueue class.return ConsoleWaitBlock::s_CreateWait(pWaitReplyMessage,pWaiter);}// Routine Description:// - Instructs this queue to attempt to callback waiting requests// Arguments:// - fNotifyAll - If true, we will notify all items in the queue. If false, we will only notify the first item.// Return Value:// - True if any block was successfully notified. False if no blocks were successful.bool ConsoleWaitQueue::NotifyWaiters(const bool fNotifyAll){return NotifyWaiters(fNotifyAll, WaitTerminationReason::NoReason);}// Routine Description:// - Instructs this queue to attempt to callback waiting requests and request termination with the given reason// Arguments:// - fNotifyAll - If true, we will notify all items in the queue. If false, we will only notify the first item.// - TerminationReason - A reason/message to pass to each waiter signaling it should terminate appropriately.// Return Value:// - True if any block was successfully notified. False if no blocks were successful.bool ConsoleWaitQueue::NotifyWaiters(const bool fNotifyAll,const WaitTerminationReason TerminationReason){bool fResult = false;auto it = _blocks.cbegin();while (!_blocks.empty() && it != _blocks.cend()){ConsoleWaitBlock* const WaitBlock = (*it);if (nullptr == WaitBlock){break;}auto const nextIt = std::next(it); // we have to capture next before it is potentially erasedif (_NotifyBlock(WaitBlock, TerminationReason)){fResult = true;}if (!fNotifyAll){break;}it = nextIt;}return fResult;}// Routine Description:// - A helper to delete successfully notified callbacks// Arguments:// - pWaitBlock - A block containing callback data// - TerminationReason - Optional reason to tell the callback to terminate. If 0, we're not requesting termination.// Return Value:// - True if callback successfully delivered data. False if callback still needs to wait longer.bool ConsoleWaitQueue::_NotifyBlock(_In_ ConsoleWaitBlock* pWaitBlock,_In_ WaitTerminationReason TerminationReason){// Attempt to notify block with the given reason.bool const fResult = pWaitBlock->Notify(TerminationReason);if (fResult){// If it was successful, delete it. (It will remove itself from appropriate queues.)delete pWaitBlock;}return fResult;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。