1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2008-2009 Andreas Persson * |
* Copyright (C) 2008-2012 Andreas Persson * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
21 |
#ifndef LSATOMIC_H |
#ifndef LSATOMIC_H |
22 |
#define LSATOMIC_H |
#define LSATOMIC_H |
23 |
|
|
24 |
/* |
/** @file |
|
* Implementation of a small subset of the C++0x atomic operations |
|
|
* (cstdatomic). |
|
25 |
* |
* |
26 |
* The supported operations are: |
* Implementation of a small subset of the C++11 atomic operations. |
27 |
|
* |
28 |
|
* Note: When working with multithreading on modern CPUs, it's |
29 |
|
* important not only to make sure that concurrent access to shared |
30 |
|
* variables is made atomically, but also to be aware of the order the |
31 |
|
* stores get visible to the loads in other threads. For example, if x |
32 |
|
* and y are shared variables with initial values of 0, the following |
33 |
|
* program: |
34 |
|
* |
35 |
|
* @code |
36 |
|
* // thread 1: |
37 |
|
* x.store(1, memory_order_relaxed); |
38 |
|
* r1 = y.load(memory_order_relaxed); |
39 |
|
* |
40 |
|
* // thread 2: |
41 |
|
* y.store(1, memory_order_relaxed); |
42 |
|
* r2 = x.load(memory_order_relaxed); |
43 |
|
* @endcode |
44 |
|
* |
45 |
|
* would have a possible outcome of r1 == 0 and r2 == 0. The threads |
46 |
|
* might for example run on separate CPU cores with separate caches, |
47 |
|
* and the propagation of the store to the other core might be delayed |
48 |
|
* and done after the loads. In that case, both loads will read the |
49 |
|
* original value of 0 from the core's own cache. |
50 |
|
* |
51 |
|
* The C++11 style operations use the memory_order parameter to let |
52 |
|
* the programmer control the way shared memory stores get visible to |
53 |
|
* loads in other threads. In the example above, relaxed order was |
54 |
|
* used, which allows the CPU and compiler to reorder the memory |
55 |
|
* accesses very freely. If memory_order_seq_cst had been used |
56 |
|
* instead, the r1 == 0 and r2 == 0 outcome would have been |
57 |
|
* impossible, as sequential consistency means that the execution of |
58 |
|
* the program can be modeled by simply interleaving the instructions |
59 |
|
* of the threads. |
60 |
|
* |
61 |
|
* The default order is memory_order_seq_cst, as it is the easiest one |
62 |
|
* to understand. It is however also the slowest. The relaxed order is |
63 |
|
* the fastest, but it can't be used if the shared variable is used to |
64 |
|
* synchronize threads for any other shared data. The third order is |
65 |
|
* acquire/release, where an acquire-load is synchronizing with a |
66 |
|
* release-store to the same variable. |
67 |
|
* |
68 |
|
* See for example http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync for |
69 |
|
* more information about the memory order parameter. |
70 |
|
* |
71 |
|
* The supported operations of the implementation in this file are: |
72 |
* |
* |
73 |
* - fences (acquire, release and seq_cst) |
* - fences (acquire, release and seq_cst) |
74 |
* |
* |
77 |
* |
* |
78 |
* The supported architectures are x86 and powerpc. |
* The supported architectures are x86 and powerpc. |
79 |
*/ |
*/ |
80 |
|
|
81 |
|
|
82 |
|
// if C++11 and gcc 4.7 or later is used, then use the standard |
83 |
|
// implementation |
84 |
|
#if __cplusplus >= 201103L && \ |
85 |
|
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) |
86 |
|
|
87 |
|
#include <atomic> |
88 |
|
|
89 |
|
namespace LinuxSampler { |
90 |
|
using std::memory_order_relaxed; |
91 |
|
using std::memory_order_acquire; |
92 |
|
using std::memory_order_release; |
93 |
|
using std::memory_order_seq_cst; |
94 |
|
using std::atomic_thread_fence; |
95 |
|
using std::atomic; |
96 |
|
} |
97 |
|
|
98 |
|
#else |
99 |
|
|
100 |
|
|
101 |
namespace LinuxSampler { |
namespace LinuxSampler { |
102 |
enum memory_order { |
enum memory_order { |
103 |
memory_order_relaxed, memory_order_acquire, |
memory_order_relaxed, memory_order_acquire, |
197 |
}; |
}; |
198 |
} |
} |
199 |
#endif |
#endif |
200 |
|
#endif |