1 /*****************************************************************************
2 
3 Copyright (c) 1997, 2019, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 
27 /** @file include/read0read.h
28  Cursor read
29 
30  Created 2/16/1997 Heikki Tuuri
31  *******************************************************/
32 
33 #ifndef read0read_h
34 #define read0read_h
35 
36 #include <stddef.h>
37 #include <algorithm>
38 
39 #include "read0types.h"
40 #include "univ.i"
41 
42 /** The MVCC read view manager */
43 class MVCC {
44  public:
45   /** Constructor
46   @param size		Number of views to pre-allocate */
47   explicit MVCC(ulint size);
48 
49   /** Destructor.
50   Free all the views in the m_free list */
51   ~MVCC();
52 
53   /**
54   Allocate and create a view.
55   @param view		view owned by this class created for the
56                           caller. Must be freed by calling close()
57   @param trx		transaction creating the view */
58   void view_open(ReadView *&view, trx_t *trx);
59 
60   /**
61   Close a view created by the above function.
62   @param view		view allocated by trx_open.
63   @param own_mutex	true if caller owns trx_sys_t::mutex */
64   void view_close(ReadView *&view, bool own_mutex);
65 
66   /**
67   Release a view that is inactive but not closed. Caller must own
68   the trx_sys_t::mutex.
69   @param view		View to release */
70   void view_release(ReadView *&view);
71 
72   /** Clones the oldest view and stores it in view. No need to
73   call view_close(). The caller owns the view that is passed in.
74   It will also move the closed views from the m_views list to the
75   m_free list. This function is called by Purge to create it view.
76   @param view		Preallocated view, owned by the caller */
77   void clone_oldest_view(ReadView *view);
78 
79   /**
80   @return the number of active views */
81   ulint size() const;
82 
83   /**
84   @return true if the view is active and valid */
is_view_active(ReadView * view)85   static bool is_view_active(ReadView *view) {
86     ut_a(view != reinterpret_cast<ReadView *>(0x1));
87 
88     return (view != nullptr && !(intptr_t(view) & 0x1));
89   }
90 
91   /**
92   Set the view creator transaction id. Note: This shouldbe set only
93   for views created by RW transactions. */
94   static void set_view_creator_trx_id(ReadView *view, trx_id_t id);
95 
96  private:
97   /**
98   Validates a read view list. */
99   bool validate() const;
100 
101   /**
102   Find a free view from the active list, if none found then allocate
103   a new view. This function will also attempt to move delete marked
104   views from the active list to the freed list.
105   @return a view to use */
106   inline ReadView *get_view();
107 
108   /**
109   Get the oldest view in the system. It will also move the delete
110   marked read views from the views list to the freed list.
111   @return oldest view if found or NULL */
112   inline ReadView *get_oldest_view() const;
113   ReadView *get_view_created_by_trx_id(trx_id_t trx_id) const;
114 
115  private:
116   // Prevent copying
117   MVCC(const MVCC &);
118   MVCC &operator=(const MVCC &);
119 
120  private:
121   typedef UT_LIST_BASE_NODE_T(ReadView) view_list_t;
122 
123   /** Free views ready for reuse. */
124   view_list_t m_free;
125 
126   /** Active and closed views, the closed views will have the
127   creator trx id set to TRX_ID_MAX */
128   view_list_t m_views;
129 };
130 
131 #endif /* read0read_h */
132