forked from p4lang/behavioral-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphv_source.cpp
118 lines (97 loc) · 2.87 KB
/
phv_source.cpp
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
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Antonin Bas ([email protected])
*
*/
#include <bm/bm_sim/phv_source.h>
#include <bm/bm_sim/phv.h>
#include <vector>
#include <mutex>
#include <iostream>
namespace bm {
class PHVSourceContextPools : public PHVSourceIface {
public:
explicit PHVSourceContextPools(size_t size)
: phv_pools(size) { }
private:
class PHVPool {
public:
void set_phv_factory(const PHVFactory *factory) {
std::unique_lock<std::mutex> lock(mutex);
assert(count == 0);
phv_factory = factory;
phvs.clear();
}
std::unique_ptr<PHV> get() {
std::unique_lock<std::mutex> lock(mutex);
count++;
if (phvs.size() == 0) {
lock.unlock();
return phv_factory->create();
}
std::unique_ptr<PHV> phv = std::move(phvs.back());
phvs.pop_back();
return phv;
}
void release(std::unique_ptr<PHV> phv) {
std::unique_lock<std::mutex> lock(mutex);
count--;
phvs.push_back(std::move(phv));
}
size_t phvs_in_use() {
std::unique_lock<std::mutex> lock(mutex);
return count;
}
private:
mutable std::mutex mutex{};
std::vector<std::unique_ptr<PHV> > phvs{};
const PHVFactory *phv_factory{nullptr};
size_t count{0};
};
std::unique_ptr<PHV> get_(cxt_id_t cxt) override {
return phv_pools.at(cxt).get();
}
void release_(cxt_id_t cxt, std::unique_ptr<PHV> phv) override {
return phv_pools.at(cxt).release(std::move(phv));
}
void set_phv_factory_(cxt_id_t cxt, const PHVFactory *factory) override {
phv_pools.at(cxt).set_phv_factory(factory);
}
size_t phvs_in_use_(cxt_id_t cxt) override {
return phv_pools.at(cxt).phvs_in_use();
}
std::vector<PHVPool> phv_pools;
};
std::unique_ptr<PHV>
PHVSourceIface::get(cxt_id_t cxt) { return get_(cxt); }
void
PHVSourceIface::release(cxt_id_t cxt, std::unique_ptr<PHV> phv) {
release_(cxt, std::move(phv));
}
void
PHVSourceIface::set_phv_factory(cxt_id_t cxt, const PHVFactory *factory) {
set_phv_factory_(cxt, factory);
}
size_t
PHVSourceIface::phvs_in_use(cxt_id_t cxt) {
return phvs_in_use_(cxt);
}
std::unique_ptr<PHVSourceIface>
PHVSourceIface::make_phv_source(size_t size) {
return std::unique_ptr<PHVSourceContextPools>(
new PHVSourceContextPools(size));
}
} // namespace bm