CUBRID Engine  latest
lockfree_transaction_system.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 // lockfree_transaction_system.hpp - lock-free transaction index management
21 //
22 // The lock-free transaction system solves problems posed by dynamic memory management of nodes in lock-free data
23 // structures, like the ABA problem of compare-and-swap primitive. The solution defers node reclamation as long as
24 // concurrent reads are possible.
25 // https://en.wikipedia.org/w/index.php?title=ABA_problem&action=edit&section=5
26 //
27 // The basic principle of the system is to mark lock-free data structure access and retired nodes with transaction
28 // id's. Each retirement marks retired node and increments the global transaction ID. Each node access (read or
29 // write) fetches current global id. When all current transactions are either inactive or their ids are greater
30 // than retired node id's, it can be deduced that no concurrent transaction is accessing the retired node, thus
31 // making it safe to reclaim.
32 //
33 // The system has two key components:
34 // 1. transaction indexes, one for each different thread that may use a lock-free structure in this system
35 // 2. transaction tables, one for each lock-free structure part of this system. a table is an array of
36 // transaction descriptors (the database transactional terminology was borrowed for familiarity).
37 //
38 // The system dictates to transaction tables the number of descriptors they need and assigns thread the index of its
39 // descriptor in each table (present or future).
40 //
41 // Each descriptor monitors the activity of a thread on a lock-free structure, by saving the global transaction
42 // id while the thread accesses the structure. Additionally, it maintains a list of the nodes this thread retires
43 // until it is safe to reclaim them
44 //
45 // Glossary explained [term will be met throught lockfree::tran implementation]
46 //
47 // - node:
48 // lock-free data structure element that retired & reclaimed
49 // - retire:
50 // the action of removing a node from lock-free data structure; no new access from concurrent threads is
51 // expected on a retired node
52 // - reclaim:
53 // the action of safe reclamation of node resources when no access from concurrent threads is possible
54 // - transaction system:
55 // group of transaction indexes and table; an index is valid throughout all tables in same system
56 // - transaction table:
57 // the set of transaction descriptors and global transaction ID equivalent for a lock-free data structure
58 // - transaction descriptor
59 // an entry of a transaction table that can be accessed by one thread only
60 //
61 
62 #ifndef _LOCKFREE_TRANSACTION_SYSTEM_HPP_
63 #define _LOCKFREE_TRANSACTION_SYSTEM_HPP_
64 
65 #include "lockfree_bitmap.hpp"
67 
68 #include <limits>
69 
70 namespace lockfree
71 {
72  namespace tran
73  {
75 
76  class system
77  {
78  public:
79  system () = delete;
80  system (size_t max_tran_count);
81  ~system () = default;
82 
84  void free_index (index idx);
85  size_t get_max_transaction_count () const;
86 
87  private:
90  };
91  }
92 } // namespace lockfree
93 
94 #endif // _LOCKFREE_TRANSACTION_SYSTEM_HPP_
static const index INVALID_INDEX
#define max(a, b)