You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.2 KiB

  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. COPYING CONDITIONS NOTICE:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation, and provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain this COPYING
  11. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  12. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  13. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  14. GRANT (below).
  15. * Redistributions in binary form must reproduce this COPYING
  16. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  17. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  18. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  19. GRANT (below) in the documentation and/or other materials
  20. provided with the distribution.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. COPYRIGHT NOTICE:
  26. TokuDB, Tokutek Fractal Tree Indexing Library.
  27. Copyright (C) 2007-2013 Tokutek, Inc.
  28. DISCLAIMER:
  29. This program is distributed in the hope that it will be useful, but
  30. WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. General Public License for more details.
  33. UNIVERSITY PATENT NOTICE:
  34. The technology is licensed by the Massachusetts Institute of
  35. Technology, Rutgers State University of New Jersey, and the Research
  36. Foundation of State University of New York at Stony Brook under
  37. United States of America Serial No. 11/760379 and to the patents
  38. and/or patent applications resulting from it.
  39. PATENT MARKING NOTICE:
  40. This software is covered by US Patent No. 8,185,551.
  41. PATENT RIGHTS GRANT:
  42. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  43. Tokutek as part of the Fractal Tree project.
  44. "PATENT CLAIMS" means the claims of patents that are owned or
  45. licensable by Tokutek, both currently or in the future; and that in
  46. the absence of this license would be infringed by THIS
  47. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  48. "PATENT CHALLENGE" shall mean a challenge to the validity,
  49. patentability, enforceability and/or non-infringement of any of the
  50. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  51. Tokutek hereby grants to you, for the term and geographical scope of
  52. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  53. irrevocable (except as stated in this section) patent license to
  54. make, have made, use, offer to sell, sell, import, transfer, and
  55. otherwise run, modify, and propagate the contents of THIS
  56. IMPLEMENTATION, where such license applies only to the PATENT
  57. CLAIMS. This grant does not include claims that would be infringed
  58. only as a consequence of further modifications of THIS
  59. IMPLEMENTATION. If you or your agent or licensee institute or order
  60. or agree to the institution of patent litigation against any entity
  61. (including a cross-claim or counterclaim in a lawsuit) alleging that
  62. THIS IMPLEMENTATION constitutes direct or contributory patent
  63. infringement, or inducement of patent infringement, then any rights
  64. granted to you under this License shall terminate as of the date
  65. such litigation is filed. If you or your agent or exclusive
  66. licensee institute or order or agree to the institution of a PATENT
  67. CHALLENGE, then Tokutek may terminate any rights granted to you
  68. under this License.
  69. */
  70. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  71. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  72. #include <memory.h>
  73. #include <toku_include/toku_portability.h>
  74. #include "rollback_log_node_cache.h"
  75. void rollback_log_node_cache::init (uint32_t max_num_avail_nodes) {
  76. XMALLOC_N(max_num_avail_nodes, m_avail_blocknums);
  77. XMALLOC_N(max_num_avail_nodes, m_hashes);
  78. m_max_num_avail = max_num_avail_nodes;
  79. m_first = 0;
  80. m_num_avail = 0;
  81. toku_pthread_mutexattr_t attr;
  82. toku_mutexattr_init(&attr);
  83. toku_mutexattr_settype(&attr, TOKU_MUTEX_ADAPTIVE);
  84. toku_mutex_init(&m_mutex, &attr);
  85. toku_mutexattr_destroy(&attr);
  86. }
  87. void rollback_log_node_cache::destroy() {
  88. toku_mutex_destroy(&m_mutex);
  89. toku_free(m_avail_blocknums);
  90. toku_free(m_hashes);
  91. }
  92. // returns true if rollback log node was successfully added,
  93. // false otherwise
  94. bool rollback_log_node_cache::give_rollback_log_node(TOKUTXN txn, ROLLBACK_LOG_NODE log){
  95. bool retval = false;
  96. toku_mutex_lock(&m_mutex);
  97. if (m_num_avail < m_max_num_avail) {
  98. retval = true;
  99. uint32_t index = m_first + m_num_avail;
  100. if (index >= m_max_num_avail) {
  101. index -= m_max_num_avail;
  102. }
  103. m_avail_blocknums[index].b = log->blocknum.b;
  104. m_hashes[index] = log->hash;
  105. m_num_avail++;
  106. }
  107. toku_mutex_unlock(&m_mutex);
  108. //
  109. // now unpin the rollback log node
  110. //
  111. if (retval) {
  112. make_rollback_log_empty(log);
  113. toku_rollback_log_unpin(txn, log);
  114. }
  115. return retval;
  116. }
  117. // if a rollback log node is available, will set log to it,
  118. // otherwise, will set log to NULL and caller is on his own
  119. // for getting a rollback log node
  120. void rollback_log_node_cache::get_rollback_log_node(TOKUTXN txn, ROLLBACK_LOG_NODE* log){
  121. BLOCKNUM b = ROLLBACK_NONE;
  122. uint32_t hash;
  123. toku_mutex_lock(&m_mutex);
  124. if (m_num_avail > 0) {
  125. b.b = m_avail_blocknums[m_first].b;
  126. hash = m_hashes[m_first];
  127. m_num_avail--;
  128. if (++m_first >= m_max_num_avail) {
  129. m_first = 0;
  130. }
  131. }
  132. toku_mutex_unlock(&m_mutex);
  133. if (b.b != ROLLBACK_NONE.b) {
  134. toku_get_and_pin_rollback_log(txn, b, hash, log);
  135. invariant(rollback_log_is_unused(*log));
  136. } else {
  137. *log = NULL;
  138. }
  139. }