<commit_before>//
// PROJECT:         Aspia Remote Desktop
// FILE:            base/message_loop/message_pump_ui.cc
// LICENSE:         See top-level directory
// PROGRAMMERS:     <NAME> (<EMAIL>)
//

#include "base/message_loop/message_pump_ui.h"
#include "base/logging.h"

namespace aspia {

// Message sent to get an additional time slice for pumping (processing) another
// task (a series of such messages creates a continuous task pump).
static const UINT kMsgHaveWork = WM_USER + 1;

MessagePumpForUI::MessagePumpForUI()
{
    bool succeeded = message_window_.Create(std::bind(&MessagePumpForUI::OnMessage,
                                                      this,
                                                      std::placeholders::_1,
                                                      std::placeholders::_2,
                                                      std::placeholders::_3,
                                                      std::placeholders::_4));
    DCHECK(succeeded);
}

void MessagePumpForUI::ScheduleWork()
{
    if (InterlockedExchange(&work_state_, HAVE_WORK) != READY)
        return; // Someone else continued the pumping.

    // Make sure the MessagePump does some work for us.
    BOOL ret = PostMessageW(message_window_.hwnd(), kMsgHaveWork, 0, 0);
    if (ret)
        return; // There was room in the Window Message queue.

    // We have failed to insert a have-work message, so there is a chance that we
    // will starve tasks/timers while sitting in a nested message loop.  Nested
    // loops only look at Windows Message queues, and don't look at *our* task
    // queues, etc., so we might not get a time slice in such. :-(
    // We could abort here, but the fear is that this failure mode is plausibly
    // common (queue is full, of about 2000 messages), so we'll do a near-graceful
    // recovery.  Nested loops are pretty transient (we think), so this will
    // probably be recoverable.
    InterlockedExchange(&work_state_, READY); // Clarify that we didn't really insert.
}

bool MessagePumpForUI::OnMessage(UINT message, WPARAM wparam, LPARAM lparam, LRESULT* result)
{
    switch (message)
    {
        case kMsgHaveWork:
            HandleWorkMessage();
            break;
    }

    return false;
}

void MessagePumpForUI::DoRunLoop()
{
    // IF this was just a simple PeekMessage() loop (servicing all possible work
    // queues), then Windows would try to achieve the following order according
    // to MSDN documentation about PeekMessage with no filter):
    //    * Sent messages
    //    * Posted messages
    //    * Sent messages (again)
    //    * WM_PAINT messages
    //    * WM_TIMER messages
    //
    // Summary: none of the above classes is starved, and sent messages has twice
    // the chance of being processed (i.e., reduced service time).<commit_msg>- Fixed C4100 warning (unreferenced formal parameter)<commit_after>//
// PROJECT:         Aspia Remote Desktop
// FILE:            base/message_loop/message_pump_ui.cc
// LICENSE:         See top-level directory
// PROGRAMMERS:     <NAME> (<EMAIL>)
//

#include "base/message_loop/message_pump_ui.h"
#include "base/logging.h"

namespace aspia {

// Message sent to get an additional time slice for pumping (processing) another
// task (a series of such messages creates a continuous task pump).
static const UINT kMsgHaveWork = WM_USER + 1;

MessagePumpForUI::MessagePumpForUI()
{
    bool succeeded = message_window_.Create(std::bind(&MessagePumpForUI::OnMessage,
                                                      this,
                                                      std::placeholders::_1,
                                                      std::placeholders::_2,
                                                      std::placeholders::_3,
                                                      std::placeholders::_4));
    DCHECK(succeeded);
}

void MessagePumpForUI::ScheduleWork()
{
    if (InterlockedExchange(&work_state_, HAVE_WORK) != READY)
        return; // Someone else continued the pumping.

    // Make sure the MessagePump does some work for us.
    BOOL ret = PostMessageW(message_window_.hwnd(), kMsgHaveWork, 0, 0);
    if (ret)
        return; // There was room in the Window Message queue.

    // We have failed to insert a have-work message, so there is a chance that we
    // will starve tasks/timers while sitting in a nested message loop.  Nested
    // loops only look at Windows Message queues, and don't look at *our* task
    // queues, etc., so we might not get a time slice in such. :-(
    // We could abort here, but the fear is that this failure mode is plausibly
    // common (queue is full, of about 2000 messages), so we'll do a near-graceful
    // recovery.  Nested loops are pretty transient (we think), so this will
    // probably be recoverable.
    InterlockedExchange(&work_state_, READY); // Clarify that we didn't really insert.
}

bool MessagePumpForUI::OnMessage(UINT message, WPARAM wparam, LPARAM lparam, LRESULT* result)
{
    UNREF(wparam);
    UNREF(lparam);
    UNREF(result);

    switch (message)
    {
        case kMsgHaveWork:
            HandleWorkMessage();
            break;
    }

    return false;
}

void MessagePumpForUI::DoRunLoop()
{
    // IF this was just a simple PeekMessage() loop (servicing all possible work
    // queues), then Windows would try to achieve the following order according
    // to MSDN documentation about PeekMessage with no filter):
    //    * Sent messages
    //    * Posted messages
    //    * Sent messages (again)
    //    * WM_PAINT messages
    //    * WM_TIMER messages
    //
    // Summary: none of the above classes is starved, and sent messages has twice
    // the chance of being processed (i.e., reduced service time).
{'fraction_non_alphanumeric': 0.08931034482758621, 'fraction_numerical': 0.004482758620689655, 'mean_word_length': 2.1255387931034484, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6702028', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1513, 'n_words': 568}