1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Haichao.chc |
17 | * \date Oct 2020 |
18 | * \brief Reserving context of vector inserting/searching |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <condition_variable> |
24 | #include <memory> |
25 | #include <queue> |
26 | #include "common/macro_define.h" |
27 | #include "../typedef.h" |
28 | |
29 | namespace proxima { |
30 | namespace be { |
31 | namespace index { |
32 | |
33 | /* |
34 | * Storage of proxima search context |
35 | */ |
36 | class ContextPool { |
37 | public: |
38 | PROXIMA_DISALLOW_COPY_AND_ASSIGN(ContextPool); |
39 | |
40 | //! Constructor |
41 | ContextPool() = default; |
42 | |
43 | //! Destructor |
44 | ~ContextPool() = default; |
45 | |
46 | //! Emplace a context from pool |
47 | void emplace(IndexContextPtr ctx); |
48 | |
49 | //! Acauire a context from pool |
50 | IndexContextPtr acquire(); |
51 | |
52 | //! Return a context to pool |
53 | void release(IndexContextPtr ctx); |
54 | |
55 | //! Clear context pool |
56 | void clear(); |
57 | |
58 | private: |
59 | std::queue<IndexContextPtr> contexts_{}; |
60 | std::mutex mutex_{}; |
61 | std::condition_variable_any not_empty_cond_{}; |
62 | }; |
63 | |
64 | |
65 | } // end namespace index |
66 | } // namespace be |
67 | } // end namespace proxima |
68 | |