2 * Copyright 2015 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 <folly/io/async/EventBaseLocal.h>
21 namespace folly { namespace detail {
23 EventBaseLocalBase::~EventBaseLocalBase() {
24 // There's a race condition if an EventBase and an EventBaseLocal destruct
25 // at the same time (each will lock eventBases_ and localStorageMutex_
26 // in the opposite order), so we dance around it with a loop and try_lock.
28 SYNCHRONIZED(eventBases_) {
29 auto it = eventBases_.begin();
30 while (it != eventBases_.end()) {
32 if (evb->localStorageMutex_.try_lock()) {
33 evb->localStorage_.erase(key_);
34 evb->localStorageToDtor_.erase(this);
35 it = eventBases_.erase(it);
36 evb->localStorageMutex_.unlock();
42 if (eventBases_.empty()) {
46 std::this_thread::yield(); // let the other thread take the eventBases_ lock
50 void* EventBaseLocalBase::getVoid(EventBase& evb) {
51 std::lock_guard<std::mutex> lg(evb.localStorageMutex_);
52 auto it2 = evb.localStorage_.find(key_);
53 if (UNLIKELY(it2 != evb.localStorage_.end())) {
54 return it2->second.get();
60 void EventBaseLocalBase::erase(EventBase& evb) {
61 std::lock_guard<std::mutex> lg(evb.localStorageMutex_);
62 evb.localStorage_.erase(key_);
63 evb.localStorageToDtor_.erase(this);
65 SYNCHRONIZED(eventBases_) {
66 eventBases_.erase(&evb);
70 void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) {
71 SYNCHRONIZED(eventBases_) {
72 eventBases_.erase(&evb);
76 void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) {
77 std::lock_guard<std::mutex> lg(evb.localStorageMutex_);
78 setVoidUnlocked(evb, std::move(ptr));
81 void EventBaseLocalBase::setVoidUnlocked(
82 EventBase& evb, std::shared_ptr<void>&& ptr) {
85 evb.localStorage_.find(key_) != evb.localStorage_.end();
87 evb.localStorage_.emplace(key_, std::move(ptr));
90 SYNCHRONIZED(eventBases_) {
91 eventBases_.insert(&evb);
93 evb.localStorageToDtor_.insert(this);
97 std::atomic<uint64_t> EventBaseLocalBase::keyCounter_{0};