BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmt_platform.h
Go to the documentation of this file.
1/// @file bslmt_platform.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_platform.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_PLATFORM
9#define INCLUDED_BSLMT_PLATFORM
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_platform bslmt_platform
15/// @brief Provide platform-dependent thread-related trait definitions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_platform
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_platform-purpose"> Purpose</a>
25/// * <a href="#bslmt_platform-classes"> Classes </a>
26/// * <a href="#bslmt_platform-description"> Description </a>
27///
28/// # Purpose {#bslmt_platform-purpose}
29/// Provide platform-dependent thread-related trait definitions.
30///
31/// # Classes {#bslmt_platform-classes}
32///
33/// - bslmt::Platform: namespace for platform-dependent thread-related traits
34///
35/// @see
36///
37/// # Description {#bslmt_platform-description}
38/// This component defines a set of traits that identify and
39/// describe a platform's concurrency properties. For example, the
40/// `ThreadPolicy` trait is ascribed a "value" (i.e., POSIX or Win32)
41/// appropriate for each supported platform. The various concurrency traits are
42/// actually types declared in the `Platform` `struct`. These types are
43/// intended to be used in specializing template implementations or to enable
44/// function overloading based on the prevalent system's characteristics.
45///
46/// This component also defines a `SemaphorePolicy` trait used for selecting a
47/// semaphore implementation. Differences among POSIX implementations lead to
48/// different semaphore policies for the same `ThreadPolicy`.
49///
50/// Finally, this component defines a `TimedSemaphorePolicy` trait used for
51/// selecting a timed-semaphore implementation. POSIX platforms that do not
52/// have a native timed-wait for semaphores require a custom (pthread-based)
53/// implementation.
54/// @}
55/** @} */
56/** @} */
57
58/** @addtogroup bsl
59 * @{
60 */
61/** @addtogroup bslmt
62 * @{
63 */
64/** @addtogroup bslmt_platform
65 * @{
66 */
67
68#include <bslscm_version.h>
69
70#include <bsls_atomic.h>
71#include <bsls_platform.h>
72
73
74namespace bslmt {
75
76 // ==============
77 // class Platform
78 // ==============
79
80/// This `struct` provides a namespace for concurrency trait definitions.
81struct Platform {
82
83 // 'ThreadPolicy' trait
84
85 struct PosixThreads {};
86 struct Win32Threads {};
87
88 #ifdef BSLS_PLATFORM_OS_UNIX
89
90 typedef PosixThreads ThreadPolicy;
91 #define BSLMT_PLATFORM_POSIX_THREADS 1
92
93 #endif
94
95 #ifdef BSLS_PLATFORM_OS_WINDOWS
96
97 typedef Win32Threads ThreadPolicy;
98 #define BSLMT_PLATFORM_WIN32_THREADS 1
99
100 #endif
101
102 // 'SemaphorePolicy' trait
103
105 struct PosixSemaphore {};
107 struct Win32Semaphore {};
108
109 #ifdef BSLS_PLATFORM_OS_UNIX
110
111 #if defined(BSLS_PLATFORM_OS_AIX)
112
113 // The POSIX semaphore on IBM has a maximum count of 32k. Other POSIX
114 // implementations support counts up to 'INT_MAX', and, historically,
115 // 'bslmt::Semaphore' also supported 'INT_MAX'. On AIX, use a semaphore
116 // that maintains the count in a separate atomic integer to enable
117 // consistent semaphore usage across platforms.
118
119 typedef CountedSemaphore SemaphorePolicy;
120 typedef PosixSemaphore CountedSemaphoreImplPolicy;
121 #define BSLMT_PLATFORM_COUNTED_SEMAPHORE
122 #define BSLMT_PLATFORM_POSIX_SEMAPHORE
123
124 #elif defined(BSLS_PLATFORM_OS_DARWIN)
125
126 // Darwin doesn't implement sem_getvalue.
127
128 typedef CountedSemaphore SemaphorePolicy;
129 typedef DarwinSemaphore CountedSemaphoreImplPolicy;
130 #define BSLMT_PLATFORM_COUNTED_SEMAPHORE
131
132 #else
133
134 typedef PosixSemaphore SemaphorePolicy;
135 #define BSLMT_PLATFORM_POSIX_SEMAPHORE
136
137 #endif
138
139 #endif
140
141 #ifdef BSLS_PLATFORM_OS_WINDOWS
142
143 typedef Win32Semaphore SemaphorePolicy;
144
145 #endif
146
147 // 'TimedSemaphorePolicy' trait
148
152
153 #ifdef BSLS_PLATFORM_OS_UNIX
154
155 #if defined(BSLS_PLATFORM_OS_SUNOS) || \
156 defined(BSLS_PLATFORM_OS_SOLARIS) || \
157 defined(BSLS_PLATFORM_OS_LINUX) \
158
159 typedef PosixAdvTimedSemaphore TimedSemaphorePolicy;
160 #define BSLMT_PLATFORM_POSIXADV_TIMEDSEMAPHORE 1
161
162 #else // 'sem_timedwait' not available; use custom pthread-based semaphore
163
164 typedef PthreadTimedSemaphore TimedSemaphorePolicy;
165
166 #endif
167
168 #endif
169
170 #ifdef BSLS_PLATFORM_OS_WINDOWS
171
172 typedef Win32TimedSemaphore TimedSemaphorePolicy;
173
174 #endif
175
176 enum {
177 // This constant can be used in synchronization mechanisms to separate
178 // member variables to prevent "false sharing". For POWER cpus:
179 // http://www.ibm.com/developerworks/power/library/pa-memory/index.html
180 // has a simple program to determine the cache line size of the CPU.
181 // Current Power cpus have 128-byte cache lines.
182 //
183 // On Solaris, to determine the cache line size on the local cpu, run:
184 // ```
185 // prtconf -pv | grep -i l1-dcache-line-size | sort -u
186 // ```
187 // Older sparc cpus have 32-byte cache lines, newer 64-byte cache
188 // lines. We'll assume 64-byte cache lines here.
189 //
190 // On Linux with 'sysfs' support,
191 // ```
192 // cat /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size
193 // ```
194 // or
195 // ```
196 // cat /proc/cpuinfo | grep cache
197 // ```
198 // Post SSE2 cpus have the clflush instruction which can be used to write
199 // a program similar to the one mentioned above for POWER cpus. Current
200 // x86/x86_64 have 64-byte cache lines.
201 //
202 // The non-L1 memory caches may obtain memory in a different quantum
203 // (e.g., the L2 cache may pre-fetch a cache line). As such, there may
204 // be incremental improvement obtained if variables are seperated by
205 // multiple cache lines.
206 //
207 // It is obviously suboptimal to determine this at compile time. We
208 // might want to do this at runtime, but this would add at least one
209 // level of indirection.
210#ifdef BSLS_PLATFORM_CPU_POWERPC
212#else
214#endif
215 };
216};
217
218} // close package namespace
219
220
221#endif
222
223// ----------------------------------------------------------------------------
224// Copyright 2015 Bloomberg Finance L.P.
225//
226// Licensed under the Apache License, Version 2.0 (the "License");
227// you may not use this file except in compliance with the License.
228// You may obtain a copy of the License at
229//
230// http://www.apache.org/licenses/LICENSE-2.0
231//
232// Unless required by applicable law or agreed to in writing, software
233// distributed under the License is distributed on an "AS IS" BASIS,
234// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
235// See the License for the specific language governing permissions and
236// limitations under the License.
237// ----------------------------- END-OF-FILE ----------------------------------
238
239/** @} */
240/** @} */
241/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344
Definition bslmt_platform.h:104
Definition bslmt_platform.h:106
Definition bslmt_platform.h:149
Definition bslmt_platform.h:105
Definition bslmt_platform.h:85
Definition bslmt_platform.h:150
Definition bslmt_platform.h:107
Definition bslmt_platform.h:86
Definition bslmt_platform.h:151
This struct provides a namespace for concurrency trait definitions.
Definition bslmt_platform.h:81
@ e_CACHE_LINE_SIZE
Definition bslmt_platform.h:213