OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc4/] [libstdc++-v3/] [doc/] [xml/] [manual/] [concurrency_extensions.xml] - Blame information for rev 519

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 424 jeremybenn
2
3
 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
4
[ ]>
5
 
6
7
8
 
9
10
  
11
    
12
      ISO C++
13
    
14
    
15
      library
16
    
17
  
18
19
 
20
Concurrency
21
 
22
23
  Design
24
 
25
  
26
    Interface to Locks and Mutexes
27
 
28
The file <ext/concurrence.h> contains all the higher-level
29
constructs for playing with threads. In contrast to the atomics layer,
30
the concurrence layer consists largely of types. All types are defined within namespace __gnu_cxx.
31
32
 
33
34
These types can be used in a portable manner, regardless of the
35
specific environment. They are carefully designed to provide optimum
36
efficiency and speed, abstracting out underlying thread calls and
37
accesses when compiling for single-threaded situations (even on hosts
38
that support multiple threads.)
39
40
 
41
The enumerated type _Lock_policy details the set of
42
available locking
43
policies: _S_single, _S_mutex,
44
and _S_atomic.
45
46
 
47
48
_S_single
49
Indicates single-threaded code that does not need locking.
50
51
 
52
53
_S_mutex
54
Indicates multi-threaded code using thread-layer abstractions.
55
56
57
_S_atomic
58
Indicates multi-threaded code using atomic operations.
59
60
61
62
 
63
The compile-time constant __default_lock_policy is set
64
to one of the three values above, depending on characteristics of the
65
host environment and the current compilation flags.
66
67
 
68
Two more datatypes make up the rest of the
69
interface: __mutex, and __scoped_lock.
70
71
 
72
73
74
 
75
The scoped lock idiom is well-discussed within the C++
76
community. This version takes a __mutex reference, and
77
locks it during construction of __scoped_locke and
78
unlocks it during destruction. This is an efficient way of locking
79
critical sections, while retaining exception-safety.
80
81
  
82
 
83
  
84
    Interface to Atomic Functions
85
 
86
 
87
88
Two functions and one type form the base of atomic support.
89
90
 
91
 
92
The type _Atomic_word is a signed integral type
93
supporting atomic operations.
94
95
 
96
97
The two functions functions are:
98
99
 
100
101
_Atomic_word
102
__exchange_and_add_dispatch(volatile _Atomic_word*, int);
103
 
104
void
105
__atomic_add_dispatch(volatile _Atomic_word*, int);
106
107
 
108
Both of these functions are declared in the header file
109
<ext/atomicity.h>, and are in namespace __gnu_cxx.
110
111
 
112
113
114
115
__exchange_and_add_dispatch
116
117
118
Adds the second argument's value to the first argument. Returns the old value.
119
120
121
122
123
__atomic_add_dispatch
124
125
126
Adds the second argument's value to the first argument. Has no return value.
127
128
129
130
 
131
132
These functions forward to one of several specialized helper
133
functions, depending on the circumstances. For instance,
134
135
 
136
137
138
__exchange_and_add_dispatch
139
140
141
 
142
143
Calls through to either of:
144
145
 
146
147
__exchange_and_add
148
149
Multi-thread version. Inlined if compiler-generated builtin atomics
150
can be used, otherwise resolved at link time to a non-builtin code
151
sequence.
152
153
154
 
155
__exchange_and_add_single
156
157
Single threaded version. Inlined.
158
159
160
 
161
However, only __exchange_and_add_dispatch
162
and __atomic_add_dispatch should be used. These functions
163
can be used in a portable manner, regardless of the specific
164
environment. They are carefully designed to provide optimum efficiency
165
and speed, abstracting out atomic accesses when they are not required
166
(even on hosts that support compiler intrinsics for atomic
167
operations.)
168
169
 
170
171
In addition, there are two macros
172
173
 
174
175
176
_GLIBCXX_READ_MEM_BARRIER
177
178
179
180
181
_GLIBCXX_WRITE_MEM_BARRIER
182
183
184
 
185
186
Which expand to the appropriate write and read barrier required by the
187
host hardware and operating system.
188
189
  
190
 
191
192
 
193
 
194
195
  Implementation
196
  
197
    Using Builtin Atomic Functions
198
 
199
The functions for atomic operations described above are either
200
implemented via compiler intrinsics (if the underlying host is
201
capable) or by library fallbacks.
202
 
203
Compiler intrinsics (builtins) are always preferred.  However, as
204
the compiler builtins for atomics are not universally implemented,
205
using them directly is problematic, and can result in undefined
206
function calls. (An example of an undefined symbol from the use
207
of __sync_fetch_and_add on an unsupported host is a
208
missing reference to __sync_fetch_and_add_4.)
209
210
 
211
In addition, on some hosts the compiler intrinsics are enabled
212
conditionally, via the -march command line flag. This makes
213
usage vary depending on the target hardware and the flags used during
214
compile.
215
216
 
217
218
If builtins are possible for bool-sized integral types,
219
_GLIBCXX_ATOMIC_BUILTINS_1 will be defined.
220
If builtins are possible for int-sized integral types,
221
_GLIBCXX_ATOMIC_BUILTINS_4 will be defined.
222
223
 
224
 
225
For the following hosts, intrinsics are enabled by default.
226
227
 
228
229
  alpha
230
  ia64
231
  powerpc
232
  s390
233
234
 
235
For others, some form of -march may work. On
236
non-ancient x86 hardware, -march=native usually does the
237
trick.
238
 
239
 For hosts without compiler intrinsics, but with capable
240
hardware, hand-crafted assembly is selected. This is the case for the following hosts:
241
242
 
243
244
  cris
245
  hppa
246
  i386
247
  i486
248
  m48k
249
  mips
250
  sparc
251
252
 
253
And for the rest, a simulated atomic lock via pthreads.
254
255
 
256
 Detailed information about compiler intrinsics for atomic operations can be found in the GCC  documentation.
257
258
 
259
 More details on the library fallbacks from the porting section.
260
261
 
262
 
263
  
264
  
265
    Thread Abstraction
266
 
267
A thin layer above IEEE 1003.1 (i.e. pthreads) is used to abstract
268
the thread interface for GCC. This layer is called "gthread," and is
269
comprised of one header file that wraps the host's default thread layer with
270
a POSIX-like interface.
271
272
 
273
 The file <gthr-default.h> points to the deduced wrapper for
274
the current host. In libstdc++ implementation files,
275
<bits/gthr.h> is used to select the proper gthreads file.
276
277
 
278
Within libstdc++ sources, all calls to underlying thread functionality
279
use this layer. More detail as to the specific interface can be found in the source documentation.
280
281
 
282
By design, the gthread layer is interoperable with the types,
283
functions, and usage found in the usual <pthread.h> file,
284
including pthread_t, pthread_once_t, pthread_create,
285
etc.
286
287
 
288
  
289
290
 
291
292
 
293
  Use
294
 
295
Typical usage of the last two constructs is demonstrated as follows:
296
297
 
298
299
#include <ext/concurrence.h>
300
 
301
namespace
302
{
303
  __gnu_cxx::__mutex safe_base_mutex;
304
} // anonymous namespace
305
 
306
namespace other
307
{
308
  void
309
  foo()
310
  {
311
    __gnu_cxx::__scoped_lock sentry(safe_base_mutex);
312
    for (int i = 0; i < max;  ++i)
313
      {
314
        _Safe_iterator_base* __old = __iter;
315
        __iter = __iter-<_M_next;
316
        __old-<_M_detach_single();
317
      }
318
}
319
320
 
321
In this sample code, an anonymous namespace is used to keep
322
the __mutex private to the compilation unit,
323
and __scoped_lock is used to guard access to the critical
324
section within the for loop, locking the mutex on creation and freeing
325
the mutex as control moves out of this block.
326
327
 
328
Several exception classes are used to keep track of
329
concurrence-related errors. These classes
330
are: __concurrence_lock_error, __concurrence_unlock_error, __concurrence_wait_error,
331
and __concurrence_broadcast_error.
332
333
 
334
 
335
336
 
337

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.