Commit 65105e81 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Switch away from using the legacy Synchronized macros

Summary: [Folly] Switch away from using the legacy `Synchronized` macros within Folly.

Reviewed By: simpkins

Differential Revision: D7118324

fbshipit-source-id: 328ecdd572d84bb99a28ddb8689bdc4ae08421a6
parent 2d40a641
...@@ -119,7 +119,7 @@ void throwHandler(void*, std::type_info* exType, void (*)(void*)) noexcept { ...@@ -119,7 +119,7 @@ void throwHandler(void*, std::type_info* exType, void (*)(void*)) noexcept {
auto exceptionId = auto exceptionId =
folly::hash::SpookyHashV2::Hash64(frames, (n + 1) * sizeof(frames[0]), 0); folly::hash::SpookyHashV2::Hash64(frames, (n + 1) * sizeof(frames[0]), 0);
SYNCHRONIZED(holder, gExceptionStats->statsHolder) { gExceptionStats->statsHolder.withWLock([&](auto& holder) {
auto it = holder.find(exceptionId); auto it = holder.find(exceptionId);
if (it != holder.end()) { if (it != holder.end()) {
++it->second.count; ++it->second.count;
...@@ -129,7 +129,7 @@ void throwHandler(void*, std::type_info* exType, void (*)(void*)) noexcept { ...@@ -129,7 +129,7 @@ void throwHandler(void*, std::type_info* exType, void (*)(void*)) noexcept {
info.frames.assign(frames + 1, frames + 1 + n); info.frames.assign(frames + 1, frames + 1 + n);
holder.emplace(exceptionId, ExceptionStats{1, std::move(info)}); holder.emplace(exceptionId, ExceptionStats{1, std::move(info)});
} }
} });
} }
struct Initializer { struct Initializer {
......
...@@ -47,20 +47,17 @@ template <typename Function> ...@@ -47,20 +47,17 @@ template <typename Function>
class CallbackHolder { class CallbackHolder {
public: public:
void registerCallback(Function f) { void registerCallback(Function f) {
SYNCHRONIZED(callbacks_) { callbacks_.wlock()->push_back(std::move(f));
callbacks_.push_back(std::move(f));
}
} }
// always inline to enforce kInternalFramesNumber // always inline to enforce kInternalFramesNumber
template <typename... Args> template <typename... Args>
FOLLY_ALWAYS_INLINE void invoke(Args... args) { FOLLY_ALWAYS_INLINE void invoke(Args... args) {
SYNCHRONIZED_CONST(callbacks_) { auto callbacksLock = callbacks_.rlock();
for (auto& cb : callbacks_) { for (auto& cb : *callbacksLock) {
cb(args...); cb(args...);
} }
} }
}
private: private:
folly::Synchronized<std::vector<Function>> callbacks_; folly::Synchronized<std::vector<Function>> callbacks_;
......
...@@ -99,14 +99,14 @@ class ThreadLocalCache { ...@@ -99,14 +99,14 @@ class ThreadLocalCache {
static void erase(EventBaseT& evb) { static void erase(EventBaseT& evb) {
for (auto& localInstance : instance().accessAllThreads()) { for (auto& localInstance : instance().accessAllThreads()) {
SYNCHRONIZED(info, localInstance.eraseInfo_) { localInstance.eraseInfo_.withWLock([&](auto& info) {
if (info.eraseList.size() >= kEraseListMaxSize) { if (info.eraseList.size() >= kEraseListMaxSize) {
info.eraseAll = true; info.eraseAll = true;
} else { } else {
info.eraseList.push_back(&evb); info.eraseList.push_back(&evb);
} }
localInstance.eraseRequested_ = true; localInstance.eraseRequested_ = true;
} });
} }
} }
...@@ -143,7 +143,7 @@ class ThreadLocalCache { ...@@ -143,7 +143,7 @@ class ThreadLocalCache {
return; return;
} }
SYNCHRONIZED(info, eraseInfo_) { eraseInfo_.withWLock([&](auto& info) {
if (info.eraseAll) { if (info.eraseAll) {
map_.clear(); map_.clear();
} else { } else {
...@@ -155,7 +155,7 @@ class ThreadLocalCache { ...@@ -155,7 +155,7 @@ class ThreadLocalCache {
info.eraseList.clear(); info.eraseList.clear();
info.eraseAll = false; info.eraseAll = false;
eraseRequested_ = false; eraseRequested_ = false;
} });
} }
std::unordered_map<EventBaseT*, FiberManager*> map_; std::unordered_map<EventBaseT*, FiberManager*> map_;
......
...@@ -90,9 +90,7 @@ class StackCache { ...@@ -90,9 +90,7 @@ class StackCache {
auto p = freeList_.back().first; auto p = freeList_.back().first;
if (!freeList_.back().second) { if (!freeList_.back().second) {
PCHECK(0 == ::mprotect(p, pagesize(), PROT_NONE)); PCHECK(0 == ::mprotect(p, pagesize(), PROT_NONE));
SYNCHRONIZED(pages, protectedPages()) { protectedPages().wlock()->insert(reinterpret_cast<intptr_t>(p));
pages.insert(reinterpret_cast<intptr_t>(p));
}
} }
freeList_.pop_back(); freeList_.pop_back();
...@@ -132,25 +130,25 @@ class StackCache { ...@@ -132,25 +130,25 @@ class StackCache {
~StackCache() { ~StackCache() {
assert(storage_); assert(storage_);
SYNCHRONIZED(pages, protectedPages()) { protectedPages().withWLock([&](auto& pages) {
for (const auto& item : freeList_) { for (const auto& item : freeList_) {
pages.erase(reinterpret_cast<intptr_t>(item.first)); pages.erase(reinterpret_cast<intptr_t>(item.first));
} }
} });
PCHECK(0 == ::munmap(storage_, allocSize_ * kNumGuarded)); PCHECK(0 == ::munmap(storage_, allocSize_ * kNumGuarded));
} }
static bool isProtected(intptr_t addr) { static bool isProtected(intptr_t addr) {
// Use a read lock for reading. // Use a read lock for reading.
SYNCHRONIZED_CONST(pages, protectedPages()) { return protectedPages().withRLock([&](auto const& pages) {
for (const auto& page : pages) { for (const auto& page : pages) {
intptr_t pageEnd = intptr_t(page + pagesize()); intptr_t pageEnd = intptr_t(page + pagesize());
if (page <= addr && addr < pageEnd) { if (page <= addr && addr < pageEnd) {
return true; return true;
} }
} }
}
return false; return false;
});
} }
private: private:
......
...@@ -20,23 +20,24 @@ namespace fibers { ...@@ -20,23 +20,24 @@ namespace fibers {
bool Semaphore::signalSlow() { bool Semaphore::signalSlow() {
// If we signalled a release, notify the waitlist // If we signalled a release, notify the waitlist
SYNCHRONIZED(waitList_) { auto waitListLock = waitList_.wlock();
auto& waitList = *waitListLock;
auto testVal = tokens_.load(std::memory_order_acquire); auto testVal = tokens_.load(std::memory_order_acquire);
if (testVal != 0) { if (testVal != 0) {
return false; return false;
} }
if (waitList_.empty()) { if (waitList.empty()) {
// If the waitlist is now empty, ensure the token count increments // If the waitlist is now empty, ensure the token count increments
// No need for CAS here as we will always be under the mutex // No need for CAS here as we will always be under the mutex
CHECK(tokens_.compare_exchange_strong( CHECK(tokens_.compare_exchange_strong(
testVal, testVal + 1, std::memory_order_relaxed)); testVal, testVal + 1, std::memory_order_relaxed));
} else { } else {
// trigger waiter if there is one // trigger waiter if there is one
waitList_.front()->post(); waitList.front()->post();
waitList_.pop(); waitList.pop();
} }
} // SYNCHRONIZED(waitList_)
return true; return true;
} }
...@@ -59,13 +60,16 @@ bool Semaphore::waitSlow() { ...@@ -59,13 +60,16 @@ bool Semaphore::waitSlow() {
// Slow path, create a baton and acquire a mutex to update the wait list // Slow path, create a baton and acquire a mutex to update the wait list
folly::fibers::Baton waitBaton; folly::fibers::Baton waitBaton;
SYNCHRONIZED(waitList_) { {
auto waitListLock = waitList_.wlock();
auto& waitList = *waitListLock;
auto testVal = tokens_.load(std::memory_order_acquire); auto testVal = tokens_.load(std::memory_order_acquire);
if (testVal != 0) { if (testVal != 0) {
return false; return false;
} }
// prepare baton and add to queue // prepare baton and add to queue
waitList_.push(&waitBaton); waitList.push(&waitBaton);
} }
// If we managed to create a baton, wait on it // If we managed to create a baton, wait on it
// This has to be done here so the mutex has been released // This has to be done here so the mutex has been released
......
...@@ -43,17 +43,13 @@ void EventBaseLocalBase::erase(EventBase& evb) { ...@@ -43,17 +43,13 @@ void EventBaseLocalBase::erase(EventBase& evb) {
evb.localStorage_.erase(key_); evb.localStorage_.erase(key_);
evb.localStorageToDtor_.erase(this); evb.localStorageToDtor_.erase(this);
SYNCHRONIZED(eventBases_) { eventBases_.wlock()->erase(&evb);
eventBases_.erase(&evb);
}
} }
void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) { void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) {
evb.dcheckIsInEventBaseThread(); evb.dcheckIsInEventBaseThread();
SYNCHRONIZED(eventBases_) { eventBases_.wlock()->erase(&evb);
eventBases_.erase(&evb);
}
} }
void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) { void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment