OSDN Git Service

* gcc.target/i386/avx-recip-vec.c: New test.
[pf3gnuchains/gcc-fork.git] / libitm / libitm_i.h
1 /* Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3
4    This file is part of the GNU Transactional Memory Library (libitm).
5
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.
10
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
14    more details.
15
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.
19
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/>.  */
24
25 /* The following are internal implementation functions and definitions.
26    To distinguish them from those defined by the Intel ABI, they all
27    begin with GTM/gtm.  */
28
29 #ifndef LIBITM_I_H
30 #define LIBITM_I_H 1
31
32 #include "libitm.h"
33 #include "config.h"
34
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unwind.h>
39 #include "local_type_traits"
40
41 #include "common.h"
42
43 namespace GTM HIDDEN {
44
45 using namespace std;
46
47 // A helper template for accessing an unsigned integral of SIZE bytes.
48 template<size_t SIZE> struct sized_integral { };
49 template<> struct sized_integral<1> { typedef uint8_t type; };
50 template<> struct sized_integral<2> { typedef uint16_t type; };
51 template<> struct sized_integral<4> { typedef uint32_t type; };
52 template<> struct sized_integral<8> { typedef uint64_t type; };
53
54 typedef unsigned int gtm_word __attribute__((mode (word)));
55
56 // These values are given to GTM_restart_transaction and indicate the
57 // reason for the restart.  The reason is used to decide what STM
58 // implementation should be used during the next iteration.
59 enum gtm_restart_reason
60 {
61   RESTART_REALLOCATE,
62   RESTART_LOCKED_READ,
63   RESTART_LOCKED_WRITE,
64   RESTART_VALIDATE_READ,
65   RESTART_VALIDATE_WRITE,
66   RESTART_VALIDATE_COMMIT,
67   RESTART_SERIAL_IRR,
68   RESTART_NOT_READONLY,
69   RESTART_CLOSED_NESTING,
70   RESTART_INIT_METHOD_GROUP,
71   NUM_RESTARTS,
72   NO_RESTART = NUM_RESTARTS
73 };
74
75 } // namespace GTM
76
77 #include "target.h"
78 #include "rwlock.h"
79 #include "aatree.h"
80 #include "cacheline.h"
81 #include "stmlock.h"
82 #include "dispatch.h"
83 #include "containers.h"
84
85 namespace GTM HIDDEN {
86
87 // This type is private to alloc.c, but needs to be defined so that
88 // the template used inside gtm_thread can instantiate.
89 struct gtm_alloc_action
90 {
91   void (*free_fn)(void *);
92   bool allocated;
93 };
94
95 // This type is private to local.c.
96 struct gtm_undolog_entry;
97
98 struct gtm_thread;
99
100 // A transaction checkpoint: data that has to saved and restored when doing
101 // closed nesting.
102 struct gtm_transaction_cp
103 {
104   gtm_jmpbuf jb;
105   size_t undolog_size;
106   aa_tree<uintptr_t, gtm_alloc_action> alloc_actions;
107   size_t user_actions_size;
108   _ITM_transactionId_t id;
109   uint32_t prop;
110   uint32_t cxa_catch_count;
111   void *cxa_unthrown;
112   // We might want to use a different but compatible dispatch method for
113   // a nested transaction.
114   abi_dispatch *disp;
115   // Nesting level of this checkpoint (1 means that this is a checkpoint of
116   // the outermost transaction).
117   uint32_t nesting;
118
119   void save(gtm_thread* tx);
120   void commit(gtm_thread* tx);
121 };
122
123 // Contains all thread-specific data required by the entire library.
124 // This includes all data relevant to a single transaction. Because most
125 // thread-specific data is about the current transaction, we also refer to
126 // the transaction-specific parts of gtm_thread as "the transaction" (the
127 // same applies to names of variables and arguments).
128 // All but the shared part of this data structure are thread-local data.
129 // gtm_thread could be split into transaction-specific structures and other
130 // per-thread data (with those parts then nested in gtm_thread), but this
131 // would make it harder to later rearrange individual members to optimize data
132 // accesses. Thus, for now we keep one flat object, and will only split it if
133 // the code gets too messy.
134 struct gtm_thread
135 {
136
137   struct user_action
138   {
139     _ITM_userCommitFunction fn;
140     void *arg;
141     bool on_commit;
142     _ITM_transactionId_t resuming_id;
143   };
144
145   // The jump buffer by which GTM_longjmp restarts the transaction.
146   // This field *must* be at the beginning of the transaction.
147   gtm_jmpbuf jb;
148
149   // Data used by local.c for the undo log for both local and shared memory.
150   vector<gtm_undolog_entry*> undolog;
151
152   // Data used by alloc.c for the malloc/free undo log.
153   aa_tree<uintptr_t, gtm_alloc_action> alloc_actions;
154
155   // Data used by useraction.c for the user-defined commit/abort handlers.
156   vector<user_action> user_actions;
157
158   // A numerical identifier for this transaction.
159   _ITM_transactionId_t id;
160
161   // The _ITM_codeProperties of this transaction as given by the compiler.
162   uint32_t prop;
163
164   // The nesting depth for subsequently started transactions. This variable
165   // will be set to 1 when starting an outermost transaction.
166   uint32_t nesting;
167
168   // Set if this transaction owns the serial write lock.
169   // Can be reset only when restarting the outermost transaction.
170   static const uint32_t STATE_SERIAL            = 0x0001;
171   // Set if the serial-irrevocable dispatch table is installed.
172   // Implies that no logging is being done, and abort is not possible.
173   // Can be reset only when restarting the outermost transaction.
174   static const uint32_t STATE_IRREVOCABLE       = 0x0002;
175
176   // A bitmask of the above.
177   uint32_t state;
178
179   // In order to reduce cacheline contention on global_tid during
180   // beginTransaction, we allocate a block of 2**N ids to the thread
181   // all at once.  This number is the next value to be allocated from
182   // the block, or 0 % 2**N if no such block is allocated.
183   _ITM_transactionId_t local_tid;
184
185   // Data used by eh_cpp.c for managing exceptions within the transaction.
186   uint32_t cxa_catch_count;
187   void *cxa_unthrown;
188   void *eh_in_flight;
189
190   // Checkpoints for closed nesting.
191   vector<gtm_transaction_cp> parent_txns;
192
193   // Data used by retry.c for deciding what STM implementation should
194   // be used for the next iteration of the transaction.
195   // Only restart_total is reset to zero when the transaction commits, the
196   // other counters are total values for all previously executed transactions.
197   uint32_t restart_reason[NUM_RESTARTS];
198   uint32_t restart_total;
199
200   // *** The shared part of gtm_thread starts here. ***
201   // Shared state is on separate cachelines to avoid false sharing with
202   // thread-local parts of gtm_thread.
203
204   // Points to the next thread in the list of all threads.
205   gtm_thread *next_thread __attribute__((__aligned__(HW_CACHELINE_SIZE)));
206
207   // If this transaction is inactive, shared_state is ~0. Otherwise, this is
208   // an active or serial transaction.
209   gtm_word shared_state;
210
211   // The lock that provides access to serial mode.  Non-serialized
212   // transactions acquire read locks; a serialized transaction aquires
213   // a write lock.
214   static gtm_rwlock serial_lock;
215
216   // The head of the list of all threads' transactions.
217   static gtm_thread *list_of_threads;
218   // The number of all registered threads.
219   static unsigned number_of_threads;
220
221   // In alloc.cc
222   void commit_allocations (bool, aa_tree<uintptr_t, gtm_alloc_action>*);
223   void record_allocation (void *, void (*)(void *));
224   void forget_allocation (void *, void (*)(void *));
225   void drop_references_allocations (const void *ptr)
226   {
227     this->alloc_actions.erase((uintptr_t) ptr);
228   }
229
230   // In beginend.cc
231   void rollback (gtm_transaction_cp *cp = 0, bool aborting = false);
232   bool trycommit ();
233   void restart (gtm_restart_reason) ITM_NORETURN;
234
235   gtm_thread();
236   ~gtm_thread();
237
238   static void *operator new(size_t);
239   static void operator delete(void *);
240
241   // Invoked from assembly language, thus the "asm" specifier on
242   // the name, avoiding complex name mangling.
243   static uint32_t begin_transaction(uint32_t, const gtm_jmpbuf *)
244         __asm__("GTM_begin_transaction") ITM_REGPARM;
245
246   // In eh_cpp.cc
247   void revert_cpp_exceptions (gtm_transaction_cp *cp = 0);
248
249   // In local.cc
250   void commit_undolog (void);
251   void rollback_undolog (size_t until_size = 0);
252   void drop_references_undolog (const void *, size_t);
253
254   // In retry.cc
255   // Must be called outside of transactions (i.e., after rollback).
256   void decide_retry_strategy (gtm_restart_reason);
257   abi_dispatch* decide_begin_dispatch (uint32_t prop);
258   void number_of_threads_changed(unsigned previous, unsigned now);
259   // Must be called from serial mode. Does not call set_abi_disp().
260   void set_default_dispatch(abi_dispatch* disp);
261
262   // In method-serial.cc
263   void serialirr_mode ();
264
265   // In useraction.cc
266   void rollback_user_actions (size_t until_size = 0);
267   void commit_user_actions ();
268 };
269
270 } // namespace GTM
271
272 #include "tls.h"
273
274 namespace GTM HIDDEN {
275
276 // An unscaled count of the number of times we should spin attempting to
277 // acquire locks before we block the current thread and defer to the OS.
278 // This variable isn't used when the standard POSIX lock implementations
279 // are used.
280 extern uint64_t gtm_spin_count_var;
281
282 extern "C" uint32_t GTM_longjmp (const gtm_jmpbuf *, uint32_t, uint32_t)
283         ITM_NORETURN ITM_REGPARM;
284
285 extern "C" void GTM_LB (const void *, size_t) ITM_REGPARM;
286
287 extern void GTM_error (const char *fmt, ...)
288         __attribute__((format (printf, 1, 2)));
289 extern void GTM_fatal (const char *fmt, ...)
290         __attribute__((noreturn, format (printf, 1, 2)));
291
292 extern abi_dispatch *dispatch_serial();
293 extern abi_dispatch *dispatch_serialirr();
294 extern abi_dispatch *dispatch_serialirr_onwrite();
295 extern abi_dispatch *dispatch_gl_wt();
296
297 extern gtm_cacheline_mask gtm_mask_stack(gtm_cacheline *, gtm_cacheline_mask);
298
299 } // namespace GTM
300
301 #endif // LIBITM_I_H