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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [doc/] [xml/] [manual/] [concurrency_extensions.xml] - Blame information for rev 742

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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