summaryrefslogtreecommitdiffstats
path: root/webkit/glue/worker_task_runner.cc
blob: 476a7fec09f98b8395ce69ab5120dd92c4d04609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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 "base/callback.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "webkit/glue/worker_task_runner.h"

using WebKit::WebWorkerRunLoop;

namespace {

class RunClosureTask : public WebWorkerRunLoop::Task {
 public:
  RunClosureTask(const base::Closure& task) : task_(task) {}
  virtual ~RunClosureTask() {}
  virtual void Run() {
    task_.Run();
  }
 private:
  base::Closure task_;
};

} // unnamed namespace

namespace webkit_glue {

struct WorkerTaskRunner::ThreadLocalState {
  ThreadLocalState(int id, const WebWorkerRunLoop& loop)
      : id_(id), run_loop_(loop) {
  }
  int id_;
  WebWorkerRunLoop run_loop_;
  ObserverList<WorkerTaskRunner::Observer> stop_observers_;
};

WorkerTaskRunner::WorkerTaskRunner() {
  // Start worker ids at 1, 0 is reserved for the main thread.
  int id = id_sequence_.GetNext();
  DCHECK(!id);
}

bool WorkerTaskRunner::PostTask(
    int id, const base::Closure& closure) {
  DCHECK(id > 0);
  base::AutoLock locker(loop_map_lock_);
  IDToLoopMap::iterator found = loop_map_.find(id);
  if (found != loop_map_.end())
    found->second.postTask(new RunClosureTask(closure));
  return found != loop_map_.end();
}

int WorkerTaskRunner::CurrentWorkerId() {
  if (!current_tls_.Get())
    return 0;
  return current_tls_.Get()->id_;
}

WorkerTaskRunner* WorkerTaskRunner::Instance() {
  static base::LazyInstance<WorkerTaskRunner>::Leaky
      worker_task_runner = LAZY_INSTANCE_INITIALIZER;
  return worker_task_runner.Pointer();
}

void WorkerTaskRunner::AddStopObserver(Observer* obs) {
  DCHECK(CurrentWorkerId() > 0);
  current_tls_.Get()->stop_observers_.AddObserver(obs);
}

void WorkerTaskRunner::RemoveStopObserver(Observer* obs) {
  DCHECK(CurrentWorkerId() > 0);
  current_tls_.Get()->stop_observers_.RemoveObserver(obs);
}

WorkerTaskRunner::~WorkerTaskRunner() {
}

void WorkerTaskRunner::OnWorkerRunLoopStarted(const WebWorkerRunLoop& loop) {
  DCHECK(!current_tls_.Get());
  int id = id_sequence_.GetNext();
  current_tls_.Set(new ThreadLocalState(id, loop));

  base::AutoLock locker_(loop_map_lock_);
  loop_map_[id] = loop;
}

void WorkerTaskRunner::OnWorkerRunLoopStopped(const WebWorkerRunLoop& loop) {
  DCHECK(current_tls_.Get());
  FOR_EACH_OBSERVER(Observer, current_tls_.Get()->stop_observers_,
                    OnWorkerRunLoopStopped());
  {
    base::AutoLock locker(loop_map_lock_);
    DCHECK(loop_map_[CurrentWorkerId()] == loop);
    loop_map_.erase(CurrentWorkerId());
  }
  delete current_tls_.Get();
  current_tls_.Set(NULL);
}

}  // namespace webkit_glue