blob: e41817d967c3e1692208b3fe04b6f733a38b5c50 (
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Copyright 2014 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 "mojo/edk/system/handle_table.h"
#include <stdint.h>
#include <limits>
namespace mojo {
namespace edk {
HandleTable::HandleTable() {}
HandleTable::~HandleTable() {}
MojoHandle HandleTable::AddDispatcher(scoped_refptr<Dispatcher> dispatcher) {
// Oops, we're out of handles.
if (next_available_handle_ == MOJO_HANDLE_INVALID)
return MOJO_HANDLE_INVALID;
MojoHandle handle = next_available_handle_++;
auto result = handles_.insert(std::make_pair(handle, Entry(dispatcher)));
DCHECK(result.second);
return handle;
}
bool HandleTable::AddDispatchersFromTransit(
const std::vector<Dispatcher::DispatcherInTransit>& dispatchers,
MojoHandle* handles) {
// Oops, we're out of handles.
if (next_available_handle_ == MOJO_HANDLE_INVALID)
return false;
DCHECK_LE(dispatchers.size(), std::numeric_limits<uint32_t>::max());
// If this insertion would cause handle overflow, we're out of handles.
if (next_available_handle_ + dispatchers.size() < next_available_handle_)
return false;
for (size_t i = 0; i < dispatchers.size(); ++i) {
MojoHandle handle = next_available_handle_++;
auto result = handles_.insert(
std::make_pair(handle, Entry(dispatchers[i].dispatcher)));
DCHECK(result.second);
handles[i] = handle;
}
return true;
}
scoped_refptr<Dispatcher> HandleTable::GetDispatcher(MojoHandle handle) const {
auto it = handles_.find(handle);
if (it == handles_.end())
return nullptr;
return it->second.dispatcher;
}
MojoResult HandleTable::GetAndRemoveDispatcher(
MojoHandle handle,
scoped_refptr<Dispatcher>* dispatcher) {
auto it = handles_.find(handle);
if (it == handles_.end())
return MOJO_RESULT_INVALID_ARGUMENT;
if (it->second.busy)
return MOJO_RESULT_BUSY;
*dispatcher = it->second.dispatcher;
handles_.erase(it);
return MOJO_RESULT_OK;
}
MojoResult HandleTable::BeginTransit(
const MojoHandle* handles,
uint32_t num_handles,
std::vector<Dispatcher::DispatcherInTransit>* dispatchers) {
dispatchers->clear();
dispatchers->reserve(num_handles);
for (size_t i = 0; i < num_handles; ++i) {
auto it = handles_.find(handles[i]);
if (it == handles_.end())
return MOJO_RESULT_INVALID_ARGUMENT;
if (it->second.busy)
return MOJO_RESULT_BUSY;
Dispatcher::DispatcherInTransit d;
d.local_handle = handles[i];
d.dispatcher = it->second.dispatcher;
if (!d.dispatcher->BeginTransit())
return MOJO_RESULT_BUSY;
it->second.busy = true;
dispatchers->push_back(d);
}
return MOJO_RESULT_OK;
}
void HandleTable::CompleteTransitAndClose(
const std::vector<Dispatcher::DispatcherInTransit>& dispatchers) {
for (const auto& dispatcher : dispatchers) {
auto it = handles_.find(dispatcher.local_handle);
DCHECK(it != handles_.end() && it->second.busy);
handles_.erase(it);
dispatcher.dispatcher->CompleteTransitAndClose();
}
}
void HandleTable::CancelTransit(
const std::vector<Dispatcher::DispatcherInTransit>& dispatchers) {
for (const auto& dispatcher : dispatchers) {
auto it = handles_.find(dispatcher.local_handle);
DCHECK(it != handles_.end() && it->second.busy);
it->second.busy = false;
dispatcher.dispatcher->CancelTransit();
}
}
void HandleTable::GetActiveHandlesForTest(std::vector<MojoHandle>* handles) {
handles->clear();
for (const auto& entry : handles_)
handles->push_back(entry.first);
}
HandleTable::Entry::Entry() {}
HandleTable::Entry::Entry(scoped_refptr<Dispatcher> dispatcher)
: dispatcher(dispatcher) {}
HandleTable::Entry::Entry(const Entry& other) = default;
HandleTable::Entry::~Entry() {}
} // namespace edk
} // namespace mojo
|