2017
[folly.git] / folly / portability / Asm.h
1 /*
2  * Copyright 2017 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 #ifdef _MSC_VER
22 # include <intrin.h>
23 #endif
24
25 namespace folly {
26 inline void asm_volatile_memory() {
27 #if defined(__clang__) || defined(__GNUC__)
28   asm volatile("" : : : "memory");
29 #elif defined(_MSC_VER)
30   ::_ReadWriteBarrier();
31 #endif
32 }
33
34 inline void asm_volatile_pause() {
35 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
36   ::_mm_pause();
37 #elif defined(__i386__) || FOLLY_X64
38   asm volatile("pause");
39 #elif FOLLY_A64 || defined(__arm__)
40   asm volatile("yield");
41 #elif FOLLY_PPC64
42   asm volatile("or 27,27,27");
43 #endif
44 }
45
46 inline void asm_pause() {
47 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
48   ::_mm_pause();
49 #elif defined(__i386__) || FOLLY_X64
50   asm("pause");
51 #elif FOLLY_A64 || defined(__arm__)
52   asm("yield");
53 #elif FOLLY_PPC64
54   asm("or 31,31,31");
55 #endif
56 }
57 }