~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to erts/include/internal/win/ethr_atomic.h

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * %CopyrightBegin%
 
3
 *
 
4
 * Copyright Ericsson AB 2010. All Rights Reserved.
 
5
 *
 
6
 * The contents of this file are subject to the Erlang Public License,
 
7
 * Version 1.1, (the "License"); you may not use this file except in
 
8
 * compliance with the License. You should have received a copy of the
 
9
 * Erlang Public License along with this software. If not, it can be
 
10
 * retrieved online at http://www.erlang.org/.
 
11
 *
 
12
 * Software distributed under the License is distributed on an "AS IS"
 
13
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
14
 * the License for the specific language governing rights and limitations
 
15
 * under the License.
 
16
 *
 
17
 * %CopyrightEnd%
 
18
 */
 
19
 
 
20
/*
 
21
 * Description: Native atomics ethread support when using VC++
 
22
 * Author: Rickard Green
 
23
 */
 
24
 
 
25
#undef ETHR_INCLUDE_ATOMIC_IMPL__
 
26
#if !defined(ETHR_WIN_ATOMIC32_H__) && defined(ETHR_ATOMIC_WANT_32BIT_IMPL__)
 
27
#define ETHR_WIN_ATOMIC32_H__
 
28
#define ETHR_INCLUDE_ATOMIC_IMPL__ 4
 
29
#undef ETHR_ATOMIC_WANT_32BIT_IMPL__
 
30
#elif !defined(ETHR_WIN_ATOMIC64_H__) && defined(ETHR_ATOMIC_WANT_64BIT_IMPL__)
 
31
#define ETHR_WIN_ATOMIC64_H__
 
32
#ifdef ETHR_HAVE__INTERLOCKEDCOMPAREEXCHANGE64
 
33
/* _InterlockedCompareExchange64() required... */
 
34
#define ETHR_INCLUDE_ATOMIC_IMPL__ 8
 
35
#endif
 
36
#undef ETHR_ATOMIC_WANT_64BIT_IMPL__
 
37
#endif
 
38
 
 
39
#ifdef ETHR_INCLUDE_ATOMIC_IMPL__
 
40
 
 
41
#if defined(_MSC_VER) && _MSC_VER >= 1400
 
42
 
 
43
#ifndef ETHR_WIN_ATOMIC_COMMON__
 
44
#define ETHR_WIN_ATOMIC_COMMON__
 
45
 
 
46
#define ETHR_HAVE_NATIVE_ATOMICS 1
 
47
 
 
48
#if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64)
 
49
#  define ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__ 1
 
50
#else
 
51
#  define ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__ 0
 
52
#endif
 
53
 
 
54
#if defined(_M_AMD64) || (defined(_M_IX86) \
 
55
                          && !defined(ETHR_PRE_PENTIUM4_COMPAT))
 
56
#  define ETHR_READ_ACQB_AND_SET_RELB_COMPILER_BARRIER_ONLY__ 1
 
57
#else
 
58
#  define ETHR_READ_ACQB_AND_SET_RELB_COMPILER_BARRIER_ONLY__ 0
 
59
#endif
 
60
/*
 
61
 * No configure test checking for interlocked acquire/release
 
62
 * versions have been written, yet. It should define
 
63
 * ETHR_HAVE_INTERLOCKED_ACQUIRE_RELEASE_BARRIERS if, and
 
64
 * only if, all used interlocked operations with barriers
 
65
 * exists.
 
66
 *
 
67
 * Note, that these are pure optimizations for the itanium
 
68
 * processor.
 
69
 */
 
70
 
 
71
#include <intrin.h>
 
72
#undef ETHR_COMPILER_BARRIER
 
73
#define ETHR_COMPILER_BARRIER _ReadWriteBarrier()
 
74
#pragma intrinsic(_ReadWriteBarrier)
 
75
#pragma intrinsic(_InterlockedCompareExchange)
 
76
 
 
77
#if defined(_M_AMD64) || (defined(_M_IX86) \
 
78
                          && !defined(ETHR_PRE_PENTIUM4_COMPAT))
 
79
#include <emmintrin.h>
 
80
#include <mmintrin.h>
 
81
#pragma intrinsic(_mm_mfence)
 
82
#define ETHR_MEMORY_BARRIER _mm_mfence()
 
83
#pragma intrinsic(_mm_sfence)
 
84
#define ETHR_WRITE_MEMORY_BARRIER _mm_sfence()
 
85
#pragma intrinsic(_mm_lfence)
 
86
#define ETHR_READ_MEMORY_BARRIER _mm_lfence()
 
87
#define ETHR_READ_DEPEND_MEMORY_BARRIER ETHR_COMPILER_BARRIER
 
88
 
 
89
#else
 
90
 
 
91
#define ETHR_MEMORY_BARRIER                                     \
 
92
do {                                                            \
 
93
    volatile long x___ = 0;                                     \
 
94
    _InterlockedCompareExchange(&x___, (long) 1, (long) 0);     \
 
95
} while (0)
 
96
 
 
97
#endif
 
98
 
 
99
#endif /* ETHR_WIN_ATOMIC_COMMON__ */
 
100
 
 
101
#if ETHR_INCLUDE_ATOMIC_IMPL__ == 4
 
102
 
 
103
#define ETHR_HAVE_NATIVE_ATOMIC32 1
 
104
 
 
105
/*
 
106
 * All used operations available as 32-bit intrinsics
 
107
 */
 
108
 
 
109
#pragma intrinsic(_InterlockedDecrement)
 
110
#pragma intrinsic(_InterlockedIncrement)
 
111
#pragma intrinsic(_InterlockedExchangeAdd)
 
112
#pragma intrinsic(_InterlockedExchange)
 
113
#pragma intrinsic(_InterlockedAnd)
 
114
#pragma intrinsic(_InterlockedOr)
 
115
#ifdef ETHR_HAVE_INTERLOCKED_ACQUIRE_RELEASE_BARRIERS
 
116
#pragma intrinsic(_InterlockedExchangeAdd_acq)
 
117
#pragma intrinsic(_InterlockedIncrement_acq)
 
118
#pragma intrinsic(_InterlockedDecrement_rel)
 
119
#pragma intrinsic(_InterlockedCompareExchange_acq)
 
120
#pragma intrinsic(_InterlockedCompareExchange_rel)
 
121
#endif
 
122
 
 
123
#define ETHR_ILCKD__(X) _Interlocked ## X
 
124
#ifdef ETHR_HAVE_INTERLOCKED_ACQUIRE_RELEASE_BARRIERS
 
125
#define ETHR_ILCKD_ACQ__(X) _Interlocked ## X ## _acq
 
126
#define ETHR_ILCKD_REL__(X) _Interlocked ## X ## _rel
 
127
#else
 
128
#define ETHR_ILCKD_ACQ__(X) _Interlocked ## X
 
129
#define ETHR_ILCKD_REL__(X) _Interlocked ## X
 
130
#endif
 
131
 
 
132
#define ETHR_NATMC_FUNC__(X) ethr_native_atomic32_ ## X
 
133
#define ETHR_ATMC_T__ ethr_native_atomic32_t
 
134
#define ETHR_AINT_T__ ethr_sint32_t
 
135
 
 
136
#elif ETHR_INCLUDE_ATOMIC_IMPL__ == 8
 
137
 
 
138
#define ETHR_HAVE_NATIVE_ATOMIC64 1
 
139
 
 
140
/*
 
141
 * _InterlockedCompareExchange64() is required. The other may not
 
142
 * be available, but if so, we can generate them.
 
143
 */
 
144
#pragma intrinsic(_InterlockedCompareExchange64)
 
145
 
 
146
#if ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__          
 
147
#define ETHR_OWN_ILCKD_INIT_VAL__(PTR) *(PTR)
 
148
#else
 
149
#define ETHR_OWN_ILCKD_INIT_VAL__(PTR) (__int64) 0
 
150
#endif
 
151
 
 
152
#define ETHR_OWN_ILCKD_BODY_IMPL__(FUNC, PTR, NEW, ACT, EXP, OPS, RET)  \
 
153
{                                                                       \
 
154
    __int64 NEW, ACT, EXP;                                              \
 
155
    ACT = ETHR_OWN_ILCKD_INIT_VAL__(PTR);                               \
 
156
    do {                                                                \
 
157
        EXP = ACT;                                                      \
 
158
        { OPS; }                                                        \
 
159
        ACT = _InterlockedCompareExchange64(PTR, NEW, EXP);             \
 
160
    } while (ACT != EXP);                                               \
 
161
    return RET;                                                         \
 
162
}
 
163
 
 
164
#define ETHR_OWN_ILCKD_1_IMPL__(FUNC, NEW, ACT, EXP, OPS, RET)          \
 
165
static __forceinline __int64                                            \
 
166
FUNC(__int64 volatile *ptr)                                             \
 
167
ETHR_OWN_ILCKD_BODY_IMPL__(FUNC, ptr, NEW, ACT, EXP, OPS, RET)
 
168
 
 
169
#define ETHR_OWN_ILCKD_2_IMPL__(FUNC, NEW, ACT, EXP, OPS, ARG, RET)     \
 
170
static __forceinline __int64                                            \
 
171
FUNC(__int64 volatile *ptr, __int64 ARG)                                \
 
172
ETHR_OWN_ILCKD_BODY_IMPL__(FUNC, ptr, NEW, ACT, EXP, OPS, RET)
 
173
 
 
174
 
 
175
#ifdef ETHR_HAVE__INTERLOCKEDDECREMENT64
 
176
#pragma intrinsic(_InterlockedDecrement64)
 
177
#else
 
178
ETHR_OWN_ILCKD_1_IMPL__(_InterlockedDecrement64, new, act, exp,
 
179
                        new = act - 1, new)
 
180
#endif
 
181
#ifdef ETHR_HAVE__INTERLOCKEDINCREMENT64
 
182
#pragma intrinsic(_InterlockedIncrement64)
 
183
#else
 
184
ETHR_OWN_ILCKD_1_IMPL__(_InterlockedIncrement64, new, act, exp,
 
185
                        new = act + 1, new)
 
186
#endif
 
187
#ifdef ETHR_HAVE__INTERLOCKEDEXCHANGEADD64
 
188
#pragma intrinsic(_InterlockedExchangeAdd64)
 
189
#else
 
190
ETHR_OWN_ILCKD_2_IMPL__(_InterlockedExchangeAdd64, new, act, exp,
 
191
                        new = act + arg, arg, act)
 
192
#endif
 
193
#ifdef ETHR_HAVE__INTERLOCKEDEXCHANGE64
 
194
#pragma intrinsic(_InterlockedExchange64)
 
195
#else
 
196
ETHR_OWN_ILCKD_2_IMPL__(_InterlockedExchange64, new, act, exp,
 
197
                        new = arg, arg, act)
 
198
#endif
 
199
#ifdef ETHR_HAVE__INTERLOCKEDAND64
 
200
#pragma intrinsic(_InterlockedAnd64)
 
201
#else
 
202
ETHR_OWN_ILCKD_2_IMPL__(_InterlockedAnd64, new, act, exp,
 
203
                        new = act & arg, arg, act)
 
204
#endif
 
205
#ifdef ETHR_HAVE__INTERLOCKEDOR64
 
206
#pragma intrinsic(_InterlockedOr64)
 
207
#else
 
208
ETHR_OWN_ILCKD_2_IMPL__(_InterlockedOr64, new, act, exp,
 
209
                        new = act | arg, arg, act)
 
210
#endif
 
211
#ifdef ETHR_HAVE_INTERLOCKED_ACQUIRE_RELEASE_BARRIERS
 
212
#pragma intrinsic(_InterlockedExchangeAdd64_acq)
 
213
#pragma intrinsic(_InterlockedIncrement64_acq)
 
214
#pragma intrinsic(_InterlockedDecrement64_rel)
 
215
#pragma intrinsic(_InterlockedCompareExchange64_acq)
 
216
#pragma intrinsic(_InterlockedCompareExchange64_rel)
 
217
#endif
 
218
 
 
219
#define ETHR_ILCKD__(X) _Interlocked ## X ## 64
 
220
#ifdef ETHR_HAVE_INTERLOCKED_ACQUIRE_RELEASE_BARRIERS
 
221
#define ETHR_ILCKD_ACQ__(X) _Interlocked ## X ## 64_acq
 
222
#define ETHR_ILCKD_REL__(X) _Interlocked ## X ## 64_rel
 
223
#else
 
224
#define ETHR_ILCKD_ACQ__(X) _Interlocked ## X ## 64
 
225
#define ETHR_ILCKD_REL__(X) _Interlocked ## X ## 64
 
226
#endif
 
227
 
 
228
#define ETHR_NATMC_FUNC__(X) ethr_native_atomic64_ ## X
 
229
#define ETHR_ATMC_T__ ethr_native_atomic64_t
 
230
#define ETHR_AINT_T__ ethr_sint64_t
 
231
 
 
232
#else
 
233
#error "Unsupported integer size"
 
234
#endif
 
235
 
 
236
typedef struct {
 
237
    volatile ETHR_AINT_T__ value;
 
238
} ETHR_ATMC_T__;
 
239
 
 
240
#if defined(ETHR_TRY_INLINE_FUNCS) || defined(ETHR_ATOMIC_IMPL__)
 
241
 
 
242
static ETHR_INLINE ETHR_AINT_T__ *
 
243
ETHR_NATMC_FUNC__(addr)(ETHR_ATMC_T__ *var)
 
244
{
 
245
    return (ETHR_AINT_T__ *) &var->value;
 
246
}
 
247
 
 
248
static ETHR_INLINE void
 
249
ETHR_NATMC_FUNC__(init)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ i)
 
250
{
 
251
#if ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__
 
252
    var->value = i;
 
253
#else
 
254
    (void) ETHR_ILCKD__(Exchange)(&var->value, i);
 
255
#endif
 
256
}
 
257
 
 
258
static ETHR_INLINE void
 
259
ETHR_NATMC_FUNC__(set)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ i)
 
260
{
 
261
#if ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__
 
262
    var->value = i;
 
263
#else
 
264
    (void) ETHR_ILCKD__(Exchange)(&var->value, i);
 
265
#endif
 
266
}
 
267
 
 
268
static ETHR_INLINE ETHR_AINT_T__
 
269
ETHR_NATMC_FUNC__(read)(ETHR_ATMC_T__ *var)
 
270
{
 
271
#if ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__
 
272
    return var->value;
 
273
#else
 
274
    return ETHR_ILCKD__(ExchangeAdd)(&var->value, (ETHR_AINT_T__) 0);
 
275
#endif
 
276
}
 
277
 
 
278
static ETHR_INLINE void
 
279
ETHR_NATMC_FUNC__(add)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ incr)
 
280
{
 
281
    (void) ETHR_ILCKD__(ExchangeAdd)(&var->value, incr);
 
282
}
 
283
 
 
284
static ETHR_INLINE ETHR_AINT_T__
 
285
ETHR_NATMC_FUNC__(add_return)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ i)
 
286
{
 
287
    return ETHR_ILCKD__(ExchangeAdd)(&var->value, i) + i;
 
288
}
 
289
 
 
290
static ETHR_INLINE void
 
291
ETHR_NATMC_FUNC__(inc)(ETHR_ATMC_T__ *var)
 
292
{
 
293
    (void) ETHR_ILCKD__(Increment)(&var->value);
 
294
}
 
295
 
 
296
static ETHR_INLINE void
 
297
ETHR_NATMC_FUNC__(dec)(ETHR_ATMC_T__ *var)
 
298
{
 
299
    (void) ETHR_ILCKD__(Decrement)(&var->value);
 
300
}
 
301
 
 
302
static ETHR_INLINE ETHR_AINT_T__
 
303
ETHR_NATMC_FUNC__(inc_return)(ETHR_ATMC_T__ *var)
 
304
{
 
305
    return ETHR_ILCKD__(Increment)(&var->value);
 
306
}
 
307
 
 
308
static ETHR_INLINE ETHR_AINT_T__
 
309
ETHR_NATMC_FUNC__(dec_return)(ETHR_ATMC_T__ *var)
 
310
{
 
311
    return ETHR_ILCKD__(Decrement)(&var->value);
 
312
}
 
313
 
 
314
static ETHR_INLINE ETHR_AINT_T__
 
315
ETHR_NATMC_FUNC__(and_retold)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ mask)
 
316
{
 
317
    return ETHR_ILCKD__(And)(&var->value, mask);
 
318
}
 
319
 
 
320
static ETHR_INLINE ETHR_AINT_T__
 
321
ETHR_NATMC_FUNC__(or_retold)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ mask)
 
322
{
 
323
    return ETHR_ILCKD__(Or)(&var->value, mask);
 
324
}
 
325
 
 
326
static ETHR_INLINE ETHR_AINT_T__
 
327
ETHR_NATMC_FUNC__(cmpxchg)(ETHR_ATMC_T__ *var,
 
328
                           ETHR_AINT_T__ new,
 
329
                           ETHR_AINT_T__ old)
 
330
{
 
331
    return ETHR_ILCKD__(CompareExchange)(&var->value, new, old);
 
332
}
 
333
 
 
334
 
 
335
static ETHR_INLINE ETHR_AINT_T__
 
336
ETHR_NATMC_FUNC__(xchg)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ new)
 
337
{
 
338
    return ETHR_ILCKD__(Exchange)(&var->value, new);
 
339
}
 
340
 
 
341
/*
 
342
 * Atomic ops with at least specified barriers.
 
343
 */
 
344
 
 
345
static ETHR_INLINE ETHR_AINT_T__
 
346
ETHR_NATMC_FUNC__(read_acqb)(ETHR_ATMC_T__ *var)
 
347
{
 
348
#if ETHR_READ_ACQB_AND_SET_RELB_COMPILER_BARRIER_ONLY__
 
349
    ETHR_AINT_T__ val = var->value;
 
350
    ETHR_COMPILER_BARRIER;
 
351
    return val;
 
352
#else
 
353
    return ETHR_ILCKD_ACQ__(ExchangeAdd)(&var->value, (ETHR_AINT_T__) 0);
 
354
#endif
 
355
}
 
356
 
 
357
static ETHR_INLINE ETHR_AINT_T__
 
358
ETHR_NATMC_FUNC__(inc_return_acqb)(ETHR_ATMC_T__ *var)
 
359
{
 
360
    return ETHR_ILCKD_ACQ__(Increment)(&var->value);
 
361
}
 
362
 
 
363
static ETHR_INLINE void
 
364
ETHR_NATMC_FUNC__(set_relb)(ETHR_ATMC_T__ *var, ETHR_AINT_T__ i)
 
365
{
 
366
#if ETHR_READ_ACQB_AND_SET_RELB_COMPILER_BARRIER_ONLY__
 
367
    ETHR_COMPILER_BARRIER;
 
368
    var->value = i;
 
369
#else
 
370
    (void) ETHR_ILCKD_REL__(Exchange)(&var->value, i);
 
371
#endif
 
372
}
 
373
 
 
374
static ETHR_INLINE void
 
375
ETHR_NATMC_FUNC__(dec_relb)(ETHR_ATMC_T__ *var)
 
376
{
 
377
    (void) ETHR_ILCKD_REL__(Decrement)(&var->value);
 
378
}
 
379
 
 
380
static ETHR_INLINE ETHR_AINT_T__
 
381
ETHR_NATMC_FUNC__(dec_return_relb)(ETHR_ATMC_T__ *var)
 
382
{
 
383
    return ETHR_ILCKD_REL__(Decrement)(&var->value);
 
384
}
 
385
 
 
386
static ETHR_INLINE ETHR_AINT_T__
 
387
ETHR_NATMC_FUNC__(cmpxchg_acqb)(ETHR_ATMC_T__ *var,
 
388
                                ETHR_AINT_T__ new,
 
389
                                ETHR_AINT_T__ old)
 
390
{
 
391
    return ETHR_ILCKD_ACQ__(CompareExchange)(&var->value, new, old);
 
392
}
 
393
 
 
394
static ETHR_INLINE ETHR_AINT_T__
 
395
ETHR_NATMC_FUNC__(cmpxchg_relb)(ETHR_ATMC_T__ *var,
 
396
                                ETHR_AINT_T__ new,
 
397
                                ETHR_AINT_T__ old)
 
398
{
 
399
    return ETHR_ILCKD_REL__(CompareExchange)(&var->value, new, old);
 
400
}
 
401
 
 
402
#endif /* ETHR_TRY_INLINE_FUNCS */
 
403
 
 
404
#undef ETHR_ILCKD__
 
405
#undef ETHR_ILCKD_ACQ__
 
406
#undef ETHR_ILCKD_REL__
 
407
#undef ETHR_NATMC_FUNC__
 
408
#undef ETHR_ATMC_T__
 
409
#undef ETHR_AINT_T__
 
410
#undef ETHR_READ_AND_SET_WITHOUT_INTERLOCKED_OP__
 
411
#undef ETHR_READ_ACQB_AND_SET_RELB_COMPILER_BARRIER_ONLY__
 
412
 
 
413
#endif /* _MSC_VER */
 
414
 
 
415
#endif /* ETHR_INCLUDE_ATOMIC_IMPL__ */