BDE 4.39.x 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.
81///
82/// See @ref bslmt_platform
83struct Platform {
84
85 // 'ThreadPolicy' trait
86
87 struct PosixThreads {};
88 struct Win32Threads {};
89
90 #ifdef BSLS_PLATFORM_OS_UNIX
91
92 typedef PosixThreads ThreadPolicy;
93 #define BSLMT_PLATFORM_POSIX_THREADS 1
94
95 #endif
96
97 #ifdef BSLS_PLATFORM_OS_WINDOWS
98
99 typedef Win32Threads ThreadPolicy;
100 #define BSLMT_PLATFORM_WIN32_THREADS 1
101
102 #endif
103
104 // 'SemaphorePolicy' trait
105
107 struct PosixSemaphore {};
109 struct Win32Semaphore {};
110
111 #ifdef BSLS_PLATFORM_OS_UNIX
112
113 #if defined(BSLS_PLATFORM_OS_AIX)
114
115 // The POSIX semaphore on IBM has a maximum count of 32k. Other POSIX
116 // implementations support counts up to 'INT_MAX', and, historically,
117 // 'bslmt::Semaphore' also supported 'INT_MAX'. On AIX, use a semaphore
118 // that maintains the count in a separate atomic integer to enable
119 // consistent semaphore usage across platforms.
120
121 typedef CountedSemaphore SemaphorePolicy;
122 typedef PosixSemaphore CountedSemaphoreImplPolicy;
123 #define BSLMT_PLATFORM_COUNTED_SEMAPHORE
124 #define BSLMT_PLATFORM_POSIX_SEMAPHORE
125
126 #elif defined(BSLS_PLATFORM_OS_DARWIN)
127
128 // Darwin doesn't implement sem_getvalue.
129
130 typedef CountedSemaphore SemaphorePolicy;
131 typedef DarwinSemaphore CountedSemaphoreImplPolicy;
132 #define BSLMT_PLATFORM_COUNTED_SEMAPHORE
133
134 #else
135
136 typedef PosixSemaphore SemaphorePolicy;
137 #define BSLMT_PLATFORM_POSIX_SEMAPHORE
138
139 #endif
140
141 #endif
142
143 #ifdef BSLS_PLATFORM_OS_WINDOWS
144
145 typedef Win32Semaphore SemaphorePolicy;
146
147 #endif
148
149 // 'TimedSemaphorePolicy' trait
150
154
155 #ifdef BSLS_PLATFORM_OS_UNIX
156
157 #if defined(BSLS_PLATFORM_OS_SOLARIS) || \
158 defined(BSLS_PLATFORM_OS_LINUX)
159
160 typedef PosixAdvTimedSemaphore TimedSemaphorePolicy;
161 #define BSLMT_PLATFORM_POSIXADV_TIMEDSEMAPHORE 1
162
163 #else // 'sem_timedwait' not available; use custom pthread-based semaphore
164
165 typedef PthreadTimedSemaphore TimedSemaphorePolicy;
166
167 #endif
168
169 #endif
170
171 #ifdef BSLS_PLATFORM_OS_WINDOWS
172
173 typedef Win32TimedSemaphore TimedSemaphorePolicy;
174
175 #endif
176
177 enum {
178 // This constant can be used in synchronization mechanisms to separate
179 // member variables to prevent "false sharing". For POWER cpus:
180 // http://www.ibm.com/developerworks/power/library/pa-memory/index.html
181 // has a simple program to determine the cache line size of the CPU.
182 // Current Power cpus have 128-byte cache lines.
183 //
184 // On Solaris, to determine the cache line size on the local cpu, run:
185 // ```
186 // prtconf -pv | grep -i l1-dcache-line-size | sort -u
187 // ```
188 // Older sparc cpus have 32-byte cache lines, newer 64-byte cache
189 // lines. We'll assume 64-byte cache lines here.
190 //
191 // On Linux with 'sysfs' support,
192 // ```
193 // cat /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size
194 // ```
195 // or
196 // ```
197 // cat /proc/cpuinfo | grep cache
198 // ```
199 // Post SSE2 cpus have the clflush instruction which can be used to write
200 // a program similar to the one mentioned above for POWER cpus. Current
201 // x86/x86_64 have 64-byte cache lines.
202 //
203 // The non-L1 memory caches may obtain memory in a different quantum
204 // (e.g., the L2 cache may pre-fetch a cache line). As such, there may
205 // be incremental improvement obtained if variables are seperated by
206 // multiple cache lines.
207 //
208 // It is obviously suboptimal to determine this at compile time. We
209 // might want to do this at runtime, but this would add at least one
210 // level of indirection.
211#ifdef BSLS_PLATFORM_CPU_POWERPC
213#else
215#endif
216 };
217};
218
219} // close package namespace
220
221
222#endif
223
224// ----------------------------------------------------------------------------
225// Copyright 2015 Bloomberg Finance L.P.
226//
227// Licensed under the Apache License, Version 2.0 (the "License");
228// you may not use this file except in compliance with the License.
229// You may obtain a copy of the License at
230//
231// http://www.apache.org/licenses/LICENSE-2.0
232//
233// Unless required by applicable law or agreed to in writing, software
234// distributed under the License is distributed on an "AS IS" BASIS,
235// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
236// See the License for the specific language governing permissions and
237// limitations under the License.
238// ----------------------------- END-OF-FILE ----------------------------------
239
240/** @} */
241/** @} */
242/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344
Definition bslmt_platform.h:106
Definition bslmt_platform.h:108
Definition bslmt_platform.h:151
Definition bslmt_platform.h:107
Definition bslmt_platform.h:87
Definition bslmt_platform.h:152
Definition bslmt_platform.h:109
Definition bslmt_platform.h:88
Definition bslmt_platform.h:153
Definition bslmt_platform.h:83
@ e_CACHE_LINE_SIZE
Definition bslmt_platform.h:214