/Users/deen/code/yugabyte-db/src/yb/util/capabilities.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/util/capabilities.h" |
15 | | |
16 | | |
17 | | #include "yb/gutil/spinlock.h" |
18 | | |
19 | | namespace yb { |
20 | | |
21 | | namespace { |
22 | | |
23 | | std::vector<CapabilityId>* capabilities_ = nullptr; |
24 | | |
25 | | class CapabilitiesShutdown { |
26 | | public: |
27 | 15.0k | ~CapabilitiesShutdown() { |
28 | 15.0k | delete capabilities_; |
29 | 15.0k | } |
30 | | }; |
31 | | |
32 | | CapabilitiesShutdown capabilities_shutdown_; |
33 | | |
34 | | // If capability non zero - add it to capabilities. |
35 | | // If out is not nullptr - copy all capabilities to it. |
36 | 208k | void CapabilityHelper(CapabilityId capability, std::vector<CapabilityId>* out) { |
37 | 208k | static base::SpinLock mutex(base::SpinLock::LINKER_INITIALIZED); |
38 | 208k | base::SpinLockHolder lock(&mutex); |
39 | 208k | if (!capabilities_) { |
40 | 15.8k | capabilities_ = new std::vector<CapabilityId>(); |
41 | 15.8k | } |
42 | 208k | if (capability) { |
43 | 42.9k | capabilities_->push_back(capability); |
44 | 42.9k | } |
45 | 208k | if (out) { |
46 | 165k | *out = *capabilities_; |
47 | 165k | } |
48 | 208k | } |
49 | | |
50 | | } // namespace |
51 | | |
52 | 42.9k | CapabilityRegisterer::CapabilityRegisterer(CapabilityId capability) { |
53 | 42.9k | CapabilityHelper(capability, nullptr /* out */); |
54 | 42.9k | } |
55 | | |
56 | 165k | std::vector<CapabilityId> Capabilities() { |
57 | 165k | std::vector<CapabilityId> result; |
58 | 165k | CapabilityHelper(0 /* capability */, &result); |
59 | 165k | return result; |
60 | 165k | } |
61 | | |
62 | | } // namespace yb |