2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "AsymmetricMemoryBarrier.h"
19 #include <folly/Exception.h>
20 #include <folly/Indestructible.h>
21 #include <folly/portability/SysMembarrier.h>
22 #include <folly/portability/SysMman.h>
29 struct DummyPageCreator {
36 kIsLinux && !detail::sysMembarrierAvailable() ? create() : nullptr;
41 static void* create() {
42 auto ptr = mmap(nullptr, 1, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
43 checkUnixError(reinterpret_cast<uintptr_t>(ptr), "mmap");
45 // Optimistically try to lock the page so it stays resident. Could make
46 // the heavy barrier faster.
47 auto r = mlock(ptr, 1);
56 // Make sure dummy page is always initialized before shutdown.
57 DummyPageCreator dummyPageCreator;
59 void mprotectMembarrier() {
60 auto dummyPage = dummyPageCreator.get();
62 // This function is required to be safe to call on shutdown,
63 // so we must leak the mutex.
64 static Indestructible<std::mutex> mprotectMutex;
65 std::lock_guard<std::mutex> lg(*mprotectMutex);
69 // We want to downgrade the page while it is resident. To do that, it must
70 // first be upgraded and forced to be resident.
71 r = mprotect(dummyPage, 1, PROT_READ | PROT_WRITE);
72 checkUnixError(r, "mprotect");
74 // Force the page to be resident. If it is already resident, almost no-op.
75 *static_cast<char*>(dummyPage) = 0;
77 // Downgrade the page. Forces a memory barrier in every core running any
78 // of the process's threads. On a sane platform.
79 r = mprotect(dummyPage, 1, PROT_READ);
80 checkUnixError(r, "mprotect");
84 void asymmetricHeavyBarrier() {
86 static const bool useSysMembarrier = detail::sysMembarrierAvailable();
87 if (useSysMembarrier) {
88 auto r = detail::sysMembarrier();
89 checkUnixError(r, "membarrier");
94 std::atomic_thread_fence(std::memory_order_seq_cst);