CUBRID Engine  latest
lockfree_bitmap.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * lock-free bitmap
21  */
22 
23 #ifndef _LOCKFREE_BITMAP_HPP_
24 #define _LOCKFREE_BITMAP_HPP_
25 
26 #include <atomic>
27 
28 namespace lockfree
29 {
30  // todo - refactoring
31  class bitmap
32  {
33  public:
34  static const float FULL_USAGE_RATIO;
36 
38  {
39  ONE_CHUNK = 0,
41  };
42 
43  bitmap ();
44  ~bitmap ();
45 
46  void init (chunking_style style, int entries_count, float usage_ratio);
47  void destroy ();
48 
49  int get_entry ();
50  void free_entry (int entry_idx);
51 
52  bool is_full () const;
53 
54  // todo: make private fields
55  /* bitfield for entries array */
56  std::atomic<unsigned int> *bitfield;
57 
58  /* capacity count */
60 
61  /* current used count */
62  std::atomic<int> entry_count_in_use;
63 
64  /* style */
66 
67  /* threshold for usage */
69 
70  /* the start chunk index for round-robin */
71  std::atomic<unsigned int> start_idx;
72  };
73 } // namespace lockfree
74 
76 
78 static const LF_BITMAP_STYLE LF_BITMAP_ONE_CHUNK = LF_BITMAP_STYLE::ONE_CHUNK;
79 static const LF_BITMAP_STYLE LF_BITMAP_LIST_OF_CHUNKS = LF_BITMAP_STYLE::LIST_OF_CHUNKS;
80 
81 // todo - replace macros
82 #define LF_BITMAP_FULL_USAGE_RATIO lockfree::bitmap::FULL_USAGE_RATIO
83 #define LF_BITMAP_95PERCENTILE_USAGE_RATIO lockfree::bitmap::NINTETYFIVE_PERCENTILE_USAGE_RATIO
84 
85 #define LF_BITFIELD_WORD_SIZE (int) (sizeof (unsigned int) * 8)
86 
87 #define LF_BITMAP_IS_FULL(bitmap) (bitmap)->is_full ()
88 
89 #define LF_BITMAP_COUNT_ALIGN(count) \
90  (((count) + (LF_BITFIELD_WORD_SIZE) - 1) & ~((LF_BITFIELD_WORD_SIZE) - 1))
91 
92 #endif // !_LOCKFREE_BITMAP_HPP_
static const float FULL_USAGE_RATIO
static const LF_BITMAP_STYLE LF_BITMAP_ONE_CHUNK
bool is_full() const
void free_entry(int entry_idx)
static const float NINTETYFIVE_PERCENTILE_USAGE_RATIO
chunking_style style
std::atomic< int > entry_count_in_use
static const LF_BITMAP_STYLE LF_BITMAP_LIST_OF_CHUNKS
std::atomic< unsigned int > * bitfield
std::atomic< unsigned int > start_idx
void init(chunking_style style, int entries_count, float usage_ratio)