2 * Copyright 2017 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>
18 #include <folly/MapUtil.h>
22 namespace folly { namespace detail {
24 EventBaseLocalBase::~EventBaseLocalBase() {
25 for (auto* evb : *eventBases_.rlock()) {
26 evb->runInEventBaseThread([ this, evb, key = key_ ] {
27 evb->localStorage_.erase(key);
28 evb->localStorageToDtor_.erase(this);
33 void* EventBaseLocalBase::getVoid(EventBase& evb) {
34 DCHECK(evb.isInEventBaseThread());
36 return folly::get_default(evb.localStorage_, key_, {}).get();
39 void EventBaseLocalBase::erase(EventBase& evb) {
40 DCHECK(evb.isInEventBaseThread());
42 evb.localStorage_.erase(key_);
43 evb.localStorageToDtor_.erase(this);
45 SYNCHRONIZED(eventBases_) {
46 eventBases_.erase(&evb);
50 void EventBaseLocalBase::onEventBaseDestruction(EventBase& evb) {
51 DCHECK(evb.isInEventBaseThread());
53 SYNCHRONIZED(eventBases_) {
54 eventBases_.erase(&evb);
58 void EventBaseLocalBase::setVoid(EventBase& evb, std::shared_ptr<void>&& ptr) {
59 DCHECK(evb.isInEventBaseThread());
62 evb.localStorage_.find(key_) != evb.localStorage_.end();
64 evb.localStorage_.emplace(key_, std::move(ptr));
67 eventBases_.wlock()->insert(&evb);
68 evb.localStorageToDtor_.insert(this);
72 std::atomic<uint64_t> EventBaseLocalBase::keyCounter_{0};