1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_socket_thread_win.h"
7 #include "base/lazy_instance.h"
8 #include "base/sequenced_task_runner.h"
9 #include "base/threading/thread.h"
13 base::LazyInstance
<scoped_refptr
<BluetoothSocketThreadWin
> > g_instance
=
14 LAZY_INSTANCE_INITIALIZER
;
17 scoped_refptr
<BluetoothSocketThreadWin
> BluetoothSocketThreadWin::Get() {
18 if (!g_instance
.Get().get()) {
19 g_instance
.Get() = new BluetoothSocketThreadWin();
21 return g_instance
.Get();
24 BluetoothSocketThreadWin::BluetoothSocketThreadWin()
25 : active_socket_count_(0) {}
27 BluetoothSocketThreadWin::~BluetoothSocketThreadWin() {}
29 void BluetoothSocketThreadWin::OnSocketActivate() {
30 DCHECK(thread_checker_
.CalledOnValidThread());
31 active_socket_count_
++;
35 void BluetoothSocketThreadWin::OnSocketDeactivate() {
36 DCHECK(thread_checker_
.CalledOnValidThread());
37 active_socket_count_
--;
38 if (active_socket_count_
== 0 && thread_
) {
45 void BluetoothSocketThreadWin::EnsureStarted() {
46 DCHECK(thread_checker_
.CalledOnValidThread());
50 base::Thread::Options thread_options
;
51 thread_options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
52 thread_
.reset(new base::Thread("BluetoothSocketThreadWin"));
53 thread_
->StartWithOptions(thread_options
);
54 task_runner_
= thread_
->message_loop_proxy();
57 scoped_refptr
<base::SequencedTaskRunner
> BluetoothSocketThreadWin::task_runner()
59 DCHECK(active_socket_count_
> 0);