1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include <memory.h>
40 #include <portability/toku_portability.h>
41 
test_main(int argc,const char * argv[])42 #include "txn/rollback_log_node_cache.h"
43 
44 toku_instr_key* rollback_log_node_cache_mutex_key;
45 
46 void rollback_log_node_cache::init(uint32_t max_num_avail_nodes) {
47     XMALLOC_N(max_num_avail_nodes, m_avail_blocknums);
48     m_max_num_avail = max_num_avail_nodes;
49     m_first = 0;
50     m_num_avail = 0;
51     toku_pthread_mutexattr_t attr;
52     toku_mutexattr_init(&attr);
53     toku_mutexattr_settype(&attr, TOKU_MUTEX_ADAPTIVE);
54     toku_mutex_init(*rollback_log_node_cache_mutex_key, &m_mutex, &attr);
55     toku_mutexattr_destroy(&attr);
56 }
57 
58 void rollback_log_node_cache::destroy() {
59     toku_mutex_destroy(&m_mutex);
60     toku_free(m_avail_blocknums);
61 }
62 
63 // returns true if rollback log node was successfully added,
64 // false otherwise
65 bool rollback_log_node_cache::give_rollback_log_node(TOKUTXN txn, ROLLBACK_LOG_NODE log){
66     bool retval = false;
67     toku_mutex_lock(&m_mutex);
68     if (m_num_avail < m_max_num_avail) {
69         retval = true;
70         uint32_t index = m_first + m_num_avail;
71         if (index >= m_max_num_avail) {
72             index -= m_max_num_avail;
73         }
74         m_avail_blocknums[index].b = log->blocknum.b;
75         m_num_avail++;
76     }
77     toku_mutex_unlock(&m_mutex);
78     //
79     // now unpin the rollback log node
80     //
81     if (retval) {
82         make_rollback_log_empty(log);
83         toku_rollback_log_unpin(txn, log);
84     }
85     return retval;
86 }
87 
88 // if a rollback log node is available, will set log to it,
89 // otherwise, will set log to NULL and caller is on his own
90 // for getting a rollback log node
91 void rollback_log_node_cache::get_rollback_log_node(TOKUTXN txn, ROLLBACK_LOG_NODE* log){
92     BLOCKNUM b = ROLLBACK_NONE;
93     toku_mutex_lock(&m_mutex);
94     if (m_num_avail > 0) {
95         b.b = m_avail_blocknums[m_first].b;
96         m_num_avail--;
97         if (++m_first >= m_max_num_avail) {
98             m_first = 0;
99         }
100     }
101     toku_mutex_unlock(&m_mutex);
102     if (b.b != ROLLBACK_NONE.b) {
103         toku_get_and_pin_rollback_log(txn, b, log);
104         invariant(rollback_log_is_unused(*log));
105     } else {
106         *log = NULL;
107     }
108 }
109 
110