proxygen
Asm.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <folly/Portability.h>
20 
21 #include <chrono>
22 #include <cstdint>
23 
24 #ifdef _MSC_VER
25 #include <intrin.h>
26 #endif
27 
28 namespace folly {
29 inline void asm_volatile_memory() {
30 #if defined(__clang__) || defined(__GNUC__)
31  asm volatile("" : : : "memory");
32 #elif defined(_MSC_VER)
33  ::_ReadWriteBarrier();
34 #endif
35 }
36 
37 inline void asm_volatile_pause() {
38 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
39  ::_mm_pause();
40 #elif defined(__i386__) || FOLLY_X64
41  asm volatile("pause");
42 #elif FOLLY_AARCH64 || defined(__arm__)
43  asm volatile("yield");
44 #elif FOLLY_PPC64
45  asm volatile("or 27,27,27");
46 #endif
47 }
48 
50 #if _MSC_VER
51  return (uint64_t)__rdtsc();
52 #elif defined(__i386__) || FOLLY_X64
53  // read the timestamp counter on x86
54  auto hi = std::uint32_t{};
55  auto lo = std::uint32_t{};
56  asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
57  return (((std::uint64_t)lo) + (((std::uint64_t)hi) << 32));
58 #else
59  // use steady_clock::now() as an approximation for the timestamp counter on
60  // non-x86 systems
61  return std::chrono::steady_clock::now().time_since_epoch().count();
62 #endif
63 }
64 } // namespace folly
std::chrono::steady_clock::time_point now()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::uint64_t asm_rdtsc()
Definition: Asm.h:49
void asm_volatile_memory()
Definition: Asm.h:29
void asm_volatile_pause()
Definition: Asm.h:37