<commit_before>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "jingle/glue/thread_wrapper.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"
#include "base/threading/thread_local.h"

namespace jingle_glue {

struct JingleThreadWrapper::PendingSend {
  PendingSend(const talk_base::Message& message_value)
      : sending_thread(JingleThreadWrapper::current()),
        message(message_value),
        done_event(true, false) {
    DCHECK(sending_thread);
  }

  JingleThreadWrapper* sending_thread;
  talk_base::Message message;
  base::WaitableEvent done_event;
};

base::LazyInstance<base::ThreadLocalPointer<JingleThreadWrapper> >
    g_jingle_thread_wrapper = LAZY_INSTANCE_INITIALIZER;

// static
void JingleThreadWrapper::EnsureForCurrentThread() {
  if (JingleThreadWrapper::current() == NULL) {
    g_jingle_thread_wrapper.Get().Set(
        new JingleThreadWrapper(MessageLoop::current()));
  }

  DCHECK_EQ(talk_base::Thread::Current(), current());
}

// static
JingleThreadWrapper* JingleThreadWrapper::current() {
  return g_jingle_thread_wrapper.Get().Get();
}

JingleThreadWrapper::JingleThreadWrapper(MessageLoop* message_loop)
    : talk_base::Thread(NULL),
      message_loop_(message_loop),
      send_allowed_(false),
      last_task_id_(0),
      pending_send_event_(true, false) {
  DCHECK_EQ(message_loop_, MessageLoop::current());

  talk_base::ThreadManager::Instance()->UnwrapCurrentThread();
  talk_base::ThreadManager::Instance()->SetCurrentThread(this);
  talk_base::MessageQueueManager::Instance()->Add(this);
  message_loop_->AddDestructionObserver(this);

  WrapCurrent();
}

JingleThreadWrapper::~JingleThreadWrapper() {
}

void JingleThreadWrapper::WillDestroyCurrentMessageLoop() {
  DCHECK_EQ(talk_base::Thread::Current(), current());<commit_msg>Currently when talk_base::Thread exists, Clear(NULL) is called from its destructor. But JingleThreadWrapper never gets a chance to remove pending messages from its queue. <commit_after>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "jingle/glue/thread_wrapper.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"
#include "base/threading/thread_local.h"

namespace jingle_glue {

struct JingleThreadWrapper::PendingSend {
  PendingSend(const talk_base::Message& message_value)
      : sending_thread(JingleThreadWrapper::current()),
        message(message_value),
        done_event(true, false) {
    DCHECK(sending_thread);
  }

  JingleThreadWrapper* sending_thread;
  talk_base::Message message;
  base::WaitableEvent done_event;
};

base::LazyInstance<base::ThreadLocalPointer<JingleThreadWrapper> >
    g_jingle_thread_wrapper = LAZY_INSTANCE_INITIALIZER;

// static
void JingleThreadWrapper::EnsureForCurrentThread() {
  if (JingleThreadWrapper::current() == NULL) {
    g_jingle_thread_wrapper.Get().Set(
        new JingleThreadWrapper(MessageLoop::current()));
  }

  DCHECK_EQ(talk_base::Thread::Current(), current());
}

// static
JingleThreadWrapper* JingleThreadWrapper::current() {
  return g_jingle_thread_wrapper.Get().Get();
}

JingleThreadWrapper::JingleThreadWrapper(MessageLoop* message_loop)
    : talk_base::Thread(NULL),
      message_loop_(message_loop),
      send_allowed_(false),
      last_task_id_(0),
      pending_send_event_(true, false) {
  DCHECK_EQ(message_loop_, MessageLoop::current());

  talk_base::ThreadManager::Instance()->UnwrapCurrentThread();
  talk_base::ThreadManager::Instance()->SetCurrentThread(this);
  talk_base::MessageQueueManager::Instance()->Add(this);
  message_loop_->AddDestructionObserver(this);

  WrapCurrent();
}

JingleThreadWrapper::~JingleThreadWrapper() {
  Clear(NULL, talk_base::MQID_ANY, NULL);
}

void JingleThreadWrapper::WillDestroyCurrentMessageLoop() {
  DCHECK_EQ(talk_base::Thread::Current(), current());
{'fraction_non_alphanumeric': 0.12378167641325535, 'fraction_numerical': 0.0024366471734892786, 'mean_word_length': 6.189141856392294, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '5082896', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1257, 'n_words': 217}