clone.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (C) 2009-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libitm_i.h"
  20. using namespace GTM;
  21. struct clone_entry
  22. {
  23. void *orig, *clone;
  24. };
  25. struct clone_table
  26. {
  27. clone_entry *table;
  28. size_t size;
  29. clone_table *next;
  30. };
  31. static clone_table *all_tables;
  32. static void *
  33. find_clone (void *ptr)
  34. {
  35. clone_table *table;
  36. for (table = all_tables; table ; table = table->next)
  37. {
  38. clone_entry *t = table->table;
  39. size_t lo = 0, hi = table->size, i;
  40. /* Quick test for whether PTR is present in this table. */
  41. if (ptr < t[0].orig || ptr > t[hi - 1].orig)
  42. continue;
  43. /* Otherwise binary search. */
  44. while (lo < hi)
  45. {
  46. i = (lo + hi) / 2;
  47. if (ptr < t[i].orig)
  48. hi = i;
  49. else if (ptr > t[i].orig)
  50. lo = i + 1;
  51. else
  52. return t[i].clone;
  53. }
  54. /* Given the quick test above, if we don't find the entry in
  55. this table then it doesn't exist. */
  56. break;
  57. }
  58. return NULL;
  59. }
  60. void * ITM_REGPARM
  61. _ITM_getTMCloneOrIrrevocable (void *ptr)
  62. {
  63. void *ret = find_clone (ptr);
  64. if (ret)
  65. return ret;
  66. gtm_thr()->serialirr_mode ();
  67. return ptr;
  68. }
  69. void * ITM_REGPARM
  70. _ITM_getTMCloneSafe (void *ptr)
  71. {
  72. void *ret = find_clone (ptr);
  73. if (ret == NULL)
  74. abort ();
  75. return ret;
  76. }
  77. static int
  78. clone_entry_compare (const void *a, const void *b)
  79. {
  80. const clone_entry *aa = (const clone_entry *)a;
  81. const clone_entry *bb = (const clone_entry *)b;
  82. if (aa->orig < bb->orig)
  83. return -1;
  84. else if (aa->orig > bb->orig)
  85. return 1;
  86. else
  87. return 0;
  88. }
  89. namespace {
  90. // Within find_clone, we know that we are inside a transaction. Because
  91. // of that, we have already synchronized with serial_lock. By taking the
  92. // serial_lock for write, we exclude all transactions while we make this
  93. // change to the clone tables, without having to synchronize on a separate
  94. // lock. Do be careful not to attempt a recursive write lock.
  95. class ExcludeTransaction
  96. {
  97. bool do_lock;
  98. public:
  99. ExcludeTransaction()
  100. {
  101. gtm_thread *tx = gtm_thr();
  102. do_lock = !(tx && (tx->state & gtm_thread::STATE_SERIAL));
  103. if (do_lock)
  104. gtm_thread::serial_lock.write_lock ();
  105. }
  106. ~ExcludeTransaction()
  107. {
  108. if (do_lock)
  109. gtm_thread::serial_lock.write_unlock ();
  110. }
  111. };
  112. } // end anon namespace
  113. void
  114. _ITM_registerTMCloneTable (void *xent, size_t size)
  115. {
  116. clone_entry *ent = static_cast<clone_entry *>(xent);
  117. clone_table *table;
  118. table = (clone_table *) xmalloc (sizeof (clone_table));
  119. table->table = ent;
  120. table->size = size;
  121. qsort (ent, size, sizeof (clone_entry), clone_entry_compare);
  122. // Hold the serial_lock while we update the ALL_TABLES datastructure.
  123. {
  124. ExcludeTransaction exclude;
  125. table->next = all_tables;
  126. all_tables = table;
  127. }
  128. }
  129. void
  130. _ITM_deregisterTMCloneTable (void *xent)
  131. {
  132. clone_entry *ent = static_cast<clone_entry *>(xent);
  133. clone_table *tab;
  134. // Hold the serial_lock while we update the ALL_TABLES datastructure.
  135. {
  136. ExcludeTransaction exclude;
  137. clone_table **pprev;
  138. for (pprev = &all_tables;
  139. tab = *pprev, tab->table != ent;
  140. pprev = &tab->next)
  141. continue;
  142. *pprev = tab->next;
  143. }
  144. free (tab);
  145. }