1 /* Copyright (C) 2009-2014 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Transactional Memory Library (libitm).
6 Libitm is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
41 static clone_table
*all_tables
;
44 find_clone (void *ptr
)
48 for (table
= all_tables
; table
; table
= table
->next
)
50 clone_entry
*t
= table
->table
;
51 size_t lo
= 0, hi
= table
->size
, i
;
53 /* Quick test for whether PTR is present in this table. */
54 if (ptr
< t
[0].orig
|| ptr
> t
[hi
- 1].orig
)
57 /* Otherwise binary search. */
63 else if (ptr
> t
[i
].orig
)
69 /* Given the quick test above, if we don't find the entry in
70 this table then it doesn't exist. */
79 _ITM_getTMCloneOrIrrevocable (void *ptr
)
81 void *ret
= find_clone (ptr
);
85 gtm_thr()->serialirr_mode ();
91 _ITM_getTMCloneSafe (void *ptr
)
93 void *ret
= find_clone (ptr
);
100 clone_entry_compare (const void *a
, const void *b
)
102 const clone_entry
*aa
= (const clone_entry
*)a
;
103 const clone_entry
*bb
= (const clone_entry
*)b
;
105 if (aa
->orig
< bb
->orig
)
107 else if (aa
->orig
> bb
->orig
)
115 // Within find_clone, we know that we are inside a transaction. Because
116 // of that, we have already synchronized with serial_lock. By taking the
117 // serial_lock for write, we exclude all transactions while we make this
118 // change to the clone tables, without having to synchronize on a separate
119 // lock. Do be careful not to attempt a recursive write lock.
121 class ExcludeTransaction
128 gtm_thread
*tx
= gtm_thr();
129 do_lock
= !(tx
&& (tx
->state
& gtm_thread::STATE_SERIAL
));
132 gtm_thread::serial_lock
.write_lock ();
135 ~ExcludeTransaction()
138 gtm_thread::serial_lock
.write_unlock ();
142 } // end anon namespace
146 _ITM_registerTMCloneTable (void *xent
, size_t size
)
148 clone_entry
*ent
= static_cast<clone_entry
*>(xent
);
151 table
= (clone_table
*) xmalloc (sizeof (clone_table
));
155 qsort (ent
, size
, sizeof (clone_entry
), clone_entry_compare
);
157 // Hold the serial_lock while we update the ALL_TABLES datastructure.
159 ExcludeTransaction exclude
;
160 table
->next
= all_tables
;
166 _ITM_deregisterTMCloneTable (void *xent
)
168 clone_entry
*ent
= static_cast<clone_entry
*>(xent
);
171 // Hold the serial_lock while we update the ALL_TABLES datastructure.
173 ExcludeTransaction exclude
;
176 for (pprev
= &all_tables
;
177 tab
= *pprev
, tab
->table
!= ent
;