<commit_before>#include "message_proxy.h"

#include <glib.h>

#include "emacsinstance.h"

MessageProxy::MessageProxy(EmacsInstance* instance)
        : instance_(instance),
          ref_count_(1),
          lock_(g_mutex_new())
{
}

MessageProxy::~MessageProxy()
{
}

MessageProxy* MessageProxy::ref()
{
    g_atomic_int_inc(&ref_count_);
    return this;
}

void MessageProxy::unref()
{
    if (g_atomic_int_dec_and_test(&ref_count_))
        delete this;
}

void MessageProxy::postTask(Task* task)
{
    g_mutex_lock(lock_);
    // TODO: If we move the message queue into the proxy, the critical
    // section would shrink; it's only really needed for the NPN call.
    if (instance_) {
        instance_->postTask(task);
    }
    g_mutex_unlock(lock_);
}

void MessageProxy::invalidate()
{
    g_mutex_lock(lock_);
    instance_ = NULL;
    g_mutex_unlock(lock_);
}

EmacsInstance* MessageProxy::emacsInstance()
{
    return instance_;
}
<commit_msg>Don't leak tasks (and thus MessageProxy) when failed to post<commit_after>#include "message_proxy.h"

#include <glib.h>

#include "emacsinstance.h"
#include "task.h"

MessageProxy::MessageProxy(EmacsInstance* instance)
        : instance_(instance),
          ref_count_(1),
          lock_(g_mutex_new())
{
}

MessageProxy::~MessageProxy()
{
}

MessageProxy* MessageProxy::ref()
{
    g_atomic_int_inc(&ref_count_);
    return this;
}

void MessageProxy::unref()
{
    if (g_atomic_int_dec_and_test(&ref_count_))
        delete this;
}

void MessageProxy::postTask(Task* task)
{
    bool posted = false;
    g_mutex_lock(lock_);
    // TODO: If we move the message queue into the proxy, the critical
    // section would shrink; it's only really needed for the NPN call.
    if (instance_) {
        instance_->postTask(task);
        posted = true;
    }
    g_mutex_unlock(lock_);
    if (!posted)
        delete task;
}

void MessageProxy::invalidate()
{
    g_mutex_lock(lock_);
    instance_ = NULL;
    g_mutex_unlock(lock_);
}

EmacsInstance* MessageProxy::emacsInstance()
{
    return instance_;
}

{'fraction_non_alphanumeric': 0.12069801260300533, 'fraction_numerical': 0.0009694619486185168, 'mean_word_length': 3.6486486486486487, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5169203', 'n_tokens_mistral': 690, 'n_tokens_neox': 687, 'n_words': 148}